fiberx-backend-toolkit 0.0.41 → 0.0.42
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.
|
@@ -21,17 +21,20 @@ const ${schema_name}: SchemaDefinitionInterface = {
|
|
|
21
21
|
primaryKey: true,
|
|
22
22
|
autoIncrement: true,
|
|
23
23
|
allowNull: false,
|
|
24
|
+
comment: "Primary key for ${table_name} rows"
|
|
24
25
|
},
|
|
25
26
|
|
|
26
27
|
created_at: {
|
|
27
28
|
type: DataTypes.DATE,
|
|
28
29
|
defaultValue: DataTypes.NOW,
|
|
30
|
+
comment: "Timestamp when ${model_name} record was created."
|
|
29
31
|
},
|
|
30
32
|
|
|
31
33
|
updated_at: {
|
|
32
34
|
type: DataTypes.DATE,
|
|
33
35
|
allowNull: true,
|
|
34
36
|
defaultValue: null,
|
|
37
|
+
comment: "Timestamp when ${model_name} record was last updated."
|
|
35
38
|
},
|
|
36
39
|
},
|
|
37
40
|
|
|
@@ -359,8 +362,14 @@ ${attributes}
|
|
|
359
362
|
}
|
|
360
363
|
|
|
361
364
|
public static registerAssociations(): void {
|
|
362
|
-
|
|
363
|
-
|
|
365
|
+
try {
|
|
366
|
+
// Example:
|
|
367
|
+
// User.belongsTo(models.Organization, { foreignKey: "org_id" });
|
|
368
|
+
}
|
|
369
|
+
catch(error: any) {
|
|
370
|
+
console.warn(\`Failed to fully register model ${schema_model_name} associations\`, { error } );
|
|
371
|
+
}
|
|
372
|
+
|
|
364
373
|
}
|
|
365
374
|
}
|
|
366
375
|
|
|
@@ -369,42 +378,65 @@ export default ${schema_model_name}
|
|
|
369
378
|
};
|
|
370
379
|
exports.SEQUELIZE_MODEL_CODE_TEMPLATE = SEQUELIZE_MODEL_CODE_TEMPLATE;
|
|
371
380
|
const SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE = (model_names, imports) => {
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
const
|
|
375
|
-
|
|
376
|
-
|
|
381
|
+
const model_array = model_names.join(",\n ");
|
|
382
|
+
const export_initialized = model_names.map((m) => ` ${m}: ${m}Model`).join(",\n");
|
|
383
|
+
const export_raw = model_names.join(",\n ");
|
|
384
|
+
return `
|
|
385
|
+
import { SequelizeConnector } from "fiberx-backend-toolkit/dist/database/main";
|
|
386
|
+
${imports}
|
|
377
387
|
|
|
378
|
-
|
|
379
|
-
throw new Error("Sequelize connection not available for ${model_name} model.");
|
|
380
|
-
}
|
|
388
|
+
const seqelize_connector = SequelizeConnector.getInstance();
|
|
381
389
|
|
|
382
|
-
|
|
390
|
+
// -------------------------
|
|
391
|
+
// Model class list
|
|
392
|
+
// -------------------------
|
|
393
|
+
const modelClasses = [
|
|
394
|
+
${model_array}
|
|
395
|
+
];
|
|
383
396
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
397
|
+
// Store initialized models
|
|
398
|
+
const initializedModels: Record<string, any> = {};
|
|
399
|
+
const associationMethods: (() => void)[] = [];
|
|
387
400
|
|
|
388
|
-
|
|
389
|
-
|
|
401
|
+
// -------------------------
|
|
402
|
+
// Initialize all models first
|
|
403
|
+
// -------------------------
|
|
404
|
+
for (const ModelClass of modelClasses) {
|
|
405
|
+
const connection_name = ModelClass.schema_def?.connection_name || "default";
|
|
406
|
+
const sequelize = seqelize_connector.connectNamedSync(connection_name);
|
|
390
407
|
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
}
|
|
394
|
-
const model_exports = model_names.map((model_name) => ` ${model_name},
|
|
395
|
-
${model_name}Model`).join(",\n");
|
|
396
|
-
return `
|
|
397
|
-
import { SequelizeConnector } from "fiberx-backend-toolkit/dist/database/main";
|
|
398
|
-
${imports}
|
|
408
|
+
if (!sequelize) {
|
|
409
|
+
throw new Error(\`Sequelize connection not available for \${ModelClass.name} model.\`);
|
|
410
|
+
}
|
|
399
411
|
|
|
412
|
+
const initialized = ModelClass.initModel(sequelize);
|
|
413
|
+
initializedModels[ModelClass.name] = initialized;
|
|
400
414
|
|
|
401
|
-
|
|
415
|
+
// Store association method
|
|
416
|
+
if (typeof ModelClass.registerAssociations === "function") {
|
|
417
|
+
associationMethods.push(() => ModelClass.registerAssociations());
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// -------------------------
|
|
422
|
+
// Register associations AFTER all models exist
|
|
423
|
+
// -------------------------
|
|
424
|
+
for (const register of associationMethods) {
|
|
425
|
+
register();
|
|
426
|
+
}
|
|
402
427
|
|
|
403
|
-
|
|
428
|
+
// -------------------------
|
|
429
|
+
// Create named initialized exports
|
|
430
|
+
// -------------------------
|
|
431
|
+
${model_names.map(m => `const ${m}Model = initializedModels["${m}"];`).join("\n")}
|
|
404
432
|
|
|
433
|
+
// -------------------------
|
|
434
|
+
// Export raw + initialized
|
|
435
|
+
// -------------------------
|
|
405
436
|
export {
|
|
406
|
-
${
|
|
407
|
-
}
|
|
437
|
+
${export_raw},
|
|
438
|
+
${model_names.map(m => ` ${m}Model`).join(",\n")}
|
|
439
|
+
};
|
|
408
440
|
`;
|
|
409
441
|
};
|
|
410
442
|
exports.SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE = SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fiberx-backend-toolkit",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.42",
|
|
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",
|