Skip to content

Commit

Permalink
fix: resolve FindOptionsOrder.nulls issue to accept FIRST/LAST in upp…
Browse files Browse the repository at this point in the history
…ercase

Closes #8970
  • Loading branch information
ahmedosama94 committed May 9, 2022
1 parent 78df84c commit 7c56c46
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3890,9 +3890,9 @@ export class SelectQueryBuilder<Entity>
? (order[key] as any).nulls
: undefined
nulls =
nulls === "first"
nulls?.toLowerCase() === "first"
? "NULLS FIRST"
: nulls === "last"
: nulls?.toLowerCase() === "last"
? "NULLS LAST"
: undefined

Expand Down
16 changes: 16 additions & 0 deletions test/github-issues/8970/entities/TestEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
Column,
Entity,
ObjectIdColumn,
PrimaryGeneratedColumn,
} from "../../../../src"

@Entity()
export class TestEntity {
@ObjectIdColumn()
@PrimaryGeneratedColumn()
id: number

@Column({ nullable: true })
testColumn: string
}
102 changes: 102 additions & 0 deletions test/github-issues/8970/issue-8970.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import "reflect-metadata"

import {
closeTestingConnections,
createTestingConnections,
} from "../../utils/test-utils"
import { DataSource } from "../../../src"
import { TestEntity } from "./entities/TestEntity"
import { expect } from "chai"

describe("query builder order nulls first/last", () => {
let dataSources: DataSource[]

before(async () => {
dataSources = await createTestingConnections({
entities: [__dirname + "/entities/*{.js,.ts}"],
enabledDrivers: [
"postgres",
"sap",
"sqlite",
"cordova",
"react-native",
"nativescript",
"sqljs",
"oracle",
"mssql",
"aurora-mysql",
"aurora-postgres",
"expo",
"better-sqlite3",
"capacitor",
"spanner",
],
schemaCreate: true,
dropSchema: false,
})

for (const dataSource of dataSources) {
const repository = dataSource.getRepository(TestEntity)

for (let i = 0; i < 5; i++) {
const entity = new TestEntity()
entity.testColumn = ""

await repository.save(entity)
}

for (let i = 0; i < 5; i++) {
const entity = new TestEntity()

await repository.save(entity)
}
}
})

after(async () => {
for (const dataSource of dataSources) {
await dataSource.dropDatabase()
}

await closeTestingConnections(dataSources)
})

const runTest = async (
firstOrLastString: "first" | "FIRST" | "last" | "LAST",
) => {
for (const dataSource of dataSources) {
const repository = dataSource.getRepository(TestEntity)
const testArray = await repository.find({
order: {
testColumn: { direction: "DESC", nulls: firstOrLastString },
},
})
const test =
["first", "FIRST"].indexOf(firstOrLastString) !== -1
? testArray.shift()
: testArray.pop()

expect(test?.testColumn).to.be.null
}
}

describe("first tests", () => {
it("should work with lowercase", async () => {
await runTest("first")
})

it("should work with uppercase", async () => {
await runTest("FIRST")
})
})

describe("last tests", async () => {
it("should work with lowercase", async () => {
await runTest("last")
})

it("should work with uppercase", async () => {
await runTest("LAST")
})
})
})

0 comments on commit 7c56c46

Please sign in to comment.