fix: only add NULL_SORT when the nullSort field is provided, and add more tests (#297)

This commit is contained in:
Elbarae Rguig 2022-08-22 14:46:23 +01:00 committed by GitHub
parent 47a35470d4
commit dd48068b09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 7 deletions

View File

@ -155,12 +155,13 @@ const paginateConfig: PaginateConfig<CatEntity> {
* Description: These are the columns that are valid to be sorted by. * Description: These are the columns that are valid to be sorted by.
*/ */
sortableColumns: ['id', 'name', 'color'], sortableColumns: ['id', 'name', 'color'],
/** /**
* 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',

View File

@ -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 () => {

View File

@ -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) {