localhost_oa_based/src/common/entity/common.entity.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-02-28 17:02:46 +08:00
import { ApiHideProperty, ApiProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';
2024-02-28 08:32:35 +08:00
import {
BaseEntity,
Column,
CreateDateColumn,
PrimaryGeneratedColumn,
UpdateDateColumn,
2024-02-29 09:29:03 +08:00
VirtualColumn
2024-02-28 17:02:46 +08:00
} from 'typeorm';
2024-02-28 08:32:35 +08:00
// 如果觉得前端转换时间太麻烦并且不考虑通用性的话可以在服务端进行转换eg: @UpdateDateColumn({ name: 'updated_at', transformer })
// const transformer: ValueTransformer = {
// to(value) {
// return value
// },
// from(value) {
// return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
// },
// }
export abstract class CommonEntity extends BaseEntity {
@PrimaryGeneratedColumn()
2024-02-28 17:02:46 +08:00
id: number;
2024-02-28 08:32:35 +08:00
@CreateDateColumn({ name: 'created_at' })
2024-02-28 17:02:46 +08:00
createdAt: Date;
2024-02-28 08:32:35 +08:00
@UpdateDateColumn({ name: 'updated_at' })
2024-02-28 17:02:46 +08:00
updatedAt: Date;
2024-02-28 08:32:35 +08:00
}
export abstract class CompleteEntity extends CommonEntity {
@ApiHideProperty()
@Exclude()
@Column({ name: 'create_by', update: false, comment: '创建者' })
2024-02-28 17:02:46 +08:00
createBy: number;
2024-02-28 08:32:35 +08:00
@ApiHideProperty()
@Exclude()
@Column({ name: 'update_by', comment: '更新者' })
2024-02-28 17:02:46 +08:00
updateBy: number;
2024-02-28 08:32:35 +08:00
/**
* service
* @see https://typeorm.io/decorator-reference#virtualcolumn
*/
@ApiProperty({ description: '创建者' })
@VirtualColumn({ query: alias => `SELECT username FROM sys_user WHERE id = ${alias}.create_by` })
2024-02-28 17:02:46 +08:00
creator: string;
2024-02-28 08:32:35 +08:00
@ApiProperty({ description: '更新者' })
@VirtualColumn({ query: alias => `SELECT username FROM sys_user WHERE id = ${alias}.update_by` })
2024-02-28 17:02:46 +08:00
updater: string;
2024-02-28 08:32:35 +08:00
}