repository.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {Collection, Item, Model, Repository as VuexRepository} from "@vuex-orm/core";
  2. import {$objectProperties} from "~/services/utils/objectProperties";
  3. import {AnyJson} from "~/types/interfaces";
  4. import {Store} from "vuex";
  5. import * as _ from "lodash";
  6. /**
  7. * @category Services/store
  8. * @class Repository
  9. * Classe Wrapper pour assurer les opérations les plus communes des Repository de VuexORM
  10. */
  11. class Repository{
  12. private store !: Store<any>
  13. /**
  14. * Set le store
  15. * @param {Store<any>} store
  16. */
  17. public setStore(store: Store<any>){
  18. this.store = store
  19. }
  20. /**
  21. * Récupération du repository du Model
  22. * @param {Model} model
  23. * @return {VuexRepository<Model>} le repository
  24. */
  25. public getRepository(model: typeof Model): VuexRepository<Model>{
  26. return this.store.$repo(model)
  27. }
  28. /**
  29. * Récupération du nom de l'entité du model
  30. * @param {Model} model
  31. * @return {string} l'entité
  32. */
  33. public getEntity(model: typeof Model): string{
  34. return this.getRepository(model).getModel().$entity()
  35. }
  36. /**
  37. * Persist l'entry dans le repository
  38. * @param {VuexRepository<Model>} repository
  39. * @param {AnyJson} entry
  40. */
  41. public persist(repository: VuexRepository<Model>, entry:AnyJson): void{
  42. if(_.isEmpty(entry))
  43. throw new Error('entry is empty')
  44. repository.save(entry)
  45. }
  46. /**
  47. * Effectue une mise à jour du store après la modification d'un champ de l'entry
  48. * @param {VuexRepository<Model>} repository
  49. * @param {AnyJson} entry
  50. * @param {any} value
  51. * @param {string} field
  52. */
  53. public updateStoreFromField(repository: VuexRepository<Model>, entry:AnyJson, value:any, field:string): void{
  54. if(!_.has(entry, field))
  55. throw new Error('field not found')
  56. entry[field] = value
  57. this.persist(repository, $objectProperties.cloneAndNest(entry))
  58. }
  59. /**
  60. * Récupération de l'Item du Model souhaité
  61. * @param {Model} model
  62. * @param {number} id
  63. * @return {Item} l'Item
  64. */
  65. public findItemFromModel(model: typeof Model, id:number): Item{
  66. const repository = this.getRepository(model)
  67. const item = repository.find(id)
  68. if(!item || typeof item === "undefined")
  69. throw new Error('Item not found')
  70. return item
  71. }
  72. /**
  73. * Récupération de la Collection du Model souhaité
  74. * @param {Model} model
  75. * @return {Collection} la collection
  76. */
  77. public findCollectionFromModel(model: typeof Model): Collection{
  78. const repository = this.getRepository(model)
  79. return repository.all()
  80. }
  81. /**
  82. * Supprime l'Item du repository
  83. * @param {Model} model
  84. * @param {number} id
  85. */
  86. public deleteItem(model: typeof Model, id: number){
  87. const repository = this.getRepository(model)
  88. repository.destroy(id)
  89. }
  90. /**
  91. * Supprime tous les Items du repository
  92. * @param {VuexRepository} repository
  93. */
  94. public cleanRepository(repository: VuexRepository){
  95. repository.flush()
  96. }
  97. }
  98. export const repositoryHelper = new Repository()