@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.
@@ -33,6 +33,8 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.getIdColumnDefinition = getIdColumnDefinition;
37
+ exports.getFkColumnType = getFkColumnType;
36
38
  exports.mapYamlTypeToSql = mapYamlTypeToSql;
37
39
  exports.getTableName = getTableName;
38
40
  exports.getForeignKeyFieldName = getForeignKeyFieldName;
@@ -67,10 +69,24 @@ const TYPE_MAPPING = {
67
69
  array: 'JSON',
68
70
  object: 'JSON'
69
71
  };
70
- function mapYamlTypeToSql(yamlType, availableAggregates, availableValueObjects) {
71
- // Simple aggregate reference → foreign key INT
72
+ function getIdColumnDefinition(idType = 'numeric') {
73
+ switch (idType) {
74
+ case 'uuid': return 'id BINARY(16) PRIMARY KEY DEFAULT (UUID_TO_BIN(UUID(), 1))';
75
+ case 'nanoid': return 'id VARCHAR(21) PRIMARY KEY';
76
+ default: return 'id INT AUTO_INCREMENT PRIMARY KEY';
77
+ }
78
+ }
79
+ function getFkColumnType(idType = 'numeric') {
80
+ switch (idType) {
81
+ case 'uuid': return 'BINARY(16)';
82
+ case 'nanoid': return 'VARCHAR(21)';
83
+ default: return 'INT';
84
+ }
85
+ }
86
+ function mapYamlTypeToSql(yamlType, availableAggregates, availableValueObjects, identifiers = 'numeric') {
87
+ // Simple aggregate reference → foreign key column matching PK type
72
88
  if (availableAggregates.has(yamlType)) {
73
- return 'INT';
89
+ return getFkColumnType(identifiers);
74
90
  }
75
91
  // Compound types: array ("Foo[]") or union ("Foo | Bar") → JSON column
76
92
  const parsed = (0, typeUtils_1.parseFieldType)(yamlType);
@@ -106,26 +122,27 @@ function buildChildToParentMap(aggregates) {
106
122
  }
107
123
  return map;
108
124
  }
109
- function generateCreateTableSQL(name, aggregate, availableAggregates, availableValueObjects, parentIdField) {
125
+ function generateCreateTableSQL(name, aggregate, availableAggregates, availableValueObjects, parentIdField, identifiers = 'numeric') {
110
126
  const tableName = getTableName(name);
111
127
  const columns = [];
112
128
  const indexes = [];
113
129
  const foreignKeys = [];
114
- columns.push(' id INT AUTO_INCREMENT PRIMARY KEY');
130
+ const fkType = getFkColumnType(identifiers);
131
+ columns.push(` ${getIdColumnDefinition(identifiers)}`);
115
132
  // Root aggregates get an ownerId column; child entities get a parent ID column.
116
133
  if (parentIdField) {
117
- columns.push(` ${parentIdField} INT NOT NULL`);
134
+ columns.push(` ${parentIdField} ${fkType} NOT NULL`);
118
135
  indexes.push(` INDEX idx_${tableName}_${parentIdField} (${parentIdField})`);
119
136
  }
120
137
  else if (aggregate.root !== false) {
121
- columns.push(' ownerId INT NOT NULL');
138
+ columns.push(` ownerId ${fkType} NOT NULL`);
122
139
  indexes.push(` INDEX idx_${tableName}_ownerId (ownerId)`);
123
140
  }
124
141
  for (const [fieldName, field] of Object.entries(aggregate.fields)) {
125
142
  if (isRelationshipField(field.type, availableAggregates)) {
126
143
  const foreignKeyName = getForeignKeyFieldName(fieldName);
127
144
  const nullable = field.required === false ? 'NULL DEFAULT NULL' : 'NOT NULL';
128
- columns.push(` ${foreignKeyName} INT ${nullable}`);
145
+ columns.push(` ${foreignKeyName} ${fkType} ${nullable}`);
129
146
  indexes.push(` INDEX idx_${tableName}_${foreignKeyName} (${foreignKeyName})`);
130
147
  const refTableName = getTableName(field.type);
131
148
  foreignKeys.push(` CONSTRAINT fk_${tableName}_${foreignKeyName} \n` +
@@ -135,7 +152,7 @@ function generateCreateTableSQL(name, aggregate, availableAggregates, availableV
135
152
  ` ON UPDATE CASCADE`);
136
153
  }
137
154
  else {
138
- const sqlType = mapYamlTypeToSql(field.type, availableAggregates, availableValueObjects);
155
+ const sqlType = mapYamlTypeToSql(field.type, availableAggregates, availableValueObjects, identifiers);
139
156
  const nullable = field.required === false ? 'NULL DEFAULT NULL' : 'NOT NULL';
140
157
  columns.push(` ${fieldName} ${sqlType} ${nullable}`);
141
158
  if (['string', 'number', 'integer', 'id'].includes(field.type)) {
@@ -154,14 +171,14 @@ function generateCreateTableSQL(name, aggregate, availableAggregates, availableV
154
171
  function generateDropTableSQL(tableName) {
155
172
  return `DROP TABLE IF EXISTS \`${tableName}\`;`;
156
173
  }
157
- function generateAddColumnSQL(tableName, fieldName, field, availableAggregates, availableValueObjects) {
174
+ function generateAddColumnSQL(tableName, fieldName, field, availableAggregates, availableValueObjects, identifiers = 'numeric') {
158
175
  if (isRelationshipField(field.type, availableAggregates)) {
159
176
  const foreignKeyName = getForeignKeyFieldName(fieldName);
160
177
  const nullable = field.required === false ? 'NULL DEFAULT NULL' : 'NOT NULL';
161
- return `ALTER TABLE \`${tableName}\` ADD COLUMN \`${foreignKeyName}\` INT ${nullable};`;
178
+ return `ALTER TABLE \`${tableName}\` ADD COLUMN \`${foreignKeyName}\` ${getFkColumnType(identifiers)} ${nullable};`;
162
179
  }
163
180
  else {
164
- const sqlType = mapYamlTypeToSql(field.type, availableAggregates, availableValueObjects);
181
+ const sqlType = mapYamlTypeToSql(field.type, availableAggregates, availableValueObjects, identifiers);
165
182
  const nullable = field.required === false ? 'NULL DEFAULT NULL' : 'NOT NULL';
166
183
  return `ALTER TABLE \`${tableName}\` ADD COLUMN \`${fieldName}\` ${sqlType} ${nullable};`;
167
184
  }
@@ -169,14 +186,14 @@ function generateAddColumnSQL(tableName, fieldName, field, availableAggregates,
169
186
  function generateDropColumnSQL(tableName, columnName) {
170
187
  return `ALTER TABLE \`${tableName}\` DROP COLUMN \`${columnName}\`;`;
171
188
  }
172
- function generateModifyColumnSQL(tableName, fieldName, field, availableAggregates, availableValueObjects) {
189
+ function generateModifyColumnSQL(tableName, fieldName, field, availableAggregates, availableValueObjects, identifiers = 'numeric') {
173
190
  if (isRelationshipField(field.type, availableAggregates)) {
174
191
  const foreignKeyName = getForeignKeyFieldName(fieldName);
175
192
  const nullable = field.required === false ? 'NULL DEFAULT NULL' : 'NOT NULL';
176
- return `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${foreignKeyName}\` INT ${nullable};`;
193
+ return `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${foreignKeyName}\` ${getFkColumnType(identifiers)} ${nullable};`;
177
194
  }
178
195
  else {
179
- const sqlType = mapYamlTypeToSql(field.type, availableAggregates, availableValueObjects);
196
+ const sqlType = mapYamlTypeToSql(field.type, availableAggregates, availableValueObjects, identifiers);
180
197
  const nullable = field.required === false ? 'NULL DEFAULT NULL' : 'NOT NULL';
181
198
  return `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${fieldName}\` ${sqlType} ${nullable};`;
182
199
  }
@@ -225,7 +242,7 @@ function sortAggregatesByDependencies(aggregates, availableAggregates) {
225
242
  }
226
243
  return sorted;
227
244
  }
228
- function compareSchemas(oldState, newAggregates, availableValueObjects) {
245
+ function compareSchemas(oldState, newAggregates, availableValueObjects, identifiers = 'numeric') {
229
246
  const sqlStatements = [];
230
247
  const availableAggregates = new Set(Object.keys(newAggregates));
231
248
  const childToParent = buildChildToParentMap(newAggregates);
@@ -236,7 +253,7 @@ function compareSchemas(oldState, newAggregates, availableValueObjects) {
236
253
  const parentName = childToParent.get(name);
237
254
  const parentIdField = parentName ? `${parentName.toLowerCase()}Id` : undefined;
238
255
  sqlStatements.push(`-- Create ${tableName} table`);
239
- sqlStatements.push(generateCreateTableSQL(name, aggregate, availableAggregates, availableValueObjects, parentIdField));
256
+ sqlStatements.push(generateCreateTableSQL(name, aggregate, availableAggregates, availableValueObjects, parentIdField, identifiers));
240
257
  sqlStatements.push('');
241
258
  }
242
259
  return sqlStatements;
@@ -259,7 +276,7 @@ function compareSchemas(oldState, newAggregates, availableValueObjects) {
259
276
  const parentIdField = parentName ? `${parentName.toLowerCase()}Id` : undefined;
260
277
  if (!oldAggregate) {
261
278
  sqlStatements.push(`-- Create ${tableName} table`);
262
- sqlStatements.push(generateCreateTableSQL(name, newAggregate, availableAggregates, availableValueObjects, parentIdField));
279
+ sqlStatements.push(generateCreateTableSQL(name, newAggregate, availableAggregates, availableValueObjects, parentIdField, identifiers));
263
280
  sqlStatements.push('');
264
281
  }
265
282
  else {
@@ -281,7 +298,7 @@ function compareSchemas(oldState, newAggregates, availableValueObjects) {
281
298
  const oldField = oldFields[fieldName];
282
299
  if (!oldField) {
283
300
  sqlStatements.push(`-- Add column ${fieldName} to ${tableName}`);
284
- sqlStatements.push(generateAddColumnSQL(tableName, fieldName, newField, availableAggregates, availableValueObjects));
301
+ sqlStatements.push(generateAddColumnSQL(tableName, fieldName, newField, availableAggregates, availableValueObjects, identifiers));
285
302
  sqlStatements.push('');
286
303
  }
287
304
  else {
@@ -289,7 +306,7 @@ function compareSchemas(oldState, newAggregates, availableValueObjects) {
289
306
  const requiredChanged = oldField.required !== newField.required;
290
307
  if (typeChanged || requiredChanged) {
291
308
  sqlStatements.push(`-- Modify column ${fieldName} in ${tableName}`);
292
- sqlStatements.push(generateModifyColumnSQL(tableName, fieldName, newField, availableAggregates, availableValueObjects));
309
+ sqlStatements.push(generateModifyColumnSQL(tableName, fieldName, newField, availableAggregates, availableValueObjects, identifiers));
293
310
  sqlStatements.push('');
294
311
  }
295
312
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@currentjs/gen",
3
- "version": "0.5.5",
3
+ "version": "0.5.6",
4
4
  "description": "CLI code generator",
5
5
  "license": "LGPL-3.0",
6
6
  "author": "Konstantin Zavalny",