@currentjs/gen 0.5.5 → 0.5.6
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/CHANGELOG.md +4 -0
- package/README.md +2 -0
- package/dist/commands/generateAll.js +7 -6
- package/dist/commands/migrateCommit.js +6 -3
- package/dist/generators/controllerGenerator.d.ts +5 -4
- package/dist/generators/controllerGenerator.js +13 -7
- package/dist/generators/domainLayerGenerator.d.ts +5 -4
- package/dist/generators/domainLayerGenerator.js +11 -8
- package/dist/generators/dtoGenerator.d.ts +5 -4
- package/dist/generators/dtoGenerator.js +29 -16
- package/dist/generators/serviceGenerator.d.ts +5 -4
- package/dist/generators/serviceGenerator.js +23 -14
- package/dist/generators/storeGenerator.d.ts +11 -4
- package/dist/generators/storeGenerator.js +147 -24
- package/dist/generators/templates/data/appYamlTemplate +1 -1
- package/dist/generators/templates/storeTemplates.d.ts +1 -1
- package/dist/generators/templates/storeTemplates.js +20 -19
- package/dist/generators/useCaseGenerator.d.ts +5 -4
- package/dist/generators/useCaseGenerator.js +10 -7
- package/dist/types/configTypes.d.ts +3 -0
- package/dist/types/configTypes.js +15 -0
- package/dist/utils/commandUtils.d.ts +1 -1
- package/dist/utils/commandUtils.js +5 -3
- package/dist/utils/migrationUtils.d.ts +8 -6
- package/dist/utils/migrationUtils.js +37 -20
- package/package.json +1 -1
|
@@ -47,6 +47,7 @@ class StoreGenerator {
|
|
|
47
47
|
constructor() {
|
|
48
48
|
this.availableValueObjects = new Map();
|
|
49
49
|
this.availableAggregates = new Set();
|
|
50
|
+
this.identifiers = 'numeric';
|
|
50
51
|
}
|
|
51
52
|
isAggregateField(fieldConfig) {
|
|
52
53
|
return (0, typeUtils_1.isAggregateReference)(fieldConfig.type, this.availableAggregates);
|
|
@@ -200,11 +201,12 @@ class StoreGenerator {
|
|
|
200
201
|
}
|
|
201
202
|
generateRowFields(fields, childInfo) {
|
|
202
203
|
const result = [];
|
|
204
|
+
const idTs = (0, configTypes_1.idTsType)(this.identifiers);
|
|
203
205
|
const ownerOrParentField = childInfo ? childInfo.parentIdField : 'ownerId';
|
|
204
|
-
result.push(` ${ownerOrParentField}:
|
|
206
|
+
result.push(` ${ownerOrParentField}: ${idTs};`);
|
|
205
207
|
fields.forEach(([fieldName, fieldConfig]) => {
|
|
206
208
|
if (this.isAggregateField(fieldConfig)) {
|
|
207
|
-
result.push(` ${fieldName}Id?:
|
|
209
|
+
result.push(` ${fieldName}Id?: ${idTs};`);
|
|
208
210
|
return;
|
|
209
211
|
}
|
|
210
212
|
const tsType = this.mapTypeToRowType(fieldConfig.type);
|
|
@@ -214,10 +216,27 @@ class StoreGenerator {
|
|
|
214
216
|
return result.join('\n');
|
|
215
217
|
}
|
|
216
218
|
generateFieldNamesStr(fields, childInfo) {
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
219
|
+
const isUuid = this.identifiers === 'uuid';
|
|
220
|
+
const ownerOrParentField = childInfo ? childInfo.parentIdField : 'ownerId';
|
|
221
|
+
const allFields = [
|
|
222
|
+
'id',
|
|
223
|
+
ownerOrParentField,
|
|
224
|
+
...fields.map(([name, config]) => this.isAggregateField(config) ? `${name}Id` : name)
|
|
225
|
+
];
|
|
226
|
+
if (!isUuid) {
|
|
227
|
+
return allFields.map(f => `\\\`${f}\\\``).join(', ');
|
|
228
|
+
}
|
|
229
|
+
// For UUID: id-type columns need BIN_TO_UUID wrapping in SELECT
|
|
230
|
+
const idFields = new Set(['id', ownerOrParentField]);
|
|
231
|
+
fields.forEach(([name, config]) => {
|
|
232
|
+
if (this.isAggregateField(config))
|
|
233
|
+
idFields.add(`${name}Id`);
|
|
234
|
+
});
|
|
235
|
+
return allFields.map(f => {
|
|
236
|
+
if (idFields.has(f))
|
|
237
|
+
return `BIN_TO_UUID(\\\`${f}\\\`, 1) as \\\`${f}\\\``;
|
|
238
|
+
return `\\\`${f}\\\``;
|
|
239
|
+
}).join(', ');
|
|
221
240
|
}
|
|
222
241
|
generateRowToModelMapping(modelName, fields, childInfo) {
|
|
223
242
|
const result = [];
|
|
@@ -411,9 +430,15 @@ class StoreGenerator {
|
|
|
411
430
|
}
|
|
412
431
|
generateListMethods(modelName, fieldNamesStr, childInfo) {
|
|
413
432
|
const isRoot = !childInfo;
|
|
414
|
-
const
|
|
433
|
+
const isUuid = this.identifiers === 'uuid';
|
|
434
|
+
const idTs = (0, configTypes_1.idTsType)(this.identifiers);
|
|
435
|
+
const ownerParam = isRoot ? `, ownerId?: ${idTs}` : '';
|
|
436
|
+
// UUID_TO_BIN(:ownerId, 1) — the ", 1" is a SQL literal inside the query string, not a JS param key
|
|
437
|
+
const ownerFilterExpr = isUuid
|
|
438
|
+
? `' AND \\\`ownerId\\\` = UUID_TO_BIN(:ownerId, 1)'`
|
|
439
|
+
: `' AND \\\`ownerId\\\` = :ownerId'`;
|
|
415
440
|
const ownerFilter = isRoot
|
|
416
|
-
? `\n const ownerFilter = ownerId != null ?
|
|
441
|
+
? `\n const ownerFilter = ownerId != null ? ${ownerFilterExpr} : '';`
|
|
417
442
|
: '';
|
|
418
443
|
const ownerFilterRef = isRoot ? '\${ownerFilter}' : '';
|
|
419
444
|
const ownerParamsSetup = isRoot
|
|
@@ -432,7 +457,7 @@ class StoreGenerator {
|
|
|
432
457
|
}
|
|
433
458
|
return [];
|
|
434
459
|
}`;
|
|
435
|
-
const getAll = ` async getAll(${isRoot ?
|
|
460
|
+
const getAll = ` async getAll(${isRoot ? `ownerId?: ${idTs}` : ''}): Promise<${modelName}[]> {${ownerFilter}
|
|
436
461
|
const params: Record<string, any> = {};${ownerParamsSetup}
|
|
437
462
|
const result = await this.db.query(
|
|
438
463
|
\`SELECT ${fieldNamesStr} FROM \\\`\${this.tableName}\\\` WHERE deletedAt IS NULL${ownerFilterRef}\`,
|
|
@@ -444,7 +469,7 @@ class StoreGenerator {
|
|
|
444
469
|
}
|
|
445
470
|
return [];
|
|
446
471
|
}`;
|
|
447
|
-
const count = ` async count(${isRoot ?
|
|
472
|
+
const count = ` async count(${isRoot ? `ownerId?: ${idTs}` : ''}): Promise<number> {${ownerFilter}
|
|
448
473
|
const params: Record<string, any> = {};${ownerParamsSetup}
|
|
449
474
|
const result = await this.db.query(
|
|
450
475
|
\`SELECT COUNT(*) as count FROM \\\`\${this.tableName}\\\` WHERE deletedAt IS NULL${ownerFilterRef}\`,
|
|
@@ -461,13 +486,26 @@ class StoreGenerator {
|
|
|
461
486
|
generateGetByParentIdMethod(modelName, fields, childInfo) {
|
|
462
487
|
if (!childInfo)
|
|
463
488
|
return '';
|
|
464
|
-
const
|
|
489
|
+
const isUuid = this.identifiers === 'uuid';
|
|
490
|
+
const idTs = (0, configTypes_1.idTsType)(this.identifiers);
|
|
465
491
|
const parentIdField = childInfo.parentIdField;
|
|
492
|
+
// Build field list for SELECT (reuse the same UUID-aware logic)
|
|
493
|
+
const idFields = new Set(['id', parentIdField]);
|
|
494
|
+
fields.forEach(([name, config]) => {
|
|
495
|
+
if (this.isAggregateField(config))
|
|
496
|
+
idFields.add(`${name}Id`);
|
|
497
|
+
});
|
|
498
|
+
const rawFields = ['id', parentIdField, ...fields.map(([name, config]) => this.isAggregateField(config) ? `${name}Id` : name)];
|
|
499
|
+
const bt = '\\`';
|
|
500
|
+
const fieldList = isUuid
|
|
501
|
+
? rawFields.map(f => idFields.has(f) ? `BIN_TO_UUID(${bt}${f}${bt}, 1) as ${bt}${f}${bt}` : `${bt}${f}${bt}`).join(', ')
|
|
502
|
+
: rawFields.map(f => `${bt}${f}${bt}`).join(', ');
|
|
503
|
+
const whereExpr = isUuid ? `\\\`${parentIdField}\\\` = UUID_TO_BIN(:parentId, 1)` : `\\\`${parentIdField}\\\` = :parentId`;
|
|
466
504
|
return `
|
|
467
505
|
|
|
468
|
-
async getByParentId(parentId:
|
|
506
|
+
async getByParentId(parentId: ${idTs}): Promise<${modelName}[]> {
|
|
469
507
|
const result = await this.db.query(
|
|
470
|
-
\`SELECT ${fieldList} FROM \\\`\${this.tableName}\\\` WHERE
|
|
508
|
+
\`SELECT ${fieldList} FROM \\\`\${this.tableName}\\\` WHERE ${whereExpr} AND deletedAt IS NULL\`,
|
|
471
509
|
{ parentId }
|
|
472
510
|
);
|
|
473
511
|
|
|
@@ -478,23 +516,32 @@ class StoreGenerator {
|
|
|
478
516
|
}`;
|
|
479
517
|
}
|
|
480
518
|
generateGetResourceOwnerMethod(childInfo) {
|
|
519
|
+
const isUuid = this.identifiers === 'uuid';
|
|
520
|
+
const idTs = (0, configTypes_1.idTsType)(this.identifiers);
|
|
521
|
+
const whereExpr = isUuid ? 'id = UUID_TO_BIN(:id, 1)' : 'id = :id';
|
|
522
|
+
const ownerSelect = isUuid ? 'BIN_TO_UUID(p.ownerId, 1) as ownerId' : 'p.ownerId';
|
|
523
|
+
const ownerSelectSimple = isUuid ? 'BIN_TO_UUID(ownerId, 1) as ownerId' : 'ownerId';
|
|
481
524
|
if (childInfo) {
|
|
482
525
|
const parentTable = childInfo.parentTableName;
|
|
483
526
|
const parentIdField = childInfo.parentIdField;
|
|
527
|
+
const joinExpr = isUuid
|
|
528
|
+
? `p.id = UUID_TO_BIN(c.\\\`${parentIdField}\\\`, 1)`
|
|
529
|
+
: `p.id = c.\\\`${parentIdField}\\\``;
|
|
530
|
+
const cWhereExpr = isUuid ? 'c.id = UUID_TO_BIN(:id, 1)' : 'c.id = :id';
|
|
484
531
|
return `
|
|
485
532
|
|
|
486
533
|
/**
|
|
487
534
|
* Get the owner ID of a resource by its ID (via parent entity).
|
|
488
535
|
* Used for pre-mutation authorization checks.
|
|
489
536
|
*/
|
|
490
|
-
async getResourceOwner(id:
|
|
537
|
+
async getResourceOwner(id: ${idTs}): Promise<${idTs} | null> {
|
|
491
538
|
const result = await this.db.query(
|
|
492
|
-
\`SELECT
|
|
539
|
+
\`SELECT ${ownerSelect} FROM \\\`\${this.tableName}\\\` c INNER JOIN \\\`${parentTable}\\\` p ON ${joinExpr} WHERE ${cWhereExpr} AND c.deletedAt IS NULL\`,
|
|
493
540
|
{ id }
|
|
494
541
|
);
|
|
495
542
|
|
|
496
543
|
if (result.success && result.data && result.data.length > 0) {
|
|
497
|
-
return result.data[0].ownerId as
|
|
544
|
+
return result.data[0].ownerId as ${idTs};
|
|
498
545
|
}
|
|
499
546
|
return null;
|
|
500
547
|
}`;
|
|
@@ -505,35 +552,109 @@ class StoreGenerator {
|
|
|
505
552
|
* Get the owner ID of a resource by its ID.
|
|
506
553
|
* Used for pre-mutation authorization checks.
|
|
507
554
|
*/
|
|
508
|
-
async getResourceOwner(id:
|
|
555
|
+
async getResourceOwner(id: ${idTs}): Promise<${idTs} | null> {
|
|
509
556
|
const result = await this.db.query(
|
|
510
|
-
\`SELECT
|
|
557
|
+
\`SELECT ${ownerSelectSimple} FROM \\\`\${this.tableName}\\\` WHERE ${whereExpr} AND deletedAt IS NULL\`,
|
|
511
558
|
{ id }
|
|
512
559
|
);
|
|
513
560
|
|
|
514
561
|
if (result.success && result.data && result.data.length > 0) {
|
|
515
|
-
return result.data[0].ownerId as
|
|
562
|
+
return result.data[0].ownerId as ${idTs};
|
|
516
563
|
}
|
|
517
564
|
return null;
|
|
518
565
|
}`;
|
|
519
566
|
}
|
|
567
|
+
generateIdHelpers() {
|
|
568
|
+
if (this.identifiers === 'nanoid') {
|
|
569
|
+
return `
|
|
570
|
+
private generateNanoId(size = 21): string {
|
|
571
|
+
const alphabet = "useABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
|
|
572
|
+
let id = "";
|
|
573
|
+
const bytes = randomBytes(size);
|
|
574
|
+
|
|
575
|
+
for (let i = 0; i < size; i++) {
|
|
576
|
+
// We use a bitwise AND with 63 because 63 is 00111111 in binary.
|
|
577
|
+
// This maps any byte to a value between 0 and 63.
|
|
578
|
+
id += alphabet[bytes[i] & 63];
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
return id;
|
|
582
|
+
}
|
|
583
|
+
`;
|
|
584
|
+
}
|
|
585
|
+
return '';
|
|
586
|
+
}
|
|
587
|
+
generateInsertIdVariables() {
|
|
588
|
+
switch (this.identifiers) {
|
|
589
|
+
case 'uuid':
|
|
590
|
+
return {
|
|
591
|
+
preLine: ' const newId = randomUUID();',
|
|
592
|
+
dataLine: ' id: newId,\n',
|
|
593
|
+
successCond: '',
|
|
594
|
+
getId: ''
|
|
595
|
+
};
|
|
596
|
+
case 'nanoid':
|
|
597
|
+
return {
|
|
598
|
+
preLine: ' const newId = this.generateNanoId();',
|
|
599
|
+
dataLine: ' id: newId,\n',
|
|
600
|
+
successCond: '',
|
|
601
|
+
getId: ''
|
|
602
|
+
};
|
|
603
|
+
default:
|
|
604
|
+
return {
|
|
605
|
+
preLine: '',
|
|
606
|
+
dataLine: '',
|
|
607
|
+
successCond: ' && result.insertId',
|
|
608
|
+
getId: 'const newId = typeof result.insertId === \'string\' ? parseInt(result.insertId, 10) : result.insertId;'
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
generateWhereIdExpr() {
|
|
613
|
+
return this.identifiers === 'uuid' ? 'id = UUID_TO_BIN(:id, 1)' : 'id = :id';
|
|
614
|
+
}
|
|
615
|
+
generateIdParamExpr() {
|
|
616
|
+
// The ", 1" in UUID_TO_BIN(:id, 1) is a literal in the SQL string, NOT a JS params key.
|
|
617
|
+
// The params object always uses just { id }, so this expression is always empty.
|
|
618
|
+
return '';
|
|
619
|
+
}
|
|
620
|
+
generateRowIdExpr() {
|
|
621
|
+
return this.identifiers === 'uuid' ? 'row.id' : 'row.id';
|
|
622
|
+
}
|
|
623
|
+
generateCryptoImport() {
|
|
624
|
+
if (this.identifiers === 'uuid')
|
|
625
|
+
return `\nimport { randomUUID } from 'crypto';`;
|
|
626
|
+
if (this.identifiers === 'nanoid')
|
|
627
|
+
return `\nimport { randomBytes } from 'crypto';`;
|
|
628
|
+
return '';
|
|
629
|
+
}
|
|
520
630
|
generateStore(modelName, aggregateConfig, childInfo) {
|
|
521
631
|
const tableName = modelName.toLowerCase();
|
|
522
632
|
const fields = Object.entries(aggregateConfig.fields);
|
|
523
633
|
// Sort fields for rowToModel to match entity constructor order (required first, optional second)
|
|
524
634
|
const sortedFields = this.sortFieldsForConstructor(fields);
|
|
525
635
|
const fieldNamesStr = this.generateFieldNamesStr(fields, childInfo);
|
|
636
|
+
const idVars = this.generateInsertIdVariables();
|
|
637
|
+
const idTs = (0, configTypes_1.idTsType)(this.identifiers);
|
|
526
638
|
const variables = {
|
|
527
639
|
ENTITY_NAME: modelName,
|
|
528
640
|
TABLE_NAME: tableName,
|
|
641
|
+
ID_TYPE: idTs,
|
|
529
642
|
ROW_FIELDS: this.generateRowFields(fields, childInfo),
|
|
530
643
|
FIELD_NAMES: fieldNamesStr,
|
|
644
|
+
ROW_ID_EXPR: this.generateRowIdExpr(),
|
|
645
|
+
WHERE_ID_EXPR: this.generateWhereIdExpr(),
|
|
646
|
+
ID_PARAM_EXPR: this.generateIdParamExpr(),
|
|
531
647
|
ROW_TO_MODEL_MAPPING: this.generateRowToModelMapping(modelName, sortedFields, childInfo),
|
|
648
|
+
INSERT_ID_PRE_LOGIC: idVars.preLine,
|
|
649
|
+
INSERT_ID_DATA: idVars.dataLine,
|
|
532
650
|
INSERT_DATA_MAPPING: this.generateInsertDataMapping(fields, childInfo),
|
|
651
|
+
INSERT_SUCCESS_COND: idVars.successCond,
|
|
652
|
+
INSERT_GET_ID: idVars.getId,
|
|
533
653
|
UPDATE_DATA_MAPPING: this.generateUpdateDataMapping(fields),
|
|
534
654
|
UPDATE_FIELDS_ARRAY: this.generateUpdateFieldsArray(fields),
|
|
535
655
|
VALUE_OBJECT_IMPORTS: this.generateValueObjectImports(fields),
|
|
536
656
|
AGGREGATE_REF_IMPORTS: this.generateAggregateRefImports(modelName, fields),
|
|
657
|
+
ID_HELPERS: this.generateIdHelpers(),
|
|
537
658
|
LIST_METHODS: this.generateListMethods(modelName, fieldNamesStr, childInfo),
|
|
538
659
|
GET_BY_PARENT_ID_METHOD: this.generateGetByParentIdMethod(modelName, fields, childInfo),
|
|
539
660
|
GET_RESOURCE_OWNER_METHOD: this.generateGetResourceOwnerMethod(childInfo)
|
|
@@ -552,12 +673,14 @@ class StoreGenerator {
|
|
|
552
673
|
ENTITY_IMPORT_ITEMS: entityImportItems.join(', '),
|
|
553
674
|
ROW_INTERFACE: rowInterface,
|
|
554
675
|
STORE_CLASS: storeClass,
|
|
676
|
+
CRYPTO_IMPORT: this.generateCryptoImport(),
|
|
555
677
|
VALUE_OBJECT_IMPORTS: variables.VALUE_OBJECT_IMPORTS,
|
|
556
678
|
AGGREGATE_REF_IMPORTS: variables.AGGREGATE_REF_IMPORTS
|
|
557
679
|
});
|
|
558
680
|
}
|
|
559
|
-
generateFromConfig(config) {
|
|
681
|
+
generateFromConfig(config, identifiers = 'numeric') {
|
|
560
682
|
const result = {};
|
|
683
|
+
this.identifiers = identifiers;
|
|
561
684
|
// First, collect all value object names and configs
|
|
562
685
|
this.availableValueObjects.clear();
|
|
563
686
|
if (config.domain.valueObjects) {
|
|
@@ -582,16 +705,16 @@ class StoreGenerator {
|
|
|
582
705
|
}
|
|
583
706
|
return result;
|
|
584
707
|
}
|
|
585
|
-
generateFromYamlFile(yamlFilePath) {
|
|
708
|
+
generateFromYamlFile(yamlFilePath, identifiers = 'numeric') {
|
|
586
709
|
const yamlContent = fs.readFileSync(yamlFilePath, 'utf8');
|
|
587
710
|
const config = (0, yaml_1.parse)(yamlContent);
|
|
588
711
|
if (!(0, configTypes_1.isValidModuleConfig)(config)) {
|
|
589
712
|
throw new Error('Configuration does not match new module format. Expected domain.aggregates structure.');
|
|
590
713
|
}
|
|
591
|
-
return this.generateFromConfig(config);
|
|
714
|
+
return this.generateFromConfig(config, identifiers);
|
|
592
715
|
}
|
|
593
|
-
async generateAndSaveFiles(yamlFilePath, moduleDir, opts) {
|
|
594
|
-
const storesByModel = this.generateFromYamlFile(yamlFilePath);
|
|
716
|
+
async generateAndSaveFiles(yamlFilePath, moduleDir, opts, identifiers = 'numeric') {
|
|
717
|
+
const storesByModel = this.generateFromYamlFile(yamlFilePath, identifiers);
|
|
595
718
|
const storesDir = path.join(moduleDir, 'infrastructure', 'stores');
|
|
596
719
|
fs.mkdirSync(storesDir, { recursive: true });
|
|
597
720
|
for (const [modelName, code] of Object.entries(storesByModel)) {
|
|
@@ -2,4 +2,4 @@ export declare const storeTemplates: {
|
|
|
2
2
|
rowInterface: string;
|
|
3
3
|
storeClass: string;
|
|
4
4
|
};
|
|
5
|
-
export declare const storeFileTemplate = "import { Injectable } from '../../../../system';\nimport { {{ENTITY_IMPORT_ITEMS}} } from '../../domain/entities/{{ENTITY_NAME}}';\nimport type { ISqlProvider } from '@currentjs/provider-mysql';{{VALUE_OBJECT_IMPORTS}}{{AGGREGATE_REF_IMPORTS}}\n\n{{ROW_INTERFACE}}\n\n{{STORE_CLASS}}\n";
|
|
5
|
+
export declare const storeFileTemplate = "import { Injectable } from '../../../../system';\nimport { {{ENTITY_IMPORT_ITEMS}} } from '../../domain/entities/{{ENTITY_NAME}}';\nimport type { ISqlProvider } from '@currentjs/provider-mysql';{{CRYPTO_IMPORT}}{{VALUE_OBJECT_IMPORTS}}{{AGGREGATE_REF_IMPORTS}}\n\n{{ROW_INTERFACE}}\n\n{{STORE_CLASS}}\n";
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.storeFileTemplate = exports.storeTemplates = void 0;
|
|
4
4
|
exports.storeTemplates = {
|
|
5
5
|
rowInterface: `export interface {{ENTITY_NAME}}Row {
|
|
6
|
-
id:
|
|
6
|
+
id: {{ID_TYPE}};
|
|
7
7
|
{{ROW_FIELDS}}
|
|
8
8
|
createdAt: string;
|
|
9
9
|
updatedAt: string;
|
|
@@ -25,20 +25,20 @@ export class {{ENTITY_NAME}}Store {
|
|
|
25
25
|
private ensureParsed(value: any): any {
|
|
26
26
|
return typeof value === 'string' ? JSON.parse(value) : value;
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
{{ID_HELPERS}}
|
|
29
29
|
private rowToModel(row: {{ENTITY_NAME}}Row): {{ENTITY_NAME}} {
|
|
30
30
|
return new {{ENTITY_NAME}}(
|
|
31
|
-
|
|
31
|
+
{{ROW_ID_EXPR}},
|
|
32
32
|
{{ROW_TO_MODEL_MAPPING}}
|
|
33
33
|
);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
{{LIST_METHODS}}
|
|
37
37
|
|
|
38
|
-
async getById(id:
|
|
38
|
+
async getById(id: {{ID_TYPE}}): Promise<{{ENTITY_NAME}} | null> {
|
|
39
39
|
const result = await this.db.query(
|
|
40
|
-
\`SELECT {{FIELD_NAMES}} FROM \\\`\${this.tableName}\\\` WHERE
|
|
41
|
-
{ id }
|
|
40
|
+
\`SELECT {{FIELD_NAMES}} FROM \\\`\${this.tableName}\\\` WHERE {{WHERE_ID_EXPR}} AND deletedAt IS NULL\`,
|
|
41
|
+
{ id{{ID_PARAM_EXPR}} }
|
|
42
42
|
);
|
|
43
43
|
|
|
44
44
|
if (result.success && result.data && result.data.length > 0) {
|
|
@@ -49,8 +49,9 @@ export class {{ENTITY_NAME}}Store {
|
|
|
49
49
|
|
|
50
50
|
async insert(entity: {{ENTITY_NAME}}): Promise<{{ENTITY_NAME}}> {
|
|
51
51
|
const now = new Date();
|
|
52
|
+
{{INSERT_ID_PRE_LOGIC}}
|
|
52
53
|
const data: Partial<{{ENTITY_NAME}}Row> = {
|
|
53
|
-
{{INSERT_DATA_MAPPING}},
|
|
54
|
+
{{INSERT_ID_DATA}}{{INSERT_DATA_MAPPING}},
|
|
54
55
|
createdAt: this.toMySQLDatetime(now),
|
|
55
56
|
updatedAt: this.toMySQLDatetime(now)
|
|
56
57
|
};
|
|
@@ -64,15 +65,15 @@ export class {{ENTITY_NAME}}Store {
|
|
|
64
65
|
cleanData
|
|
65
66
|
);
|
|
66
67
|
|
|
67
|
-
if (result.success
|
|
68
|
-
|
|
68
|
+
if (result.success{{INSERT_SUCCESS_COND}}) {
|
|
69
|
+
{{INSERT_GET_ID}}
|
|
69
70
|
return this.getById(newId) as Promise<{{ENTITY_NAME}}>;
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
throw new Error('Failed to insert {{ENTITY_NAME}}');
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
async update(id:
|
|
76
|
+
async update(id: {{ID_TYPE}}, entity: {{ENTITY_NAME}}): Promise<{{ENTITY_NAME}}> {
|
|
76
77
|
const now = new Date();
|
|
77
78
|
const rawData: Partial<{{ENTITY_NAME}}Row> = {
|
|
78
79
|
{{UPDATE_DATA_MAPPING}},
|
|
@@ -85,8 +86,8 @@ export class {{ENTITY_NAME}}Store {
|
|
|
85
86
|
.map(f => \`\\\`\${f}\\\` = :\${f}\`).join(', ');
|
|
86
87
|
|
|
87
88
|
const result = await this.db.query(
|
|
88
|
-
\`UPDATE \\\`\${this.tableName}\\\` SET \${updateFields}, updatedAt = :updatedAt WHERE
|
|
89
|
-
{ ...cleanData, id }
|
|
89
|
+
\`UPDATE \\\`\${this.tableName}\\\` SET \${updateFields}, updatedAt = :updatedAt WHERE {{WHERE_ID_EXPR}}\`,
|
|
90
|
+
{ ...cleanData, id{{ID_PARAM_EXPR}} }
|
|
90
91
|
);
|
|
91
92
|
|
|
92
93
|
if (result.success) {
|
|
@@ -96,20 +97,20 @@ export class {{ENTITY_NAME}}Store {
|
|
|
96
97
|
throw new Error('Failed to update {{ENTITY_NAME}}');
|
|
97
98
|
}
|
|
98
99
|
|
|
99
|
-
async softDelete(id:
|
|
100
|
+
async softDelete(id: {{ID_TYPE}}): Promise<boolean> {
|
|
100
101
|
const now = new Date();
|
|
101
102
|
const result = await this.db.query(
|
|
102
|
-
\`UPDATE \\\`\${this.tableName}\\\` SET deletedAt = :deletedAt WHERE
|
|
103
|
-
{ deletedAt: this.toMySQLDatetime(now), id }
|
|
103
|
+
\`UPDATE \\\`\${this.tableName}\\\` SET deletedAt = :deletedAt WHERE {{WHERE_ID_EXPR}}\`,
|
|
104
|
+
{ deletedAt: this.toMySQLDatetime(now), id{{ID_PARAM_EXPR}} }
|
|
104
105
|
);
|
|
105
106
|
|
|
106
107
|
return result.success;
|
|
107
108
|
}
|
|
108
109
|
|
|
109
|
-
async hardDelete(id:
|
|
110
|
+
async hardDelete(id: {{ID_TYPE}}): Promise<boolean> {
|
|
110
111
|
const result = await this.db.query(
|
|
111
|
-
\`DELETE FROM \\\`\${this.tableName}\\\` WHERE
|
|
112
|
-
{ id }
|
|
112
|
+
\`DELETE FROM \\\`\${this.tableName}\\\` WHERE {{WHERE_ID_EXPR}}\`,
|
|
113
|
+
{ id{{ID_PARAM_EXPR}} }
|
|
113
114
|
);
|
|
114
115
|
|
|
115
116
|
return result.success;
|
|
@@ -119,7 +120,7 @@ export class {{ENTITY_NAME}}Store {
|
|
|
119
120
|
};
|
|
120
121
|
exports.storeFileTemplate = `import { Injectable } from '../../../../system';
|
|
121
122
|
import { {{ENTITY_IMPORT_ITEMS}} } from '../../domain/entities/{{ENTITY_NAME}}';
|
|
122
|
-
import type { ISqlProvider } from '@currentjs/provider-mysql';{{VALUE_OBJECT_IMPORTS}}{{AGGREGATE_REF_IMPORTS}}
|
|
123
|
+
import type { ISqlProvider } from '@currentjs/provider-mysql';{{CRYPTO_IMPORT}}{{VALUE_OBJECT_IMPORTS}}{{AGGREGATE_REF_IMPORTS}}
|
|
123
124
|
|
|
124
125
|
{{ROW_INTERFACE}}
|
|
125
126
|
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { ModuleConfig } from '../types/configTypes';
|
|
1
|
+
import { ModuleConfig, IdentifierType } from '../types/configTypes';
|
|
2
2
|
export declare class UseCaseGenerator {
|
|
3
3
|
private availableAggregates;
|
|
4
|
+
private identifiers;
|
|
4
5
|
private generateUseCaseMethod;
|
|
5
6
|
private generateGetResourceOwnerMethod;
|
|
6
7
|
private generateUseCase;
|
|
7
|
-
generateFromConfig(config: ModuleConfig): Record<string, string>;
|
|
8
|
-
generateFromYamlFile(yamlFilePath: string): Record<string, string>;
|
|
8
|
+
generateFromConfig(config: ModuleConfig, identifiers?: IdentifierType): Record<string, string>;
|
|
9
|
+
generateFromYamlFile(yamlFilePath: string, identifiers?: IdentifierType): Record<string, string>;
|
|
9
10
|
generateAndSaveFiles(yamlFilePath: string, moduleDir: string, opts?: {
|
|
10
11
|
force?: boolean;
|
|
11
12
|
skipOnConflict?: boolean;
|
|
12
|
-
}): Promise<void>;
|
|
13
|
+
}, identifiers?: IdentifierType): Promise<void>;
|
|
13
14
|
}
|
|
@@ -44,6 +44,7 @@ const typeUtils_1 = require("../utils/typeUtils");
|
|
|
44
44
|
class UseCaseGenerator {
|
|
45
45
|
constructor() {
|
|
46
46
|
this.availableAggregates = new Map();
|
|
47
|
+
this.identifiers = 'numeric';
|
|
47
48
|
}
|
|
48
49
|
generateUseCaseMethod(modelName, actionName, useCaseConfig) {
|
|
49
50
|
const methodName = actionName;
|
|
@@ -100,7 +101,7 @@ class UseCaseGenerator {
|
|
|
100
101
|
}).join('\n');
|
|
101
102
|
const returnStatement = '\n return result;';
|
|
102
103
|
const methodParams = actionName === 'list'
|
|
103
|
-
? `input: ${inputType}, ownerId?:
|
|
104
|
+
? `input: ${inputType}, ownerId?: ${(0, configTypes_1.idTsType)(this.identifiers)}`
|
|
104
105
|
: `input: ${inputType}`;
|
|
105
106
|
return ` async ${methodName}(${methodParams}): Promise<${returnType}> {
|
|
106
107
|
${handlerCalls}${returnStatement}
|
|
@@ -108,12 +109,13 @@ ${handlerCalls}${returnStatement}
|
|
|
108
109
|
}
|
|
109
110
|
generateGetResourceOwnerMethod(modelName) {
|
|
110
111
|
const serviceVar = `${modelName.toLowerCase()}Service`;
|
|
112
|
+
const idTs = (0, configTypes_1.idTsType)(this.identifiers);
|
|
111
113
|
return `
|
|
112
114
|
/**
|
|
113
115
|
* Get the owner ID of a resource by its ID.
|
|
114
116
|
* Used for pre-mutation authorization checks in controllers.
|
|
115
117
|
*/
|
|
116
|
-
async getResourceOwner(id:
|
|
118
|
+
async getResourceOwner(id: ${idTs}): Promise<${idTs} | null> {
|
|
117
119
|
return await this.${serviceVar}.getResourceOwner(id);
|
|
118
120
|
}`;
|
|
119
121
|
}
|
|
@@ -151,9 +153,10 @@ export class ${className} {
|
|
|
151
153
|
${methods}${getResourceOwnerMethod}
|
|
152
154
|
}`;
|
|
153
155
|
}
|
|
154
|
-
generateFromConfig(config) {
|
|
156
|
+
generateFromConfig(config, identifiers = 'numeric') {
|
|
155
157
|
var _a;
|
|
156
158
|
const result = {};
|
|
159
|
+
this.identifiers = identifiers;
|
|
157
160
|
// Collect all aggregates to know which are roots
|
|
158
161
|
this.availableAggregates.clear();
|
|
159
162
|
if ((_a = config.domain) === null || _a === void 0 ? void 0 : _a.aggregates) {
|
|
@@ -167,16 +170,16 @@ ${methods}${getResourceOwnerMethod}
|
|
|
167
170
|
});
|
|
168
171
|
return result;
|
|
169
172
|
}
|
|
170
|
-
generateFromYamlFile(yamlFilePath) {
|
|
173
|
+
generateFromYamlFile(yamlFilePath, identifiers = 'numeric') {
|
|
171
174
|
const yamlContent = fs.readFileSync(yamlFilePath, 'utf8');
|
|
172
175
|
const config = (0, yaml_1.parse)(yamlContent);
|
|
173
176
|
if (!(0, configTypes_1.isValidModuleConfig)(config)) {
|
|
174
177
|
throw new Error('Configuration does not match new module format. Expected useCases structure.');
|
|
175
178
|
}
|
|
176
|
-
return this.generateFromConfig(config);
|
|
179
|
+
return this.generateFromConfig(config, identifiers);
|
|
177
180
|
}
|
|
178
|
-
async generateAndSaveFiles(yamlFilePath, moduleDir, opts) {
|
|
179
|
-
const useCasesByModel = this.generateFromYamlFile(yamlFilePath);
|
|
181
|
+
async generateAndSaveFiles(yamlFilePath, moduleDir, opts, identifiers = 'numeric') {
|
|
182
|
+
const useCasesByModel = this.generateFromYamlFile(yamlFilePath, identifiers);
|
|
180
183
|
const useCasesDir = path.join(moduleDir, 'application', 'useCases');
|
|
181
184
|
fs.mkdirSync(useCasesDir, { recursive: true });
|
|
182
185
|
for (const [modelName, code] of Object.entries(useCasesByModel)) {
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Type definitions for the Clean Architecture module configuration
|
|
3
3
|
*/
|
|
4
|
+
export type IdentifierType = 'numeric' | 'uuid' | 'nanoid';
|
|
5
|
+
export declare function normalizeIdentifierType(value: string): IdentifierType;
|
|
6
|
+
export declare function idTsType(identifiers: IdentifierType): 'number' | 'string';
|
|
4
7
|
export interface FieldDefinition {
|
|
5
8
|
type: string;
|
|
6
9
|
constraints?: {
|
|
@@ -3,7 +3,22 @@
|
|
|
3
3
|
* Type definitions for the Clean Architecture module configuration
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.normalizeIdentifierType = normalizeIdentifierType;
|
|
7
|
+
exports.idTsType = idTsType;
|
|
6
8
|
exports.isValidModuleConfig = isValidModuleConfig;
|
|
9
|
+
function normalizeIdentifierType(value) {
|
|
10
|
+
const lower = value.toLowerCase();
|
|
11
|
+
if (lower === 'id' || lower === 'numeric')
|
|
12
|
+
return 'numeric';
|
|
13
|
+
if (lower === 'uuid')
|
|
14
|
+
return 'uuid';
|
|
15
|
+
if (lower === 'nanoid')
|
|
16
|
+
return 'nanoid';
|
|
17
|
+
throw new Error(`Unknown identifier type: "${value}". Expected: numeric, uuid, or nanoid`);
|
|
18
|
+
}
|
|
19
|
+
function idTsType(identifiers) {
|
|
20
|
+
return identifiers === 'numeric' ? 'number' : 'string';
|
|
21
|
+
}
|
|
7
22
|
// Type guard to validate module config (domain + useCases)
|
|
8
23
|
function isValidModuleConfig(config) {
|
|
9
24
|
return config && typeof config === 'object' && 'domain' in config && 'useCases' in config;
|
|
@@ -28,7 +28,7 @@ export interface ModuleEntryResolved {
|
|
|
28
28
|
path: string;
|
|
29
29
|
database: string;
|
|
30
30
|
styling: string;
|
|
31
|
-
identifiers:
|
|
31
|
+
identifiers: import('../types/configTypes').IdentifierType;
|
|
32
32
|
}
|
|
33
33
|
export declare function getModuleEntries(config: AppConfig): ModuleEntryResolved[];
|
|
34
34
|
export declare function shouldIncludeModule(moduleYamlRel: string, moduleName?: string): boolean;
|
|
@@ -51,9 +51,10 @@ const serviceGenerator_1 = require("../generators/serviceGenerator");
|
|
|
51
51
|
const controllerGenerator_1 = require("../generators/controllerGenerator");
|
|
52
52
|
const storeGenerator_1 = require("../generators/storeGenerator");
|
|
53
53
|
const templateGenerator_1 = require("../generators/templateGenerator");
|
|
54
|
+
const configTypes_1 = require("../types/configTypes");
|
|
54
55
|
const DEFAULT_DATABASE = 'mysql';
|
|
55
56
|
const DEFAULT_STYLING = 'bootstrap';
|
|
56
|
-
const DEFAULT_IDENTIFIERS = '
|
|
57
|
+
const DEFAULT_IDENTIFIERS = 'numeric';
|
|
57
58
|
function loadAppConfig(yamlPath) {
|
|
58
59
|
const raw = fs.readFileSync(yamlPath, 'utf8');
|
|
59
60
|
const parsed = (0, yaml_1.parse)(raw);
|
|
@@ -81,7 +82,8 @@ function getModuleEntries(config) {
|
|
|
81
82
|
const global = config.config || {};
|
|
82
83
|
const database = (_a = global.database) !== null && _a !== void 0 ? _a : DEFAULT_DATABASE;
|
|
83
84
|
const styling = (_b = global.styling) !== null && _b !== void 0 ? _b : DEFAULT_STYLING;
|
|
84
|
-
const
|
|
85
|
+
const rawIdentifiers = (_c = global.identifiers) !== null && _c !== void 0 ? _c : DEFAULT_IDENTIFIERS;
|
|
86
|
+
const identifiers = (0, configTypes_1.normalizeIdentifierType)(rawIdentifiers);
|
|
85
87
|
const globalConfig = { database, styling, identifiers };
|
|
86
88
|
return Object.entries(config.modules).map(([name, entry]) => {
|
|
87
89
|
var _a, _b, _c;
|
|
@@ -90,7 +92,7 @@ function getModuleEntries(config) {
|
|
|
90
92
|
path: entry.path,
|
|
91
93
|
database: (_a = entry.database) !== null && _a !== void 0 ? _a : globalConfig.database,
|
|
92
94
|
styling: (_b = entry.styling) !== null && _b !== void 0 ? _b : globalConfig.styling,
|
|
93
|
-
identifiers: (_c = entry.identifiers) !== null && _c !== void 0 ? _c : globalConfig.identifiers
|
|
95
|
+
identifiers: (0, configTypes_1.normalizeIdentifierType)((_c = entry.identifiers) !== null && _c !== void 0 ? _c : globalConfig.identifiers)
|
|
94
96
|
});
|
|
95
97
|
});
|
|
96
98
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AggregateConfig, AggregateFieldConfig } from '../types/configTypes';
|
|
1
|
+
import { AggregateConfig, AggregateFieldConfig, IdentifierType } from '../types/configTypes';
|
|
2
2
|
export interface SchemaState {
|
|
3
3
|
aggregates: Record<string, AggregateConfig>;
|
|
4
4
|
version: string;
|
|
@@ -21,7 +21,9 @@ export interface ForeignKeyInfo {
|
|
|
21
21
|
REFERENCED_TABLE_NAME: string;
|
|
22
22
|
REFERENCED_COLUMN_NAME: string;
|
|
23
23
|
}
|
|
24
|
-
export declare function
|
|
24
|
+
export declare function getIdColumnDefinition(idType?: IdentifierType): string;
|
|
25
|
+
export declare function getFkColumnType(idType?: IdentifierType): string;
|
|
26
|
+
export declare function mapYamlTypeToSql(yamlType: string, availableAggregates: Set<string>, availableValueObjects?: Set<string>, identifiers?: IdentifierType): string;
|
|
25
27
|
/** Table name matches the store convention: singular lowercase aggregate name. */
|
|
26
28
|
export declare function getTableName(aggregateName: string): string;
|
|
27
29
|
export declare function getForeignKeyFieldName(fieldName: string): string;
|
|
@@ -31,15 +33,15 @@ export declare function isRelationshipField(fieldType: string, availableAggregat
|
|
|
31
33
|
* Used to determine parent ID column names for child entity tables.
|
|
32
34
|
*/
|
|
33
35
|
export declare function buildChildToParentMap(aggregates: Record<string, AggregateConfig>): Map<string, string>;
|
|
34
|
-
export declare function generateCreateTableSQL(name: string, aggregate: AggregateConfig, availableAggregates: Set<string>, availableValueObjects?: Set<string>, parentIdField?: string): string;
|
|
36
|
+
export declare function generateCreateTableSQL(name: string, aggregate: AggregateConfig, availableAggregates: Set<string>, availableValueObjects?: Set<string>, parentIdField?: string, identifiers?: IdentifierType): string;
|
|
35
37
|
export declare function generateDropTableSQL(tableName: string): string;
|
|
36
|
-
export declare function generateAddColumnSQL(tableName: string, fieldName: string, field: AggregateFieldConfig, availableAggregates: Set<string>, availableValueObjects?: Set<string
|
|
38
|
+
export declare function generateAddColumnSQL(tableName: string, fieldName: string, field: AggregateFieldConfig, availableAggregates: Set<string>, availableValueObjects?: Set<string>, identifiers?: IdentifierType): string;
|
|
37
39
|
export declare function generateDropColumnSQL(tableName: string, columnName: string): string;
|
|
38
|
-
export declare function generateModifyColumnSQL(tableName: string, fieldName: string, field: AggregateFieldConfig, availableAggregates: Set<string>, availableValueObjects?: Set<string
|
|
40
|
+
export declare function generateModifyColumnSQL(tableName: string, fieldName: string, field: AggregateFieldConfig, availableAggregates: Set<string>, availableValueObjects?: Set<string>, identifiers?: IdentifierType): string;
|
|
39
41
|
export declare function loadSchemaState(stateFilePath: string): SchemaState | null;
|
|
40
42
|
export declare function saveSchemaState(stateFilePath: string, state: SchemaState): void;
|
|
41
43
|
export declare function loadMigrationLog(logFilePath: string): MigrationLog;
|
|
42
44
|
export declare function saveMigrationLog(logFilePath: string, log: MigrationLog): void;
|
|
43
|
-
export declare function compareSchemas(oldState: SchemaState | null, newAggregates: Record<string, AggregateConfig>, availableValueObjects?: Set<string
|
|
45
|
+
export declare function compareSchemas(oldState: SchemaState | null, newAggregates: Record<string, AggregateConfig>, availableValueObjects?: Set<string>, identifiers?: IdentifierType): string[];
|
|
44
46
|
export declare function generateTimestamp(): string;
|
|
45
47
|
export declare function getMigrationFileName(timestamp: string): string;
|