-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbasic-data-access-object.ts
More file actions
121 lines (105 loc) · 3.2 KB
/
basic-data-access-object.ts
File metadata and controls
121 lines (105 loc) · 3.2 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
111
112
113
114
115
116
117
118
119
120
121
import { Knex } from 'knex';
import { LRUStorage } from '../../caching/lru-storage.js';
import { isObjectEmpty } from '../../helpers/is-object-empty.js';
import { KnexManager } from '../../knex-manager/knex-manager.js';
import { ConnectionParams } from '../shared/data-structures/connections-params.ds.js';
import { TableSettingsDS } from '../shared/data-structures/table-settings.ds.js';
import { TableStructureDS } from '../shared/data-structures/table-structure.ds.js';
export class BasicDataAccessObject {
protected connection: ConnectionParams;
constructor(connection: ConnectionParams) {
this.connection = connection;
}
public invalidateMetadataCache(): void {
LRUStorage.invalidateConnectionTableMetadata(this.connection);
}
protected async configureKnex(): Promise<Knex<any, any[]>> {
const knexManager = KnexManager.knexStorage();
return knexManager.get(this.connection.type)(this.connection);
}
protected findAvailableFields(settings: TableSettingsDS, tableStructure: Array<TableStructureDS>): Array<string> {
let availableFields: Array<string> = [];
const fieldsFromStructure = tableStructure.map((el) => {
return el.column_name;
});
if (isObjectEmpty(settings)) {
availableFields = tableStructure.map((el) => {
return el.column_name;
});
return availableFields;
}
const excludedFields = settings.excluded_fields;
if (settings.list_fields && settings.list_fields.length > 0) {
const validListFields = settings.list_fields.filter((fieldName) => {
return fieldsFromStructure.includes(fieldName);
});
const additionalFields = fieldsFromStructure.filter((fieldName) => {
return !settings.list_fields.includes(fieldName);
});
availableFields = [...validListFields, ...additionalFields];
} else {
availableFields = tableStructure.map((el) => {
return el.column_name;
});
}
if (excludedFields && excludedFields.length > 0) {
for (const field of excludedFields) {
const delIndex = availableFields.indexOf(field);
if (delIndex >= 0) {
availableFields.splice(availableFields.indexOf(field), 1);
}
}
}
return availableFields;
}
protected validateNamesAndThrowError(names: string | Array<string>): void {
if (typeof names === 'string') {
if (!this.isValidName(names)) {
throw new Error(`Parameter "${names}" is invalid`);
}
}
if (Array.isArray(names)) {
for (const name of names) {
if (!this.isValidName(name)) {
throw new Error(`Parameter "${name}" is invalid`);
}
}
}
}
protected isDateTimeType(columnTypeName: string): boolean {
const dateTimeDataTypes = [
// PostgreSQL
'DATE',
'TIME',
'TIMETZ',
'TIMESTAMP',
'TIMESTAMPTZ',
// MySQL
'DATE',
'DATETIME',
'TIMESTAMP',
'TIME',
'YEAR',
// MS SQL Server
'DATE',
'DATETIME',
'DATETIME2',
'DATETIMEOFFSET',
'SMALLDATETIME',
'TIME',
// OracleDB
'DATE',
'TIMESTAMP',
'TIMESTAMP WITH TIME ZONE',
'TIMESTAMP WITH LOCAL TIME ZONE',
// IBM Db2
'DATE',
'TIME',
'TIMESTAMP',
];
return dateTimeDataTypes.includes(columnTypeName.toUpperCase());
}
private isValidName(name: string): boolean {
return typeof name === 'string' && name.length > 0 && /^[a-zA-Z0-9_]+$/.test(name);
}
}