nestapp/src/modules/content/entities/post.entity.ts
2025-05-20 13:46:58 +08:00

81 lines
2.1 KiB
TypeScript

import { Exclude, Expose, Type } from 'class-transformer';
import {
BaseEntity,
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')
export class PostEntity extends BaseEntity {
@Expose()
@PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
id: string;
@Expose()
@Column({ comment: '文章标题' })
title: string;
@Expose({ groups: ['post-detail'] })
@Column({ comment: '文章内容', type: 'text' })
body: string;
@Expose()
@Column({ comment: '文章描述', nullable: true })
summary?: string;
@Expose()
@Expose()
@Column({ comment: '关键字', type: 'simple-array', nullable: true })
keywords?: string[];
@Expose()
@Column({ comment: '文章类型', type: 'enum', enum: PostBodyType, default: PostBodyType.HTML })
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: '更新时间', nullable: true })
updatedAt?: Date;
@Expose()
@OneToMany(() => CategoryEntity, (category) => category.posts, {
nullable: true,
onDelete: 'SET NULL',
})
category: Relation<CategoryEntity>;
@Expose()
@ManyToMany(() => TagEntity, (tag) => tag.posts, { cascade: true })
@JoinTable()
tags: Relation<TagEntity>[];
@OneToMany(() => CommentEntity, (comment) => comment.post, { cascade: true })
comments: Relation<CommentEntity>[];
}