Skip to content

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 skills accessor on the user model: user.skills.
  • all of the skills's related users using the users accessor on the skill model: skill.users.

Remarks

Configured using the ManyToManyConfiguration tuple.

Extends

Type Parameters

Type ParameterDescription
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

ParameterType
keystring
swarmUnifiedEventBus
ownerTableTM
ownerPKPKT
pivotTableJM
pivotOwnerFKJPKT
pivotTargetFKJPKF
targetTableFM
targetPKPKF

Returns

ManyToMany<OM, TM, PKT, JM, JPKT, JPKF, FM, PKF>

Overrides

ts
HasManyThrough<OM, TM, PKT, FM, PKF, JM, JPKT, JPKF>.constructor

Methods

prepare()

ts
prepare(
   host: any,
   emitter: any,
onChangeDetected?: () => void): Promise<any[]>;

Parameters

ParameterType
hostany
emitterany
onChangeDetected?() => void

Returns

Promise<any[]>

Overrides

ts
HasManyThrough.prepare;