fiberx-backend-toolkit 0.0.62 → 0.0.64

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.
@@ -1,18 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE = exports.SEQUELIZE_MODEL_CODE_TEMPLATE = exports.SEQUELIZE_UPDATE_EXISTING_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = exports.SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = exports.SEQUELIZE_SEEDER_TEMPLATE = exports.SEQUELIZE_SCHEMA_CODE_TEMPLATE = void 0;
4
+ const INDENT = " ".repeat(4);
5
+ const indentBlock = (code, level) => {
6
+ const indentation = INDENT.repeat(level);
7
+ return code
8
+ .split("\n")
9
+ .map(line => line.trim().length ? indentation + line : line)
10
+ .join("\n");
11
+ };
4
12
  const serializeSequelizeDataType = (type) => {
5
13
  if (!type)
6
14
  return "undefined";
7
15
  if (type.key) {
8
16
  const key = type.key;
9
17
  if (type.options?.length)
10
- return `Sequelize.${key}(${type.options.length})`;
18
+ return `DataTypes.${key}(${type.options.length})`;
11
19
  if (type.options?.values)
12
- return `Sequelize.${key}(${JSON.stringify(type.options.values)})`;
20
+ return `DataTypes.${key}(${JSON.stringify(type.options.values)})`;
13
21
  if (type.options?.precision && type.options?.scale)
14
- return `Sequelize.${key}(${type.options.precision}, ${type.options.scale})`;
15
- return `Sequelize.${key}`;
22
+ return `DataTypes.${key}(${type.options.precision}, ${type.options.scale})`;
23
+ return `DataTypes.${key}`;
16
24
  }
17
25
  return "undefined";
18
26
  };
@@ -36,23 +44,29 @@ const serializeSequelizeColumnDefinition = (column) => {
36
44
  }
37
45
  }
38
46
  return `{
39
- ${parts.join(",\n ")}
40
- }`;
47
+ ${indentBlock(parts.join(",\n"), 1)}
48
+ }`;
41
49
  };
42
50
  const serializeIndexDefinition = (index) => {
43
51
  const clone = { ...index };
44
52
  delete clone.name;
45
- return JSON.stringify(clone, null, 4);
53
+ delete clone.fields;
54
+ delete clone.comment;
55
+ const entries = Object.entries(clone)
56
+ .map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
57
+ .join(",\n");
58
+ return entries;
46
59
  };
47
60
  const serializeSequelizeColumnsObject = (columns) => {
48
61
  const rendered = Object.entries(columns)
49
- .map(([col_name, col_Def]) => {
50
- return `"${col_name}": ${serializeSequelizeColumnDefinition(col_Def)}`;
62
+ .map(([col_name, col_def]) => {
63
+ const col_code = serializeSequelizeColumnDefinition(col_def);
64
+ return `"${col_name}": ${col_code}`;
51
65
  })
52
- .join(",\n ");
66
+ .join(",\n");
53
67
  return `{
54
- ${rendered}
55
- }`;
68
+ ${indentBlock(rendered, 1)}
69
+ }`;
56
70
  };
57
71
  const serializeSequelizeIndexesArray = (indexes) => {
58
72
  return indexes.map(index => {
@@ -113,7 +127,7 @@ const SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = (schema_table_n
113
127
  const rendered_columns = serializeSequelizeColumnsObject(schema_definition.columns);
114
128
  const rendered_indexes = serializeSequelizeIndexesArray(schema_definition.indexes || []);
115
129
  return `
116
- import { QueryInterface, Sequelize } from "sequelize";
130
+ import { QueryInterface, Sequelize, DataTypes } from "sequelize";
117
131
  import { LoggerUtil } from "fiberx-backend-toolkit/dist/utils/main";
118
132
 
119
133
  class Create${schema_model_name}TableMigration {
@@ -128,19 +142,20 @@ class Create${schema_model_name}TableMigration {
128
142
 
129
143
  await queryInterface.createTable(
130
144
  table_name,
131
- ${rendered_columns}
145
+ ${indentBlock(rendered_columns, 4)}
132
146
  );
133
147
 
148
+
134
149
  ${schema_definition.indexes?.map(index => `
135
150
  await queryInterface.addIndex(
136
151
  table_name,
137
152
  ${JSON.stringify(index.fields)},
138
153
  {
139
154
  name: "${index.name}",
140
- ${serializeIndexDefinition(index).replace(/^{|}$/g, "")}
155
+ ${indentBlock(serializeIndexDefinition(index), 2)}
141
156
  }
142
157
  );
143
- `).join("") || ""}
158
+ `).join("")}
144
159
 
145
160
  this.logger.success(\`✅ Table "\${table_name}" created successfully.\`);
146
161
  }
@@ -193,7 +208,7 @@ class Update${model_name}TableMigration {
193
208
  await queryInterface.addColumn(
194
209
  table_name,
195
210
  "${col}",
196
- ${serializeSequelizeColumnDefinition(current_schema.columns[col])}
211
+ ${indentBlock(serializeSequelizeColumnDefinition(current_schema.columns[col]), 1)}
197
212
  );
198
213
  `).join("")}
199
214
 
@@ -202,7 +217,7 @@ class Update${model_name}TableMigration {
202
217
  await queryInterface.changeColumn(
203
218
  table_name,
204
219
  "${col}",
205
- ${serializeSequelizeColumnDefinition(current_schema.columns[col])}
220
+ ${indentBlock(serializeSequelizeColumnDefinition(current_schema.columns[col]), 1)}
206
221
  );
207
222
  `).join("")}
208
223
 
@@ -224,7 +239,7 @@ class Update${model_name}TableMigration {
224
239
  ${JSON.stringify(idx.fields)},
225
240
  {
226
241
  name: "${idx.name}",
227
- ${serializeIndexDefinition(idx).replace(/^{|}$/g, "")}
242
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 2)}
228
243
  }
229
244
  );
230
245
  ` : "";
@@ -242,7 +257,7 @@ class Update${model_name}TableMigration {
242
257
  ${JSON.stringify(idx.fields)},
243
258
  {
244
259
  name: "${idx.name}",
245
- ${serializeIndexDefinition(idx).replace(/^{|}$/g, "")}
260
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 2)}
246
261
  }
247
262
  );
248
263
  ` : "";
@@ -281,7 +296,7 @@ class Update${model_name}TableMigration {
281
296
  ${JSON.stringify(idx.fields)},
282
297
  {
283
298
  name: "${idx.name}",
284
- ${serializeIndexDefinition(idx).replace(/^{|}$/g, "")}
299
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 2)}
285
300
  }
286
301
  );
287
302
  ` : "";
@@ -296,7 +311,7 @@ class Update${model_name}TableMigration {
296
311
  ${JSON.stringify(idx.fields)},
297
312
  {
298
313
  name: "${idx.name}",
299
- ${serializeIndexDefinition(idx).replace(/^{|}$/g, "")}
314
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 2)}
300
315
  }
301
316
  );
302
317
  ` : "";
@@ -307,7 +322,7 @@ class Update${model_name}TableMigration {
307
322
  await queryInterface.changeColumn(
308
323
  table_name,
309
324
  "${col}",
310
- ${serializeSequelizeColumnDefinition(previous_schema.columns[col])}
325
+ ${indentBlock(serializeSequelizeColumnDefinition(previous_schema.columns[col]), 1)}
311
326
  );
312
327
  `).join("")}
313
328
 
@@ -319,7 +334,7 @@ class Update${model_name}TableMigration {
319
334
  await queryInterface.addColumn(
320
335
  table_name,
321
336
  "${col}",
322
- ${serializeSequelizeColumnDefinition(previous_schema.columns[col])}
337
+ ${indentBlock(serializeSequelizeColumnDefinition(previous_schema.columns[col]), 1)}
323
338
  );
324
339
  `).join("")}
325
340
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fiberx-backend-toolkit",
3
- "version": "0.0.62",
3
+ "version": "0.0.64",
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",