Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve FindOptionsOrder.nulls to allow FIRST/LAST in uppercase #8978

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
69 changes: 69 additions & 0 deletions test/github-issues/8970/issue-8970.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import "reflect-metadata"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src"
import { TestEntity } from "./entities/TestEntity"
import { expect } from "chai"

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

before(async () => {
dataSources = await createTestingConnections({
entities: [__dirname + "/entities/*{.js,.ts}"],
enabledDrivers: ["postgres", "sqlite", "better-sqlite3"],
schemaCreate: true,
dropSchema: false,
})
})
beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))

const runTest = async (
dataSource: DataSource,
firstOrLastString: "first" | "FIRST" | "last" | "LAST",
) => {
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
}

it(`should work with uppercase/lowercase first/last`, async () => {
return Promise.all(
dataSources.map(async (dataSource) => {
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)
}

await runTest(dataSource, "first")
await runTest(dataSource, "FIRST")
await runTest(dataSource, "last")
await runTest(dataSource, "LAST")
}),
)
})
})