Преглед изворни кода

refactor: Calls sequelize setup methods after loading all models

Owen Diffey пре 4 месеци
родитељ
комит
8dd24ae33c

+ 4 - 10
backend/src/modules/DataModule.ts

@@ -123,7 +123,7 @@ export class DataModule extends BaseModule {
 
 		await this._sequelize.authenticate();
 
-		const setupAssociationFunctions: Function[] = [];
+		const setupFunctions: Function[] = [];
 
 		await forEachIn(
 			await readdir(
@@ -140,8 +140,7 @@ export class DataModule extends BaseModule {
 					default: ModelClass,
 					schema,
 					options = {},
-					setup,
-					setupAssociations
+					setup
 				} = await import(`${modelFile.path}/${modelFile.name}`);
 
 				const tableName = inflection.camelize(
@@ -155,10 +154,7 @@ export class DataModule extends BaseModule {
 					sequelize: this._sequelize
 				});
 
-				if (typeof setup === "function") await setup();
-
-				if (typeof setupAssociations === "function")
-					setupAssociationFunctions.push(setupAssociations);
+				if (typeof setup === "function") setupFunctions.push(setup);
 
 				await this._loadModelEvents(ModelClass.name);
 
@@ -166,9 +162,7 @@ export class DataModule extends BaseModule {
 			}
 		);
 
-		setupAssociationFunctions.forEach(setupAssociation => {
-			setupAssociation();
-		});
+		await forEachIn(setupFunctions, setup => setup());
 
 		await this._sequelize.sync();
 

+ 3 - 5
backend/src/modules/DataModule/models/News.ts

@@ -67,6 +67,9 @@ export const schema = {
 export const options = {};
 
 export const setup = async () => {
+	News.belongsTo(User, { foreignKey: "createdBy" });
+	User.hasMany(News, { foreignKey: "createdBy" });
+
 	News.afterSave(async record => {
 		const oldDoc = record.previous();
 		const doc = record.get();
@@ -141,9 +144,4 @@ export const setup = async () => {
 	});
 };
 
-export const setupAssociations = () => {
-	News.belongsTo(User, { foreignKey: "createdBy" });
-	User.hasMany(News, { foreignKey: "createdBy" });
-};
-
 export default News;

+ 0 - 4
backend/src/modules/DataModule/models/Station.ts

@@ -162,8 +162,4 @@ export const setup = async () => {
 	});
 };
 
-export const setupAssociations = () => {
-	// TODO
-};
-
 export default Station;