vuex-orm / vuex-orm

The Vuex plugin to enable Object-Relational Mapping access to the Vuex Store.
https://vuex-orm.org
MIT License
2.36k stars 165 forks source link

[Feature] .withRecursive() for models with self associations #537

Open SzNagyMisu opened 4 years ago

SzNagyMisu commented 4 years ago

I have a model like the following one:

class Person extends Model {
  static entity = 'people'
  static fields () {
    return {
      id: this.attr(),
      name: this.string(),
      parentId: this.string(),
      parent: this.belongsTo(Person, 'parentId')
    }
  }
}

And I need a getter iterating through the ancestors of one person:

get fullName () {
  return this.parent ? `${this.name} son of ${this.parent.fullName}` : this.name
}

Now, vuex-orm has a method: withAllRecursive, but I have two reasons not to use it for this scenario:

  1. Say, Person model has other relations, but I only need this one
  2. withAllRecursive goes only to a specified depth (defaulting to 3) but I don't know how many ancestors there are for a given Person.

I suggest a new query method withRecursive with the following usage:

Person.query().withRecursive('parent').find(123).fullName

Any thoughts on this?

kiaking commented 4 years ago

Sorry for the late response! Yeah this makes sense. Let's mark this as an enhancement issue 👍