Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: BaseEntity finder methods to properly typecheck lazy relations c…
…onditions (#5710) Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
- Loading branch information
Showing
4 changed files
with
84 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
20 changes: 20 additions & 0 deletions
20
...lations/lazy-relations/lazy-relations-loading-via-base-entitiy-finders/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,20 @@ | ||
import {Entity} from "../../../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../../../src/decorator/columns/Column"; | ||
import {Post} from "./Post"; | ||
import {OneToMany} from "../../../../../../src/decorator/relations/OneToMany"; | ||
import { BaseEntity } from "../../../../../../src"; | ||
|
||
@Entity() | ||
export class Category extends BaseEntity { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@OneToMany(() => Post, post => post.category) | ||
posts: Promise<Post[]>; | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...l/relations/lazy-relations/lazy-relations-loading-via-base-entitiy-finders/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,21 @@ | ||
import {Entity} from "../../../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../../../src/decorator/columns/Column"; | ||
import {Category} from "./Category"; | ||
import {ManyToOne} from "../../../../../../src/decorator/relations/ManyToOne"; | ||
import {BaseEntity} from "../../../../../../src"; | ||
|
||
@Entity() | ||
export class Post extends BaseEntity { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@ManyToOne(() => Category, category => category.posts, { | ||
cascade: ["insert"] | ||
}) | ||
category: Promise<Category>; | ||
} |
34 changes: 34 additions & 0 deletions
34
...ctional/relations/lazy-relations/lazy-relations-loading-via-base-entitiy-finders/index.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 {Post} from "./entity/Post"; | ||
import {Category} from "./entity/Category"; | ||
import "reflect-metadata"; | ||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../../utils/test-utils"; | ||
import {Connection} from "../../../../../src/connection/Connection"; | ||
import {expect} from "chai"; | ||
import {PromiseUtils} from "../../../../../src"; | ||
|
||
describe("lazy-relations-loading-via-base-entity-finders", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
// we can properly test lazy-relations only on one platform | ||
enabledDrivers: ["mysql"] | ||
})); | ||
|
||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("works", () => PromiseUtils.runInSequence(connections, async connection => { | ||
Category.useConnection(connection); | ||
Post.useConnection(connection); | ||
const category = new Category(); | ||
category.name = "hello"; | ||
await category.save(); | ||
const post = new Post(); | ||
post.title = "hello post"; | ||
post.category = Promise.resolve(category); | ||
await post.save(); | ||
expect((await Post.findOneOrFail({category})).id).equal(post.id); | ||
expect((await Post.findOneOrFail({category: {id: category.id}})).id).equal(post.id); | ||
})); | ||
}); |