toJSON: Hide ’pivot" but extract some pivot attrs as instance attrs


#1

Imagine, I have a model users and groups with manyToMany relation. Connecting table has an additional attribute role, defining user’s role in each group:

groups() {
    return this.belongsToMany('App/Models/Group', 'userId', 'groupId')
       .pivotModel(UserGroup)
       .withPivot(['role'])
  }

When requesting user.groups().fetch() ... toJSON(), I would like to get

{
  name: 'John',
  role: 'customer'
}

but currently Adonis 4.0 produces only:

{
  name: 'John',
  pivot: { userId: 1, groupId: 2, role: 'customer' }
}

In Adonis 3.x I was able to set computed property role and then:

static get computed() {
    return ['role']
  }

  getRole() {
    return this._pivot_role
  }

  static get hidden() {
    return ['_pivot_userId', '_pivot_groupId', '_pivot_role']
  }

To get the desired result. However, it appears to be that Adonis 4.0 does not pass pivot props into getter functions and ignores pivot in hidden getter. So

// This works but looks real dirty
getRole(ctx) {
    return this.$relations.pivot.$attributes.role
  }

// This does not work at all
  static get hidden() {
    return ['pivot']
  }

What would be the best way to hide pivot key from JSON output of the instance and to extract role right into attrs instead?