orm.ts 1.1 KB

12345678910111213141516171819202122232425262728
  1. import {ormState} from '~/types/interfaces'
  2. import {defineStore} from "pinia";
  3. import ApiResource from "~/models/ApiResource";
  4. export const useOrmStore = defineStore('orm', {
  5. state: (): ormState => {
  6. return {
  7. /**
  8. * Store the initial value of the entities, just after the fetch, to make reinitialization possible
  9. */
  10. initialValues: new Map<string, Map<number, ApiResource>>()
  11. }
  12. },
  13. actions: {
  14. storeInitialValue(model: typeof ApiResource, entity: ApiResource) {
  15. if (!this.initialValues.has(model.entity)) {
  16. this.initialValues.set(model.entity, new Map())
  17. }
  18. // @ts-ignore
  19. this.initialValues.get(model.entity).set(entity.id, useCloneDeep(entity))
  20. // TODO: voir si structuredClone est compatible avec les navigateurs supportés, et si ça vaut le coup
  21. // de l'utiliser à la place du clonedeep de lodash
  22. // >> https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
  23. }
  24. }
  25. })