repository.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. public createNewModelInstance(model: typeof Model):Model{
  29. return this.getRepository(model).make()
  30. }
  31. /**
  32. * Récupération du nom de l'entité du model
  33. * @param {Model} model
  34. * @return {string} l'entité
  35. */
  36. public getEntity(model: typeof Model): string{
  37. return this.getRepository(model).getModel().$entity()
  38. }
  39. /**
  40. * Persist l'entry dans le repository
  41. * @param {Model} model
  42. * @param {AnyJson} entry
  43. */
  44. public persist(model: typeof Model, entry:AnyJson): void{
  45. if(_.isEmpty(entry))
  46. throw new Error('entry is empty')
  47. this.getRepository(model).save(entry)
  48. }
  49. /**
  50. * Effectue une mise à jour du store après la modification d'un champ de l'entry
  51. * @param {Model} model
  52. * @param {AnyJson} entry
  53. * @param {any} value
  54. * @param {string} field
  55. */
  56. public updateStoreFromField(model: typeof Model, entry:AnyJson, value:any, field:string): void{
  57. if(!_.has(entry, field))
  58. throw new Error('field not found')
  59. entry[field] = value
  60. this.persist(model, $objectProperties.cloneAndNest(entry))
  61. }
  62. /**
  63. * Récupération de l'Item du Model souhaité
  64. * @param {Model} model
  65. * @param {number} id
  66. * @return {Item} l'Item
  67. */
  68. public findItemFromModel(model: typeof Model, id:number): Item{
  69. const repository = this.getRepository(model)
  70. const item = repository.find(id)
  71. if(!item || typeof item === "undefined")
  72. throw new Error('Item not found')
  73. return item
  74. }
  75. /**
  76. * Récupération de la Collection du Model souhaité
  77. * @param {Model} model
  78. * @return {Collection} la collection
  79. */
  80. public findCollectionFromModel(model: typeof Model): Collection{
  81. const repository = this.getRepository(model)
  82. return repository.all()
  83. }
  84. /**
  85. * Supprime l'Item du repository
  86. * @param {Model} model
  87. * @param {number} id
  88. */
  89. public deleteItem(model: typeof Model, id: number){
  90. const repository = this.getRepository(model)
  91. repository.destroy(id)
  92. }
  93. /**
  94. * Supprime tous les Items du repository
  95. * @param {VuexRepository} repository
  96. */
  97. public cleanRepository(repository: VuexRepository){
  98. repository.flush()
  99. }
  100. }
  101. export const repositoryHelper = new Repository()