-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateRestBindings.ts
425 lines (307 loc) · 13.4 KB
/
generateRestBindings.ts
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
#!/usr/bin/env node
const {execSync} = require('child_process');
const fs = require('fs');
const path = require('path');
const Handlebars = require('handlebars');
import {version} from '../package.json';
const args = process.argv.slice(2); // Slice the first two elements
const argMap = {};
for (let i = 0; i < args.length; i += 2) {
argMap[args[i]] = args[i + 1];
}
const createDirIfNotExists = dir =>
!fs.existsSync(dir) ? fs.mkdirSync(dir, {recursive: true}) : undefined;
class MySQLDump {
static mysqlcnf: string = '';
static mysqldump: string = '';
static DB_USER = argMap['--user'] || 'root';
static DB_PASS = argMap['--pass'] || 'password';
static DB_HOST = argMap['--host'] || '127.0.0.1';
static DB_PORT = argMap['--port'] || '3306';
static DB_NAME = argMap['--dbname'] || 'CarbonPHP';
static DB_PREFIX = argMap['--prefix'] || 'carbon_';
static RELATIVE_OUTPUT_DIR = argMap['--output'] || '/src/api/rest';
static OUTPUT_DIR = path.join(process.cwd(), MySQLDump.RELATIVE_OUTPUT_DIR);
static buildCNF(cnfFile: string = '') {
if (this.mysqlcnf !== '') {
return this.mysqlcnf;
}
const cnf = [
'[client]',
`user = ${this.DB_USER}`,
`password = ${this.DB_PASS}`,
`host = ${this.DB_HOST}`,
`port = ${this.DB_PORT}`,
'',
];
cnf.push(``);
if ('' === cnfFile) {
cnfFile = path.join(process.cwd(), '/mysql.cnf');
}
try {
fs.writeFileSync(cnfFile, cnf.join('\n'));
fs.chmodSync(cnfFile, 0o750);
console.log(`Successfully created mysql.cnf file in (${cnfFile})`);
} catch (error) {
console.error(`Failed to store file contents of mysql.cnf in (${process.cwd()})`, error);
process.exit(1);
}
return (this.mysqlcnf = cnfFile);
}
static MySQLDump(mysqldump: string = 'mysqldump', data = false, schemas = true, outputFile = '', otherOption = '', specificTable: string = '') {
if (outputFile === '') {
outputFile = path.join(process.cwd(), 'mysqldump.sql');
}
if (!data && !schemas) {
console.warn("MysqlDump is running with --no-create-info and --no-data. Why?");
}
const defaultsExtraFile = this.buildCNF();
const hexBlobOption = data ? '--hex-blob ' : '--no-data ';
const createInfoOption = schemas ? '' : ' --no-create-info ';
const cmd = `${mysqldump} --defaults-extra-file="${defaultsExtraFile}" ${otherOption} --skip-add-locks --single-transaction --quick ${createInfoOption}${hexBlobOption}${this.DB_NAME} ${specificTable} > '${outputFile}'`;
this.executeAndCheckStatus(cmd);
return (this.mysqldump = outputFile);
}
static executeAndCheckStatus(command: string, exitOnFailure = true, output: any[] = []) {
try {
const stdout = execSync(command, {encoding: 'utf-8'});
output.push(stdout);
} catch (e) {
console.log(`Command output::`, e);
if (exitOnFailure) {
process.exit(1);
}
}
}
}
createDirIfNotExists(MySQLDump.OUTPUT_DIR)
const pathRuntimeReference = MySQLDump.RELATIVE_OUTPUT_DIR.replace(/(^\/(src\/)?)|(\/+$)/g, '')
// Usage example
const dumpFileLocation = MySQLDump.MySQLDump();
/*type ColumnInfo = {
type: string;
length?: string;
autoIncrement: boolean;
notNull: boolean;
defaultValue?: string;
};*/
type foreignKeyInfo = {
TABLE: string,
CONSTRAINT: string,
FOREIGN_KEY: string,
REFERENCES: string,
ON_DELETE: string,
ON_UPDATE: string
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function determineTypeScriptType(mysqlType) {
switch (mysqlType.toLowerCase()) {
case 'varchar':
case 'text':
case 'char':
case 'datetime':
case 'timestamp':
case 'date':
return 'string';
case 'int':
case 'bigint':
case 'smallint':
case 'decimal':
case 'float':
case 'double':
case 'tinyint':
return 'number';
case 'boolean':
return 'boolean';
case 'json':
return 'any'; // or 'object' based on usage
default:
return 'string';
}
}
const parseSQLToTypeScript = (sql: string) => {
const tableMatches = sql.matchAll(/CREATE\s+TABLE\s+`?(\w+)`?\s+\(((.|\n)+?)\)\s*(ENGINE=.+?);/gm);
let tableData: {
[TableName: string]: {
RELATIVE_OUTPUT_DIR: string,
TABLE_NAME: string,
TABLE_DEFINITION: string,
TABLE_CONSTRAINT: {},
TABLE_NAME_SHORT: string,
TABLE_NAME_LOWER: string,
TABLE_NAME_UPPER: string,
TABLE_NAME_PASCAL_CASE: string,
TABLE_NAME_SHORT_PASCAL_CASE: string,
TABLE_REFERENCED_BY?: {},
TABLE_REFERENCES?: {},
PRIMARY: string[],
PRIMARY_SHORT: string[],
COLUMNS: {},
COLUMNS_UPPERCASE: {},
TYPE_VALIDATION: {},
REGEX_VALIDATION: {},
}
} = {};
let references: foreignKeyInfo[] = [];
// @ts-ignore
for (const tableMatch of tableMatches) {
const tableName = tableMatch[1];
const columnDefinitions = tableMatch[2];
let columns = {};
// Improved regular expression to match column definitions
const columnRegex = /\s*`([^`]*)`\s+(\w+)(?:\(([^)]+)\))?\s*(NOT NULL)?\s*(AUTO_INCREMENT)?\s*(DEFAULT\s+'.*?'|DEFAULT\s+\S+)?/gm;
let columnMatch;
const columnDefinitionsLines = columnDefinitions.split('\n');
columnDefinitionsLines.forEach(line => {
if (!line.match(/(PRIMARY KEY|UNIQUE KEY|CONSTRAINT)/)) {
while ((columnMatch = columnRegex.exec(line))) {
columns[columnMatch[1]] = {
type: columnMatch[2],
length: columnMatch[3] || '',
notNull: !!columnMatch[4],
autoIncrement: !!columnMatch[5],
defaultValue: columnMatch[6] ? columnMatch[6].replace(/^DEFAULT\s+/i, '') : ''
};
}
}
});
// Extract primary keys
const primaryKeyMatch = columnDefinitions.match(/PRIMARY KEY \(([^)]+)\)/i);
const primaryKeys = primaryKeyMatch
? primaryKeyMatch[1].split(',').map(key => key.trim().replace(/`/g, ''))
: [];
// Extract foreign keys
const foreignKeyRegex: RegExp = /CONSTRAINT `([^`]+)` FOREIGN KEY \(`([^`]+)`\) REFERENCES `([^`]+)` \(`([^`]+)`\)( ON DELETE (\w+))?( ON UPDATE (\w+))?/g;
let foreignKeyMatch: RegExpExecArray | null;
while ((foreignKeyMatch = foreignKeyRegex.exec(columnDefinitions))) {
const constraintName = foreignKeyMatch[1];
const localColumn = foreignKeyMatch[2];
const foreignTable = foreignKeyMatch[3];
const foreignColumn = foreignKeyMatch[4];
const onDeleteAction = foreignKeyMatch[6] || '';
const onUpdateAction = foreignKeyMatch[8] || '';
references.push({
TABLE: tableName,
CONSTRAINT: constraintName,
FOREIGN_KEY: localColumn,
REFERENCES: `${foreignTable}.${foreignColumn}`,
ON_DELETE: onDeleteAction,
ON_UPDATE: onUpdateAction
});
}
let REACT_IMPORT: false|string = false, CARBON_REACT_INSTANCE : false|string = false;
if (argMap['--react'] || false) {
const reactArgSplit = argMap['--react'].split(';')
if (reactArgSplit.length !== 2) {
console.error("React requires two arguments, the import and the carbon react instance statement. Example: --react 'import CustomCarbonReactApplication from \"src/CustomCarbonReactApplication.tsx\"; CustomCarbonReactApplication.instance'");
process.exit(1);
}
[REACT_IMPORT, CARBON_REACT_INSTANCE] = reactArgSplit;
}
const tsModel = {
RELATIVE_OUTPUT_DIR: pathRuntimeReference,
TABLE_NAME: tableName,
TABLE_DEFINITION: tableMatch[0],
TABLE_CONSTRAINT: references,
REST_URL_EXPRESSION: argMap['--restUrlExpression'] || '"/rest/"',
TABLE_NAME_SHORT: tableName.replace(MySQLDump.DB_PREFIX, ''),
TABLE_NAME_LOWER: tableName.toLowerCase(),
TABLE_NAME_UPPER: tableName.toUpperCase(),
TABLE_NAME_PASCAL_CASE: tableName.split('_').map(capitalizeFirstLetter).join('_'),
TABLE_NAME_SHORT_PASCAL_CASE: tableName.replace(MySQLDump.DB_PREFIX, '').split('_').map(capitalizeFirstLetter).join('_'),
PRIMARY: primaryKeys.map(pk => `${tableName}.${pk}`),
PRIMARY_SHORT: primaryKeys,
COLUMNS: {},
COLUMNS_UPPERCASE: {},
TYPE_VALIDATION: {},
REGEX_VALIDATION: {},
TABLE_REFERENCES: {},
TABLE_REFERENCED_BY: {},
REACT_IMPORT: REACT_IMPORT,
CARBON_REACT_INSTANCE: CARBON_REACT_INSTANCE,
};
for (const colName in columns) {
tsModel.COLUMNS[`${tableName}.${colName}`] = colName;
tsModel.COLUMNS_UPPERCASE[colName.toUpperCase()] = tableName + '.' + colName;
const typescript_type = determineTypeScriptType(columns[colName].type.toLowerCase()) === "number" ? "number" : "string"
tsModel.TYPE_VALIDATION[`${tableName}.${colName}`] = {
COLUMN_NAME: colName,
MYSQL_TYPE: columns[colName].type.toLowerCase(),
TYPESCRIPT_TYPE: typescript_type,
TYPESCRIPT_TYPE_IS_STRING: 'string' === typescript_type,
TYPESCRIPT_TYPE_IS_NUMBER: 'number' === typescript_type,
MAX_LENGTH: columns[colName].length,
AUTO_INCREMENT: columns[colName].autoIncrement,
NOT_NULL: columns[colName].notNull,
SKIP_COLUMN_IN_POST: !columns[colName].notNull && !columns[colName].defaultValue,
};
}
tableData[tableName] = tsModel;
}
for (const ref of references) {
const foreignTable = ref.REFERENCES.split('.')[0];
const foreignColumn = ref.REFERENCES.split('.')[1];
const tableName = ref.TABLE;
const columnName = ref.FOREIGN_KEY;
const constraintName = ref.CONSTRAINT;
if (!tableData[foreignTable]) {
console.log(`Foreign table ${foreignTable} not found for ${ref.TABLE}.${ref.CONSTRAINT}`);
continue;
}
if (!('TABLE_REFERENCED_BY' in tableData[foreignTable])) {
tableData[foreignTable].TABLE_REFERENCED_BY = {};
}
// @ts-ignore
if (!tableData[foreignTable].TABLE_REFERENCED_BY[foreignColumn]) {
// @ts-ignore
tableData[foreignTable].TABLE_REFERENCED_BY[foreignColumn] = [];
}
// @ts-ignore
tableData[foreignTable].TABLE_REFERENCED_BY[foreignColumn].push({
TABLE: tableName,
COLUMN: columnName,
CONSTRAINT: constraintName
});
if (!tableData[tableName].TABLE_REFERENCES) {
tableData[tableName].TABLE_REFERENCES = {};
}
// @ts-ignore
if (!tableData[tableName].TABLE_REFERENCES[columnName]) {
// @ts-ignore
tableData[tableName].TABLE_REFERENCES[columnName] = [];
}
// @ts-ignore
tableData[tableName].TABLE_REFERENCES[columnName].push({
TABLE: foreignTable,
COLUMN: foreignColumn,
CONSTRAINT: constraintName
});
}
const tables = Object.values(tableData);
return {
C6VERSION: version,
PREFIX: MySQLDump.DB_PREFIX,
TABLES: tables,
RestTableNames: tables.map(table => "'" + table.TABLE_NAME + "'").join('\n | '),
RestShortTableNames: tables.map(table => "'" + table.TABLE_NAME_SHORT + "'").join('\n | '),
RestTableInterfaces: tables.map(table => 'i' + table.TABLE_NAME_SHORT_PASCAL_CASE).join('\n | '),
};
};
// use dumpFileLocation to get sql
const sql = fs.readFileSync(dumpFileLocation, 'utf-8');
const tableData = parseSQLToTypeScript(sql);
// write to file
fs.writeFileSync(path.join(process.cwd(), 'C6MySqlDump.json'), JSON.stringify(tableData));
// import this file src/assets/handlebars/C6.tsx.handlebars for a mustache template
const c6Template = fs.readFileSync(path.resolve(__dirname, 'assets/handlebars/C6.ts.handlebars'), 'utf-8');
fs.writeFileSync(path.join(MySQLDump.OUTPUT_DIR, 'C6.ts'), Handlebars.compile(c6Template)(tableData));
const template = fs.readFileSync(path.resolve(__dirname, 'assets/handlebars/Table.ts.handlebars'), 'utf-8');
const testTemplate = fs.readFileSync(path.resolve(__dirname, 'assets/handlebars/Table.test.ts.handlebars'), 'utf-8');
Object.values(tableData.TABLES).forEach((tableData) => {
const tableName = tableData.TABLE_NAME_SHORT_PASCAL_CASE
fs.writeFileSync(path.join(MySQLDump.OUTPUT_DIR, tableName + '.ts'), Handlebars.compile(template)(tableData));
fs.writeFileSync(path.join(MySQLDump.OUTPUT_DIR, tableName + '.test.ts'), Handlebars.compile(testTemplate)(tableData));
})
console.log('Successfully created CarbonORM bindings!')