瀏覽代碼

replace orm store by a better use of repos

Olivier Massot 3 年之前
父節點
當前提交
67d6470550
共有 2 個文件被更改,包括 26 次插入47 次删除
  1. 26 19
      services/data/entityManager.ts
  2. 0 28
      store/orm.ts

+ 26 - 19
services/data/entityManager.ts

@@ -9,7 +9,6 @@ import {useProfileAccessStore} from "~/store/profile/access";
 import ApiResource from "~/models/ApiResource";
 import ApiResource from "~/models/ApiResource";
 import {MyProfile} from "~/models/Access/MyProfile";
 import {MyProfile} from "~/models/Access/MyProfile";
 import { v4 as uuid4 } from 'uuid';
 import { v4 as uuid4 } from 'uuid';
-import {useOrmStore} from "~/store/orm";
 
 
 /**
 /**
  * Entity manager: make operations on the models defined with the Pinia-Orm library
  * Entity manager: make operations on the models defined with the Pinia-Orm library
@@ -17,6 +16,8 @@ import {useOrmStore} from "~/store/orm";
  * @see https://pinia-orm.codedredd.de/
  * @see https://pinia-orm.codedredd.de/
  */
  */
 class EntityManager {
 class EntityManager {
+    private CLONE_PREFIX = '_clone_'
+
     private apiRequestService: ApiRequestService;
     private apiRequestService: ApiRequestService;
 
 
     public constructor(apiRequestService: ApiRequestService) {
     public constructor(apiRequestService: ApiRequestService) {
@@ -50,12 +51,12 @@ class EntityManager {
         }
         }
         repository.save(entity)
         repository.save(entity)
 
 
-        EntityManager.saveInitialState(model, entity)
+        this.saveInitialState(model, entity)
 
 
         return entity
         return entity
     }
     }
 
 
-    private update(entity: ApiResource, newEntity: ApiResource) {
+    private reactiveUpdate(entity: ApiResource, newEntity: ApiResource) {
         // On met à jour l'entité par référence, pour maintenir la réactivité lorsque l'entité est réactive
         // On met à jour l'entité par référence, pour maintenir la réactivité lorsque l'entité est réactive
         // @see http://underscorejs.org/#extend
         // @see http://underscorejs.org/#extend
         useExtend(entity, newEntity)
         useExtend(entity, newEntity)
@@ -125,7 +126,7 @@ class EntityManager {
         const hydraResponse = await HydraDenormalizer.denormalize(response)
         const hydraResponse = await HydraDenormalizer.denormalize(response)
         const returnedEntity = this.new(model, hydraResponse.data)
         const returnedEntity = this.new(model, hydraResponse.data)
 
 
-        EntityManager.saveInitialState(model, returnedEntity)
+        this.saveInitialState(model, returnedEntity)
 
 
         // Save data into the store
         // Save data into the store
         repository.save(returnedEntity)
         repository.save(returnedEntity)
@@ -134,7 +135,7 @@ class EntityManager {
             await this.refreshProfile()
             await this.refreshProfile()
         }
         }
 
 
-        this.update(entity, returnedEntity)
+        this.reactiveUpdate(entity, returnedEntity)
     }
     }
 
 
     /**
     /**
@@ -152,7 +153,7 @@ class EntityManager {
             await this.apiRequestService.delete(url)
             await this.apiRequestService.delete(url)
         }
         }
 
 
-        // update the store
+        // reactiveUpdate the store
         repository.destroy(entity.id)
         repository.destroy(entity.id)
     }
     }
 
 
@@ -163,21 +164,19 @@ class EntityManager {
      * @param entity
      * @param entity
      */
      */
     public reset(model: typeof ApiResource, entity: ApiResource) {
     public reset(model: typeof ApiResource, entity: ApiResource) {
-        const initialEntity = EntityManager.getInitialStateOf(model, entity.id)
+        const initialEntity = this.getInitialStateOf(model, entity.id)
         if (initialEntity === null) {
         if (initialEntity === null) {
             throw new Error('no initial state recorded for this object - abort [' + model.entity + '/' + entity.id + ']')
             throw new Error('no initial state recorded for this object - abort [' + model.entity + '/' + entity.id + ']')
         }
         }
 
 
-        EntityManager.saveInitialState(model, initialEntity)
-
         const repository = this.getRepository(model)
         const repository = this.getRepository(model)
         repository.save(initialEntity)
         repository.save(initialEntity)
 
 
-        this.update(entity, initialEntity)
+        this.reactiveUpdate(entity, initialEntity)
     }
     }
 
 
     /**
     /**
-     * Re-fetch the user profile and update the store
+     * Re-fetch the user profile and reactiveUpdate the store
      */
      */
     public async refreshProfile() {
     public async refreshProfile() {
         const response = await this.apiRequestService.get('api/my_profile')
         const response = await this.apiRequestService.get('api/my_profile')
@@ -226,8 +225,14 @@ class EntityManager {
      * @param entity
      * @param entity
      * @private
      * @private
      */
      */
-    private static saveInitialState(model: typeof ApiResource, entity: ApiResource) {
-        useOrmStore().storeInitialValue(model, entity)
+    private saveInitialState(model: typeof ApiResource, entity: ApiResource) {
+        const repository = this.getRepository(model)
+
+        // Clone and prefix id
+        const clone = useCloneDeep(entity)
+        clone.id = this.CLONE_PREFIX + clone.id
+
+        repository.save(clone)
     }
     }
 
 
     /**
     /**
@@ -237,16 +242,18 @@ class EntityManager {
      * @param id
      * @param id
      * @private
      * @private
      */
      */
-    private static getInitialStateOf(model: typeof ApiResource, id: number): ApiResource | null {
-        const ormStore = useOrmStore()
+    private getInitialStateOf(model: typeof ApiResource, id: string | number): ApiResource | null {
+        const repository = this.getRepository(model)
 
 
-        //@ts-ignore
-        if (!ormStore.initialValues.has(model.entity) || !ormStore.initialValues.get(model.entity).has(id)) {
+        // Find the clone by id
+        const entity = repository.find(this.CLONE_PREFIX + id)
+        if (entity === null) {
             return null
             return null
         }
         }
 
 
-        //@ts-ignore
-        return ormStore.initialValues.get(model.entity).get(id)
+        // Restore the initial id
+        entity.id = id
+        return entity
     }
     }
 }
 }
 
 

+ 0 - 28
store/orm.ts

@@ -1,28 +0,0 @@
-import {ormState} from '~/types/interfaces'
-import {defineStore} from "pinia";
-import ApiResource from "~/models/ApiResource";
-
-export const useOrmStore = defineStore('orm', {
-    state: (): ormState => {
-        return {
-            /**
-             * Store the initial value of the entities, just after the fetch, to make reinitialization possible
-             */
-            initialValues: new Map<string, Map<number, ApiResource>>()
-        }
-    },
-    actions: {
-        storeInitialValue(model: typeof ApiResource, entity: ApiResource) {
-            if (!this.initialValues.has(model.entity)) {
-                this.initialValues.set(model.entity, new Map())
-            }
-
-            // @ts-ignore
-            this.initialValues.get(model.entity).set(entity.id, useCloneDeep(entity))
-
-            // TODO: voir si structuredClone est compatible avec les navigateurs supportés, et si ça vaut le coup
-            //       de l'utiliser à la place du clonedeep de lodash
-            //        >> https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
-        }
-    }
-})