|
1 | 1 | import sjson from 'secure-json-parse'; |
2 | 2 | import { |
3 | | - AfterLoad, |
4 | | - BeforeInsert, |
5 | | - BeforeUpdate, |
6 | | - Column, |
7 | | - Entity, |
8 | | - JoinColumn, |
9 | | - ManyToOne, |
10 | | - PrimaryGeneratedColumn, |
11 | | - Relation, |
| 3 | + AfterLoad, |
| 4 | + BeforeInsert, |
| 5 | + BeforeUpdate, |
| 6 | + Column, |
| 7 | + CreateDateColumn, |
| 8 | + Entity, |
| 9 | + JoinColumn, |
| 10 | + ManyToOne, |
| 11 | + PrimaryGeneratedColumn, |
| 12 | + Relation, |
| 13 | + UpdateDateColumn, |
12 | 14 | } from 'typeorm'; |
13 | 15 | import { WidgetTypeEnum } from '../../enums/index.js'; |
14 | 16 | import { TableSettingsEntity } from '../table-settings/common-table-settings/table-settings.entity.js'; |
15 | 17 |
|
16 | 18 | @Entity('table_widget') |
17 | 19 | export class TableWidgetEntity { |
18 | | - @PrimaryGeneratedColumn('uuid') |
19 | | - id: string; |
| 20 | + @PrimaryGeneratedColumn('uuid') |
| 21 | + id: string; |
20 | 22 |
|
21 | | - @Column() |
22 | | - field_name: string; |
| 23 | + @Column() |
| 24 | + field_name: string; |
23 | 25 |
|
24 | | - @Column({ default: null, type: 'varchar' }) |
25 | | - widget_type?: WidgetTypeEnum; |
| 26 | + @Column({ default: null, type: 'varchar' }) |
| 27 | + widget_type?: WidgetTypeEnum; |
26 | 28 |
|
27 | | - @Column('json', { default: null }) |
28 | | - widget_params: string; |
| 29 | + @Column('json', { default: null }) |
| 30 | + widget_params: string; |
29 | 31 |
|
30 | | - @Column('json', { default: null }) |
31 | | - widget_options: string; |
| 32 | + @Column('json', { default: null }) |
| 33 | + widget_options: string; |
32 | 34 |
|
33 | | - @Column({ default: null }) |
34 | | - name?: string; |
| 35 | + @Column({ default: null }) |
| 36 | + name?: string; |
35 | 37 |
|
36 | | - @Column({ default: null }) |
37 | | - description?: string; |
| 38 | + @Column({ default: null }) |
| 39 | + description?: string; |
38 | 40 |
|
39 | | - @BeforeUpdate() |
40 | | - stringifyOptionsOnUpdate() { |
41 | | - try { |
42 | | - if (this.widget_options) { |
43 | | - this.widget_options = JSON.stringify(this.widget_options); |
44 | | - } |
45 | | - } catch (e) { |
46 | | - console.error('-> Error widget options stringify ' + e.message); |
47 | | - } |
48 | | - } |
| 41 | + @CreateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' }) |
| 42 | + created_at: Date; |
49 | 43 |
|
50 | | - @BeforeInsert() |
51 | | - stringifyOptions() { |
52 | | - try { |
53 | | - if (this.widget_options) { |
54 | | - this.widget_options = JSON.stringify(this.widget_options); |
55 | | - } |
56 | | - } catch (e) { |
57 | | - console.error('-> Error widget options stringify ' + e.message); |
58 | | - } |
59 | | - } |
| 44 | + @UpdateDateColumn({ type: 'timestamp', nullable: true, default: null }) |
| 45 | + updated_at: Date; |
60 | 46 |
|
61 | | - @AfterLoad() |
62 | | - parseOptions() { |
63 | | - try { |
64 | | - if (this.widget_options) { |
65 | | - this.widget_options = sjson.parse(this.widget_options, null, { |
66 | | - protoAction: 'remove', |
67 | | - constructorAction: 'remove', |
68 | | - }); |
69 | | - } |
70 | | - } catch (e) { |
71 | | - console.error('-> Error widget options parse ' + e.message); |
72 | | - } |
73 | | - } |
| 47 | + @BeforeUpdate() |
| 48 | + stringifyOptionsOnUpdate() { |
| 49 | + try { |
| 50 | + if (this.widget_options) { |
| 51 | + this.widget_options = JSON.stringify(this.widget_options); |
| 52 | + } |
| 53 | + } catch (e) { |
| 54 | + console.error('-> Error widget options stringify ' + e.message); |
| 55 | + } |
| 56 | + } |
74 | 57 |
|
75 | | - @ManyToOne(() => TableSettingsEntity, (settings) => settings.table_widgets, { onDelete: 'CASCADE' }) |
76 | | - @JoinColumn() |
77 | | - settings: Relation<TableSettingsEntity>; |
| 58 | + @BeforeInsert() |
| 59 | + stringifyOptions() { |
| 60 | + try { |
| 61 | + if (this.widget_options) { |
| 62 | + this.widget_options = JSON.stringify(this.widget_options); |
| 63 | + } |
| 64 | + } catch (e) { |
| 65 | + console.error('-> Error widget options stringify ' + e.message); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @AfterLoad() |
| 70 | + parseOptions() { |
| 71 | + try { |
| 72 | + if (this.widget_options) { |
| 73 | + this.widget_options = sjson.parse(this.widget_options, null, { |
| 74 | + protoAction: 'remove', |
| 75 | + constructorAction: 'remove', |
| 76 | + }); |
| 77 | + } |
| 78 | + } catch (e) { |
| 79 | + console.error('-> Error widget options parse ' + e.message); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @ManyToOne( |
| 84 | + () => TableSettingsEntity, |
| 85 | + (settings) => settings.table_widgets, |
| 86 | + { onDelete: 'CASCADE' }, |
| 87 | + ) |
| 88 | + @JoinColumn() |
| 89 | + settings: Relation<TableSettingsEntity>; |
78 | 90 | } |
0 commit comments