Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store.$db is not a function #21

Open
Marchiuzzz opened this issue May 19, 2021 · 1 comment
Open

store.$db is not a function #21

Marchiuzzz opened this issue May 19, 2021 · 1 comment

Comments

@Marchiuzzz
Copy link

I've tried using let results = this.$store.getters['entities/allDirty']();, however I get store.$db is not a function error, what could be wrong?

"@vuex-orm/core": "0.36.1",
"@vuex-orm/plugin-change-flags": "^1.2.3",

@zorn-v
Copy link

zorn-v commented Aug 2, 2021

Those getters like a crap. If you need something like Model.allDirty() - do not use them, and forgot about it existence.
Better to make base class wth allDirty static method and extends from it.
Something like

//BaseModel
import { Model } from '@vuex-orm/core'

export default class Base extends Model {
  static fetchAll() {
    return this.api().get(this.entity)
  }

  static getModified() {
    function clearServiceFields(ent) {
      Object.keys(ent).forEach(key => {
        if (key[0] === '$') {
          delete ent[key]
        }
      })
      return ent
    }
    const created = this.query().where('$isNew', true).get().map(clearServiceFields)
    const updated = this.query().where(rec => {
      return rec.$isDirty && !rec.$isNew && !rec.$trashed()
    }).get().map(clearServiceFields)
    const deleted = this.query().onlyTrashed().get().map(clearServiceFields)

    if (created.length || updated.length || deleted.length) {
      return {created, updated, deleted}
    }

    return null
  }

  static saveAll() {
    const modified = this.getModified()
    if (modified) {
      return this.api().post(this.entity, modified, {save: false})
    }
    return Promise.resolve()
  }
}

Actual model

import BaseModel from './BaseModel'
import { v4 as uuid4 } from 'uuid'

export default class Project extends BaseModel {
  static entity = 'projects'
  static primaryKey = '_id'

  static fields () {
    return {
      _id: this.uid(() => uuid4()),
      title: this.string(''),
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants