Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: soft delete recursive cascade (#8436)
* feat: soft-delete-recursive-cascade * fix get entity primary key * better mapping for get parent ids * fix use identifier and not entity from subject object * remove only from test * fix: replace primary key name to valid name * fix: test bug * fix: change returning to work on mssql * fix: work for oracle * add comments to subject executor Co-authored-by: oxeye-yuvalk <oxeye-yuvalk@oxeye.io>
- Loading branch information
1 parent
3d6c5da
commit d0f32b3
Showing
5 changed files
with
191 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
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,75 @@ | ||
import "reflect-metadata"; | ||
import { Connection, Repository } from "../../../src/index"; | ||
import { reloadTestingDatabases, createTestingConnections, closeTestingConnections } from "../../utils/test-utils"; | ||
import { expect } from "chai"; | ||
import { Category } from "./entity/Category"; | ||
import { Post } from "./entity/Post"; | ||
import {Author} from "./entity/Author"; | ||
|
||
describe("Soft Delete Recursive cascade", () => { | ||
|
||
// ------------------------------------------------------------------------- | ||
// Configuration | ||
// ------------------------------------------------------------------------- | ||
|
||
// connect to db | ||
let connections: Connection[] = []; | ||
|
||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
|
||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
// ------------------------------------------------------------------------- | ||
// Specifications | ||
// ------------------------------------------------------------------------- | ||
|
||
describe("when a Post is removed from a Category", () => { | ||
let categoryRepository: Repository<Category>; | ||
let postRepository: Repository<Post>; | ||
let authorRepository: Repository<Author>; | ||
|
||
beforeEach(async () => { | ||
await Promise.all(connections.map(async connection => { | ||
categoryRepository = connection.getRepository(Category); | ||
postRepository = connection.getRepository(Post); | ||
authorRepository = connection.getRepository(Author); | ||
})); | ||
const firstPost: Post = new Post(); | ||
firstPost.authors = [ | ||
new Author(), | ||
new Author() | ||
]; | ||
const secondPost: Post = new Post(); | ||
secondPost.authors = [ | ||
new Author(), | ||
new Author() | ||
]; | ||
const categoryToInsert = new Category(); | ||
categoryToInsert.posts = [ | ||
firstPost, | ||
secondPost | ||
]; | ||
|
||
await categoryRepository.save(categoryToInsert); | ||
let insertedCategory: Category = await categoryRepository.findOneOrFail(); | ||
await categoryRepository.softRemove(insertedCategory); | ||
}); | ||
|
||
it("should delete the category", async () => { | ||
const categoryCount = await categoryRepository.count(); | ||
expect(categoryCount).to.equal(0); | ||
}); | ||
|
||
it("should delete the all the posts", async () => { | ||
const postCount = await postRepository.count(); | ||
expect(postCount).to.equal(0); | ||
}); | ||
it("should delete the all the authors", async () => { | ||
const authorsCount = await authorRepository.count(); | ||
expect(authorsCount).to.equal(0); | ||
}); | ||
}); | ||
}); |
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,24 @@ | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../src/decorator/columns/Column"; | ||
import {ManyToOne} from "../../../../src/decorator/relations/ManyToOne"; | ||
import {JoinColumn} from "../../../../src/decorator/relations/JoinColumn"; | ||
import {DeleteDateColumn} from "../../../../src"; | ||
import {Post} from "./Post"; | ||
|
||
@Entity() | ||
export class Author { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
postId: string; | ||
|
||
@ManyToOne(() => Post, post => post.authors) | ||
@JoinColumn({ name: "postId" }) | ||
post: Post; | ||
|
||
@DeleteDateColumn() | ||
deletedAt?: Date; | ||
} |
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 {Post} from "./Post"; | ||
import {OneToMany} from "../../../../src/decorator/relations/OneToMany"; | ||
import {DeleteDateColumn} from "../../../../src"; | ||
|
||
@Entity() | ||
export class Category { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@OneToMany(() => Post, post => post.category, { | ||
cascade: true | ||
}) | ||
posts: Post[]; | ||
|
||
@DeleteDateColumn() | ||
deletedAt?: Date; | ||
} |
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,30 @@ | ||
import {Category} from "./Category"; | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../src/decorator/columns/Column"; | ||
import {ManyToOne} from "../../../../src/decorator/relations/ManyToOne"; | ||
import {JoinColumn} from "../../../../src/decorator/relations/JoinColumn"; | ||
import {DeleteDateColumn, OneToMany} from "../../../../src"; | ||
import {Author} from "./Author"; | ||
|
||
@Entity() | ||
export class Post { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
categoryId: string; | ||
|
||
@ManyToOne(() => Category, category => category.posts) | ||
@JoinColumn({ name: "categoryId" }) | ||
category: Category; | ||
|
||
@OneToMany(() => Author, author => author.post, { | ||
cascade: true | ||
}) | ||
authors: Author[]; | ||
|
||
@DeleteDateColumn() | ||
deletedAt?: Date; | ||
} |