-
-
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: use a prefix on SelectQueryBuilder internal parameters (#5178)
* Use a leading underscore on SelectQueryBuilder internal parameters * Add test case for issue #5174 * Fix lint issues * Improve test for issue #5174 * Use a different prefix for the former ...ids * Remove .only
- Loading branch information
Showing
4 changed files
with
85 additions
and
4 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,14 @@ | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
import {PrimaryColumn, OneToMany} from "../../../../src"; | ||
import {User} from "./User"; | ||
|
||
@Entity() | ||
export class Role { | ||
|
||
@PrimaryColumn() | ||
id: string; | ||
|
||
@OneToMany(_ => User, user => user.role, { cascade: true }) | ||
users: User[]; | ||
|
||
} |
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,14 @@ | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
import {PrimaryColumn, ManyToOne} from "../../../../src"; | ||
import {Role} from "./Role"; | ||
|
||
@Entity() | ||
export class User { | ||
|
||
@PrimaryColumn() | ||
id: number; | ||
|
||
@ManyToOne(_ => Role, role => role.users) | ||
role: Role; | ||
|
||
} |
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,52 @@ | ||
import "reflect-metadata"; | ||
import {expect} from "chai"; | ||
import {Connection} from "../../../src"; | ||
import {User} from "./entity/User"; | ||
import {Role} from "./entity/Role"; | ||
import {createTestingConnections, reloadTestingDatabases, closeTestingConnections} from "../../utils/test-utils"; | ||
|
||
describe("github issues > #5174 `selectQueryBuilder.take` messes up the query when using the `ids` parameter", () => { | ||
|
||
let connections: Connection[]; | ||
|
||
before(async () => { | ||
connections = await createTestingConnections({ | ||
entities: [User, Role], | ||
schemaCreate: true, | ||
dropSchema: true | ||
}); | ||
}); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should allow the 'ids' parameter without messing up the query when using .take", () => | ||
Promise.all(connections.map(async (connection) => { | ||
const roleRepository = connection.getRepository(Role); | ||
const userRepository = connection.getRepository(User); | ||
|
||
const userWithId = (id: number) => ({ id }); | ||
|
||
await roleRepository.save([ | ||
{ id: "a", users: [1, 2, 3].map(userWithId) }, | ||
{ id: "b", users: [9, 10, 11, 12, 13].map(userWithId) }, | ||
{ id: "c", users: [14, 15, 16, 17].map(userWithId) } | ||
]); | ||
|
||
|
||
const results = await userRepository.createQueryBuilder("user") | ||
.leftJoinAndSelect("user.role", "role") | ||
.where("role.id IN (:...ids)", { ids: ["a", "c"] }) | ||
.take(5) | ||
.orderBy("user.id") | ||
.getMany(); | ||
|
||
expect(results).to.be.deep.equal([ | ||
{ id: 1, role: { id: "a" } }, | ||
{ id: 2, role: { id: "a" } }, | ||
{ id: 3, role: { id: "a" } }, | ||
{ id: 14, role: { id: "c" } }, | ||
{ id: 15, role: { id: "c" } } | ||
]); | ||
}))); | ||
|
||
}); |