add relations

This commit is contained in:
liuyi 2025-05-20 13:46:58 +08:00
parent a7b685b2e1
commit de0d2fe333
4 changed files with 47 additions and 8 deletions

View File

@ -1,9 +1,20 @@
import { Column, Entity, OneToMany, PrimaryColumn, Relation } from 'typeorm';
import {
BaseEntity,
Column,
Entity,
OneToMany,
PrimaryColumn,
Relation,
Tree,
TreeChildren,
TreeParent,
} from 'typeorm';
import { PostEntity } from '@/modules/content/entities/post.entity';
@Entity('content_category')
export class CategoryEntity {
@Tree('materialized-path')
export class CategoryEntity extends BaseEntity {
@PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
id: string;
@ -14,5 +25,13 @@ export class CategoryEntity {
customOrder: number;
@OneToMany(() => PostEntity, (post) => post.category, { cascade: true })
posts: Relation<PostEntity[]>;
posts: Relation<PostEntity>[];
depth = 0;
@TreeParent({ onDelete: 'NO ACTION' })
parent: Relation<CategoryEntity> | null;
@TreeChildren({ cascade: true })
children: Relation<CategoryEntity>[];
}

View File

@ -1,9 +1,21 @@
import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryColumn, Relation } from 'typeorm';
import {
BaseEntity,
Column,
CreateDateColumn,
Entity,
ManyToOne,
PrimaryColumn,
Relation,
Tree,
TreeChildren,
TreeParent,
} from 'typeorm';
import { PostEntity } from '@/modules/content/entities/post.entity';
@Entity('content_comment')
export class CommentEntity {
@Tree('materialized-path')
export class CommentEntity extends BaseEntity {
@PrimaryColumn({ type: 'varchar', length: 36, generated: 'uuid' })
id: string;
@ -19,4 +31,12 @@ export class CommentEntity {
onUpdate: 'CASCADE',
})
post: Relation<PostEntity>;
depth = 0;
@TreeParent({ onDelete: 'CASCADE' })
parent: Relation<CommentEntity> | null;
@TreeChildren({ cascade: true })
children: Relation<CommentEntity>[];
}

View File

@ -73,8 +73,8 @@ export class PostEntity extends BaseEntity {
@Expose()
@ManyToMany(() => TagEntity, (tag) => tag.posts, { cascade: true })
@JoinTable()
tags: Relation<TagEntity[]>;
tags: Relation<TagEntity>[];
@OneToMany(() => CommentEntity, (comment) => comment.post, { cascade: true })
comments: Relation<CommentEntity[]>;
comments: Relation<CommentEntity>[];
}

View File

@ -16,5 +16,5 @@ export class TagEntity {
@Expose()
@ManyToMany(() => PostEntity, (post) => post.tags)
posts: Relation<PostEntity[]>;
posts: Relation<PostEntity>[];
}