From 60fa0e2ea495b48096d8d6de9fcbe08d7ba30a77 Mon Sep 17 00:00:00 2001 From: nicosefer Date: Tue, 3 Aug 2021 19:37:03 -0300 Subject: [PATCH 1/2] move apply withDeleted logic first --- src/find-options/FindOptionsUtils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/find-options/FindOptionsUtils.ts b/src/find-options/FindOptionsUtils.ts index 61cd07bebb..5a415c2f6f 100644 --- a/src/find-options/FindOptionsUtils.ts +++ b/src/find-options/FindOptionsUtils.ts @@ -96,6 +96,10 @@ export class FindOptionsUtils { const metadata = qb.expressionMap.mainAlias!.metadata; // apply all options from FindOptions + if (options.withDeleted) { + qb.withDeleted(); + } + if (options.select) { qb.select([]); options.select.forEach(select => { @@ -169,10 +173,6 @@ export class FindOptionsUtils { } } - if (options.withDeleted) { - qb.withDeleted(); - } - if (options.loadRelationIds === true) { qb.loadAllRelationIds(); From b953e895933e35a2e1cb8ca394ece9cacf0a6f55 Mon Sep 17 00:00:00 2001 From: nicosefer Date: Tue, 3 Aug 2021 19:37:03 -0300 Subject: [PATCH 2/2] move apply withDeleted logic first --- src/find-options/FindOptionsUtils.ts | 8 ++-- test/github-issues/7490/entity/Image.ts | 13 +++++ test/github-issues/7490/entity/User.ts | 15 ++++++ test/github-issues/7490/issue-7490.ts | 63 +++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 test/github-issues/7490/entity/Image.ts create mode 100644 test/github-issues/7490/entity/User.ts create mode 100644 test/github-issues/7490/issue-7490.ts diff --git a/src/find-options/FindOptionsUtils.ts b/src/find-options/FindOptionsUtils.ts index 61cd07bebb..5a415c2f6f 100644 --- a/src/find-options/FindOptionsUtils.ts +++ b/src/find-options/FindOptionsUtils.ts @@ -96,6 +96,10 @@ export class FindOptionsUtils { const metadata = qb.expressionMap.mainAlias!.metadata; // apply all options from FindOptions + if (options.withDeleted) { + qb.withDeleted(); + } + if (options.select) { qb.select([]); options.select.forEach(select => { @@ -169,10 +173,6 @@ export class FindOptionsUtils { } } - if (options.withDeleted) { - qb.withDeleted(); - } - if (options.loadRelationIds === true) { qb.loadAllRelationIds(); diff --git a/test/github-issues/7490/entity/Image.ts b/test/github-issues/7490/entity/Image.ts new file mode 100644 index 0000000000..e9de82a58b --- /dev/null +++ b/test/github-issues/7490/entity/Image.ts @@ -0,0 +1,13 @@ +import {Column, Entity, PrimaryGeneratedColumn, DeleteDateColumn} from "../../../../src"; + +@Entity() +export class Image { + @PrimaryGeneratedColumn() + id: number; + + @Column() + url: string; + + @DeleteDateColumn() + deletedAt: Date; +} diff --git a/test/github-issues/7490/entity/User.ts b/test/github-issues/7490/entity/User.ts new file mode 100644 index 0000000000..e9c6cb5210 --- /dev/null +++ b/test/github-issues/7490/entity/User.ts @@ -0,0 +1,15 @@ +import {Column,JoinColumn, Entity, PrimaryGeneratedColumn, OneToOne} from "../../../../src"; +import { Image } from "./Image"; + +@Entity() +export class User { + @PrimaryGeneratedColumn() + id: number; + + @Column() + name: string; + + @OneToOne(() => Image) + @JoinColumn() + picture: Image; +} diff --git a/test/github-issues/7490/issue-7490.ts b/test/github-issues/7490/issue-7490.ts new file mode 100644 index 0000000000..17c07b03b8 --- /dev/null +++ b/test/github-issues/7490/issue-7490.ts @@ -0,0 +1,63 @@ +import "reflect-metadata"; +import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; +import { Connection } from "../../../src/connection/Connection"; +import { expect } from "chai"; +import { User } from "./entity/User"; +import { Image } from "./entity/Image"; + + +describe("github issues > #7490 Not returning soft deleted nested entities using withDeleted:true", () => { + + let connections: Connection[]; + before(async () => connections = await createTestingConnections({ + entities: [__dirname + "/entity/*{.js,.ts}"], + schemaCreate: true, + dropSchema: true, + })); + beforeEach(() => reloadTestingDatabases(connections)); + after(() => closeTestingConnections(connections)); + + it("should find with soft deleted relations", () => Promise.all(connections.map(async connection => { + const imageRepository = connection.getRepository(Image); + const userRepository = connection.getRepository(User); + + const image1 = new Image(); + image1.url = "image-1.jpg"; + + const image2 = new Image(); + image2.url = "image-2.jpg"; + + + const user1 = new User(); + user1.name = "user-1"; + user1.picture = image1; + + const user2 = new User(); + user2.name = "user-2"; + user2.picture = image2; + + await imageRepository.save(image1); + await imageRepository.save(image2); + await userRepository.save(user1); + await userRepository.save(user2); + + const users = await userRepository.find({ + relations: ["picture"] + }); + + expect(users[0].picture.deletedAt).to.equal(null); + expect(users[1].picture.deletedAt).to.equal(null); + + await imageRepository.softDelete(image1); + + const usersWithSoftDelete = await userRepository.find({ + withDeleted: true, + relations: ["picture"] + }); + + expect(usersWithSoftDelete[0].picture.deletedAt).to.not.equal(null); + expect(usersWithSoftDelete[1].picture.deletedAt).to.equal(null); + }))); + + // you can add additional tests if needed +}); \ No newline at end of file