@reldens/storage 0.48.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 +96 -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)
|
|
@@ -295,16 +297,6 @@ class EntitiesGenerator
|
|
|
295
297
|
return passwordFields;
|
|
296
298
|
}
|
|
297
299
|
|
|
298
|
-
getEditPropertiesRemoval(fieldsToRemove)
|
|
299
|
-
{
|
|
300
|
-
if(1 === fieldsToRemove.length){
|
|
301
|
-
return 'let editProperties = [...propertiesKeys];\n editProperties.splice(editProperties.indexOf(\''+
|
|
302
|
-
fieldsToRemove[0]
|
|
303
|
-
+'\'), 1);';
|
|
304
|
-
}
|
|
305
|
-
return 'let editProperties = sc.removeFromArray([...propertiesKeys], [\''+fieldsToRemove.join('\', \'')+'\']);';
|
|
306
|
-
}
|
|
307
|
-
|
|
308
300
|
getListPropertiesDeclaration(columns)
|
|
309
301
|
{
|
|
310
302
|
let fieldsToRemove = this.getFieldsToRemoveFromList(columns);
|
|
@@ -406,26 +398,60 @@ class EntitiesGenerator
|
|
|
406
398
|
return '';
|
|
407
399
|
}
|
|
408
400
|
|
|
409
|
-
|
|
401
|
+
detectExistingEntities()
|
|
410
402
|
{
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
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};
|
|
413
|
+
}
|
|
414
|
+
Logger.info('Detected '+Object.keys(this.existingEntities).length+' existing entities.');
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
filterTablesToGenerate(tables)
|
|
418
|
+
{
|
|
419
|
+
if(this.isOverride){
|
|
420
|
+
return tables;
|
|
415
421
|
}
|
|
416
|
-
let
|
|
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']);
|
|
417
442
|
if(!configTemplateContent) {
|
|
418
|
-
Logger.critical('Failed to read entities config template file: '+
|
|
443
|
+
Logger.critical('Failed to read entities config template file: '+this.templates['entities-config']);
|
|
419
444
|
return false;
|
|
420
445
|
}
|
|
421
|
-
let
|
|
422
|
-
let entitiesConfigExport = this.getEntitiesConfigExport();
|
|
446
|
+
let allEntities = Object.assign({}, this.existingEntities, this.generatedEntities);
|
|
423
447
|
let replacements = {
|
|
424
|
-
requireStatements,
|
|
425
|
-
entitiesConfigExport
|
|
448
|
+
requireStatements: this.getRequireStatements(allEntities),
|
|
449
|
+
entitiesConfigExport: this.getEntitiesConfigExport(allEntities)
|
|
426
450
|
};
|
|
427
|
-
|
|
428
|
-
|
|
451
|
+
if(!FileHandler.writeFile(
|
|
452
|
+
this.entitiesConfigPath,
|
|
453
|
+
this.applyReplacements(configTemplateContent, replacements)
|
|
454
|
+
)){
|
|
429
455
|
Logger.critical('Failed to write entities config file: '+this.entitiesConfigPath);
|
|
430
456
|
return false;
|
|
431
457
|
}
|
|
@@ -433,25 +459,29 @@ class EntitiesGenerator
|
|
|
433
459
|
return true;
|
|
434
460
|
}
|
|
435
461
|
|
|
436
|
-
getRequireStatements()
|
|
462
|
+
getRequireStatements(allEntities)
|
|
437
463
|
{
|
|
438
464
|
let requireStatements = [];
|
|
439
|
-
for(let tableName of Object.keys(
|
|
440
|
-
let entity =
|
|
441
|
-
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', '');
|
|
442
469
|
requireStatements.push(
|
|
443
|
-
'const { '+
|
|
470
|
+
'const { '+ entityClassName+' } = require(\'./entities/'+entityFileName+'\');'
|
|
444
471
|
);
|
|
445
472
|
}
|
|
446
473
|
return requireStatements.join('\n');
|
|
447
474
|
}
|
|
448
475
|
|
|
449
|
-
getEntitiesConfigExport()
|
|
476
|
+
getEntitiesConfigExport(allEntities)
|
|
450
477
|
{
|
|
451
478
|
let entitiesConfigExport = [];
|
|
452
|
-
for(let tableName of Object.keys(
|
|
453
|
-
|
|
454
|
-
|
|
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
|
+
);
|
|
455
485
|
}
|
|
456
486
|
return entitiesConfigExport.join(',\n ');
|
|
457
487
|
}
|
|
@@ -468,8 +498,10 @@ class EntitiesGenerator
|
|
|
468
498
|
Logger.critical('Failed to read entities translations template file: '+translationsTemplatePath);
|
|
469
499
|
return false;
|
|
470
500
|
}
|
|
471
|
-
let
|
|
472
|
-
|
|
501
|
+
let translationsContent = translationsTemplateContent.replace(
|
|
502
|
+
/{{labels}}/g,
|
|
503
|
+
this.getTranslationLabels(Object.assign({}, this.existingEntities, this.generatedEntities))
|
|
504
|
+
);
|
|
473
505
|
if(!FileHandler.writeFile(this.entitiesTranslationsPath, translationsContent)){
|
|
474
506
|
Logger.critical('Failed to write entities translations file: '+this.entitiesTranslationsPath);
|
|
475
507
|
return false;
|
|
@@ -478,12 +510,15 @@ class EntitiesGenerator
|
|
|
478
510
|
return true;
|
|
479
511
|
}
|
|
480
512
|
|
|
481
|
-
getTranslationLabels()
|
|
513
|
+
getTranslationLabels(allEntities)
|
|
482
514
|
{
|
|
483
515
|
let labels = [];
|
|
484
|
-
for(let tableName of Object.keys(
|
|
485
|
-
|
|
486
|
-
|
|
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
|
+
);
|
|
487
522
|
}
|
|
488
523
|
return labels.join(',\n ');
|
|
489
524
|
}
|
|
@@ -500,9 +535,10 @@ class EntitiesGenerator
|
|
|
500
535
|
Logger.critical('Failed to read registered models template file: '+registeredTemplatePath);
|
|
501
536
|
return false;
|
|
502
537
|
}
|
|
538
|
+
let allEntities = Object.assign({}, this.existingEntities, this.generatedEntities);
|
|
503
539
|
let replacements = {
|
|
504
|
-
registeredModels: this.getRegisteredModels(driverKey),
|
|
505
|
-
registeredEntitiesObject: this.getRegisteredEntitiesObject(driverKey)
|
|
540
|
+
registeredModels: this.getRegisteredModels(allEntities, driverKey),
|
|
541
|
+
registeredEntitiesObject: this.getRegisteredEntitiesObject(allEntities, driverKey)
|
|
506
542
|
};
|
|
507
543
|
let registeredContent = this.applyReplacements(registeredTemplateContent, replacements);
|
|
508
544
|
let driverFolder = FileHandler.joinPaths(this.modelsFolder, driverKey);
|
|
@@ -515,30 +551,33 @@ class EntitiesGenerator
|
|
|
515
551
|
return true;
|
|
516
552
|
}
|
|
517
553
|
|
|
518
|
-
getRegisteredModels(driverKey)
|
|
554
|
+
getRegisteredModels(allEntities, driverKey)
|
|
519
555
|
{
|
|
520
556
|
let registeredModels = [];
|
|
521
|
-
for(let tableName of Object.keys(
|
|
522
|
-
let entity =
|
|
523
|
-
if(driverKey !== entity.driverKey){
|
|
557
|
+
for(let tableName of Object.keys(allEntities)){
|
|
558
|
+
let entity = allEntities[tableName];
|
|
559
|
+
if(entity.driverKey && driverKey !== entity.driverKey){
|
|
524
560
|
continue;
|
|
525
561
|
}
|
|
562
|
+
let modelClassName = entity.modelClassName || sc.capitalizedCamelCase(tableName)+'Model';
|
|
563
|
+
let modelFileName = entity.modelFileName || sc.kebabCase(tableName)+'-model.js';
|
|
526
564
|
registeredModels.push(
|
|
527
|
-
'const { '+
|
|
565
|
+
'const { '+modelClassName+' } = require(\'./'+modelFileName.replace('.js', '')+'\');'
|
|
528
566
|
);
|
|
529
567
|
}
|
|
530
568
|
return registeredModels.join('\n');
|
|
531
569
|
}
|
|
532
570
|
|
|
533
|
-
getRegisteredEntitiesObject(driverKey)
|
|
571
|
+
getRegisteredEntitiesObject(allEntities, driverKey)
|
|
534
572
|
{
|
|
535
573
|
let registeredEntitiesObject = [];
|
|
536
|
-
for(let tableName of Object.keys(
|
|
537
|
-
let entity =
|
|
538
|
-
if(driverKey !== entity.driverKey){
|
|
574
|
+
for(let tableName of Object.keys(allEntities)){
|
|
575
|
+
let entity = allEntities[tableName];
|
|
576
|
+
if(entity.driverKey && driverKey !== entity.driverKey){
|
|
539
577
|
continue;
|
|
540
578
|
}
|
|
541
|
-
|
|
579
|
+
let modelClassName = entity.modelClassName || sc.capitalizedCamelCase(tableName)+'Model';
|
|
580
|
+
registeredEntitiesObject.push(sc.camelCase(tableName)+': '+modelClassName);
|
|
542
581
|
}
|
|
543
582
|
return registeredEntitiesObject.join(',\n ');
|
|
544
583
|
}
|
|
@@ -571,6 +610,11 @@ class EntitiesGenerator
|
|
|
571
610
|
if(!this.ensureFoldersExist()){
|
|
572
611
|
return false;
|
|
573
612
|
}
|
|
613
|
+
this.detectExistingEntities();
|
|
614
|
+
let tablesToGenerate = this.filterTablesToGenerate(tables);
|
|
615
|
+
if(0 === Object.keys(tablesToGenerate).length && !this.isOverride){
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
574
618
|
let driverKey = sc.get(
|
|
575
619
|
this.connectionData,
|
|
576
620
|
'driver',
|
|
@@ -580,8 +624,8 @@ class EntitiesGenerator
|
|
|
580
624
|
Logger.critical('Failed to fetch driver key.');
|
|
581
625
|
return false;
|
|
582
626
|
}
|
|
583
|
-
for(let tableName of Object.keys(
|
|
584
|
-
let tableData =
|
|
627
|
+
for(let tableName of Object.keys(tablesToGenerate)){
|
|
628
|
+
let tableData = tablesToGenerate[tableName];
|
|
585
629
|
await this.generateEntityFile(tableName, tableData);
|
|
586
630
|
await this.generateModelFile(tableName, tableData, driverKey);
|
|
587
631
|
}
|