fix: restore postgres default null sort behavior
BREAKING CHANGE: don't mess with default behavior of db driver
This commit is contained in:
parent
73e61950e2
commit
9421b109bc
@ -161,7 +161,6 @@ const paginateConfig: PaginateConfig<CatEntity> {
|
|||||||
/**
|
/**
|
||||||
* Required: false
|
* Required: false
|
||||||
* Type: 'first' | 'last'
|
* Type: 'first' | 'last'
|
||||||
* Default: 'first'
|
|
||||||
* Description: (ONLY WORKS WITH POSTGRES) Define whether to put null values
|
* Description: (ONLY WORKS WITH POSTGRES) Define whether to put null values
|
||||||
* at the beginning or end of the result set.
|
* at the beginning or end of the result set.
|
||||||
*/
|
*/
|
||||||
|
@ -425,23 +425,6 @@ describe('paginate', () => {
|
|||||||
expect(result.data).toStrictEqual(expectedResult)
|
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 () => {
|
||||||
const config: PaginateConfig<CatEntity> = {
|
const config: PaginateConfig<CatEntity> = {
|
||||||
sortableColumns: ['name', 'color'],
|
sortableColumns: ['name', 'color'],
|
||||||
|
@ -100,8 +100,6 @@ export async function paginate<T extends ObjectLiteral>(
|
|||||||
|
|
||||||
const queryBuilder = repo instanceof Repository ? repo.createQueryBuilder('__root') : repo
|
const queryBuilder = repo instanceof Repository ? repo.createQueryBuilder('__root') : repo
|
||||||
|
|
||||||
const isPostgres = ['postgres', 'cockroachdb'].includes(queryBuilder.connection.options.type)
|
|
||||||
|
|
||||||
if (repo instanceof Repository && !config.relations && config.loadEagerRelations === true) {
|
if (repo instanceof Repository && !config.relations && config.loadEagerRelations === true) {
|
||||||
if (!config.relations) {
|
if (!config.relations) {
|
||||||
FindOptionsUtils.joinEagerRelations(queryBuilder, queryBuilder.alias, repo.metadata)
|
FindOptionsUtils.joinEagerRelations(queryBuilder, queryBuilder.alias, repo.metadata)
|
||||||
@ -143,7 +141,7 @@ export async function paginate<T extends ObjectLiteral>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let nullSort: 'NULLS LAST' | 'NULLS FIRST' | undefined = isPostgres ? 'NULLS FIRST' : undefined
|
let nullSort: 'NULLS LAST' | 'NULLS FIRST' | undefined = undefined
|
||||||
if (config.nullSort) {
|
if (config.nullSort) {
|
||||||
nullSort = config.nullSort === 'last' ? 'NULLS LAST' : 'NULLS FIRST'
|
nullSort = config.nullSort === 'last' ? 'NULLS LAST' : 'NULLS FIRST'
|
||||||
}
|
}
|
||||||
@ -238,7 +236,7 @@ export async function paginate<T extends ObjectLiteral>(
|
|||||||
parameters: [alias, `:${property.column}`],
|
parameters: [alias, `:${property.column}`],
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPostgres) {
|
if (['postgres', 'cockroachdb'].includes(queryBuilder.connection.options.type)) {
|
||||||
condition.parameters[0] = `CAST(${condition.parameters[0]} AS text)`
|
condition.parameters[0] = `CAST(${condition.parameters[0]} AS text)`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user