add relations

This commit is contained in:
liuyi 2025-05-20 14:24:07 +08:00
parent de0d2fe333
commit a95cc267ef
4 changed files with 30 additions and 1 deletions

View File

@ -1,3 +1,4 @@
import { Exclude, Expose, Type } from 'class-transformer';
import {
BaseEntity,
Column,
@ -12,26 +13,34 @@ import {
import { PostEntity } from '@/modules/content/entities/post.entity';
@Exclude()
@Entity('content_category')
@Tree('materialized-path')
export class CategoryEntity extends BaseEntity {
@Expose()
@PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
id: string;
@Expose()
@Column({ comment: '分类名称', unique: true })
name: string;
@Expose({ groups: ['category-tree', 'category-list', 'category-detail'] })
@Column({ comment: '分类排序', default: 0 })
customOrder: number;
@OneToMany(() => PostEntity, (post) => post.category, { cascade: true })
posts: Relation<PostEntity>[];
@Expose({ groups: ['category-list'] })
depth = 0;
@Expose({ groups: ['category-detail', 'category-list'] })
@TreeParent({ onDelete: 'NO ACTION' })
parent: Relation<CategoryEntity> | null;
@Type(() => CategoryEntity)
@Expose({ groups: ['category-tree'] })
@TreeChildren({ cascade: true })
children: Relation<CategoryEntity>[];
}

View File

@ -1,3 +1,4 @@
import { Exclude, Expose, Type } from 'class-transformer';
import {
BaseEntity,
Column,
@ -13,18 +14,24 @@ import {
import { PostEntity } from '@/modules/content/entities/post.entity';
@Exclude()
@Entity('content_comment')
@Tree('materialized-path')
export class CommentEntity extends BaseEntity {
@Expose()
@PrimaryColumn({ type: 'varchar', length: 36, generated: 'uuid' })
id: string;
@Expose()
@Column({ comment: '评论内容', type: 'text' })
body: string;
@Expose()
@CreateDateColumn({ comment: '创建时间' })
@Type(() => Date)
createdAt: Date;
@Expose()
@ManyToOne(() => PostEntity, (post) => post.comments, {
nullable: false,
onDelete: 'CASCADE',
@ -32,11 +39,14 @@ export class CommentEntity extends BaseEntity {
})
post: Relation<PostEntity>;
@Expose({ groups: ['comment-list'] })
depth = 0;
@Expose({ groups: ['comment-detail', 'comment-list'] })
@TreeParent({ onDelete: 'CASCADE' })
parent: Relation<CommentEntity> | null;
@Expose({ groups: ['comment-tree'] })
@TreeChildren({ cascade: true })
children: Relation<CommentEntity>[];
}

View File

@ -63,6 +63,9 @@ export class PostEntity extends BaseEntity {
@UpdateDateColumn({ comment: '更新时间', nullable: true })
updatedAt?: Date;
@Expose()
commentCount: number;
@Expose()
@OneToMany(() => CategoryEntity, (category) => category.posts, {
nullable: true,
@ -71,6 +74,7 @@ export class PostEntity extends BaseEntity {
category: Relation<CategoryEntity>;
@Expose()
@Type(() => TagEntity)
@ManyToMany(() => TagEntity, (tag) => tag.posts, { cascade: true })
@JoinTable()
tags: Relation<TagEntity>[];

View File

@ -1,20 +1,26 @@
import { Expose } from 'class-transformer';
import { Exclude, Expose } from 'class-transformer';
import { Column, Entity, ManyToMany, PrimaryColumn, Relation } from 'typeorm';
import { PostEntity } from '@/modules/content/entities/post.entity';
@Exclude()
@Entity('content_tag')
export class TagEntity {
@Expose()
@PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
id: string;
@Expose()
@Column({ comment: '标签名称', unique: true })
name: string;
@Expose()
@Column({ comment: '标签描述', nullable: true })
desc?: string;
@Expose()
postCount: number;
@ManyToMany(() => PostEntity, (post) => post.tags)
posts: Relation<PostEntity>[];
}