| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import { Collection, Item, Model, Repository as VuexRepository } from '@vuex-orm/core'
- import { Store } from 'vuex'
- import * as _ from 'lodash'
- import { $objectProperties } from '~/services/utils/objectProperties'
- import { AnyJson } from '~/types/interfaces'
- /**
- * @category Services/store
- * @class Repository
- * Classe Wrapper pour assurer les opérations les plus communes des Repository de VuexORM
- */
- class Repository {
- private store !: Store<any>
- /**
- * Set le store
- * @param {Store<any>} store
- */
- public setStore (store: Store<any>) {
- this.store = store
- }
- /**
- * Récupération du repository du Model
- * @param {Model} model
- * @return {VuexRepository<Model>} le repository
- */
- public getRepository (model: typeof Model): VuexRepository<Model> {
- return this.store.$repo(model)
- }
- public createNewModelInstance (model: typeof Model):Model {
- return this.getRepository(model).make()
- }
- /**
- * Récupération du nom de l'entité du model
- * @param {Model} model
- * @return {string} l'entité
- */
- public getEntity (model: typeof Model): string {
- return this.getRepository(model).getModel().$entity()
- }
- /**
- * Persist l'entry dans le repository
- * @param {Model} model
- * @param {AnyJson} entry
- */
- public persist (model: typeof Model, entry:AnyJson): void {
- if (_.isEmpty(entry)) { throw new Error('entry is empty') }
- this.getRepository(model).save(entry)
- }
- /**
- * Effectue une mise à jour du store après la modification d'un champ de l'entry
- * @param {Model} model
- * @param {AnyJson} entry
- * @param {any} value
- * @param {string} field
- */
- public updateStoreFromField (model: typeof Model, entry:AnyJson, value:any, field:string): void {
- if (!_.has(entry, field)) { throw new Error('field not found') }
- entry[field] = value
- this.persist(model, $objectProperties.cloneAndNest(entry))
- }
- /**
- * Récupération de l'Item du Model souhaité
- * @param {Model} model
- * @param {number} id
- * @return {Item} l'Item
- */
- public findItemFromModel (model: typeof Model, id:number): Item {
- const repository = this.getRepository(model)
- const item = repository.find(id)
- if (!item || typeof item === 'undefined') { throw new Error('Item not found') }
- return item
- }
- /**
- * Récupération de la Collection du Model souhaité
- * @param {Model} model
- * @return {Collection} la collection
- */
- public findCollectionFromModel (model: typeof Model): Collection {
- const repository = this.getRepository(model)
- return repository.all()
- }
- /**
- * Supprime l'Item du repository
- * @param {Model} model
- * @param {number} id
- */
- public deleteItem (model: typeof Model, id: number) {
- const repository = this.getRepository(model)
- repository.destroy(id)
- }
- /**
- * Supprime tous les Items du repository
- * @param {VuexRepository} repository
- */
- public cleanRepository (repository: VuexRepository) {
- repository.flush()
- }
- }
- export const repositoryHelper = new Repository()
|