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
@ -159,8 +159,9 @@ const paginateConfig: PaginateConfig<CatEntity> {
|
|||||||
/**
|
/**
|
||||||
* Required: false
|
* Required: false
|
||||||
* Type: 'first' | 'last'
|
* Type: 'first' | 'last'
|
||||||
* Default: 'last'
|
* Default: 'first'
|
||||||
* Description: Define whether to put null values at the beginning or end of the result set.
|
* Description: (ONLY WORKS WITH POSTGRES) Define whether to put null values
|
||||||
|
* at the beginning or end of the result set.
|
||||||
*/
|
*/
|
||||||
nullSort: 'last',
|
nullSort: 'last',
|
||||||
|
|
||||||
|
@ -200,7 +200,24 @@ describe('paginate', () => {
|
|||||||
const config: PaginateConfig<CatEntity> = {
|
const config: PaginateConfig<CatEntity> = {
|
||||||
sortableColumns: ['age', 'createdAt'],
|
sortableColumns: ['age', 'createdAt'],
|
||||||
nullSort: 'last',
|
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 = {
|
const query: PaginateQuery = {
|
||||||
path: '',
|
path: '',
|
||||||
@ -208,8 +225,27 @@ describe('paginate', () => {
|
|||||||
|
|
||||||
const result = await paginate<CatEntity>(query, catRepo, config)
|
const result = await paginate<CatEntity>(query, catRepo, config)
|
||||||
|
|
||||||
expect(result.meta.sortBy).toStrictEqual([['age', 'DESC']])
|
const expectedResult = [cats[cats.length - 1], ...cats.slice(0, cats.length - 1).reverse()]
|
||||||
expect(result.data).toStrictEqual(cats)
|
|
||||||
|
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 () => {
|
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()
|
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) {
|
if (query.sortBy) {
|
||||||
for (const order of query.sortBy) {
|
for (const order of query.sortBy) {
|
||||||
|
Loading…
Reference in New Issue
Block a user