Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ export default class Paginator<Entity> {
}

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;
}
Expand Down
35 changes: 34 additions & 1 deletion test/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/utils/prepareData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down