fix: camelcase named virtual columns on postgres

This commit is contained in:
ppetzold 2023-03-17 23:14:59 +01:00
parent a6648e8d62
commit 95439bc66f
4 changed files with 40 additions and 16 deletions

View File

@ -238,7 +238,7 @@ const paginateConfig: PaginateConfig<CatEntity> {
* Description: Load eager relations using TypeORM's eager property. * Description: Load eager relations using TypeORM's eager property.
* Only works if `relations` is not defined. * Only works if `relations` is not defined.
*/ */
loadEagerRelations: true loadEagerRelations: true,
/** /**
* Required: false * Required: false

View File

@ -35,10 +35,7 @@ export class CatEntity {
@Column({ nullable: true }) @Column({ nullable: true })
age: number | null age: number | null
@Column({ @Column({ type: 'text' }) // We don't use enum type as it makes it easier when testing across different db drivers.
type: 'simple-enum',
enum: CutenessLevel,
})
cutenessLevel: CutenessLevel cutenessLevel: CutenessLevel
@Column(() => SizeEmbed) @Column(() => SizeEmbed)

View File

@ -44,7 +44,7 @@ describe('paginate', () => {
database: ':memory:', database: ':memory:',
}), }),
synchronize: true, synchronize: true,
logging: false, logging: true,
entities: [CatEntity, CatToyEntity, CatHomeEntity, CatHomePillowEntity], entities: [CatEntity, CatToyEntity, CatHomeEntity, CatHomePillowEntity],
}) })
await dataSource.initialize() await dataSource.initialize()
@ -118,7 +118,7 @@ describe('paginate', () => {
const entities = dataSource.entityMetadatas const entities = dataSource.entityMetadatas
const tableNames = entities.map((entity) => `"${entity.tableName}"`).join(', ') const tableNames = entities.map((entity) => `"${entity.tableName}"`).join(', ')
await dataSource.query(`TRUNCATE ${tableNames} CASCADE;`) await dataSource.query(`TRUNCATE ${tableNames} RESTART IDENTITY CASCADE;`)
}) })
} }
@ -463,6 +463,27 @@ describe('paginate', () => {
expect(result.data).toStrictEqual([cats[3], cats[4], cats[1], cats[0], cats[2]]) expect(result.data).toStrictEqual([cats[3], cats[4], cats[1], cats[0], cats[2]])
}) })
it('should sort result by camelcase columns', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['cutenessLevel', 'name'],
}
const query: PaginateQuery = {
path: '',
sortBy: [
['cutenessLevel', 'ASC'],
['name', 'ASC'],
],
}
const result = await paginate<CatEntity>(query, catRepo, config)
expect(result.meta.sortBy).toStrictEqual([
['cutenessLevel', 'ASC'],
['name', 'ASC'],
])
expect(result.data).toStrictEqual([cats[4], cats[0], cats[2], cats[3], cats[1]])
})
it('should return result based on search term', async () => { it('should return result based on search term', async () => {
const config: PaginateConfig<CatEntity> = { const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id', 'name', 'color'], sortableColumns: ['id', 'name', 'color'],
@ -904,15 +925,9 @@ describe('paginate', () => {
expect(result.links.current).toBe('?page=1&limit=20&sortBy=size.height:ASC&sortBy=size.length:ASC') expect(result.links.current).toBe('?page=1&limit=20&sortBy=size.height:ASC&sortBy=size.length:ASC')
}) })
// TODO: Make all tests pass postgres driver.
if (process.env.DB === 'postgres') {
// We end postgres coverage here. See TODO above.
return
}
it('should return result based on sort on embedded entity when other relations loaded', async () => { it('should return result based on sort on embedded entity when other relations loaded', async () => {
const config: PaginateConfig<CatEntity> = { const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id', 'name', 'size.height', 'size.length', 'size.width'], sortableColumns: ['id', 'name', 'size.height', 'size.length', 'size.width', 'toys.(size.height)'],
searchableColumns: ['name'], searchableColumns: ['name'],
relations: ['home', 'toys'], relations: ['home', 'toys'],
} }
@ -921,6 +936,7 @@ describe('paginate', () => {
sortBy: [ sortBy: [
['size.height', 'DESC'], ['size.height', 'DESC'],
['size.length', 'DESC'], ['size.length', 'DESC'],
['toys.(size.height)', 'DESC'],
], ],
} }
@ -954,7 +970,9 @@ describe('paginate', () => {
const orderedCats = [copyCats[3], copyCats[1], copyCats[2], copyCats[0], copyCats[4]] const orderedCats = [copyCats[3], copyCats[1], copyCats[2], copyCats[0], copyCats[4]]
expect(result.data).toStrictEqual(orderedCats) expect(result.data).toStrictEqual(orderedCats)
expect(result.links.current).toBe('?page=1&limit=20&sortBy=size.height:DESC&sortBy=size.length:DESC') expect(result.links.current).toBe(
'?page=1&limit=20&sortBy=size.height:DESC&sortBy=size.length:DESC&sortBy=toys.(size.height):DESC'
)
}) })
it('should return result based on sort on embedded entity on one-to-many relation', async () => { it('should return result based on sort on embedded entity on one-to-many relation', async () => {
@ -1810,6 +1828,12 @@ describe('paginate', () => {
expect(result.links.current).toBe('?page=1&limit=20&sortBy=home.countCat:ASC') expect(result.links.current).toBe('?page=1&limit=20&sortBy=home.countCat:ASC')
}) })
// TODO: Make all tests pass postgres driver.
if (process.env.DB === 'postgres') {
// We end postgres coverage here. See TODO above.
return
}
it('should return result based on or between range filter', async () => { it('should return result based on or between range filter', async () => {
const config: PaginateConfig<CatEntity> = { const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'], sortableColumns: ['id'],

View File

@ -170,7 +170,10 @@ export async function paginate<T extends ObjectLiteral>(
const { isVirtualProperty } = extractVirtualProperty(queryBuilder, columnProperties) const { isVirtualProperty } = extractVirtualProperty(queryBuilder, columnProperties)
const isRelation = checkIsRelation(queryBuilder, columnProperties.propertyPath) const isRelation = checkIsRelation(queryBuilder, columnProperties.propertyPath)
const isEmbeded = checkIsEmbedded(queryBuilder, columnProperties.propertyPath) const isEmbeded = checkIsEmbedded(queryBuilder, columnProperties.propertyPath)
const alias = fixColumnAlias(columnProperties, queryBuilder.alias, isRelation, isVirtualProperty, isEmbeded) let alias = fixColumnAlias(columnProperties, queryBuilder.alias, isRelation, isVirtualProperty, isEmbeded)
if (isVirtualProperty) {
alias = `"${alias}"`
}
queryBuilder.addOrderBy(alias, order[1], nullSort) queryBuilder.addOrderBy(alias, order[1], nullSort)
} }