Skip to content

Commit

Permalink
fix: BaseEntity finder methods to properly typecheck lazy relations c…
Browse files Browse the repository at this point in the history
…onditions (#5710)

Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
  • Loading branch information
bogdan and pleerock committed Nov 11, 2021
1 parent 0334d10 commit 0665ff5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/find-options/FindConditions.ts
Expand Up @@ -3,6 +3,13 @@ import {FindOperator} from "./FindOperator";
/**
* Used for find operations.
*/
export type FindConditions<T> = {
[P in keyof T]?: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>;
export type FindCondition<T> = FindConditions<T> | FindOperator<FindConditions<T>>;

/**
* Used for find operations.
*/
export type FindConditions<T> = T | {
[P in keyof T]?: T[P] extends Promise<infer U>
? FindCondition<U>
: FindCondition<T[P]>;
};
@@ -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[]>;

}
@@ -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>;
}
@@ -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);
}));
});

0 comments on commit 0665ff5

Please sign in to comment.