repository.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. import {OrderByVuexOrm} from "~/types/types";
  7. /**
  8. * Classe Wrapper pour assurer les opérations les plus communes des Repository de VuexORM
  9. */
  10. class Repository {
  11. private store !: Store<any>
  12. /**
  13. * Set le store
  14. *
  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. *
  23. * @param {Model} model
  24. * @return {VuexRepository<Model>} le repository
  25. */
  26. public getRepository (model: typeof Model): VuexRepository<Model> {
  27. return this.store.$repo(model)
  28. }
  29. public createNewModelInstance (model: typeof Model): Model {
  30. return this.getRepository(model).make()
  31. }
  32. /**
  33. * Récupération du nom de l'entité du model
  34. *
  35. * @param {Model} model
  36. * @return {string} l'entité
  37. */
  38. public getEntity (model: typeof Model): string {
  39. return this.getRepository(model).getModel().$entity()
  40. }
  41. /**
  42. * Créer une entry dans le repository
  43. *
  44. * @param {Model} model
  45. * @param {AnyJson} entry
  46. */
  47. public make (model: typeof Model, entry?: AnyJson): Model {
  48. return this.getRepository(model).make(entry)
  49. }
  50. /**
  51. * Persist l'entry dans le repository
  52. *
  53. * @param {Model} model
  54. * @param {AnyJson} entry
  55. */
  56. public persist (model: typeof Model, entry: AnyJson): Model {
  57. if (_.isEmpty(entry)) { throw new Error('entry is empty') }
  58. return this.getRepository(model).save(entry)
  59. }
  60. /**
  61. * Effectue une mise à jour du store après la modification d'un champ de l'entry
  62. *
  63. * @param {Model} model
  64. * @param {AnyJson} entry
  65. * @param {any} value
  66. * @param {string} field
  67. */
  68. public updateStoreFromField (model: typeof Model, entry: AnyJson, value: any, field: string): void {
  69. if (!_.has(entry, field)) { throw new Error('field not found') }
  70. entry[field] = value
  71. this.persist(model, $objectProperties.cloneAndNest(entry))
  72. }
  73. /**
  74. * Récupération de l'Item du Model souhaité
  75. *
  76. * @param {Model} model
  77. * @param {number} id
  78. * @return {Item} l'Item
  79. */
  80. public findItemFromModel (model: typeof Model, id: number): Item {
  81. const repository = this.getRepository(model)
  82. const item = repository.find(id)
  83. if (!item || typeof item === 'undefined') { throw new Error('Item not found') }
  84. return item
  85. }
  86. /**
  87. * Récupération de la Collection du Model souhaité
  88. *
  89. * @param {Model} model
  90. * @param {OrderByVuexOrm} orderBy
  91. * @return {Collection} la collection
  92. */
  93. public findCollectionFromModel (model: typeof Model, orderBy?: OrderByVuexOrm): Collection {
  94. const repository = this.getRepository(model)
  95. if(orderBy){
  96. for(const orderKey in orderBy){
  97. repository.orderBy(orderKey, orderBy[orderKey])
  98. }
  99. }
  100. return repository.all()
  101. }
  102. /**
  103. * Supprime l'Item du repository
  104. *
  105. * @param {Model} model
  106. * @param {number} id
  107. */
  108. public deleteItem (model: typeof Model, id: number) {
  109. const repository = this.getRepository(model)
  110. repository.destroy(id)
  111. }
  112. /**
  113. * Supprime tous les Items du repository
  114. *
  115. * @param {VuexRepository} repository
  116. */
  117. public cleanRepository (repository: VuexRepository) {
  118. repository.flush()
  119. }
  120. }
  121. export const repositoryHelper = new Repository()