Skip to content

Commit

Permalink
fix(sql): allow using dot inside custom order by expression
Browse files Browse the repository at this point in the history
Closes #1067
  • Loading branch information
B4nan committed Nov 10, 2020
1 parent 7167646 commit 11e8c56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/knex/src/query/QueryBuilderHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,22 @@ export class QueryBuilderHelper {
getQueryOrder(type: QueryType, orderBy: FlatQueryOrderMap, populate: Dictionary<string>): string {
const ret: string[] = [];
Object.keys(orderBy).forEach(k => {
const direction = orderBy[k];
const order = Utils.isNumber<QueryOrderNumeric>(direction) ? QueryOrderNumeric[direction] : direction;

if (QueryBuilderHelper.isCustomExpression(k)) {
ret.push(`${k} ${order.toLowerCase()}`);
return;
}

// eslint-disable-next-line prefer-const
let [alias, field] = this.splitField(k);
alias = populate[alias] || alias;
Utils.splitPrimaryKeys(field).forEach(f => {
const direction = orderBy[k];
const prop = this.getProperty(f, alias);
const noPrefix = (prop && prop.persist === false) || QueryBuilderHelper.isCustomExpression(f);
const order = Utils.isNumber<QueryOrderNumeric>(direction) ? QueryOrderNumeric[direction] : direction;
const column = this.mapper(noPrefix ? f : `${alias}.${f}`, type);
/* istanbul ignore next */
const rawColumn = Utils.isString(column) ? column.split('.').map(e => this.knex.ref(e)).join('.') : column;

ret.push(`${rawColumn} ${order.toLowerCase()}`);
Expand Down
12 changes: 12 additions & 0 deletions tests/QueryBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ describe('QueryBuilder', () => {
qb.select('*').where({ name: 'test 123', type: PublisherType.GLOBAL }).orderBy({ name: QueryOrder.DESC, type: QueryOrder.ASC }).limit(2, 1);
expect(qb.getQuery()).toEqual('select `e0`.* from `publisher2` as `e0` where `e0`.`name` = ? and `e0`.`type` = ? order by `e0`.`name` desc, `e0`.`type` asc limit ? offset ?');
expect(qb.getParams()).toEqual(['test 123', PublisherType.GLOBAL, 2, 1]);

const qb1 = orm.em.createQueryBuilder(Publisher2);
qb1.select('*')
.where({ name: 'test 123', type: PublisherType.GLOBAL })
.orderBy({ [`(point(location_latitude, location_longitude) <@> point(${53}, ${9}))`]: 'ASC' });
expect(qb1.getFormattedQuery()).toBe('select `e0`.* from `publisher2` as `e0` where `e0`.`name` = \'test 123\' and `e0`.`type` = \'global\' order by (point(location_latitude, location_longitude) <@> point(53, 9)) asc');

const qb2 = orm.em.createQueryBuilder(Publisher2);
qb2.select('*')
.where({ name: 'test 123', type: PublisherType.GLOBAL })
.orderBy({ [`(point(location_latitude, location_longitude) <@> point(${53.46}, ${9.90}))`]: 'ASC' });
expect(qb2.getFormattedQuery()).toBe('select `e0`.* from `publisher2` as `e0` where `e0`.`name` = \'test 123\' and `e0`.`type` = \'global\' order by (point(location_latitude, location_longitude) <@> point(53.46, 9.9)) asc');
});

test('select query picks read replica', async () => {
Expand Down

0 comments on commit 11e8c56

Please sign in to comment.