Skip to content

Commit 7ab9c2c

Browse files
Modifying saving of the creation of new categories
1 parent 5438e80 commit 7ab9c2c

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/models/PostModel.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ export class PostModel {
7070
})
7171
matched: RequestModel[];
7272

73-
@ManyToMany(() => CategoryModel, (category) => category.posts)
73+
@ManyToMany(() => CategoryModel, (category) => category.posts, {
74+
cascade: true,
75+
})
7476
@JoinTable({
7577
name: "post_categories",
7678
joinColumn: { name: "posts", referencedColumnName: "id" },

src/repositories/CategoryRepository.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ export class CategoryRepository extends AbstractRepository<CategoryModel> {
1919
.getMany();
2020

2121
const existingNames = new Set(existing.map((c) => c.name));
22+
23+
const newNames = [
24+
...new Set(names.filter((name) => !existingNames.has(name))),
25+
];
2226

23-
const newCategories = names
24-
.filter((name) => !existingNames.has(name))
25-
.map((name) => this.repository.create({ name }));
27+
// Create in-memory entities (not saving them)
28+
const newCategories = newNames.map((name) => this.repository.create({ name }));
2629

2730
if (newCategories.length > 0) {
2831
await this.repository.save(newCategories);

0 commit comments

Comments
 (0)