1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
| import { Index, PrimaryGeneratedColumn, Column } from 'typeorm';
| import * as moment from 'moment';
| import { CoolBaseEntity } from '@cool-midway/core';
|
| /**
| * 时间转换器
| */
| export const transformerTime = {
| to(value) {
| return value
| ? moment(value).format('YYYY-MM-DD HH:mm:ss')
| : moment().format('YYYY-MM-DD HH:mm:ss');
| },
| from(value) {
| return value;
| },
| };
|
| /**
| * Json转换器
| */
| export const transformerJson = {
| to: value => value,
| from: value => {
| // 确保从数据库返回的是对象
| if (typeof value === 'string') {
| try {
| return JSON.parse(value);
| } catch (e) {
| return value;
| }
| }
| return value;
| },
| };
| /**
| * 实体基类
| */
| export abstract class BaseEntity extends CoolBaseEntity {
| // 默认自增
| @PrimaryGeneratedColumn('increment', {
| comment: 'ID',
| })
| id: number;
|
| @Index()
| @Column({
| comment: '创建时间',
| type: 'varchar',
| transformer: transformerTime,
| })
| createTime: Date;
|
| @Index()
| @Column({
| comment: '更新时间',
| type: 'varchar',
| transformer: transformerTime,
| })
| updateTime: Date;
|
| @Index()
| @Column({ comment: '租户ID', nullable: true })
| tenantId: number;
| }
|
|