Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: eager relation respects children relations (#5685)
- Loading branch information
Showing
5 changed files
with
180 additions
and
12 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
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,24 @@ | ||
import {Column, Entity, JoinColumn, OneToMany, OneToOne, PrimaryGeneratedColumn} from "../../../../src"; | ||
import {User} from "./User"; | ||
|
||
@Entity() | ||
export class Company { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@OneToOne( | ||
() => User, | ||
) | ||
@JoinColumn() | ||
admin: User; | ||
|
||
@OneToMany( | ||
() => User, | ||
user => user.company, | ||
) | ||
public staff?: Array<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,22 @@ | ||
import {Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn} from "../../../../src"; | ||
import {Company} from "./Company"; | ||
|
||
@Entity() | ||
export class User { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@ManyToOne( | ||
() => Company, | ||
company => company.staff, | ||
{ | ||
eager: true, // <- this cases the bug. | ||
}, | ||
) | ||
@JoinColumn() | ||
company: Company; | ||
} |
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,85 @@ | ||
import {expect} from "chai"; | ||
import "reflect-metadata"; | ||
import {Connection} from "../../../src"; | ||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; | ||
import {Company} from "./entity/Company"; | ||
import {User} from "./entity/User"; | ||
|
||
describe("github issues > #5684 eager relation skips children relations", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [User, Company], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should select children of an eager relation", () => Promise.all(connections.map(async connection => { | ||
const company = new Company(); | ||
company.name = "company"; | ||
await connection.getRepository(Company).save(company); | ||
|
||
const userAdmin = new User(); | ||
userAdmin.name = "admin"; | ||
userAdmin.company = company; | ||
await connection.getRepository(User).save(userAdmin); | ||
|
||
const userNormal = new User(); | ||
userNormal.name = "normal"; | ||
userNormal.company = company; | ||
await connection.getRepository(User).save(userNormal); | ||
|
||
company.admin = userAdmin; | ||
await connection.getRepository(Company).save(company); | ||
|
||
const assert = (user?: User): void => { | ||
expect(user && user.company && user.company.admin) | ||
.to.be.a.instanceOf(User, "loads nested relation of an eager relation"); | ||
expect(user && user.company && user.company.staff) | ||
.to.have.length(2, "loads nested relation of an eager relation"); | ||
for (const member of user && user.company.staff || []) { | ||
expect(member).to.be.a.instanceOf(User, "loads nested relation of an eager relation"); | ||
expect(member.company).to.be.a.instanceOf(Company, "loads nested relation of an eager relation"); | ||
expect(member.company.admin).to.be.a.instanceOf(User, "loads nested relation of an eager relation"); | ||
expect(member.company.admin.company).to.be.a.instanceOf(Company, "still loads an eager relation"); | ||
} | ||
}; | ||
|
||
const relations = [ | ||
"company", | ||
"company.admin", // <-- can't be loaded without the fix. | ||
"company.staff", // <-- can't be loaded without the fix. | ||
"company.staff.company", // <-- can't be loaded without the fix. | ||
"company.staff.company.admin", // <-- can't be loaded without the fix. | ||
]; | ||
|
||
const user1 = await connection.getRepository(User).findOne(userAdmin.id, { | ||
relations: [...relations], | ||
}); | ||
assert(user1); | ||
const user2 = await connection.getRepository(User).findOneOrFail(userAdmin.id, { | ||
relations: [...relations], | ||
}); | ||
assert(user2); | ||
const users3 = await connection.getRepository(User).find({ | ||
where: { | ||
id: userAdmin.id, | ||
}, | ||
relations: [...relations], | ||
}); | ||
assert(users3.pop()); | ||
const [users4] = await connection.getRepository(User).findAndCount({ | ||
where: { | ||
id: userAdmin.id, | ||
}, | ||
relations: [...relations], | ||
}); | ||
assert(users4.pop()); | ||
const users5 = await connection.getRepository(User).findByIds([userAdmin.id], { | ||
relations: [...relations], | ||
}); | ||
assert(users5.pop()); | ||
}))); | ||
}); |