-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsaved-db-query.entity.ts
More file actions
110 lines (95 loc) · 2.9 KB
/
Copy pathsaved-db-query.entity.ts
File metadata and controls
110 lines (95 loc) · 2.9 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import sjson from 'secure-json-parse';
import {
AfterLoad,
BeforeInsert,
BeforeUpdate,
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
Relation,
} from 'typeorm';
import { Encryptor } from '../../../helpers/encryption/encryptor.js';
import { ConnectionEntity } from '../../connection/connection.entity.js';
import { DashboardWidgetEntity } from '../dashboard-widget/dashboard-widget.entity.js';
import { DashboardWidgetTypeEnum } from '../../../enums/dashboard-widget-type.enum.js';
@Entity('saved_db_query')
export class SavedDbQueryEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
name: string;
@Column({ type: 'text', default: null, nullable: true })
description: string | null;
@Column({ type: 'varchar', nullable: true, default: DashboardWidgetTypeEnum.Chart })
widget_type: DashboardWidgetTypeEnum;
@Column({ type: 'varchar', default: null, nullable: true })
chart_type: string | null;
@Column('json', { default: null, nullable: true })
widget_options: string | null;
@Column({ type: 'text', nullable: true, default: null })
query_text: string;
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created_at: Date;
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
updated_at: Date;
@BeforeInsert()
encryptQueryTextStringifyOptionsOnInsert(): void {
if (this.query_text) {
this.query_text = Encryptor.encryptData(this.query_text);
}
try {
if (this.widget_options && typeof this.widget_options === 'object') {
this.widget_options = JSON.stringify(this.widget_options);
}
} catch (e) {
console.error('-> Error widget options stringify ' + e.message);
}
}
@BeforeUpdate()
encryptQueryTextStringifyOptionsOnUpdate(): void {
this.updated_at = new Date();
if (this.query_text) {
this.query_text = Encryptor.encryptData(this.query_text);
}
try {
if (this.widget_options && typeof this.widget_options === 'object') {
this.widget_options = JSON.stringify(this.widget_options);
}
} catch (e) {
console.error('-> Error widget options stringify ' + e.message);
}
}
@AfterLoad()
decryptQueryTextParseOptions(): void {
if (this.query_text) {
this.query_text = Encryptor.decryptData(this.query_text);
}
try {
if (this.widget_options && typeof this.widget_options === 'string') {
this.widget_options = sjson.parse(this.widget_options, null, {
protoAction: 'remove',
constructorAction: 'remove',
});
}
} catch (e) {
console.error('-> Error widget options parse ' + e.message);
}
}
@ManyToOne(
() => ConnectionEntity,
(connection) => connection.saved_db_queries,
{ onDelete: 'CASCADE' },
)
@JoinColumn({ name: 'connection_id' })
connection: Relation<ConnectionEntity>;
@Column({ type: 'varchar', length: 38 })
connection_id: string;
@OneToMany(
() => DashboardWidgetEntity,
(widget) => widget.query,
)
widgets: Relation<DashboardWidgetEntity>[];
}