belongsToMany

Usage

import { Model } from 'pinia-orm'
import Role from './Role'
import RoleUser from './RoleUser'
class User extends Model {
  static entity = 'users'
  static fields () {
    return {
      id: this.attr(null),
      roles: this.belongsToMany(Role, RoleUser, 'user_id', 'role_id')
    }
  }
}

With Decorator

import { Model } from 'pinia-orm'
import { Attr, BelongsToMany, Str } from 'pinia-orm/decorators'
import Role from './Role'
import RoleUser from './RoleUser'
class User extends Model {
  static entity = 'users'
  
  @Attr(null) declare id: number | null
  @BelongsToMany(() => Role, () => RoleUser, 'user_id', 'role_id') declare roles: Role[]
}

Typescript Declarations

function belongsToMany(
  related: typeof Model,
  pivot: typeof Model,
  foreignPivotKey: string,
  relatedPivotKey: string,
  parentKey?: string,
  relatedKey?: string,
): BelongsToMany