-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmysql.ts
More file actions
428 lines (384 loc) · 17.1 KB
/
mysql.ts
File metadata and controls
428 lines (384 loc) · 17.1 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import dayjs from 'dayjs';
import { AdminForthResource, IAdminForthSingleFilter, IAdminForthAndOrFilter, IAdminForthDataSourceConnector, AdminForthConfig } from '../types/Back.js';
import { AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections, } from '../types/Common.js';
import AdminForthBaseConnector from './baseConnector.js';
import mysql from 'mysql2/promise';
import { dbLogger, afLogger } from '../modules/logger.js';
class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
async setupClient(url): Promise<void> {
try {
this.client = mysql.createPool({
uri: url,
waitForConnections: true,
connectionLimit: 10, // Adjust based on your needs
queueLimit: 0
});
} catch (e) {
afLogger.error(`Failed to connect to MySQL: ${e}`);
}
}
OperatorsMap = {
[AdminForthFilterOperators.EQ]: '=',
[AdminForthFilterOperators.NE]: '<>',
[AdminForthFilterOperators.GT]: '>',
[AdminForthFilterOperators.LT]: '<',
[AdminForthFilterOperators.GTE]: '>=',
[AdminForthFilterOperators.LTE]: '<=',
[AdminForthFilterOperators.LIKE]: 'LIKE',
[AdminForthFilterOperators.ILIKE]: 'ILIKE',
[AdminForthFilterOperators.IN]: 'IN',
[AdminForthFilterOperators.NIN]: 'NOT IN',
[AdminForthFilterOperators.AND]: 'AND',
[AdminForthFilterOperators.OR]: 'OR',
[AdminForthFilterOperators.IS_EMPTY]: 'IS NULL',
[AdminForthFilterOperators.IS_NOT_EMPTY]: 'IS NOT NULL',
};
SortDirectionsMap = {
[AdminForthSortDirections.asc]: 'ASC',
[AdminForthSortDirections.desc]: 'DESC',
};
async getAllTables(): Promise<Array<string>> {
const [rows] = await this.client.query(
`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = DATABASE() AND table_type = 'BASE TABLE';
`
);
return rows.map((row: any) => row.TABLE_NAME);
}
async getAllColumnsInTable(tableName: string): Promise<Array<{ name: string; sampleValue?: any }>> {
const [columns] = await this.client.query(
`SELECT column_name FROM information_schema.columns WHERE table_name = ? AND table_schema = DATABASE()`,
[tableName]
);
const columnNames = columns.map((c: any) => c.COLUMN_NAME);
const orderByField = ['updated_at', 'created_at', 'id'].find(f => columnNames.includes(f));
let [rows] = orderByField
? await this.client.query(`SELECT * FROM \`${tableName}\` ORDER BY \`${orderByField}\` DESC LIMIT 1`)
: await this.client.query(`SELECT * FROM \`${tableName}\` LIMIT 1`);
const sampleRow = rows[0] || {};
return columns.map((col: any) => ({
name: col.COLUMN_NAME,
sampleValue: sampleRow[col.COLUMN_NAME],
}));
}
async hasMySQLCascadeFk(resource: AdminForthResource, config: AdminForthConfig): Promise<boolean> {
const cascadeColumn = resource.columns.find(c => c.foreignResource?.onDelete === 'cascade');
if (!cascadeColumn) return false;
const parentResource = config.resources.find(r => r.resourceId === cascadeColumn.foreignResource.resourceId);
if (!parentResource) return false;
const [rows] = await this.client.execute(
`
SELECT 1
FROM information_schema.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = DATABASE()
AND TABLE_NAME = ?
AND REFERENCED_TABLE_NAME = ?
AND DELETE_RULE = 'CASCADE'
LIMIT 1
`,
[resource.table, parentResource.table]
);
const hasCascadeOnTable = (rows as any[]).length > 0;
const isUploadPluginInstalled = resource.plugins?.some(p => p.className === "UploadPlugin");
if (hasCascadeOnTable && isUploadPluginInstalled) {
afLogger.warn(`Table "${resource.table}" has ON DELETE CASCADE and UploadPlugin installed, which may conflict with adminForth cascade deletion`);
}
return hasCascadeOnTable;
}
async discoverFields(resource: AdminForthResource, config: AdminForthConfig) {
const [results] = await this.client.execute("SHOW COLUMNS FROM " + resource.table);
await this.hasMySQLCascadeFk(resource, config);
const fieldTypes = {};
results.forEach((row) => {
const field: any = {};
const baseType = row.Type.toLowerCase();
if (baseType == 'tinyint(1)') {
field.type = AdminForthDataTypes.BOOLEAN;
field._underlineType = 'bool';
} else if (baseType.startsWith('tinyint')) {
field.type = AdminForthDataTypes.INTEGER;
field._underlineType = 'tinyint';
field.minValue = baseType.includes('unsigned') ? 0 : -128;
field.maxValue = baseType.includes('unsigned') ? 255 : 127;
} else if (baseType.startsWith('smallint')) {
field.type = AdminForthDataTypes.INTEGER;
field._underlineType = 'tinyint';
field.minValue = baseType.includes('unsigned') ? 0 : -32768;
field.maxValue = baseType.includes('unsigned') ? 65535 : 32767;
} else if (baseType.startsWith('int') || baseType.endsWith('int')) {
field.type = AdminForthDataTypes.INTEGER;
field._underlineType = 'int';
field.minValue = baseType.includes('unsigned') ? 0 : null;
} else if (baseType.startsWith('dec') || baseType.startsWith('numeric')) {
field.type = AdminForthDataTypes.DECIMAL;
field._underlineType = 'decimal';
const [precision, scale] = baseType.match(/\d+/g);
field.precision = parseInt(precision);
field.scale = parseInt(scale);
field.minValue = baseType.includes('unsigned') ? 0 : null;
} else if (baseType.startsWith('float') || baseType.startsWith('double') || baseType.startsWith('real')) {
field.type = AdminForthDataTypes.FLOAT;
field._underlineType = 'float';
field.minValue = baseType.includes('unsigned') ? 0 : null;
} else if (baseType.startsWith('varchar')) {
field.type = AdminForthDataTypes.STRING;
field._underlineType = 'varchar';
const length = baseType.match(/\d+/);
field.maxLength = length ? parseInt(length[0]) : null;
} else if (baseType.startsWith('char')) {
field.type = AdminForthDataTypes.STRING;
field._underlineType = 'char';
const length = baseType.match(/\d+/);
field.minLength = length ? parseInt(length[0]) : null;
field.maxLength = length ? parseInt(length[0]) : null;
} else if (baseType.endsWith('text')) {
field.type = AdminForthDataTypes.TEXT;
field._underlineType = 'text';
} else if (baseType.startsWith('enum')) {
field.type = AdminForthDataTypes.STRING;
field._underlineType = 'enum';
} else if (baseType.startsWith('json')) {
field.type = AdminForthDataTypes.JSON;
field._underlineType = 'json';
} else if (baseType.startsWith('time')) {
field.type = AdminForthDataTypes.TIME;
field._underlineType = 'time';
} else if (baseType.startsWith('datetime') || baseType.startsWith('timestamp')) {
field.type = AdminForthDataTypes.DATETIME;
field._underlineType = 'timestamp';
} else if (baseType.startsWith('date')) {
field.type = AdminForthDataTypes.DATE;
field._underlineType = 'date';
} else if (baseType.startsWith('year')) {
field.type = AdminForthDataTypes.INTEGER;
field._underlineType = 'year';
field.minValue = 1901;
field.maxValue = 2155;
} else {
field.type = 'unknown'
}
field._baseTypeDebug = baseType;
field.primaryKey = row.Key === 'PRI';
field.default = row.Default;
field.required = row.Null === 'NO' && !row.Default;
fieldTypes[row.Field] = field
});
return fieldTypes;
}
getFieldValue(field, value) {
if (field.type == AdminForthDataTypes.DATETIME) {
if (!value) {
return null;
}
return dayjs(value).toISOString();
} else if (field.type == AdminForthDataTypes.DATE) {
return value || null;
} else if (field.type == AdminForthDataTypes.TIME) {
return value || null;
} else if (field.type == AdminForthDataTypes.BOOLEAN) {
return value === null ? null : !!value;
} else if (field.type == AdminForthDataTypes.JSON) {
if (typeof value === 'string') {
try {
return JSON.parse(value);
} catch (e) {
return {'error': `Failed to parse JSON: ${e.message}`}
}
} else if (typeof value === 'object') {
return value;
} else {
afLogger.error(`JSON field value is not string or object, but has type: ${typeof value}`);
afLogger.error(`Field:, ${field}`);
return {}
}
}
return value;
}
setFieldValue(field, value) {
if (field.type == AdminForthDataTypes.DATETIME) {
if (!value) {
return null;
}
return dayjs(value).format('YYYY-MM-DD HH:mm:ss');
} else if (field.type == AdminForthDataTypes.BOOLEAN) {
return value === null ? null : (value ? true : false);
} else if (field.type == AdminForthDataTypes.JSON) {
if (field._underlineType === 'json') {
return value;
} else {
return JSON.stringify(value);
}
}
return value;
}
getFilterString(filter: IAdminForthSingleFilter | IAdminForthAndOrFilter): string {
if ((filter as IAdminForthSingleFilter).field) {
// Field-to-field comparison support
if ((filter as IAdminForthSingleFilter).rightField) {
const left = (filter as IAdminForthSingleFilter).field;
const right = (filter as IAdminForthSingleFilter).rightField;
const operator = this.OperatorsMap[filter.operator];
return `${left} ${operator} ${right}`;
}
// filter is a Single filter
let placeholder = '?';
let field = (filter as IAdminForthSingleFilter).field;
let operator = this.OperatorsMap[filter.operator];
// Handle IS_EMPTY and IS_NOT_EMPTY operators
if (filter.operator == AdminForthFilterOperators.IS_EMPTY || filter.operator == AdminForthFilterOperators.IS_NOT_EMPTY) {
return `${field} ${operator}`;
} else if (filter.operator == AdminForthFilterOperators.IN || filter.operator == AdminForthFilterOperators.NIN) {
placeholder = `(${(filter as IAdminForthSingleFilter).value.map(() => '?').join(', ')})`;
} else if (filter.operator == AdminForthFilterOperators.ILIKE) {
placeholder = `LOWER(?)`;
field = `LOWER(${field})`;
operator = 'LIKE';
} else if (filter.operator == AdminForthFilterOperators.NE) {
if (filter.value === null) {
operator = 'IS NOT';
placeholder = 'NULL';
} else {
// for not equal, we need to add a null check
// because nullish field will not match != value
placeholder = `${placeholder} OR ${field} IS NULL)`;
field = `(${field}`;
}
} else if (filter.operator == AdminForthFilterOperators.EQ && filter.value === null) {
operator = 'IS';
placeholder = 'NULL';
}
return `${field} ${operator} ${placeholder}`;
}
// filter is a single insecure raw sql
if ((filter as IAdminForthSingleFilter).insecureRawSQL) {
return (filter as IAdminForthSingleFilter).insecureRawSQL;
}
// filter is a AndOr filter
return (filter as IAdminForthAndOrFilter).subFilters.map((f) => {
if ((f as IAdminForthSingleFilter).field || (f as IAdminForthSingleFilter).insecureRawSQL) {
// subFilter is a Single filter
return this.getFilterString(f);
}
// subFilter is a AndOr filter - add parentheses
return `(${this.getFilterString(f)})`;
}).join(` ${this.OperatorsMap[filter.operator]} `);
}
getFilterParams(filter: IAdminForthSingleFilter | IAdminForthAndOrFilter): any[] {
if ((filter as IAdminForthSingleFilter).field) {
if ((filter as IAdminForthSingleFilter).rightField) {
// No params for field-to-field comparisons
return [];
}
// filter is a Single filter
// Handle IS_EMPTY and IS_NOT_EMPTY operators - no params needed
if (filter.operator == AdminForthFilterOperators.IS_EMPTY || filter.operator == AdminForthFilterOperators.IS_NOT_EMPTY) {
return [];
} else if (filter.operator == AdminForthFilterOperators.LIKE || filter.operator == AdminForthFilterOperators.ILIKE) {
return [`%${filter.value}%`];
} else if (filter.operator == AdminForthFilterOperators.IN || filter.operator == AdminForthFilterOperators.NIN) {
return filter.value;
} else if (filter.operator == AdminForthFilterOperators.EQ && (filter as IAdminForthSingleFilter).value === null) {
return [];
} else if (filter.operator == AdminForthFilterOperators.NE && (filter as IAdminForthSingleFilter).value === null) {
return [];
} else {
return [(filter as IAdminForthSingleFilter).value];
}
}
// filter is a Single insecure raw sql
if ((filter as IAdminForthSingleFilter).insecureRawSQL) {
return [];
}
// filter is a AndOrFilter
return (filter as IAdminForthAndOrFilter).subFilters.reduce((params: any[], f: IAdminForthSingleFilter | IAdminForthAndOrFilter) => {
return params.concat(this.getFilterParams(f));
}, []);
}
whereClauseAndValues(filters: IAdminForthAndOrFilter) : {
sql: string,
values: any[],
} {
return filters.subFilters.length ? {
sql: `WHERE ${this.getFilterString(filters)}`,
values: this.getFilterParams(filters)
} : { sql: '', values: [] };
}
async getDataWithOriginalTypes({ resource, limit, offset, sort, filters }): Promise<any[]> {
const columns = resource.dataSourceColumns.map((col) => `${col.name}`).join(', ');
const tableName = resource.table;
const { sql: where, values: filterValues } = this.whereClauseAndValues(filters);
const orderBy = sort.length ? `ORDER BY ${sort.map((s) => `${s.field} ${this.SortDirectionsMap[s.direction]}`).join(', ')}` : '';
let selectQuery = `SELECT ${columns} FROM ${tableName}`;
if (where) selectQuery += ` ${where}`;
if (orderBy) selectQuery += ` ${orderBy}`;
if (limit) selectQuery += ` LIMIT ${limit}`;
if (offset) selectQuery += ` OFFSET ${offset}`;
dbLogger.trace(`🪲📜 MySQL Q: ${selectQuery} values: ${JSON.stringify(filterValues)}`);
const [results] = await this.client.execute(selectQuery, filterValues);
return results.map((row) => {
const newRow = {};
for (const [key, value] of Object.entries(row)) {
newRow[key] = value;
}
return newRow;
});
}
async getCount({ resource, filters }: { resource: AdminForthResource; filters: IAdminForthAndOrFilter; }): Promise<number> {
const tableName = resource.table;
// validate and normalize in case this method is called from dataAPI
if (filters) {
const filterValidation = this.validateAndNormalizeFilters(filters, resource);
if (!filterValidation.ok) {
throw new Error(filterValidation.error);
}
}
const { sql: where, values: filterValues } = this.whereClauseAndValues(filters);
const q = `SELECT COUNT(*) FROM ${tableName} ${where}`;
dbLogger.trace(`🪲📜 MySQL Q: ${q} values: ${JSON.stringify(filterValues)}`);
const [results] = await this.client.execute(q, filterValues);
return +results[0]["COUNT(*)"];
}
async getMinMaxForColumnsWithOriginalTypes({ resource, columns }) {
const tableName = resource.table;
const result = {};
await Promise.all(columns.map(async (col) => {
const q = `SELECT MIN(${col.name}) as min, MAX(${col.name}) as max FROM ${tableName}`;
dbLogger.trace(`🪲📜 MySQL Q: ${q}`);
const [results] = await this.client.execute(q);
const { min, max } = results[0];
result[col.name] = {
min, max,
};
}))
return result;
}
async createRecordOriginalValues({ resource, record }): Promise<string> {
const tableName = resource.table;
const columns = Object.keys(record);
const placeholders = columns.map(() => '?').join(', ');
const values = columns.map((colName) => typeof record[colName] === 'undefined' ? null : record[colName]);
const q = `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (${placeholders})`;
dbLogger.trace(`🪲📜 MySQL Q: ${q} values: ${JSON.stringify(values)}`);
const ret = await this.client.execute(q, values);
return ret.insertId;
}
async updateRecordOriginalValues({ resource, recordId, newValues }) {
const values = [...Object.values(newValues), recordId];
const columnsWithPlaceholders = Object.keys(newValues).map((col, i) => `${col} = ?`).join(', ');
const q = `UPDATE ${resource.table} SET ${columnsWithPlaceholders} WHERE ${this.getPrimaryKey(resource)} = ?`;
dbLogger.trace(`🪲📜 MySQL Q: ${q} values: ${JSON.stringify(values)}`);
await this.client.execute(q, values);
}
async deleteRecord({ resource, recordId }): Promise<boolean> {
const q = `DELETE FROM ${resource.table} WHERE ${this.getPrimaryKey(resource)} = ?`;
dbLogger.trace(`🪲📜 MySQL Q: ${q} values: ${JSON.stringify([recordId])}`);
const res = await this.client.execute(q, [recordId]);
return res.rowCount > 0;
}
async close() {
await this.client.end();
}
}
export default MysqlConnector;