@reldens/storage 0.42.0 → 0.44.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -44,58 +44,20 @@ class EntitiesGenerator
|
|
|
44
44
|
};
|
|
45
45
|
this.server = sc.get(props, 'server', false);
|
|
46
46
|
this.connectionData = sc.get(props, 'connectionData', false);
|
|
47
|
-
if(!this.server && !this.connectionData){
|
|
48
|
-
ErrorManager.error('Either server or connectionData must be provided to EntitiesGenerator.');
|
|
49
|
-
}
|
|
50
47
|
this.generatedEntities = {};
|
|
51
48
|
}
|
|
52
49
|
|
|
53
|
-
ensureFoldersExist()
|
|
54
|
-
{
|
|
55
|
-
if(!FileHandler.exists(this.generationFolder)){
|
|
56
|
-
try {
|
|
57
|
-
FileHandler.createFolder(this.generationFolder, { recursive: true });
|
|
58
|
-
} catch (error) {
|
|
59
|
-
ErrorManager.error('Failed to create generation folder: '+error.message);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if(!FileHandler.exists(this.entitiesFolder)){
|
|
63
|
-
try {
|
|
64
|
-
FileHandler.createFolder(this.entitiesFolder, { recursive: true });
|
|
65
|
-
} catch (error) {
|
|
66
|
-
ErrorManager.error('Failed to create entities folder: '+error.message);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if(!FileHandler.exists(this.modelsFolder)){
|
|
70
|
-
try {
|
|
71
|
-
FileHandler.createFolder(this.modelsFolder, { recursive: true });
|
|
72
|
-
} catch (error) {
|
|
73
|
-
ErrorManager.error('Failed to create models folder: '+error.message);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
for(let driverKey of Object.keys(this.driverMap)){
|
|
77
|
-
let driverFolder = FileHandler.joinPaths(this.modelsFolder, driverKey);
|
|
78
|
-
if(!FileHandler.exists(driverFolder)){
|
|
79
|
-
try {
|
|
80
|
-
FileHandler.createFolder(driverFolder, { recursive: true });
|
|
81
|
-
} catch (error) {
|
|
82
|
-
ErrorManager.error('Failed to create driver folder: '+error.message);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
50
|
async generateEntityFile(tableName, tableData)
|
|
89
51
|
{
|
|
90
52
|
let entityTemplatePath = this.templates['entity'];
|
|
91
53
|
if(!FileHandler.exists(entityTemplatePath)){
|
|
92
|
-
|
|
54
|
+
Logger.critical('Entity template file not found: '+entityTemplatePath);
|
|
55
|
+
return false;
|
|
93
56
|
}
|
|
94
|
-
let entityTemplateContent;
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
ErrorManager.error('Failed to read entity template file: '+error.message);
|
|
57
|
+
let entityTemplateContent = FileHandler.fetchFileContents(entityTemplatePath);
|
|
58
|
+
if(!entityTemplateContent) {
|
|
59
|
+
Logger.critical('Failed to read entity template file: '+entityTemplatePath);
|
|
60
|
+
return false;
|
|
99
61
|
}
|
|
100
62
|
let entityClassName = sc.capitalizedCamelCase(tableName)+'Entity';
|
|
101
63
|
let titleProperty = this.determineTitleProperty(tableData.columns);
|
|
@@ -114,7 +76,8 @@ class EntitiesGenerator
|
|
|
114
76
|
}
|
|
115
77
|
let editPropertiesRemoval = this.getEditPropertiesRemoval(fieldsToRemoveFromEdit);
|
|
116
78
|
let listPropertiesDeclaration = this.getListPropertiesDeclaration(tableData.columns);
|
|
117
|
-
let needsSc = listPropertiesDeclaration.includes('sc.removeFromArray')
|
|
79
|
+
let needsSc = listPropertiesDeclaration.includes('sc.removeFromArray')
|
|
80
|
+
|| editPropertiesRemoval.includes('sc.removeFromArray');
|
|
118
81
|
let scRequire = needsSc ? '\nconst { sc } = require(\'@reldens/utils\');' : '';
|
|
119
82
|
let replacements = {
|
|
120
83
|
entityClassName,
|
|
@@ -128,25 +91,26 @@ class EntitiesGenerator
|
|
|
128
91
|
let entityContent = this.applyReplacements(entityTemplateContent, replacements);
|
|
129
92
|
let fileName = sc.kebabCase(tableName)+'-entity.js';
|
|
130
93
|
let filePath = FileHandler.joinPaths(this.entitiesFolder, fileName);
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
this.generatedEntities[tableName] = {
|
|
135
|
-
entityClassName,
|
|
136
|
-
entityFileName: fileName,
|
|
137
|
-
tableName,
|
|
138
|
-
titleProperty
|
|
139
|
-
};
|
|
140
|
-
return true;
|
|
141
|
-
} catch (error) {
|
|
142
|
-
ErrorManager.error('Failed to write entity file: '+error.message);
|
|
94
|
+
if(!FileHandler.writeFile(filePath, entityContent)){
|
|
95
|
+
Logger.critical('Failed to write entity file: '+filePath);
|
|
96
|
+
return false;
|
|
143
97
|
}
|
|
98
|
+
Logger.info('Generated entity file: '+filePath);
|
|
99
|
+
this.generatedEntities[tableName] = {
|
|
100
|
+
entityClassName,
|
|
101
|
+
entityFileName: fileName,
|
|
102
|
+
tableName,
|
|
103
|
+
titleProperty
|
|
104
|
+
};
|
|
105
|
+
return true;
|
|
144
106
|
}
|
|
145
107
|
|
|
146
108
|
getEditPropertiesRemoval(fieldsToRemove)
|
|
147
109
|
{
|
|
148
110
|
if(1 === fieldsToRemove.length){
|
|
149
|
-
return 'let editProperties = [...showProperties];\n editProperties.splice(editProperties.indexOf(\''+
|
|
111
|
+
return 'let editProperties = [...showProperties];\n editProperties.splice(editProperties.indexOf(\''+
|
|
112
|
+
fieldsToRemove[0]
|
|
113
|
+
+'\'), 1);';
|
|
150
114
|
}
|
|
151
115
|
return 'let editProperties = sc.removeFromArray([...showProperties], [\''+fieldsToRemove.join('\', \'')+'\']);';
|
|
152
116
|
}
|
|
@@ -183,7 +147,9 @@ class EntitiesGenerator
|
|
|
183
147
|
propertiesConfig.push(propertyKey+': {}');
|
|
184
148
|
continue;
|
|
185
149
|
}
|
|
186
|
-
propertiesConfig.push(
|
|
150
|
+
propertiesConfig.push(
|
|
151
|
+
propertyKey+': {\n '+props.join(',\n ')+ '\n }'
|
|
152
|
+
);
|
|
187
153
|
}
|
|
188
154
|
return propertiesConfig.join(',\n ');
|
|
189
155
|
}
|
|
@@ -239,7 +205,11 @@ class EntitiesGenerator
|
|
|
239
205
|
for(let i = 0; i < enumValues.length; i++){
|
|
240
206
|
availableValues.push('{value: '+(i+1)+', label: \''+enumValues[i]+'\'}');
|
|
241
207
|
}
|
|
242
|
-
props.push(
|
|
208
|
+
props.push(
|
|
209
|
+
'availableValues: [\n '+
|
|
210
|
+
availableValues.join(',\n ')+
|
|
211
|
+
'\n ]'
|
|
212
|
+
);
|
|
243
213
|
}
|
|
244
214
|
|
|
245
215
|
parseEnumValues(columnType)
|
|
@@ -284,7 +254,9 @@ class EntitiesGenerator
|
|
|
284
254
|
return 'let listProperties = showProperties;';
|
|
285
255
|
}
|
|
286
256
|
if(1 === fieldsToRemove.length){
|
|
287
|
-
return 'let listProperties = [...showProperties];\n listProperties.splice(listProperties.indexOf(\''+
|
|
257
|
+
return 'let listProperties = [...showProperties];\n listProperties.splice(listProperties.indexOf(\''+
|
|
258
|
+
fieldsToRemove[0]+
|
|
259
|
+
'\'), 1);';
|
|
288
260
|
}
|
|
289
261
|
return 'let listProperties = sc.removeFromArray([...showProperties], [\''+fieldsToRemove.join('\', \'')+'\']);';
|
|
290
262
|
}
|
|
@@ -306,13 +278,13 @@ class EntitiesGenerator
|
|
|
306
278
|
{
|
|
307
279
|
let modelTemplatePath = this.templates[driverKey];
|
|
308
280
|
if(!FileHandler.exists(modelTemplatePath)){
|
|
309
|
-
|
|
281
|
+
Logger.critical('Model template file "'+modelTemplatePath+'" not found for driver "'+driverKey+'".');
|
|
282
|
+
return false;
|
|
310
283
|
}
|
|
311
|
-
let modelTemplateContent;
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
ErrorManager.error('Failed to read model template file: '+error.message);
|
|
284
|
+
let modelTemplateContent = FileHandler.fetchFileContents(modelTemplatePath);
|
|
285
|
+
if(!modelTemplateContent){
|
|
286
|
+
Logger.critical('Failed to read model template file: '+modelTemplatePath);
|
|
287
|
+
return false;
|
|
316
288
|
}
|
|
317
289
|
let modelClassName = sc.capitalizedCamelCase(tableName)+'Model';
|
|
318
290
|
let modelPropertiesList = Object.keys(tableData.columns).join(', ');
|
|
@@ -332,17 +304,20 @@ class EntitiesGenerator
|
|
|
332
304
|
let modelContent = this.applyReplacements(modelTemplateContent, replacements);
|
|
333
305
|
let fileName = sc.kebabCase(tableName)+'-model.js';
|
|
334
306
|
let driverFolder = FileHandler.joinPaths(this.modelsFolder, driverKey);
|
|
307
|
+
if(!FileHandler.createFolder(driverFolder)){
|
|
308
|
+
Logger.critical('Failed to create driver folder.');
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
335
311
|
let filePath = FileHandler.joinPaths(driverFolder, fileName);
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
this.generatedEntities[tableName].modelClassName = modelClassName;
|
|
340
|
-
this.generatedEntities[tableName].modelFileName = fileName;
|
|
341
|
-
this.generatedEntities[tableName].driverKey = driverKey;
|
|
342
|
-
return true;
|
|
343
|
-
} catch (error) {
|
|
344
|
-
ErrorManager.error('Failed to write model file: '+error.message);
|
|
312
|
+
if(!FileHandler.writeFile(filePath, modelContent)){
|
|
313
|
+
Logger.critical('Failed to write model file: '+filePath);
|
|
314
|
+
return false;
|
|
345
315
|
}
|
|
316
|
+
Logger.info('Generated model file: '+filePath);
|
|
317
|
+
this.generatedEntities[tableName].modelClassName = modelClassName;
|
|
318
|
+
this.generatedEntities[tableName].modelFileName = fileName;
|
|
319
|
+
this.generatedEntities[tableName].driverKey = driverKey;
|
|
320
|
+
return true;
|
|
346
321
|
}
|
|
347
322
|
|
|
348
323
|
getEntityPropertiesDefinition(columns, driverKey)
|
|
@@ -390,16 +365,13 @@ class EntitiesGenerator
|
|
|
390
365
|
{
|
|
391
366
|
let configTemplatePath = this.templates['entities-config'];
|
|
392
367
|
if(!FileHandler.exists(configTemplatePath)){
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
let configTemplateContent;
|
|
396
|
-
try {
|
|
397
|
-
configTemplateContent = FileHandler.fetchFileContents(configTemplatePath);
|
|
398
|
-
} catch (error) {
|
|
399
|
-
ErrorManager.error('Failed to read entities config template file: '+error.message);
|
|
368
|
+
Logger.critical('Entities config template file not found: '+configTemplatePath);
|
|
369
|
+
return false;
|
|
400
370
|
}
|
|
401
|
-
|
|
402
|
-
|
|
371
|
+
let configTemplateContent = FileHandler.fetchFileContents(configTemplatePath);
|
|
372
|
+
if(!configTemplateContent) {
|
|
373
|
+
Logger.critical('Failed to read entities config template file: '+configTemplatePath);
|
|
374
|
+
return false;
|
|
403
375
|
}
|
|
404
376
|
let requireStatements = this.getRequireStatements();
|
|
405
377
|
let entitiesConfigExport = this.getEntitiesConfigExport();
|
|
@@ -408,13 +380,12 @@ class EntitiesGenerator
|
|
|
408
380
|
entitiesConfigExport
|
|
409
381
|
};
|
|
410
382
|
let configContent = this.applyReplacements(configTemplateContent, replacements);
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
return true;
|
|
415
|
-
} catch (error) {
|
|
416
|
-
ErrorManager.error('Failed to write entities config file: '+error.message);
|
|
383
|
+
if (!FileHandler.writeFile(this.entitiesConfigPath, configContent)){
|
|
384
|
+
Logger.critical('Failed to write entities config file: '+this.entitiesConfigPath);
|
|
385
|
+
return false;
|
|
417
386
|
}
|
|
387
|
+
Logger.info('Generated entities config file: '+this.entitiesConfigPath);
|
|
388
|
+
return true;
|
|
418
389
|
}
|
|
419
390
|
|
|
420
391
|
getRequireStatements()
|
|
@@ -422,7 +393,10 @@ class EntitiesGenerator
|
|
|
422
393
|
let requireStatements = [];
|
|
423
394
|
for(let tableName in this.generatedEntities){
|
|
424
395
|
let entity = this.generatedEntities[tableName];
|
|
425
|
-
|
|
396
|
+
let nameWithoutExtension = entity.entityFileName.replace('.js', '');
|
|
397
|
+
requireStatements.push(
|
|
398
|
+
'const { '+ entity.entityClassName+' } = require(\'./entities/'+nameWithoutExtension+'\');'
|
|
399
|
+
);
|
|
426
400
|
}
|
|
427
401
|
return requireStatements.join('\n');
|
|
428
402
|
}
|
|
@@ -433,7 +407,10 @@ class EntitiesGenerator
|
|
|
433
407
|
for(let tableName in this.generatedEntities){
|
|
434
408
|
let entity = this.generatedEntities[tableName];
|
|
435
409
|
let camelCaseKey = sc.camelCase(tableName);
|
|
436
|
-
|
|
410
|
+
let capitalizedCase = sc.capitalizedCamelCase(tableName);
|
|
411
|
+
entitiesConfigExport.push(
|
|
412
|
+
camelCaseKey+': '+entity.entityClassName+'.propertiesConfig({parentItemLabel: \''+capitalizedCase+'\'})'
|
|
413
|
+
);
|
|
437
414
|
}
|
|
438
415
|
return entitiesConfigExport.join(',\n ');
|
|
439
416
|
}
|
|
@@ -442,39 +419,29 @@ class EntitiesGenerator
|
|
|
442
419
|
{
|
|
443
420
|
let translationsTemplatePath = this.templates['entities-translations'];
|
|
444
421
|
if(!FileHandler.exists(translationsTemplatePath)){
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
let translationsTemplateContent;
|
|
448
|
-
try {
|
|
449
|
-
translationsTemplateContent = FileHandler.fetchFileContents(translationsTemplatePath);
|
|
450
|
-
} catch (error) {
|
|
451
|
-
ErrorManager.error('Failed to read entities translations template file: '+error.message);
|
|
422
|
+
Logger.critical('Entities translations template file not found: '+translationsTemplatePath);
|
|
423
|
+
return false;
|
|
452
424
|
}
|
|
453
|
-
|
|
454
|
-
|
|
425
|
+
let translationsTemplateContent = FileHandler.fetchFileContents(translationsTemplatePath);
|
|
426
|
+
if(!translationsTemplateContent){
|
|
427
|
+
Logger.critical('Failed to read entities translations template file: '+translationsTemplatePath);
|
|
428
|
+
return false;
|
|
455
429
|
}
|
|
456
430
|
let labels = this.getTranslationLabels();
|
|
457
|
-
let translationsContent = translationsTemplateContent.replace(
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
try {
|
|
462
|
-
FileHandler.writeFile(this.entitiesTranslationsPath, translationsContent);
|
|
463
|
-
Logger.info('Generated entities translations file: '+this.entitiesTranslationsPath);
|
|
464
|
-
return true;
|
|
465
|
-
} catch (error) {
|
|
466
|
-
ErrorManager.error('Failed to write entities translations file: '+error.message);
|
|
431
|
+
let translationsContent = translationsTemplateContent.replace(/{{labels}}/g, labels);
|
|
432
|
+
if(!FileHandler.writeFile(this.entitiesTranslationsPath, translationsContent)){
|
|
433
|
+
Logger.critical('Failed to write entities translations file: '+this.entitiesTranslationsPath);
|
|
434
|
+
return false;
|
|
467
435
|
}
|
|
436
|
+
Logger.info('Generated entities translations file: '+this.entitiesTranslationsPath);
|
|
437
|
+
return true;
|
|
468
438
|
}
|
|
469
439
|
|
|
470
440
|
getTranslationLabels()
|
|
471
441
|
{
|
|
472
442
|
let labels = [];
|
|
473
443
|
for(let tableName in this.generatedEntities){
|
|
474
|
-
let humanReadable = tableName
|
|
475
|
-
.split('_')
|
|
476
|
-
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
|
477
|
-
.join(' ');
|
|
444
|
+
let humanReadable = tableName.split('_').map(word => word.charAt(0).toUpperCase()+word.slice(1)).join(' ');
|
|
478
445
|
labels.push('\''+tableName+'\': \''+humanReadable+'\'');
|
|
479
446
|
}
|
|
480
447
|
return labels.join(',\n ');
|
|
@@ -484,33 +451,27 @@ class EntitiesGenerator
|
|
|
484
451
|
{
|
|
485
452
|
let registeredTemplatePath = this.templates['registered-models'];
|
|
486
453
|
if(!FileHandler.exists(registeredTemplatePath)){
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
let registeredTemplateContent;
|
|
490
|
-
try {
|
|
491
|
-
registeredTemplateContent = FileHandler.fetchFileContents(registeredTemplatePath);
|
|
492
|
-
} catch (error) {
|
|
493
|
-
ErrorManager.error('Failed to read registered models template file: '+error.message);
|
|
454
|
+
Logger.critical('Registered models template file not found: '+registeredTemplatePath);
|
|
455
|
+
return false;
|
|
494
456
|
}
|
|
495
|
-
|
|
496
|
-
|
|
457
|
+
let registeredTemplateContent = FileHandler.fetchFileContents(registeredTemplatePath);
|
|
458
|
+
if(!registeredTemplateContent){
|
|
459
|
+
Logger.critical('Failed to read registered models template file: '+registeredTemplatePath);
|
|
460
|
+
return false;
|
|
497
461
|
}
|
|
498
|
-
let registeredModels = this.getRegisteredModels(driverKey);
|
|
499
|
-
let registeredEntitiesObject = this.getRegisteredEntitiesObject(driverKey);
|
|
500
462
|
let replacements = {
|
|
501
|
-
registeredModels,
|
|
502
|
-
registeredEntitiesObject
|
|
463
|
+
registeredModels: this.getRegisteredModels(driverKey),
|
|
464
|
+
registeredEntitiesObject: this.getRegisteredEntitiesObject(driverKey)
|
|
503
465
|
};
|
|
504
466
|
let registeredContent = this.applyReplacements(registeredTemplateContent, replacements);
|
|
505
467
|
let driverFolder = FileHandler.joinPaths(this.modelsFolder, driverKey);
|
|
506
468
|
let filePath = FileHandler.joinPaths(driverFolder, 'registered-models-'+driverKey+'.js');
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
return true;
|
|
511
|
-
} catch (error) {
|
|
512
|
-
ErrorManager.error('Failed to write registered models file: '+error.message);
|
|
469
|
+
if(!FileHandler.writeFile(filePath, registeredContent)){
|
|
470
|
+
Logger.critical('Failed to write registered models file: '+filePath);
|
|
471
|
+
return false;
|
|
513
472
|
}
|
|
473
|
+
Logger.info('Generated registered models file: '+filePath);
|
|
474
|
+
return true;
|
|
514
475
|
}
|
|
515
476
|
|
|
516
477
|
getRegisteredModels(driverKey)
|
|
@@ -518,9 +479,12 @@ class EntitiesGenerator
|
|
|
518
479
|
let registeredModels = [];
|
|
519
480
|
for(let tableName in this.generatedEntities){
|
|
520
481
|
let entity = this.generatedEntities[tableName];
|
|
521
|
-
if(
|
|
522
|
-
|
|
482
|
+
if(driverKey !== entity.driverKey){
|
|
483
|
+
continue;
|
|
523
484
|
}
|
|
485
|
+
registeredModels.push(
|
|
486
|
+
'const { '+entity.modelClassName+' } = require(\'./'+entity.modelFileName.replace('.js', '')+'\');'
|
|
487
|
+
);
|
|
524
488
|
}
|
|
525
489
|
return registeredModels.join('\n');
|
|
526
490
|
}
|
|
@@ -530,10 +494,10 @@ class EntitiesGenerator
|
|
|
530
494
|
let registeredEntitiesObject = [];
|
|
531
495
|
for(let tableName in this.generatedEntities){
|
|
532
496
|
let entity = this.generatedEntities[tableName];
|
|
533
|
-
if(
|
|
534
|
-
|
|
535
|
-
registeredEntitiesObject.push(camelCaseKey+': '+entity.modelClassName);
|
|
497
|
+
if(driverKey !== entity.driverKey){
|
|
498
|
+
continue;
|
|
536
499
|
}
|
|
500
|
+
registeredEntitiesObject.push(sc.camelCase(tableName)+': '+entity.modelClassName);
|
|
537
501
|
}
|
|
538
502
|
return registeredEntitiesObject.join(',\n ');
|
|
539
503
|
}
|
|
@@ -541,15 +505,13 @@ class EntitiesGenerator
|
|
|
541
505
|
applyReplacements(content, replacements)
|
|
542
506
|
{
|
|
543
507
|
let result = content;
|
|
544
|
-
for(let placeholder
|
|
545
|
-
result = result.replace(
|
|
546
|
-
new RegExp('{{'+placeholder+'}}', 'g'),
|
|
547
|
-
replacements[placeholder]
|
|
548
|
-
);
|
|
508
|
+
for(let placeholder of Object.keys(replacements)){
|
|
509
|
+
result = result.replace(new RegExp('{{'+placeholder+'}}','g'), replacements[placeholder]);
|
|
549
510
|
}
|
|
550
511
|
return result;
|
|
551
512
|
}
|
|
552
513
|
|
|
514
|
+
|
|
553
515
|
async generate()
|
|
554
516
|
{
|
|
555
517
|
if(!this.server){
|
|
@@ -566,7 +528,9 @@ class EntitiesGenerator
|
|
|
566
528
|
Logger.critical('EntitiesGenerator tables fetch failed.');
|
|
567
529
|
return false;
|
|
568
530
|
}
|
|
569
|
-
this.ensureFoldersExist()
|
|
531
|
+
if(!this.ensureFoldersExist()){
|
|
532
|
+
return false;
|
|
533
|
+
}
|
|
570
534
|
let driverKey = sc.get(
|
|
571
535
|
this.connectionData,
|
|
572
536
|
'driver',
|
|
@@ -595,7 +559,8 @@ class EntitiesGenerator
|
|
|
595
559
|
let driverKey = sc.get(this.connectionData, 'driver', 'objection-js');
|
|
596
560
|
let driverClassMapped = this.driverMap[driverKey];
|
|
597
561
|
if(!driverClassMapped){
|
|
598
|
-
|
|
562
|
+
Logger.critical('Unsupported driver: '+driverKey);
|
|
563
|
+
return false;
|
|
599
564
|
}
|
|
600
565
|
this.server = new driverClassMapped({
|
|
601
566
|
client: sc.get(this.connectionData, 'client', 'mysql2'),
|
|
@@ -610,6 +575,23 @@ class EntitiesGenerator
|
|
|
610
575
|
return this.server;
|
|
611
576
|
}
|
|
612
577
|
|
|
578
|
+
ensureFoldersExist()
|
|
579
|
+
{
|
|
580
|
+
if(!FileHandler.createFolder(this.generationFolder)){
|
|
581
|
+
Logger.critical('Failed to create generation folder.');
|
|
582
|
+
return false;
|
|
583
|
+
}
|
|
584
|
+
if(!FileHandler.createFolder(this.entitiesFolder)){
|
|
585
|
+
Logger.critical('Failed to create entities folder.');
|
|
586
|
+
return false;
|
|
587
|
+
}
|
|
588
|
+
if(!FileHandler.createFolder(this.modelsFolder)){
|
|
589
|
+
Logger.critical('Failed to create models folder.');
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
return true;
|
|
593
|
+
}
|
|
594
|
+
|
|
613
595
|
}
|
|
614
596
|
|
|
615
597
|
module.exports.EntitiesGenerator = EntitiesGenerator;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const {
|
|
7
|
+
const { Logger, sc } = require('@reldens/utils');
|
|
8
8
|
|
|
9
9
|
class MySQLTablesProvider
|
|
10
10
|
{
|
|
@@ -30,7 +30,8 @@ class MySQLTablesProvider
|
|
|
30
30
|
try {
|
|
31
31
|
let result = await server.rawQuery(tablesQuery);
|
|
32
32
|
if(!sc.isArray(result) || 0 === result.length){
|
|
33
|
-
|
|
33
|
+
Logger.critical('No tables found in the database.');
|
|
34
|
+
return false;
|
|
34
35
|
}
|
|
35
36
|
let tables = {};
|
|
36
37
|
for(let row of result){
|
|
@@ -63,18 +64,20 @@ class MySQLTablesProvider
|
|
|
63
64
|
' AND REFERENCED_TABLE_NAME IS NOT NULL;';
|
|
64
65
|
|
|
65
66
|
let foreignKeysResult = await server.rawQuery(foreignKeysQuery);
|
|
66
|
-
if(sc.isArray(foreignKeysResult)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
67
|
+
if(!sc.isArray(foreignKeysResult) || 0 < foreignKeysResult.length){
|
|
68
|
+
return tables;
|
|
69
|
+
}
|
|
70
|
+
for(let row of foreignKeysResult){
|
|
71
|
+
if(tables[row.TABLE_NAME] && tables[row.TABLE_NAME].columns[row.COLUMN_NAME]){
|
|
72
|
+
tables[row.TABLE_NAME].columns[row.COLUMN_NAME].referencedTable = row.REFERENCED_TABLE_NAME;
|
|
73
|
+
tables[row.TABLE_NAME].columns[row.COLUMN_NAME].referencedColumn = row.REFERENCED_COLUMN_NAME;
|
|
72
74
|
}
|
|
73
75
|
}
|
|
74
76
|
return tables;
|
|
75
77
|
} catch(error) {
|
|
76
|
-
|
|
78
|
+
Logger.critical('Failed to fetch tables from database: '+error.message);
|
|
77
79
|
}
|
|
80
|
+
return false;
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
}
|
|
@@ -31,9 +31,9 @@ class PrismaSchemaGenerator
|
|
|
31
31
|
execSync('npx prisma generate', { stdio: 'inherit' });
|
|
32
32
|
return true;
|
|
33
33
|
} catch(error) {
|
|
34
|
-
|
|
35
|
-
return false;
|
|
34
|
+
Logger.critical('Failed to generate Prisma schema. '+error.message);
|
|
36
35
|
}
|
|
36
|
+
return false;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
generateSchemaFile()
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reldens/storage",
|
|
3
3
|
"scope": "@reldens",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.44.0",
|
|
5
5
|
"description": "Reldens - Storage",
|
|
6
6
|
"author": "Damian A. Pastorini",
|
|
7
7
|
"license": "MIT",
|
|
@@ -32,19 +32,11 @@
|
|
|
32
32
|
"database",
|
|
33
33
|
"db",
|
|
34
34
|
"system",
|
|
35
|
-
"game",
|
|
36
|
-
"mmorpg",
|
|
37
|
-
"rpg",
|
|
38
|
-
"dwd",
|
|
39
|
-
"colyseus",
|
|
40
|
-
"phaser",
|
|
41
|
-
"parcel",
|
|
42
35
|
"nodejs",
|
|
43
|
-
"mmo",
|
|
44
|
-
"multiplayer",
|
|
45
|
-
"rol",
|
|
46
36
|
"platform",
|
|
47
|
-
"framework"
|
|
37
|
+
"framework",
|
|
38
|
+
"library",
|
|
39
|
+
"lib"
|
|
48
40
|
],
|
|
49
41
|
"bugs": {
|
|
50
42
|
"url": "https://github.com/damian-pastorini/reldens-storage/issues"
|