-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve FindOptionsOrder.nulls issue to accept FIRST/LAST in upp…
…ercase Closes #8970
- Loading branch information
1 parent
78df84c
commit 19cb8b3
Showing
3 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import "reflect-metadata" | ||
|
||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
} from "../../utils/test-utils" | ||
import { DatabaseType, DataSource } from "../../../src" | ||
import { TestEntity } from "./entities/TestEntity" | ||
import { expect } from "chai" | ||
|
||
describe("query builder order nulls first/last", async () => { | ||
let dataSources: DataSource[] | ||
const enabledDrivers: DatabaseType[] = [ | ||
"postgres", | ||
"sqlite", | ||
"better-sqlite3", | ||
] | ||
|
||
before(async () => { | ||
dataSources = await createTestingConnections({ | ||
entities: [__dirname + "/entities/*{.js,.ts}"], | ||
enabledDrivers, | ||
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 ( | ||
dataSource: DataSource | undefined, | ||
firstOrLastString: "first" | "FIRST" | "last" | "LAST", | ||
) => { | ||
if (!dataSource) return | ||
|
||
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 | ||
} | ||
|
||
for (const enabledDriver of enabledDrivers) { | ||
it(`first should work with lowercase (${enabledDriver})`, async () => { | ||
const dataSource = dataSources.find( | ||
(dataSource) => dataSource.options.type === enabledDriver, | ||
) | ||
expect(dataSource).to.exist | ||
await runTest(dataSource, "first") | ||
}) | ||
|
||
it(`FIRST should work with uppercase (${enabledDriver})`, async () => { | ||
const dataSource = dataSources.find( | ||
(dataSource) => dataSource.options.type === enabledDriver, | ||
) | ||
expect(dataSource).to.exist | ||
await runTest(dataSource, "FIRST") | ||
}) | ||
|
||
it(`last should work with lowercase (${enabledDriver})`, async () => { | ||
const dataSource = dataSources.find( | ||
(dataSource) => dataSource.options.type === enabledDriver, | ||
) | ||
expect(dataSource).to.exist | ||
await runTest(dataSource, "last") | ||
}) | ||
|
||
it(`LAST should work with uppercase (${enabledDriver})`, async () => { | ||
const dataSource = dataSources.find( | ||
(dataSource) => dataSource.options.type === enabledDriver, | ||
) | ||
expect(dataSource).to.exist | ||
await runTest(dataSource, "LAST") | ||
}) | ||
} | ||
}) |