@reldens/storage 0.47.0 → 0.49.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.
- package/lib/entities-generator.js +100 -52
- package/package.json +1 -1
|
@@ -44,7 +44,9 @@ class EntitiesGenerator
|
|
|
44
44
|
};
|
|
45
45
|
this.server = sc.get(props, 'server', false);
|
|
46
46
|
this.connectionData = sc.get(props, 'connectionData', false);
|
|
47
|
+
this.isOverride = sc.get(props, 'isOverride', false);
|
|
47
48
|
this.generatedEntities = {};
|
|
49
|
+
this.existingEntities = {};
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
async generateEntityFile(tableName, tableData)
|
|
@@ -193,6 +195,10 @@ class EntitiesGenerator
|
|
|
193
195
|
props.push('type: \'datetime\'');
|
|
194
196
|
return;
|
|
195
197
|
}
|
|
198
|
+
if('text' === type || 'longtext' === type || 'mediumtext' === type || 'tinytext' === type){
|
|
199
|
+
props.push('type: \'textarea\'');
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
196
202
|
if('tinyint' === type){
|
|
197
203
|
props.push('type: \'boolean\'');
|
|
198
204
|
return;
|
|
@@ -291,16 +297,6 @@ class EntitiesGenerator
|
|
|
291
297
|
return passwordFields;
|
|
292
298
|
}
|
|
293
299
|
|
|
294
|
-
getEditPropertiesRemoval(fieldsToRemove)
|
|
295
|
-
{
|
|
296
|
-
if(1 === fieldsToRemove.length){
|
|
297
|
-
return 'let editProperties = [...propertiesKeys];\n editProperties.splice(editProperties.indexOf(\''+
|
|
298
|
-
fieldsToRemove[0]
|
|
299
|
-
+'\'), 1);';
|
|
300
|
-
}
|
|
301
|
-
return 'let editProperties = sc.removeFromArray([...propertiesKeys], [\''+fieldsToRemove.join('\', \'')+'\']);';
|
|
302
|
-
}
|
|
303
|
-
|
|
304
300
|
getListPropertiesDeclaration(columns)
|
|
305
301
|
{
|
|
306
302
|
let fieldsToRemove = this.getFieldsToRemoveFromList(columns);
|
|
@@ -402,26 +398,60 @@ class EntitiesGenerator
|
|
|
402
398
|
return '';
|
|
403
399
|
}
|
|
404
400
|
|
|
405
|
-
|
|
401
|
+
detectExistingEntities()
|
|
406
402
|
{
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
403
|
+
if(!FileHandler.exists(this.entitiesFolder)){
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
let entityFiles = FileHandler.getFilesInFolder(this.entitiesFolder, ['.js']);
|
|
407
|
+
for(let file of entityFiles){
|
|
408
|
+
if(!file.endsWith('-entity.js')){
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
let tableName = file.replace('-entity.js', '').replace(/-/g, '_');
|
|
412
|
+
this.existingEntities[tableName] = {entityFileName: file, tableName};
|
|
411
413
|
}
|
|
412
|
-
|
|
414
|
+
Logger.info('Detected '+Object.keys(this.existingEntities).length+' existing entities.');
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
filterTablesToGenerate(tables)
|
|
418
|
+
{
|
|
419
|
+
if(this.isOverride){
|
|
420
|
+
return tables;
|
|
421
|
+
}
|
|
422
|
+
let filteredTables = {};
|
|
423
|
+
let newTablesCount = 0;
|
|
424
|
+
for(let tableName of Object.keys(tables)){
|
|
425
|
+
if(!this.existingEntities[tableName]){
|
|
426
|
+
filteredTables[tableName] = tables[tableName];
|
|
427
|
+
newTablesCount++;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
if(0 === newTablesCount){
|
|
431
|
+
Logger.info('No new tables found. All entities are up to date.');
|
|
432
|
+
}
|
|
433
|
+
if(0 < newTablesCount){
|
|
434
|
+
Logger.info('Found '+newTablesCount+' new tables to generate entities for.');
|
|
435
|
+
}
|
|
436
|
+
return filteredTables;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
generateEntitiesConfigFile()
|
|
440
|
+
{
|
|
441
|
+
let configTemplateContent = FileHandler.fetchFileContents(this.templates['entities-config']);
|
|
413
442
|
if(!configTemplateContent) {
|
|
414
|
-
Logger.critical('Failed to read entities config template file: '+
|
|
443
|
+
Logger.critical('Failed to read entities config template file: '+this.templates['entities-config']);
|
|
415
444
|
return false;
|
|
416
445
|
}
|
|
417
|
-
let
|
|
418
|
-
let entitiesConfigExport = this.getEntitiesConfigExport();
|
|
446
|
+
let allEntities = Object.assign({}, this.existingEntities, this.generatedEntities);
|
|
419
447
|
let replacements = {
|
|
420
|
-
requireStatements,
|
|
421
|
-
entitiesConfigExport
|
|
448
|
+
requireStatements: this.getRequireStatements(allEntities),
|
|
449
|
+
entitiesConfigExport: this.getEntitiesConfigExport(allEntities)
|
|
422
450
|
};
|
|
423
|
-
|
|
424
|
-
|
|
451
|
+
if(!FileHandler.writeFile(
|
|
452
|
+
this.entitiesConfigPath,
|
|
453
|
+
this.applyReplacements(configTemplateContent, replacements)
|
|
454
|
+
)){
|
|
425
455
|
Logger.critical('Failed to write entities config file: '+this.entitiesConfigPath);
|
|
426
456
|
return false;
|
|
427
457
|
}
|
|
@@ -429,25 +459,29 @@ class EntitiesGenerator
|
|
|
429
459
|
return true;
|
|
430
460
|
}
|
|
431
461
|
|
|
432
|
-
getRequireStatements()
|
|
462
|
+
getRequireStatements(allEntities)
|
|
433
463
|
{
|
|
434
464
|
let requireStatements = [];
|
|
435
|
-
for(let tableName of Object.keys(
|
|
436
|
-
let entity =
|
|
437
|
-
let
|
|
465
|
+
for(let tableName of Object.keys(allEntities)){
|
|
466
|
+
let entity = allEntities[tableName];
|
|
467
|
+
let entityClassName = sc.get(entity, 'entityClassName', sc.capitalizedCamelCase(tableName)+'Entity');
|
|
468
|
+
let entityFileName = sc.get(entity, 'entityFileName', sc.kebabCase(tableName)+'-entity').replace('.js', '');
|
|
438
469
|
requireStatements.push(
|
|
439
|
-
'const { '+
|
|
470
|
+
'const { '+ entityClassName+' } = require(\'./entities/'+entityFileName+'\');'
|
|
440
471
|
);
|
|
441
472
|
}
|
|
442
473
|
return requireStatements.join('\n');
|
|
443
474
|
}
|
|
444
475
|
|
|
445
|
-
getEntitiesConfigExport()
|
|
476
|
+
getEntitiesConfigExport(allEntities)
|
|
446
477
|
{
|
|
447
478
|
let entitiesConfigExport = [];
|
|
448
|
-
for(let tableName of Object.keys(
|
|
449
|
-
|
|
450
|
-
|
|
479
|
+
for(let tableName of Object.keys(allEntities)){
|
|
480
|
+
entitiesConfigExport.push(
|
|
481
|
+
sc.camelCase(tableName)+': '
|
|
482
|
+
+sc.get(allEntities[tableName], 'entityClassName', sc.capitalizedCamelCase(tableName)+'Entity')
|
|
483
|
+
+'.propertiesConfig()'
|
|
484
|
+
);
|
|
451
485
|
}
|
|
452
486
|
return entitiesConfigExport.join(',\n ');
|
|
453
487
|
}
|
|
@@ -464,8 +498,10 @@ class EntitiesGenerator
|
|
|
464
498
|
Logger.critical('Failed to read entities translations template file: '+translationsTemplatePath);
|
|
465
499
|
return false;
|
|
466
500
|
}
|
|
467
|
-
let
|
|
468
|
-
|
|
501
|
+
let translationsContent = translationsTemplateContent.replace(
|
|
502
|
+
/{{labels}}/g,
|
|
503
|
+
this.getTranslationLabels(Object.assign({}, this.existingEntities, this.generatedEntities))
|
|
504
|
+
);
|
|
469
505
|
if(!FileHandler.writeFile(this.entitiesTranslationsPath, translationsContent)){
|
|
470
506
|
Logger.critical('Failed to write entities translations file: '+this.entitiesTranslationsPath);
|
|
471
507
|
return false;
|
|
@@ -474,12 +510,15 @@ class EntitiesGenerator
|
|
|
474
510
|
return true;
|
|
475
511
|
}
|
|
476
512
|
|
|
477
|
-
getTranslationLabels()
|
|
513
|
+
getTranslationLabels(allEntities)
|
|
478
514
|
{
|
|
479
515
|
let labels = [];
|
|
480
|
-
for(let tableName of Object.keys(
|
|
481
|
-
|
|
482
|
-
|
|
516
|
+
for(let tableName of Object.keys(allEntities)){
|
|
517
|
+
labels.push(
|
|
518
|
+
'\''+tableName+'\': \''
|
|
519
|
+
+tableName.split('_').map(word => word.charAt(0).toUpperCase()+word.slice(1)).join(' ')
|
|
520
|
+
+'\''
|
|
521
|
+
);
|
|
483
522
|
}
|
|
484
523
|
return labels.join(',\n ');
|
|
485
524
|
}
|
|
@@ -496,9 +535,10 @@ class EntitiesGenerator
|
|
|
496
535
|
Logger.critical('Failed to read registered models template file: '+registeredTemplatePath);
|
|
497
536
|
return false;
|
|
498
537
|
}
|
|
538
|
+
let allEntities = Object.assign({}, this.existingEntities, this.generatedEntities);
|
|
499
539
|
let replacements = {
|
|
500
|
-
registeredModels: this.getRegisteredModels(driverKey),
|
|
501
|
-
registeredEntitiesObject: this.getRegisteredEntitiesObject(driverKey)
|
|
540
|
+
registeredModels: this.getRegisteredModels(allEntities, driverKey),
|
|
541
|
+
registeredEntitiesObject: this.getRegisteredEntitiesObject(allEntities, driverKey)
|
|
502
542
|
};
|
|
503
543
|
let registeredContent = this.applyReplacements(registeredTemplateContent, replacements);
|
|
504
544
|
let driverFolder = FileHandler.joinPaths(this.modelsFolder, driverKey);
|
|
@@ -511,30 +551,33 @@ class EntitiesGenerator
|
|
|
511
551
|
return true;
|
|
512
552
|
}
|
|
513
553
|
|
|
514
|
-
getRegisteredModels(driverKey)
|
|
554
|
+
getRegisteredModels(allEntities, driverKey)
|
|
515
555
|
{
|
|
516
556
|
let registeredModels = [];
|
|
517
|
-
for(let tableName of Object.keys(
|
|
518
|
-
let entity =
|
|
519
|
-
if(driverKey !== entity.driverKey){
|
|
557
|
+
for(let tableName of Object.keys(allEntities)){
|
|
558
|
+
let entity = allEntities[tableName];
|
|
559
|
+
if(entity.driverKey && driverKey !== entity.driverKey){
|
|
520
560
|
continue;
|
|
521
561
|
}
|
|
562
|
+
let modelClassName = entity.modelClassName || sc.capitalizedCamelCase(tableName)+'Model';
|
|
563
|
+
let modelFileName = entity.modelFileName || sc.kebabCase(tableName)+'-model.js';
|
|
522
564
|
registeredModels.push(
|
|
523
|
-
'const { '+
|
|
565
|
+
'const { '+modelClassName+' } = require(\'./'+modelFileName.replace('.js', '')+'\');'
|
|
524
566
|
);
|
|
525
567
|
}
|
|
526
568
|
return registeredModels.join('\n');
|
|
527
569
|
}
|
|
528
570
|
|
|
529
|
-
getRegisteredEntitiesObject(driverKey)
|
|
571
|
+
getRegisteredEntitiesObject(allEntities, driverKey)
|
|
530
572
|
{
|
|
531
573
|
let registeredEntitiesObject = [];
|
|
532
|
-
for(let tableName of Object.keys(
|
|
533
|
-
let entity =
|
|
534
|
-
if(driverKey !== entity.driverKey){
|
|
574
|
+
for(let tableName of Object.keys(allEntities)){
|
|
575
|
+
let entity = allEntities[tableName];
|
|
576
|
+
if(entity.driverKey && driverKey !== entity.driverKey){
|
|
535
577
|
continue;
|
|
536
578
|
}
|
|
537
|
-
|
|
579
|
+
let modelClassName = entity.modelClassName || sc.capitalizedCamelCase(tableName)+'Model';
|
|
580
|
+
registeredEntitiesObject.push(sc.camelCase(tableName)+': '+modelClassName);
|
|
538
581
|
}
|
|
539
582
|
return registeredEntitiesObject.join(',\n ');
|
|
540
583
|
}
|
|
@@ -567,6 +610,11 @@ class EntitiesGenerator
|
|
|
567
610
|
if(!this.ensureFoldersExist()){
|
|
568
611
|
return false;
|
|
569
612
|
}
|
|
613
|
+
this.detectExistingEntities();
|
|
614
|
+
let tablesToGenerate = this.filterTablesToGenerate(tables);
|
|
615
|
+
if(0 === Object.keys(tablesToGenerate).length && !this.isOverride){
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
570
618
|
let driverKey = sc.get(
|
|
571
619
|
this.connectionData,
|
|
572
620
|
'driver',
|
|
@@ -576,8 +624,8 @@ class EntitiesGenerator
|
|
|
576
624
|
Logger.critical('Failed to fetch driver key.');
|
|
577
625
|
return false;
|
|
578
626
|
}
|
|
579
|
-
for(let tableName of Object.keys(
|
|
580
|
-
let tableData =
|
|
627
|
+
for(let tableName of Object.keys(tablesToGenerate)){
|
|
628
|
+
let tableData = tablesToGenerate[tableName];
|
|
581
629
|
await this.generateEntityFile(tableName, tableData);
|
|
582
630
|
await this.generateModelFile(tableName, tableData, driverKey);
|
|
583
631
|
}
|