Search code examples
javascriptvue.jsvuex

Vuex ORM Many-to-Many Relationship with pivot table with additional attributes


i have a table: User, Role and the table has a many-to-many relationship with table pivots with additional attributes (approved, priority)

  • User(id, name)
  • Role(id, name)
  • RoleUser(user_id, role_id, approved, priority).

How do I enter data, update data, retrieve data, and deleting data inside pivot tables (RoleUser) using vuex-orm?

my code looks like this:

class User extends Model {
    static entity = 'users'

    static fields() {
        return {
            id: this.attr(null),
            name: this.attr(''),
            roles: this.belongsToMany(Role, RoleUser, 'user_id', 'role_id')
        }
    }
}

class Role extends Model {
    static entity = 'roles'

    static fields() {
        return {
            id: this.attr(null),
            name: this.attr(''),
            users: this.belongsToMany(User, RoleUser, 'role_id', 'user_id')
        }
    }
}

class RoleUser extends Model {
    static entity = 'roleUser'

    static primaryKey = ['role_id', 'user_id']

    static fields() {
        return {
            role_id: this.attr(null),
            user_id: this.attr(null),
            approved: this.attr(''),
            priority: this.attr(''),
        }
    }
}

Thank you in advance.


Solution

  • currently not yet implemented (when this answer is made).

    current version: vuex-orm 0.31.7

    open issue: [FEATURE REQUEST] - Access to fields in pivot model

    thanks you for Kia King Ishii for the time and response given (via slack)