From a7b685b2e19aa5b8a89c243ef0fd11d02e1ffb51 Mon Sep 17 00:00:00 2001 From: liuyi Date: Tue, 20 May 2025 13:10:19 +0800 Subject: [PATCH] add relations --- .../content/entities/CategoryEntity.ts | 7 +++++- .../content/entities/comment.entity.ts | 11 +++++++++- src/modules/content/entities/post.entity.ts | 22 +++++++++++++++++++ src/modules/content/entities/tag.entity.ts | 9 +++++++- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/modules/content/entities/CategoryEntity.ts b/src/modules/content/entities/CategoryEntity.ts index 09eac6c..86e35ba 100644 --- a/src/modules/content/entities/CategoryEntity.ts +++ b/src/modules/content/entities/CategoryEntity.ts @@ -1,4 +1,6 @@ -import { Column, Entity, PrimaryColumn } from 'typeorm'; +import { Column, Entity, OneToMany, PrimaryColumn, Relation } from 'typeorm'; + +import { PostEntity } from '@/modules/content/entities/post.entity'; @Entity('content_category') export class CategoryEntity { @@ -10,4 +12,7 @@ export class CategoryEntity { @Column({ comment: '分类排序', default: 0 }) customOrder: number; + + @OneToMany(() => PostEntity, (post) => post.category, { cascade: true }) + posts: Relation; } diff --git a/src/modules/content/entities/comment.entity.ts b/src/modules/content/entities/comment.entity.ts index 0a587a3..181cad4 100644 --- a/src/modules/content/entities/comment.entity.ts +++ b/src/modules/content/entities/comment.entity.ts @@ -1,4 +1,6 @@ -import { Column, CreateDateColumn, Entity, PrimaryColumn } from 'typeorm'; +import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryColumn, Relation } from 'typeorm'; + +import { PostEntity } from '@/modules/content/entities/post.entity'; @Entity('content_comment') export class CommentEntity { @@ -10,4 +12,11 @@ export class CommentEntity { @CreateDateColumn({ comment: '创建时间' }) createdAt: Date; + + @ManyToOne(() => PostEntity, (post) => post.comments, { + nullable: false, + onDelete: 'CASCADE', + onUpdate: 'CASCADE', + }) + post: Relation; } diff --git a/src/modules/content/entities/post.entity.ts b/src/modules/content/entities/post.entity.ts index 1a2b95f..a7a47fb 100644 --- a/src/modules/content/entities/post.entity.ts +++ b/src/modules/content/entities/post.entity.ts @@ -4,11 +4,18 @@ import { Column, CreateDateColumn, Entity, + JoinTable, + ManyToMany, + OneToMany, PrimaryColumn, + Relation, UpdateDateColumn, } from 'typeorm'; import { PostBodyType } from '@/modules/content/constants'; +import { CategoryEntity } from '@/modules/content/entities/CategoryEntity'; +import { CommentEntity } from '@/modules/content/entities/comment.entity'; +import { TagEntity } from '@/modules/content/entities/tag.entity'; @Exclude() @Entity('content_posts') @@ -55,4 +62,19 @@ export class PostEntity extends BaseEntity { @Type(() => Date) @UpdateDateColumn({ comment: '更新时间', nullable: true }) updatedAt?: Date; + + @Expose() + @OneToMany(() => CategoryEntity, (category) => category.posts, { + nullable: true, + onDelete: 'SET NULL', + }) + category: Relation; + + @Expose() + @ManyToMany(() => TagEntity, (tag) => tag.posts, { cascade: true }) + @JoinTable() + tags: Relation; + + @OneToMany(() => CommentEntity, (comment) => comment.post, { cascade: true }) + comments: Relation; } diff --git a/src/modules/content/entities/tag.entity.ts b/src/modules/content/entities/tag.entity.ts index b3e83d3..92bb285 100644 --- a/src/modules/content/entities/tag.entity.ts +++ b/src/modules/content/entities/tag.entity.ts @@ -1,4 +1,7 @@ -import { Column, Entity, PrimaryColumn } from 'typeorm'; +import { Expose } from 'class-transformer'; +import { Column, Entity, ManyToMany, PrimaryColumn, Relation } from 'typeorm'; + +import { PostEntity } from '@/modules/content/entities/post.entity'; @Entity('content_tag') export class TagEntity { @@ -10,4 +13,8 @@ export class TagEntity { @Column({ comment: '标签描述', nullable: true }) desc?: string; + + @Expose() + @ManyToMany(() => PostEntity, (post) => post.tags) + posts: Relation; }