fiberx-backend-toolkit 0.0.29 → 0.0.31

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.
@@ -7,6 +7,7 @@ import { DataTypes } from "sequelize";
7
7
  import { SchemaDefinitionInterface } from "fiberx-backend-toolkit/dist/types/schema_type";
8
8
 
9
9
  const ${schema_name}: SchemaDefinitionInterface = {
10
+ connection_name: "default",
10
11
  database_name: "public",
11
12
  migration_priority: ${migration_priority},
12
13
 
@@ -328,14 +329,19 @@ const SEQUELIZE_MODEL_CODE_TEMPLATE = (schema_model_name, schema_file_name, sche
328
329
  return `
329
330
  import { Model, Sequelize } from "sequelize";
330
331
  import ${schema_model_name}Schema from "@/database/schemas/${schema_file_name.replace(".ts", "")}";
332
+ import { SchemaDefinitionInterface } from "fiberx-backend-toolkit";
331
333
 
332
334
 
333
335
  class ${schema_model_name} extends Model {
334
336
  ${attributes}
335
337
 
338
+ public static schema_def: SchemaDefinitionInterface = ${schema_model_name}Schema
339
+
336
340
  public static initModel(sequelize: Sequelize): typeof ${schema_model_name} {
337
- sequelize: Sequelize
338
- ): ${schema_model_name} {
341
+ if (!sequelize) {
342
+ throw new Error("Sequelize instance is required to initialize the ${schema_model_name} model.");
343
+ }
344
+
339
345
  ${schema_model_name}.init(
340
346
  ${schema_model_name}Schema.columns,
341
347
  {
@@ -363,28 +369,36 @@ export default ${schema_model_name}
363
369
  };
364
370
  exports.SEQUELIZE_MODEL_CODE_TEMPLATE = SEQUELIZE_MODEL_CODE_TEMPLATE;
365
371
  const SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE = (model_names, imports) => {
366
- const exports = model_names.join(",\n ");
367
- return `
368
- import { Sequelize } from "sequelize";
369
- ${imports}
372
+ const model_initializers = model_names.map((model_name) => {
373
+ return `
374
+ export const ${model_name}Model = (): typeof ${model_name} => {
375
+ const connection_name = ${model_name}.schema_def?.connection_name || "default";
370
376
 
371
- const models = {
372
- ${exports}
373
- };
377
+ const sequelize = seqelize_connector.getConnection(connection_name);
378
+
379
+ if (!sequelize) {
380
+ throw new Error(
381
+ "Sequelize connection not available for ${model_name} model."
382
+ );
383
+ }
374
384
 
375
- export const initModels = (sequelize: Sequelize) => {
376
- Object.values(models).forEach(model => {
377
- model.initModel(sequelize);
378
- });
385
+ const initialized = ${model_name}.initModel(sequelize);
379
386
 
380
- Object.values(models).forEach(model => {
381
- model.registerAssociations();
382
- });
387
+ if (${model_name}.registerAssociations) {
388
+ ${model_name}.registerAssociations();
389
+ }
383
390
 
384
- return models;
391
+ return initialized;
385
392
  };
393
+ `;
394
+ }).join("\n");
395
+ return `
396
+ import { SequelizeConnector } from "fiberx-backend-toolkit/dist/database/main";
397
+ ${imports}
398
+
399
+ const seqelize_connector = SequelizeConnector.getInstance();
386
400
 
387
- export default models;
401
+ ${model_initializers}
388
402
  `;
389
403
  };
390
404
  exports.SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE = SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE;
@@ -25,6 +25,7 @@ class SchemaNormalizerUtil {
25
25
  }
26
26
  static normalizeSchema(schema) {
27
27
  return {
28
+ connection_name: schema.connection_name,
28
29
  database_name: schema.database_name,
29
30
  table_name: schema.table_name,
30
31
  model_name: schema.model_name,
@@ -36,6 +36,7 @@ export interface IndexFieldOptionsInterface {
36
36
  comment?: string;
37
37
  }
38
38
  export interface SchemaDefinitionInterface {
39
+ connection_name: string;
39
40
  database_name: string;
40
41
  migration_priority: number;
41
42
  table_name: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fiberx-backend-toolkit",
3
- "version": "0.0.29",
3
+ "version": "0.0.31",
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",