Selaa lähdekoodia

feat: Add loadModels to model store

Owen Diffey 1 vuosi sitten
vanhempi
commit
87ac0e09d0
2 muutettua tiedostoa jossa 59 lisäystä ja 14 poistoa
  1. 35 13
      frontend/src/composables/useModels.ts
  2. 24 1
      frontend/src/stores/model.ts

+ 35 - 13
frontend/src/composables/useModels.ts

@@ -66,18 +66,8 @@ export const useModels = () => {
 		delete subscriptions.value[type][modelName][uuid];
 	};
 
-	const registerModels = async (
-		storeModels: any[],
-		relations?: Record<string, string | string[]>
-	) => {
-		const registeredModels = await modelStore.registerModels(
-			storeModels,
-			relations
-		);
-
-		models.value.push(...registeredModels);
-
-		await Promise.all(
+	const setupDeletedSubscriptions = (registeredModels: any[]) =>
+		Promise.all(
 			registeredModels
 				.filter(
 					(model, index) =>
@@ -100,6 +90,37 @@ export const useModels = () => {
 				})
 		);
 
+	const registerModels = async (
+		storeModels: any[],
+		relations?: Record<string, string | string[]>
+	) => {
+		const registeredModels = await modelStore.registerModels(
+			storeModels,
+			relations
+		);
+
+		models.value.push(...registeredModels);
+
+		await setupDeletedSubscriptions(registeredModels);
+
+		return registeredModels;
+	};
+
+	const loadModels = async (
+		modelName: string,
+		modelIds: string | string[],
+		relations?: Record<string, string | string[]>
+	) => {
+		const registeredModels = await modelStore.loadModels(
+			modelName,
+			modelIds,
+			relations
+		);
+
+		models.value.push(...registeredModels);
+
+		await setupDeletedSubscriptions(registeredModels);
+
 		return registeredModels;
 	};
 
@@ -139,6 +160,7 @@ export const useModels = () => {
 		onDeleted,
 		removeCallback,
 		registerModels,
-		unregisterModels
+		unregisterModels,
+		loadModels
 	};
 };

+ 24 - 1
frontend/src/stores/model.ts

@@ -222,6 +222,28 @@ export const useModelStore = defineStore("model", () => {
 		return runJob(`data.${modelName}.findById`, { _id });
 	};
 
+	const loadModels = async (
+		modelName: string,
+		modelIds: string | string[],
+		relations?: Record<string, string | string[]>
+	) =>
+		Promise.all(
+			(Array.isArray(modelIds) ? modelIds : [modelIds]).map(
+				async modelId => {
+					let model = models.value.find(
+						model =>
+							model._id === modelId && model._name === modelName
+					);
+
+					model ??= await findById(modelName, modelId);
+
+					const [ref] = await registerModels(model, relations);
+
+					return ref;
+				}
+			)
+		);
+
 	return {
 		models,
 		permissions,
@@ -234,6 +256,7 @@ export const useModelStore = defineStore("model", () => {
 		unregisterModels,
 		getUserModelPermissions,
 		hasPermission,
-		findById
+		findById,
+		loadModels
 	};
 });