Skip to content

Commit

Permalink
fix: resolve not returning soft deleted relations with withDeleted fi…
Browse files Browse the repository at this point in the history
…nd option (#8017)
  • Loading branch information
nicosefer committed Aug 10, 2021
1 parent 5a3767f commit 65cbcc7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/find-options/FindOptionsUtils.ts
Expand Up @@ -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 => {
Expand Down Expand Up @@ -169,10 +173,6 @@ export class FindOptionsUtils {
}
}

if (options.withDeleted) {
qb.withDeleted();
}

if (options.loadRelationIds === true) {
qb.loadAllRelationIds();

Expand Down
3 changes: 3 additions & 0 deletions test/functional/query-builder/soft-delete/entity/Photo.ts
@@ -1,4 +1,5 @@
import {Entity} from "../../../../../src/decorator/entity/Entity";
import {DeleteDateColumn} from "../../../../../src/decorator/columns/DeleteDateColumn";
import {PrimaryGeneratedColumn} from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Column} from "../../../../../src/decorator/columns/Column";
import {Counters} from "./Counters";
Expand All @@ -15,4 +16,6 @@ export class Photo {
@Column(type => Counters)
counters: Counters;

@DeleteDateColumn()
deletedAt: Date;
}
7 changes: 6 additions & 1 deletion test/functional/query-builder/soft-delete/entity/User.ts
Expand Up @@ -2,6 +2,8 @@ import {Entity} from "../../../../../src/decorator/entity/Entity";
import {PrimaryGeneratedColumn} from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {DeleteDateColumn} from "../../../../../src/decorator/columns/DeleteDateColumn";
import {Column} from "../../../../../src/decorator/columns/Column";
import {Photo} from "./Photo";
import {JoinColumn,OneToOne} from "../../../../../src";

@Entity()
export class User {
Expand All @@ -15,7 +17,10 @@ export class User {
@Column()
likesCount: number = 0;

@OneToOne(() => Photo)
@JoinColumn()
picture: Photo;

@DeleteDateColumn()
deletedAt: Date;

}
Expand Up @@ -235,4 +235,44 @@ describe("query builder > soft-delete", () => {

})));

});
it("should find with soft deleted relations", () => Promise.all(connections.map(async connection => {
const photoRepository = connection.getRepository(Photo);
const userRepository = connection.getRepository(User);

const photo1 = new Photo();
photo1.url = "image-1.jpg";

const photo2 = new Photo();
photo2.url = "image-2.jpg";

const user1 = new User();
user1.name = "user-1";
user1.picture = photo1;

const user2 = new User();
user2.name = "user-2";
user2.picture = photo2;

await photoRepository.save(photo1);
await photoRepository.save(photo2);
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 photoRepository.softDelete(photo1);

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

0 comments on commit 65cbcc7

Please sign in to comment.