From d00151e008d1a620f26b7fe30f6517ff0740086a Mon Sep 17 00:00:00 2001 From: ruifernando7 Date: Thu, 23 Sep 2021 13:44:42 +0700 Subject: [PATCH 1/2] fix: missing order from query builder --- src/Paginator.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Paginator.ts b/src/Paginator.ts index 3fc9cd0..1a21d1c 100644 --- a/src/Paginator.ts +++ b/src/Paginator.ts @@ -120,7 +120,11 @@ export default class Paginator { } builder.take(this.limit + 1); - builder.orderBy(this.buildOrder()); + + const paginationKeyOrders = this.buildOrder(); + Object.keys(paginationKeyOrders).forEach(orderKey => { + builder.addOrderBy(orderKey, paginationKeyOrders[orderKey] === 'ASC' ? 'ASC' : 'DESC') + }); return builder; } From a205e0fa5e6c87149d95c3bf61e45f28e113af9e Mon Sep 17 00:00:00 2001 From: Diogo Batista Date: Fri, 28 Oct 2022 17:44:53 +0200 Subject: [PATCH 2/2] added test for ruifernando7 fix --- test/pagination.ts | 35 ++++++++++++++++++++++++++++++++++- test/utils/prepareData.ts | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/test/pagination.ts b/test/pagination.ts index 4fa4d1a..1f2f23c 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; import { createConnection, getConnection } from 'typeorm'; import { createQueryBuilder } from './utils/createQueryBuilder'; -import { prepareData } from './utils/prepareData'; +import { prepareData, setTimestamp } from './utils/prepareData'; import { User } from './entities/User'; import { Photo } from './entities/Photo'; import { buildPaginator } from '../src/index'; @@ -118,6 +118,39 @@ describe('TypeORM cursor-based pagination test', () => { expect(result.cursor.afterCursor).to.eq(null); }); + describe('when the query has an addOrderBy defined', () => { + it('should return entities with the correct order', async () => { + const data = [ + { + name: 'z-user', + camelCaseColumn: setTimestamp(1), + }, + { + name: 'a-user', + camelCaseColumn: setTimestamp(2), + }, + ]; + + await getConnection().getRepository(User).save(data); + + const queryBuilder = createQueryBuilder(User, 'user').addOrderBy( + 'UPPER(user.name)', + 'ASC', + ); + const paginator = buildPaginator({ + entity: User, + query: { + limit: 1, + }, + }); + + const result = await paginator.paginate(queryBuilder); + + expect(result.data[0].name).to.eq(data[1].name); + expect(result.data[0].name).to.eq(data[0].name); + }); + }); + after(async () => { await getConnection().query('TRUNCATE TABLE users RESTART IDENTITY CASCADE;'); await getConnection().close(); diff --git a/test/utils/prepareData.ts b/test/utils/prepareData.ts index f2edc05..cd23710 100644 --- a/test/utils/prepareData.ts +++ b/test/utils/prepareData.ts @@ -2,7 +2,7 @@ import { getConnection } from 'typeorm'; import { User } from '../entities/User'; import { Snake } from '../entities/Snake'; -function setTimestamp(i: number): Date { +export function setTimestamp(i: number): Date { const now = new Date(); now.setMinutes(now.getMinutes() + i);