Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add soft remove and recover methods to entity (#5854)
* Implement soft remove and recover for entity. * Add test for entity soft remove and recover. * Fix entity soft remove and recover test.
- Loading branch information
1 parent
e584297
commit 9d2b8e0
Showing
4 changed files
with
143 additions
and
4 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
106 changes: 106 additions & 0 deletions
106
test/functional/repository/soft-delete/entity-soft-remove.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,106 @@ | ||
import "reflect-metadata"; | ||
import {expect} from "chai"; | ||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils"; | ||
import {Connection} from "../../../../src/connection/Connection"; | ||
import {Post} from "./entity/Post"; | ||
import { PostWithoutDeleteDateColumn } from "./entity/PostWithoutDeleteDateColumn"; | ||
import { MissingDeleteDateColumnError } from "../../../../src/error/MissingDeleteDateColumnError"; | ||
import { PromiseUtils } from "../../../../src"; | ||
|
||
describe("entity > soft-remove", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should perform soft removal and recovery correctly", () => PromiseUtils.runInSequence(connections, async connection => { | ||
Post.useConnection(connection); // change connection each time because of AR specifics | ||
|
||
const postRepository = connection.getRepository(Post); | ||
|
||
// save a new posts | ||
const newPost1 = postRepository.create({ | ||
id: 1, | ||
name: "post#1" | ||
}); | ||
const newPost2 = postRepository.create({ | ||
id: 2, | ||
name: "post#2" | ||
}); | ||
|
||
await postRepository.save(newPost1); | ||
await postRepository.save(newPost2); | ||
|
||
// soft-remove one | ||
await newPost1.softRemove(); | ||
|
||
// load to check | ||
const loadedPosts = await postRepository.find({ withDeleted: true }); | ||
|
||
// assert | ||
loadedPosts.length.should.be.equal(2); | ||
|
||
const loadedPost1 = loadedPosts.find(p => p.id === 1); | ||
expect(loadedPost1).to.exist; | ||
expect(loadedPost1!.deletedAt).to.be.instanceof(Date); | ||
expect(loadedPost1!.name).to.equals("post#1"); | ||
const loadedPost2 = loadedPosts.find(p => p.id === 2); | ||
expect(loadedPost2).to.exist; | ||
expect(loadedPost2!.deletedAt).to.equals(null); | ||
expect(loadedPost2!.name).to.equals("post#2"); | ||
|
||
// recover one | ||
await loadedPost1!.recover(); | ||
// load to check | ||
const recoveredPosts = await postRepository.find({ withDeleted: true }); | ||
|
||
// assert | ||
recoveredPosts.length.should.be.equal(2); | ||
|
||
const recoveredPost1 = recoveredPosts.find(p => p.id === 1); | ||
expect(recoveredPost1).to.exist; | ||
expect(recoveredPost1!.deletedAt).to.equals(null); | ||
expect(recoveredPost1!.name).to.equals("post#1"); | ||
const recoveredPost2 = recoveredPosts.find(p => p.id === 2); | ||
expect(recoveredPost2).to.exist; | ||
expect(recoveredPost2!.deletedAt).to.equals(null); | ||
expect(recoveredPost2!.name).to.equals("post#2"); | ||
|
||
})); | ||
|
||
it("should throw error when delete date column is missing", () => PromiseUtils.runInSequence(connections, async connection => { | ||
PostWithoutDeleteDateColumn.useConnection(connection); // change connection each time because of AR specifics | ||
|
||
const postRepository = connection.getRepository(PostWithoutDeleteDateColumn); | ||
|
||
// save a new posts | ||
const newPost1 = postRepository.create({ | ||
id: 1, | ||
name: "post#1" | ||
}); | ||
|
||
await postRepository.save(newPost1); | ||
|
||
let error1: Error | undefined; | ||
try { | ||
// soft-remove one | ||
await newPost1.softRemove(); | ||
} catch (err) { | ||
error1 = err; | ||
} | ||
expect(error1).to.be.an.instanceof(MissingDeleteDateColumnError); | ||
|
||
let error2: Error | undefined; | ||
try { | ||
// recover one | ||
await newPost1.recover(); | ||
} catch (err) { | ||
error2 = err; | ||
} | ||
expect(error2).to.be.an.instanceof(MissingDeleteDateColumnError); | ||
|
||
})); | ||
}); |
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
5 changes: 3 additions & 2 deletions
5
test/functional/repository/soft-delete/entity/PostWithoutDeleteDateColumn.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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import {Entity} from "../../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../../src/decorator/columns/Column"; | ||
import {BaseEntity} from "../../../../../src"; | ||
|
||
@Entity() | ||
export class PostWithoutDeleteDateColumn { | ||
export class PostWithoutDeleteDateColumn extends BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
} | ||
} |