add relations
This commit is contained in:
parent
de0d2fe333
commit
a95cc267ef
@ -1,3 +1,4 @@
|
|||||||
|
import { Exclude, Expose, Type } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
BaseEntity,
|
BaseEntity,
|
||||||
Column,
|
Column,
|
||||||
@ -12,26 +13,34 @@ import {
|
|||||||
|
|
||||||
import { PostEntity } from '@/modules/content/entities/post.entity';
|
import { PostEntity } from '@/modules/content/entities/post.entity';
|
||||||
|
|
||||||
|
@Exclude()
|
||||||
@Entity('content_category')
|
@Entity('content_category')
|
||||||
@Tree('materialized-path')
|
@Tree('materialized-path')
|
||||||
export class CategoryEntity extends BaseEntity {
|
export class CategoryEntity extends BaseEntity {
|
||||||
|
@Expose()
|
||||||
@PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
|
@PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
|
@Expose()
|
||||||
@Column({ comment: '分类名称', unique: true })
|
@Column({ comment: '分类名称', unique: true })
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
@Expose({ groups: ['category-tree', 'category-list', 'category-detail'] })
|
||||||
@Column({ comment: '分类排序', default: 0 })
|
@Column({ comment: '分类排序', default: 0 })
|
||||||
customOrder: number;
|
customOrder: number;
|
||||||
|
|
||||||
@OneToMany(() => PostEntity, (post) => post.category, { cascade: true })
|
@OneToMany(() => PostEntity, (post) => post.category, { cascade: true })
|
||||||
posts: Relation<PostEntity>[];
|
posts: Relation<PostEntity>[];
|
||||||
|
|
||||||
|
@Expose({ groups: ['category-list'] })
|
||||||
depth = 0;
|
depth = 0;
|
||||||
|
|
||||||
|
@Expose({ groups: ['category-detail', 'category-list'] })
|
||||||
@TreeParent({ onDelete: 'NO ACTION' })
|
@TreeParent({ onDelete: 'NO ACTION' })
|
||||||
parent: Relation<CategoryEntity> | null;
|
parent: Relation<CategoryEntity> | null;
|
||||||
|
|
||||||
|
@Type(() => CategoryEntity)
|
||||||
|
@Expose({ groups: ['category-tree'] })
|
||||||
@TreeChildren({ cascade: true })
|
@TreeChildren({ cascade: true })
|
||||||
children: Relation<CategoryEntity>[];
|
children: Relation<CategoryEntity>[];
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { Exclude, Expose, Type } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
BaseEntity,
|
BaseEntity,
|
||||||
Column,
|
Column,
|
||||||
@ -13,18 +14,24 @@ import {
|
|||||||
|
|
||||||
import { PostEntity } from '@/modules/content/entities/post.entity';
|
import { PostEntity } from '@/modules/content/entities/post.entity';
|
||||||
|
|
||||||
|
@Exclude()
|
||||||
@Entity('content_comment')
|
@Entity('content_comment')
|
||||||
@Tree('materialized-path')
|
@Tree('materialized-path')
|
||||||
export class CommentEntity extends BaseEntity {
|
export class CommentEntity extends BaseEntity {
|
||||||
|
@Expose()
|
||||||
@PrimaryColumn({ type: 'varchar', length: 36, generated: 'uuid' })
|
@PrimaryColumn({ type: 'varchar', length: 36, generated: 'uuid' })
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
|
@Expose()
|
||||||
@Column({ comment: '评论内容', type: 'text' })
|
@Column({ comment: '评论内容', type: 'text' })
|
||||||
body: string;
|
body: string;
|
||||||
|
|
||||||
|
@Expose()
|
||||||
@CreateDateColumn({ comment: '创建时间' })
|
@CreateDateColumn({ comment: '创建时间' })
|
||||||
|
@Type(() => Date)
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
|
|
||||||
|
@Expose()
|
||||||
@ManyToOne(() => PostEntity, (post) => post.comments, {
|
@ManyToOne(() => PostEntity, (post) => post.comments, {
|
||||||
nullable: false,
|
nullable: false,
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
@ -32,11 +39,14 @@ export class CommentEntity extends BaseEntity {
|
|||||||
})
|
})
|
||||||
post: Relation<PostEntity>;
|
post: Relation<PostEntity>;
|
||||||
|
|
||||||
|
@Expose({ groups: ['comment-list'] })
|
||||||
depth = 0;
|
depth = 0;
|
||||||
|
|
||||||
|
@Expose({ groups: ['comment-detail', 'comment-list'] })
|
||||||
@TreeParent({ onDelete: 'CASCADE' })
|
@TreeParent({ onDelete: 'CASCADE' })
|
||||||
parent: Relation<CommentEntity> | null;
|
parent: Relation<CommentEntity> | null;
|
||||||
|
|
||||||
|
@Expose({ groups: ['comment-tree'] })
|
||||||
@TreeChildren({ cascade: true })
|
@TreeChildren({ cascade: true })
|
||||||
children: Relation<CommentEntity>[];
|
children: Relation<CommentEntity>[];
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,9 @@ export class PostEntity extends BaseEntity {
|
|||||||
@UpdateDateColumn({ comment: '更新时间', nullable: true })
|
@UpdateDateColumn({ comment: '更新时间', nullable: true })
|
||||||
updatedAt?: Date;
|
updatedAt?: Date;
|
||||||
|
|
||||||
|
@Expose()
|
||||||
|
commentCount: number;
|
||||||
|
|
||||||
@Expose()
|
@Expose()
|
||||||
@OneToMany(() => CategoryEntity, (category) => category.posts, {
|
@OneToMany(() => CategoryEntity, (category) => category.posts, {
|
||||||
nullable: true,
|
nullable: true,
|
||||||
@ -71,6 +74,7 @@ export class PostEntity extends BaseEntity {
|
|||||||
category: Relation<CategoryEntity>;
|
category: Relation<CategoryEntity>;
|
||||||
|
|
||||||
@Expose()
|
@Expose()
|
||||||
|
@Type(() => TagEntity)
|
||||||
@ManyToMany(() => TagEntity, (tag) => tag.posts, { cascade: true })
|
@ManyToMany(() => TagEntity, (tag) => tag.posts, { cascade: true })
|
||||||
@JoinTable()
|
@JoinTable()
|
||||||
tags: Relation<TagEntity>[];
|
tags: Relation<TagEntity>[];
|
||||||
|
@ -1,20 +1,26 @@
|
|||||||
import { Expose } from 'class-transformer';
|
import { Exclude, Expose } from 'class-transformer';
|
||||||
import { Column, Entity, ManyToMany, PrimaryColumn, Relation } from 'typeorm';
|
import { Column, Entity, ManyToMany, PrimaryColumn, Relation } from 'typeorm';
|
||||||
|
|
||||||
import { PostEntity } from '@/modules/content/entities/post.entity';
|
import { PostEntity } from '@/modules/content/entities/post.entity';
|
||||||
|
|
||||||
|
@Exclude()
|
||||||
@Entity('content_tag')
|
@Entity('content_tag')
|
||||||
export class TagEntity {
|
export class TagEntity {
|
||||||
|
@Expose()
|
||||||
@PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
|
@PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
|
@Expose()
|
||||||
@Column({ comment: '标签名称', unique: true })
|
@Column({ comment: '标签名称', unique: true })
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
@Expose()
|
||||||
@Column({ comment: '标签描述', nullable: true })
|
@Column({ comment: '标签描述', nullable: true })
|
||||||
desc?: string;
|
desc?: string;
|
||||||
|
|
||||||
@Expose()
|
@Expose()
|
||||||
|
postCount: number;
|
||||||
|
|
||||||
@ManyToMany(() => PostEntity, (post) => post.tags)
|
@ManyToMany(() => PostEntity, (post) => post.tags)
|
||||||
posts: Relation<PostEntity>[];
|
posts: Relation<PostEntity>[];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user