Sfoglia il codice sorgente

refactor: Call DataModule.getModel directly

Owen Diffey 1 anno fa
parent
commit
d3824882bb

+ 2 - 5
backend/src/JobContext.ts

@@ -6,6 +6,7 @@ import { Log } from "@/LogBook";
 import { JobOptions } from "@/types/JobOptions";
 import { Jobs, Modules } from "@/types/Modules";
 import { Models } from "@/types/Models";
+import DataModule from "@/modules/DataModule";
 
 export default class JobContext {
 	public readonly job: Job;
@@ -76,15 +77,11 @@ export default class JobContext {
 		}).execute();
 	}
 
-	public async getModel(model: keyof Models) {
-		return this.executeJob("data", "getModel", model);
-	}
-
 	public async getUser() {
 		if (!this._session?.userId)
 			throw new Error("No user found for session");
 
-		const User = await this.getModel("users");
+		const User = await DataModule.getModel("users");
 
 		const user = await User.findById(this._session.userId);
 

+ 2 - 2
backend/src/main.ts

@@ -4,7 +4,7 @@ import ModuleManager from "@/ModuleManager";
 import LogBook from "@/LogBook";
 import JobQueue from "@/JobQueue";
 import JobStatistics from "@/JobStatistics";
-
+import DataModule from "@/modules/DataModule";
 
 process.removeAllListeners("uncaughtException");
 process.on("uncaughtException", err => {
@@ -19,7 +19,7 @@ process.on("uncaughtException", err => {
 });
 
 ModuleManager.startup().then(async () => {
-	const Model = await JobQueue.runJob("data", "getModel", { name: "news" });
+	const Model = await DataModule.getModel("news");
 	// console.log("Model", Model);
 	const abcs = await Model.findOne({}).newest();
 	console.log("Abcs", abcs);

+ 4 - 3
backend/src/modules/APIModule.ts

@@ -11,6 +11,7 @@ import Job from "@/Job";
 import { Models } from "@/types/Models";
 import ModuleManager from "@/ModuleManager";
 import JobQueue from "@/JobQueue";
+import DataModule from "@/modules/DataModule";
 
 export class APIModule extends BaseModule {
 	private _subscriptions: Record<string, Set<string>>;
@@ -84,7 +85,7 @@ export class APIModule extends BaseModule {
 	): Promise<ReturnType> {
 		let session;
 		if (sessionId) {
-			const Session = await context.getModel("sessions");
+			const Session = await DataModule.getModel("sessions");
 
 			session = await Session.findByIdAndUpdate(sessionId, {
 				updatedAt: Date.now()
@@ -134,7 +135,7 @@ export class APIModule extends BaseModule {
 
 		let user;
 		if (sessionId) {
-			const Session = await context.getModel("sessions");
+			const Session = await DataModule.getModel("sessions");
 
 			const session = await Session.findByIdAndUpdate(sessionId, {
 				updatedAt: Date.now()
@@ -218,7 +219,7 @@ export class APIModule extends BaseModule {
 		const user = await context.getUser().catch(() => null);
 		const permissions = await context.getUserPermissions();
 
-		const Model = await context.getModel(modelName);
+		const Model = await DataModule.getModel(modelName);
 
 		if (!Model) throw new Error("Model not found");
 

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

@@ -126,9 +126,7 @@ export class DataModule extends BaseModule {
 		this._dependentModules = ["events"];
 
 		this._jobConfig = {
-			getModel: false,
-			find: "disabled",
-			addC: "disabled"
+			getModel: "disabled"
 		};
 	}
 
@@ -323,7 +321,7 @@ export class DataModule extends BaseModule {
 				patchEventEmitter.on(event, async ({ doc, oldDoc }) => {
 					const modelId = doc?._id ?? oldDoc?._id;
 
-					const Model = await this.getModel(null, modelName);
+					const Model = await this.getModel(modelName);
 
 					if (doc) doc = Model.hydrate(doc);
 
@@ -467,17 +465,12 @@ export class DataModule extends BaseModule {
 	 *
 	 * @returns Model
 	 */
-	public async getModel<ModelName extends keyof Models>(
-		jobContext?: JobContext,
-		payload: ModelName | { name: ModelName }
-	) {
+	public async getModel<ModelName extends keyof Models>(name: ModelName) {
 		if (!this._models) throw new Error("Models not loaded");
 
 		if (this.getStatus() !== ModuleStatus.STARTED)
 			throw new Error("Module not started");
 
-		const name = typeof payload === "object" ? payload.name : payload;
-
 		return this._models[name];
 	}