Skip to main content
zerotal

Documentation


Documentation / zerotal / orm / manyToMany

Function: manyToMany()

manyToMany(related, options): (_value, context) => void

Defined in: packages/orm/src/model/decorators/manyToMany.ts:43

Declare a many-to-many relation through a pivot table (e.g. UserRole via role_user). The relation property is a ManyToMany collection that behaves as an array yet also exposes pivot methods (attach / detach / sync / toggle).

Parameters

() => unknown

Lazy factory returning the related model class.

options

ManyToManyOptions

Pivot table and key configuration.

Returns

(_value, context) => void

Example

class User extends BaseModel {
  \@manyToMany(() => Role, {
    pivotTable: 'role_user',
    pivotForeignKey: 'user_id',
    pivotRelatedKey: 'role_id',
  })
  roles!: ManyToMany<Role>;
}