repository.ts 3.0 KB

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