From 0e56f0fcf8ec3f2ec37fee92f75ba09262801655 Mon Sep 17 00:00:00 2001 From: Umed Khudoiberdiev Date: Tue, 18 Apr 2023 22:52:05 +0500 Subject: [PATCH] fix: select + addOrderBy broke in 0.3.14 (#9961) * added test for #9960 * fixing the issue * limiting only to postgres because of NULLS FIRST syntax * lint --- src/query-builder/SelectQueryBuilder.ts | 4 +- .../9960/entity/ExampleEntity.ts | 12 ++++++ test/github-issues/9960/issue-9960.ts | 42 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 test/github-issues/9960/entity/ExampleEntity.ts create mode 100644 test/github-issues/9960/issue-9960.ts diff --git a/src/query-builder/SelectQueryBuilder.ts b/src/query-builder/SelectQueryBuilder.ts index 9a0f915720..d3b01053fe 100644 --- a/src/query-builder/SelectQueryBuilder.ts +++ b/src/query-builder/SelectQueryBuilder.ts @@ -2515,9 +2515,7 @@ export class SelectQueryBuilder column.databaseName, ) return ( - this.escape(orderAlias) + - " " + - orderBys[columnName] + this.escape(orderAlias) + " " + orderValue ) } } diff --git a/test/github-issues/9960/entity/ExampleEntity.ts b/test/github-issues/9960/entity/ExampleEntity.ts new file mode 100644 index 0000000000..0a5bef90e7 --- /dev/null +++ b/test/github-issues/9960/entity/ExampleEntity.ts @@ -0,0 +1,12 @@ +import { Entity } from "../../../../src/decorator/entity/Entity" +import { Column } from "../../../../src/decorator/columns/Column" +import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn" + +@Entity() +export class ExampleEntity { + @PrimaryGeneratedColumn() + id: number + + @Column() + name: string +} diff --git a/test/github-issues/9960/issue-9960.ts b/test/github-issues/9960/issue-9960.ts new file mode 100644 index 0000000000..6891766392 --- /dev/null +++ b/test/github-issues/9960/issue-9960.ts @@ -0,0 +1,42 @@ +import { DataSource } from "../../../src" +import { + closeTestingConnections, + createTestingConnections, + reloadTestingDatabases, +} from "../../utils/test-utils" +import { ExampleEntity } from "./entity/ExampleEntity" +import { expect } from "chai" + +describe("github issues > #9960", () => { + let dataSources: DataSource[] + + before(async () => { + dataSources = await createTestingConnections({ + entities: [ExampleEntity], + enabledDrivers: ["postgres"], + }) + }) + + beforeEach(() => reloadTestingDatabases(dataSources)) + after(() => closeTestingConnections(dataSources)) + + it("select + order by must work without issues", async () => { + await Promise.all( + dataSources.map(async (dataSource) => { + const example1 = new ExampleEntity() + example1.name = "example #1" + await dataSource.manager.save(example1) + + const examples = await dataSource.manager + .createQueryBuilder(ExampleEntity, "example") + .select(["example.id", "example.name"]) + .addOrderBy("example.name", "DESC", "NULLS LAST") + .take(1) + .skip(0) + .getMany() + + expect(examples).to.be.eql([{ id: 1, name: "example #1" }]) + }), + ) + }) +})