repository.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { Collection, Item, Model, Repository as VuexRepository } from '@vuex-orm/core'
  2. import { Store } from 'vuex'
  3. import * as _ from 'lodash'
  4. import { $objectProperties } from '~/services/utils/objectProperties'
  5. import { AnyJson } from '~/types/interfaces'
  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)) { throw new Error('entry is empty') }
  46. this.getRepository(model).save(entry)
  47. }
  48. /**
  49. * Effectue une mise à jour du store après la modification d'un champ de l'entry
  50. * @param {Model} model
  51. * @param {AnyJson} entry
  52. * @param {any} value
  53. * @param {string} field
  54. */
  55. public updateStoreFromField (model: typeof Model, entry:AnyJson, value:any, field:string): void {
  56. if (!_.has(entry, field)) { throw new Error('field not found') }
  57. entry[field] = value
  58. this.persist(model, $objectProperties.cloneAndNest(entry))
  59. }
  60. /**
  61. * Récupération de l'Item du Model souhaité
  62. * @param {Model} model
  63. * @param {number} id
  64. * @return {Item} l'Item
  65. */
  66. public findItemFromModel (model: typeof Model, id:number): Item {
  67. const repository = this.getRepository(model)
  68. const item = repository.find(id)
  69. if (!item || typeof item === 'undefined') { 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()