fiberx-backend-toolkit 0.0.62 → 0.0.63

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 colCode = serializeSequelizeColumnDefinition(col_def);
64
+ return `"${col_name}": ${colCode}`;
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,22 @@ 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
 
134
- ${schema_definition.indexes?.map(index => `
148
+ ${schema_definition.indexes?.map(index => {
149
+ const indexCode = `
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
+ `;
159
+ return indentBlock(indexCode.trim(), 3);
160
+ }).join("\n\n") || ""}
144
161
 
145
162
  this.logger.success(\`✅ Table "\${table_name}" created successfully.\`);
146
163
  }
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.63",
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",