import { Exclude, Expose, Type } from 'class-transformer'; import { BaseEntity, Column, CreateDateColumn, DeleteDateColumn, Entity, Index, JoinTable, ManyToMany, ManyToOne, OneToMany, PrimaryColumn, Relation, UpdateDateColumn, } from 'typeorm'; import { UserEntity } from '@/modules/user/entities/user.entity'; import { PostBodyType } from '../constants'; import { CategoryEntity } from './category.entity'; import { CommentEntity } from './comment.entity'; import { TagEntity } from './tag.entity'; @Exclude() @Entity('content_posts') export class PostEntity extends BaseEntity { @Expose() @PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 }) id: string; @Expose() @Column({ comment: '文章标题' }) @Index({ fulltext: true }) title: string; @Expose() @Column({ comment: '文章内容', type: 'text' }) @Index({ fulltext: true }) body: string; @Expose({ groups: ['post-detail'] }) @Column({ comment: '文章描述', nullable: true }) @Index({ fulltext: true }) summary?: string; @Expose() @Column({ comment: '关键字', type: 'simple-array', nullable: true }) keywords?: string[]; @Expose() @Column({ comment: '文章类型', type: 'varchar', // 如果是mysql或者postgresql你可以使用enum类型 // enum: PostBodyType, default: PostBodyType.MD, }) type: PostBodyType; @Expose() @Column({ comment: '发布时间', type: 'varchar', nullable: true, }) publishedAt?: Date | null; @Expose() @Column({ comment: '自定义文章排序', default: 0 }) customOrder: number; @Expose() @Type(() => Date) @CreateDateColumn({ comment: '创建时间', }) createdAt: Date; @Expose() @Type(() => Date) @UpdateDateColumn({ comment: '更新时间', }) updatedAt: Date; @Expose() @Type(() => Date) @DeleteDateColumn({ comment: '删除时间', nullable: true, }) deleteAt: Date; @Expose({ groups: ['post-detail'] }) @ManyToOne(() => CategoryEntity, (category) => category.posts, { nullable: true, onDelete: 'SET NULL', }) category: Relation; @Expose() @JoinTable() @ManyToMany(() => TagEntity, (tag) => tag.posts, { cascade: true, }) tags: Relation; @Expose() @OneToMany(() => CommentEntity, (comment) => comment.post) comments: Relation; @Expose() @Column({ comment: '文章评论量', default: 0, select: false }) commentCount: number; @Expose() commentCounts: number; @Expose() @ManyToOne(() => UserEntity, (user) => user.posts, { nullable: false, onDelete: 'CASCADE', onUpdate: 'CASCADE', }) author: Relation; }