fix: only add NULL_SORT when the nullSort field is provided, and add more tests (#297)
This commit is contained in:
parent
47a35470d4
commit
dd48068b09
@ -155,12 +155,13 @@ const paginateConfig: PaginateConfig<CatEntity> {
|
||||
* Description: These are the columns that are valid to be sorted by.
|
||||
*/
|
||||
sortableColumns: ['id', 'name', 'color'],
|
||||
|
||||
|
||||
/**
|
||||
* Required: false
|
||||
* Type: 'first' | 'last'
|
||||
* Default: 'last'
|
||||
* Description: Define whether to put null values at the beginning or end of the result set.
|
||||
* Default: 'first'
|
||||
* Description: (ONLY WORKS WITH POSTGRES) Define whether to put null values
|
||||
* at the beginning or end of the result set.
|
||||
*/
|
||||
nullSort: 'last',
|
||||
|
||||
|
@ -200,7 +200,24 @@ describe('paginate', () => {
|
||||
const config: PaginateConfig<CatEntity> = {
|
||||
sortableColumns: ['age', 'createdAt'],
|
||||
nullSort: 'last',
|
||||
defaultSortBy: [['age', 'DESC']],
|
||||
defaultSortBy: [['age', 'ASC']],
|
||||
}
|
||||
const query: PaginateQuery = {
|
||||
path: '',
|
||||
}
|
||||
|
||||
const result = await paginate<CatEntity>(query, catRepo, config)
|
||||
const expectedResult = [...cats.slice(0, -1).reverse(), cats.slice(-1)[0]]
|
||||
|
||||
expect(result.meta.sortBy).toStrictEqual([['age', 'ASC']])
|
||||
expect(result.data).toStrictEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('should put null values first when sorting', async () => {
|
||||
const config: PaginateConfig<CatEntity> = {
|
||||
sortableColumns: ['age', 'createdAt'],
|
||||
nullSort: 'first',
|
||||
defaultSortBy: [['age', 'ASC']],
|
||||
}
|
||||
const query: PaginateQuery = {
|
||||
path: '',
|
||||
@ -208,8 +225,27 @@ describe('paginate', () => {
|
||||
|
||||
const result = await paginate<CatEntity>(query, catRepo, config)
|
||||
|
||||
expect(result.meta.sortBy).toStrictEqual([['age', 'DESC']])
|
||||
expect(result.data).toStrictEqual(cats)
|
||||
const expectedResult = [cats[cats.length - 1], ...cats.slice(0, cats.length - 1).reverse()]
|
||||
|
||||
expect(result.meta.sortBy).toStrictEqual([['age', 'ASC']])
|
||||
expect(result.data).toStrictEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('should put null values first when nullSort is not specified', async () => {
|
||||
const config: PaginateConfig<CatEntity> = {
|
||||
sortableColumns: ['age', 'createdAt'],
|
||||
defaultSortBy: [['age', 'ASC']],
|
||||
}
|
||||
const query: PaginateQuery = {
|
||||
path: '',
|
||||
}
|
||||
|
||||
const result = await paginate<CatEntity>(query, catRepo, config)
|
||||
|
||||
const expectedCats = cats.slice()
|
||||
|
||||
expect(result.meta.sortBy).toStrictEqual([['age', 'ASC']])
|
||||
expect(result.data).toStrictEqual(expectedCats.reverse())
|
||||
})
|
||||
|
||||
it('should sort result by multiple columns', async () => {
|
||||
|
@ -173,7 +173,10 @@ export async function paginate<T>(
|
||||
|
||||
if (config.sortableColumns.length < 1) throw new ServiceUnavailableException()
|
||||
|
||||
const NULL_SORT = config.nullSort === 'last' ? 'NULLS LAST' : 'NULLS FIRST'
|
||||
let NULL_SORT: 'NULLS LAST' | 'NULLS FIRST'
|
||||
if (config.nullSort) {
|
||||
NULL_SORT = config.nullSort === 'last' ? 'NULLS LAST' : 'NULLS FIRST'
|
||||
}
|
||||
|
||||
if (query.sortBy) {
|
||||
for (const order of query.sortBy) {
|
||||
|
Loading…
Reference in New Issue
Block a user