Skip to content

Commit

Permalink
fix(core): fix ordering by json properties
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Apr 30, 2021
1 parent b9eaadd commit 53bef71
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
}

orderBy(orderBy: QueryOrderMap): this {
QueryHelper.inlinePrimaryKeyObjects(orderBy, this.metadata.find(this.entityName)!, this.metadata);
this._orderBy = CriteriaNodeFactory.createNode(this.metadata, this.entityName, orderBy).process(this);
const processed = QueryHelper.processWhere(orderBy, this.entityName, this.metadata, this.platform, false)!;
this._orderBy = CriteriaNodeFactory.createNode(this.metadata, this.entityName, processed).process(this);
return this;
}

Expand Down
25 changes: 25 additions & 0 deletions tests/QueryBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,31 @@ describe('QueryBuilder', () => {
const qb13 = pg.em.createQueryBuilder(Book2).where({ meta: { foo: { $lte: 123 } } });
expect(qb13.getFormattedQuery()).toBe(`select "e0".*, "e0".price * 1.19 as "price_taxed" from "book2" as "e0" where ("meta"->>'foo')::float8 <= 123`);

// order by json property
await pg.em.nativeDelete(Book2, {});
await pg.em.nativeDelete(Author2, {});
await pg.em.nativeInsert(Author2, { name: 'n', email: 'e', id: 1 });
await pg.em.getDriver().nativeInsertMany(Book2.name, [
{ uuid: '123e4567-e89b-12d3-a456-426614174001', title: 't1', author: 1, meta: { foo: 3, bar: { str: 'c', num: 3 } } },
{ uuid: '123e4567-e89b-12d3-a456-426614174002', title: 't2', author: 1, meta: { foo: 2, bar: { str: 'b', num: 1 } } },
{ uuid: '123e4567-e89b-12d3-a456-426614174003', title: 't3', author: 1, meta: { foo: 1, bar: { str: 'a', num: 2 } } },
]);

const qb14 = pg.em.createQueryBuilder(Book2).orderBy({ meta: { foo: 'asc' } });
expect(qb14.getFormattedQuery()).toBe(`select "e0".*, "e0".price * 1.19 as "price_taxed" from "book2" as "e0" order by "meta"->>'foo' asc`);
const res14 = await qb14.getResultList();
expect(res14.map(r => r.title)).toEqual(['t3', 't2', 't1']);

const qb15 = pg.em.createQueryBuilder(Book2).orderBy({ meta: { bar: { str: 'asc' } } });
expect(qb15.getFormattedQuery()).toBe(`select "e0".*, "e0".price * 1.19 as "price_taxed" from "book2" as "e0" order by "meta"->'bar'->>'str' asc`);
const res15 = await qb15.getResultList();
expect(res15.map(r => r.title)).toEqual(['t3', 't2', 't1']);

const qb16 = pg.em.createQueryBuilder(Book2).orderBy({ meta: { bar: { num: QueryOrder.DESC } } });
expect(qb16.getFormattedQuery()).toBe(`select "e0".*, "e0".price * 1.19 as "price_taxed" from "book2" as "e0" order by "meta"->'bar'->>'num' desc`);
const res16 = await qb16.getResultList();
expect(res16.map(r => r.title)).toEqual(['t1', 't3', 't2']);

await pg.close(true);
});

Expand Down

0 comments on commit 53bef71

Please sign in to comment.