diff --git a/README.md b/README.md index 789b789..1798cf6 100644 --- a/README.md +++ b/README.md @@ -238,7 +238,7 @@ const paginateConfig: PaginateConfig { * Description: Load eager relations using TypeORM's eager property. * Only works if `relations` is not defined. */ - loadEagerRelations: true + loadEagerRelations: true, /** * Required: false diff --git a/src/__tests__/cat.entity.ts b/src/__tests__/cat.entity.ts index 0dc19cc..b5ba5bd 100644 --- a/src/__tests__/cat.entity.ts +++ b/src/__tests__/cat.entity.ts @@ -35,10 +35,7 @@ export class CatEntity { @Column({ nullable: true }) age: number | null - @Column({ - type: 'simple-enum', - enum: CutenessLevel, - }) + @Column({ type: 'text' }) // We don't use enum type as it makes it easier when testing across different db drivers. cutenessLevel: CutenessLevel @Column(() => SizeEmbed) diff --git a/src/paginate.spec.ts b/src/paginate.spec.ts index f45ea0c..c31441f 100644 --- a/src/paginate.spec.ts +++ b/src/paginate.spec.ts @@ -44,7 +44,7 @@ describe('paginate', () => { database: ':memory:', }), synchronize: true, - logging: false, + logging: true, entities: [CatEntity, CatToyEntity, CatHomeEntity, CatHomePillowEntity], }) await dataSource.initialize() @@ -118,7 +118,7 @@ describe('paginate', () => { const entities = dataSource.entityMetadatas 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]]) }) + it('should sort result by camelcase columns', async () => { + const config: PaginateConfig = { + sortableColumns: ['cutenessLevel', 'name'], + } + const query: PaginateQuery = { + path: '', + sortBy: [ + ['cutenessLevel', 'ASC'], + ['name', 'ASC'], + ], + } + + const result = await paginate(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 () => { const config: PaginateConfig = { 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') }) - // 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 () => { const config: PaginateConfig = { - sortableColumns: ['id', 'name', 'size.height', 'size.length', 'size.width'], + sortableColumns: ['id', 'name', 'size.height', 'size.length', 'size.width', 'toys.(size.height)'], searchableColumns: ['name'], relations: ['home', 'toys'], } @@ -921,6 +936,7 @@ describe('paginate', () => { sortBy: [ ['size.height', '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]] 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 () => { @@ -1810,6 +1828,12 @@ describe('paginate', () => { 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 () => { const config: PaginateConfig = { sortableColumns: ['id'], diff --git a/src/paginate.ts b/src/paginate.ts index 80e95c5..fa162b0 100644 --- a/src/paginate.ts +++ b/src/paginate.ts @@ -170,7 +170,10 @@ export async function paginate( const { isVirtualProperty } = extractVirtualProperty(queryBuilder, columnProperties) const isRelation = checkIsRelation(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) }