fiberx-backend-toolkit 0.0.63 → 0.0.65

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.
@@ -24,7 +24,7 @@ const serializeSequelizeDataType = (type) => {
24
24
  }
25
25
  return "undefined";
26
26
  };
27
- const serializeSequelizeColumnDefinition = (column) => {
27
+ const serializeSequelizeColumnDefinition = (column, indent = 4) => {
28
28
  const parts = [];
29
29
  for (const [key, value] of Object.entries(column)) {
30
30
  if (key === "type") {
@@ -44,8 +44,9 @@ const serializeSequelizeColumnDefinition = (column) => {
44
44
  }
45
45
  }
46
46
  return `{
47
- ${indentBlock(parts.join(",\n"), 1)}
48
- }`;
47
+ ${indentBlock(parts.join(",\n"), indent)}
48
+ ${indentBlock("}", indent === 1 ? 0 : 3)}
49
+ `;
49
50
  };
50
51
  const serializeIndexDefinition = (index) => {
51
52
  const clone = { ...index };
@@ -60,13 +61,14 @@ const serializeIndexDefinition = (index) => {
60
61
  const serializeSequelizeColumnsObject = (columns) => {
61
62
  const rendered = Object.entries(columns)
62
63
  .map(([col_name, col_def]) => {
63
- const colCode = serializeSequelizeColumnDefinition(col_def);
64
- return `"${col_name}": ${colCode}`;
64
+ const col_code = serializeSequelizeColumnDefinition(col_def, 1);
65
+ return `"${col_name}": ${col_code}`;
65
66
  })
66
67
  .join(",\n");
67
68
  return `{
68
- ${indentBlock(rendered, 1)}
69
- }`;
69
+ ${indentBlock(rendered, 4)}
70
+ ${indentBlock("}", 3)}
71
+ `;
70
72
  };
71
73
  const serializeSequelizeIndexesArray = (indexes) => {
72
74
  return indexes.map(index => {
@@ -142,22 +144,19 @@ class Create${schema_model_name}TableMigration {
142
144
 
143
145
  await queryInterface.createTable(
144
146
  table_name,
145
- ${indentBlock(rendered_columns, 4)}
147
+ ${indentBlock(rendered_columns, 1)}
146
148
  );
147
149
 
148
- ${schema_definition.indexes?.map(index => {
149
- const indexCode = `
150
+ ${schema_definition.indexes?.map(index => `
150
151
  await queryInterface.addIndex(
151
152
  table_name,
152
153
  ${JSON.stringify(index.fields)},
153
154
  {
154
155
  name: "${index.name}",
155
- ${indentBlock(serializeIndexDefinition(index), 2)}
156
+ ${indentBlock(serializeIndexDefinition(index), 1)}
156
157
  }
157
158
  );
158
- `;
159
- return indentBlock(indexCode.trim(), 3);
160
- }).join("\n\n") || ""}
159
+ `).join("")}
161
160
 
162
161
  this.logger.success(\`✅ Table "\${table_name}" created successfully.\`);
163
162
  }
@@ -184,7 +183,7 @@ export default Create${schema_model_name}TableMigration;
184
183
  exports.SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE;
185
184
  const SEQUELIZE_UPDATE_EXISTING_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = (table_name, model_name, diff, current_schema, previous_schema) => {
186
185
  return `
187
- import { QueryInterface, Sequelize } from "sequelize";
186
+ import { QueryInterface, Sequelize, DataTypes } from "sequelize";
188
187
  import { LoggerUtil } from "fiberx-backend-toolkit/dist/utils/main";
189
188
 
190
189
  class Update${model_name}TableMigration {
@@ -210,7 +209,7 @@ class Update${model_name}TableMigration {
210
209
  await queryInterface.addColumn(
211
210
  table_name,
212
211
  "${col}",
213
- ${serializeSequelizeColumnDefinition(current_schema.columns[col])}
212
+ ${indentBlock(serializeSequelizeColumnDefinition(current_schema.columns[col]), 1)}
214
213
  );
215
214
  `).join("")}
216
215
 
@@ -219,7 +218,7 @@ class Update${model_name}TableMigration {
219
218
  await queryInterface.changeColumn(
220
219
  table_name,
221
220
  "${col}",
222
- ${serializeSequelizeColumnDefinition(current_schema.columns[col])}
221
+ ${indentBlock(serializeSequelizeColumnDefinition(current_schema.columns[col]), 1)}
223
222
  );
224
223
  `).join("")}
225
224
 
@@ -241,7 +240,7 @@ class Update${model_name}TableMigration {
241
240
  ${JSON.stringify(idx.fields)},
242
241
  {
243
242
  name: "${idx.name}",
244
- ${serializeIndexDefinition(idx).replace(/^{|}$/g, "")}
243
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 1)}
245
244
  }
246
245
  );
247
246
  ` : "";
@@ -259,7 +258,7 @@ class Update${model_name}TableMigration {
259
258
  ${JSON.stringify(idx.fields)},
260
259
  {
261
260
  name: "${idx.name}",
262
- ${serializeIndexDefinition(idx).replace(/^{|}$/g, "")}
261
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 1)}
263
262
  }
264
263
  );
265
264
  ` : "";
@@ -298,7 +297,7 @@ class Update${model_name}TableMigration {
298
297
  ${JSON.stringify(idx.fields)},
299
298
  {
300
299
  name: "${idx.name}",
301
- ${serializeIndexDefinition(idx).replace(/^{|}$/g, "")}
300
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 1)}
302
301
  }
303
302
  );
304
303
  ` : "";
@@ -313,7 +312,7 @@ class Update${model_name}TableMigration {
313
312
  ${JSON.stringify(idx.fields)},
314
313
  {
315
314
  name: "${idx.name}",
316
- ${serializeIndexDefinition(idx).replace(/^{|}$/g, "")}
315
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 1)}
317
316
  }
318
317
  );
319
318
  ` : "";
@@ -324,7 +323,7 @@ class Update${model_name}TableMigration {
324
323
  await queryInterface.changeColumn(
325
324
  table_name,
326
325
  "${col}",
327
- ${serializeSequelizeColumnDefinition(previous_schema.columns[col])}
326
+ ${indentBlock(serializeSequelizeColumnDefinition(previous_schema.columns[col]), 1)}
328
327
  );
329
328
  `).join("")}
330
329
 
@@ -336,7 +335,7 @@ class Update${model_name}TableMigration {
336
335
  await queryInterface.addColumn(
337
336
  table_name,
338
337
  "${col}",
339
- ${serializeSequelizeColumnDefinition(previous_schema.columns[col])}
338
+ ${indentBlock(serializeSequelizeColumnDefinition(previous_schema.columns[col]), 1)}
340
339
  );
341
340
  `).join("")}
342
341
 
@@ -45,20 +45,20 @@ class SchemaDiffUtil {
45
45
  // Indexes
46
46
  const prev_indexes = prev?.indexes || [];
47
47
  const curr_indexes = curr?.indexes || [];
48
- const prev_map = new Map(prev_indexes.map(i => [i.name, i]));
49
- const curr_map = new Map(curr_indexes.map(i => [i.name, i]));
50
- for (const [name, curr_idx] of curr_map) {
51
- const prev_idx = prev_map.get(name);
48
+ const prev_map = new Map(prev_indexes.map(i => [SchemaDiffUtil.indexKey(i), i]));
49
+ const curr_map = new Map(curr_indexes.map(i => [SchemaDiffUtil.indexKey(i), i]));
50
+ for (const [key, curr_idx] of curr_map) {
51
+ const prev_idx = prev_map.get(key);
52
52
  if (!prev_idx) {
53
- diff.indexes.added.push(name);
53
+ diff.indexes.added.push(curr_idx.name);
54
54
  }
55
55
  else if (!input_validator_util_1.default.isDeepEqual(prev_idx, curr_idx)) {
56
- diff.indexes.modified.push(name);
56
+ diff.indexes.modified.push(curr_idx.name);
57
57
  }
58
58
  }
59
- for (const [name] of prev_map) {
60
- if (!curr_map.has(name)) {
61
- diff.indexes.removed.push(name);
59
+ for (const [key, prev_idx] of prev_map) {
60
+ if (!curr_map.has(key)) {
61
+ diff.indexes.removed.push(prev_idx.name);
62
62
  }
63
63
  }
64
64
  return diff;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fiberx-backend-toolkit",
3
- "version": "0.0.63",
3
+ "version": "0.0.65",
4
4
  "description": "A TypeScript backend toolkit providing shared domain logic, infrastructure helpers, and utilities for FiberX server-side applications and services.",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",