-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: nested eager relations in a lazy-loaded entity are not loaded (#…
- Loading branch information
Showing
7 changed files
with
256 additions
and
6 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
21 changes: 21 additions & 0 deletions
21
test/functional/relations/eager-relations/lazy-nested-eager-relations/entity/Category.ts
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,21 @@ | ||
import {Entity} from "../../../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../../../src/decorator/columns/Column"; | ||
import {ManyToMany} from "../../../../../../src/decorator/relations/ManyToMany"; | ||
import {JoinTable} from "../../../../../../src/decorator/relations/JoinTable"; | ||
import {Post} from "./Post"; | ||
|
||
@Entity() | ||
export class Category { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@ManyToMany(type => Post, post => post.categories2) | ||
@JoinTable() | ||
posts2: Post[]; | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
test/functional/relations/eager-relations/lazy-nested-eager-relations/entity/Editor.ts
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 { Entity } from "../../../../../../src/decorator/entity/Entity"; | ||
import { ManyToOne } from "../../../../../../src/decorator/relations/ManyToOne"; | ||
import { User } from "./User"; | ||
import { Post } from "./Post"; | ||
import { | ||
JoinColumn, | ||
OneToOne, | ||
PrimaryGeneratedColumn, | ||
} from "../../../../../../src"; | ||
|
||
@Entity() | ||
export class Editor { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@OneToOne((type) => User, { eager: true }) | ||
@JoinColumn() | ||
user: User; | ||
|
||
@ManyToOne((type) => Post, { lazy: true }) | ||
post: Promise<Post>; | ||
} |
34 changes: 34 additions & 0 deletions
34
test/functional/relations/eager-relations/lazy-nested-eager-relations/entity/Post.ts
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,34 @@ | ||
import {Entity} from "../../../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../../../src/decorator/columns/Column"; | ||
import {ManyToMany} from "../../../../../../src/decorator/relations/ManyToMany"; | ||
import {JoinTable} from "../../../../../../src/decorator/relations/JoinTable"; | ||
import {Category} from "./Category"; | ||
import {User} from "./User"; | ||
import {ManyToOne} from "../../../../../../src/decorator/relations/ManyToOne"; | ||
import {OneToMany} from "../../../../../../src/decorator/relations/OneToMany"; | ||
import {Editor} from "./Editor"; | ||
|
||
@Entity() | ||
export class Post { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@ManyToMany(type => Category, { eager: true }) | ||
@JoinTable() | ||
categories1: Category[]; | ||
|
||
@ManyToMany(type => Category, category => category.posts2, { eager: true }) | ||
categories2: Category[]; | ||
|
||
@ManyToOne(type => User, { eager: true }) | ||
author: User; | ||
|
||
@OneToMany(type => Editor, editor => editor.post, { eager: true }) | ||
editors: Editor[]; | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
test/functional/relations/eager-relations/lazy-nested-eager-relations/entity/Profile.ts
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 {PrimaryGeneratedColumn} from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../../../src/decorator/columns/Column"; | ||
|
||
@Entity() | ||
export class Profile { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
about: string; | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
test/functional/relations/eager-relations/lazy-nested-eager-relations/entity/User.ts
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 {Entity} from "../../../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../../../src/decorator/columns/Column"; | ||
import {OneToOne} from "../../../../../../src/decorator/relations/OneToOne"; | ||
import {JoinColumn} from "../../../../../../src/decorator/relations/JoinColumn"; | ||
import {Profile} from "./Profile"; | ||
|
||
@Entity() | ||
export class User { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
firstName: string; | ||
|
||
@Column() | ||
lastName: string; | ||
|
||
@OneToOne(type => Profile, { eager: true }) | ||
@JoinColumn() | ||
profile: Profile; | ||
|
||
} |
123 changes: 123 additions & 0 deletions
123
...onal/relations/eager-relations/lazy-nested-eager-relations/lazy-nested-eager-relations.ts
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,123 @@ | ||
import "reflect-metadata"; | ||
import { Connection } from "../../../../../src/connection/Connection"; | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../../../utils/test-utils"; | ||
import { User } from "./entity/User"; | ||
import { Profile } from "./entity/Profile"; | ||
import { Editor } from "./entity/Editor"; | ||
import { Post } from "./entity/Post"; | ||
import { Category } from "./entity/Category"; | ||
import { expect } from "chai"; | ||
|
||
describe("relations > eager relations > lazy nested eager relations", () => { | ||
let connections: Connection[]; | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
})) | ||
); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
async function prepareData(connection: Connection) { | ||
const profile = new Profile(); | ||
profile.about = "I cut trees!"; | ||
await connection.manager.save(profile); | ||
|
||
const user = new User(); | ||
user.firstName = "Timber"; | ||
user.lastName = "Saw"; | ||
user.profile = profile; | ||
await connection.manager.save(user); | ||
|
||
const primaryCategory1 = new Category(); | ||
primaryCategory1.name = "primary category #1"; | ||
await connection.manager.save(primaryCategory1); | ||
|
||
const primaryCategory2 = new Category(); | ||
primaryCategory2.name = "primary category #2"; | ||
await connection.manager.save(primaryCategory2); | ||
|
||
const secondaryCategory1 = new Category(); | ||
secondaryCategory1.name = "secondary category #1"; | ||
await connection.manager.save(secondaryCategory1); | ||
|
||
const secondaryCategory2 = new Category(); | ||
secondaryCategory2.name = "secondary category #2"; | ||
await connection.manager.save(secondaryCategory2); | ||
|
||
const post = new Post(); | ||
post.title = "about eager relations"; | ||
post.categories1 = [primaryCategory1, primaryCategory2]; | ||
post.categories2 = [secondaryCategory1, secondaryCategory2]; | ||
post.author = user; | ||
await connection.manager.save(post); | ||
|
||
const editor = new Editor(); | ||
editor.post = Promise.resolve(post); | ||
editor.user = user; | ||
await connection.manager.save(editor); | ||
} | ||
|
||
it("should load all eager relations nested inside a lazy relation", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
await prepareData(connection); | ||
|
||
const loadedEditor = await connection.manager.findOne(Editor, 1); | ||
|
||
const loadedPost = await loadedEditor?.post; | ||
|
||
expect(loadedPost?.categories1).to.have.deep.members([ | ||
{ | ||
id: 1, | ||
name: "primary category #1", | ||
}, | ||
{ | ||
id: 2, | ||
name: "primary category #2", | ||
}, | ||
]); | ||
|
||
expect(loadedPost?.categories2).to.have.deep.members([ | ||
{ | ||
id: 3, | ||
name: "secondary category #1", | ||
}, | ||
{ | ||
id: 4, | ||
name: "secondary category #2", | ||
}, | ||
]); | ||
|
||
expect(loadedPost?.author).to.deep.equal({ | ||
id: 1, | ||
firstName: "Timber", | ||
lastName: "Saw", | ||
profile: { | ||
id: 1, | ||
about: "I cut trees!", | ||
}, | ||
}); | ||
|
||
expect(loadedPost?.editors).to.have.deep.members([ | ||
{ | ||
id: 1, | ||
user: { | ||
id: 1, | ||
firstName: "Timber", | ||
lastName: "Saw", | ||
profile: { | ||
id: 1, | ||
about: "I cut trees!", | ||
}, | ||
}, | ||
}, | ||
]); | ||
}) | ||
)); | ||
}); |