Class: ManyToMany<OM, TM, PKT, JM, JPKT, JPKF, FM, PKF>
Represents a many-to-many relationship between models where the IDs of both models are stored in a join table (pivot table).
Description
For example, a user has many skills, and a skill can be associated with many users.
In your configuration you would have:
typescript
{
...
models: {
users: {
schema: '++id, email, createdAt, updatedAt',
properties: ['id', 'email', 'password'],
primaryKey: 'id',
relationships: {
skills: [ManyToMany, 'skills', 'user_skills', 'user_id', 'skill_id'],
}
},
skills: {
schema: '++id, name',
properties: ['id', 'name'],
relationships: {
users: [ManyToMany, 'users'],
}
}
user_skills: {
schema: '++id, user_id, skill_id, notes',
properties: ['id', 'user_id', 'skill_id', 'notes'],
}
},
...
}Which will then allow you to access :
- all of the user's related skill using the
skillsaccessor on theusermodel:user.skills. - all of the skills's related users using the
usersaccessor on theskillmodel:skill.users.
Remarks
Configured using the ManyToManyConfiguration tuple.
Extends
HasManyThrough<OM,TM,PKT,FM,PKF,JM,JPKT,JPKF>
Type Parameters
| Type Parameter | Description |
|---|---|
OM extends Record<string, PlainObject> | the map of all models in the database |
TM extends StringKeyOf<OM> | the table of the originating model |
PKT extends StringKeyOf<OM[TM]> | the property used as the primary key in the originating model |
JM extends StringKeyOf<OM> | the table of the join model |
JPKT extends StringKeyOf<OM[JM]> | the property used as the foreign key in the join model that references the originating model |
JPKF extends StringKeyOf<OM[JM]> | the property used as the foreign key in the join model that references the foreign model |
FM extends StringKeyOf<OM> | the table of the foreign model |
PKF extends StringKeyOf<OM[FM]> | the property used as the primary key in the foreign model |
Constructors
Constructor
ts
new ManyToMany<OM, TM, PKT, JM, JPKT, JPKF, FM, PKF>(
key: string,
swarm: UnifiedEventBus,
ownerTable: TM,
ownerPK: PKT,
pivotTable: JM,
pivotOwnerFK: JPKT,
pivotTargetFK: JPKF,
targetTable: FM,
targetPK: PKF): ManyToMany<OM, TM, PKT, JM, JPKT, JPKF, FM, PKF>;Parameters
| Parameter | Type |
|---|---|
key | string |
swarm | UnifiedEventBus |
ownerTable | TM |
ownerPK | PKT |
pivotTable | JM |
pivotOwnerFK | JPKT |
pivotTargetFK | JPKF |
targetTable | FM |
targetPK | PKF |
Returns
ManyToMany<OM, TM, PKT, JM, JPKT, JPKF, FM, PKF>
Overrides
ts
HasManyThrough<OM, TM, PKT, FM, PKF, JM, JPKT, JPKF>.constructorMethods
prepare()
ts
prepare(
host: any,
emitter: any,
onChangeDetected?: () => void): Promise<any[]>;Parameters
| Parameter | Type |
|---|---|
host | any |
emitter | any |
onChangeDetected? | () => void |
Returns
Promise<any[]>
Overrides
ts
HasManyThrough.prepare;unref()
ts
unref(): Promise<void>;Returns
Promise<void>
Overrides
ts
HasManyThrough.unref;