Class: HasManyThrough<OM, TM, PKT, FM, PKF, JM, JPKT, JPKF>
Represents a one-to-many relationship between models where the ID of the originating model is stored as a property of one or many "glue" models which connect to the foreign model.
Description
For example, a user has many posts, which has many comments which has many users:
In your configuration you would have:
typescript
{
...
models: {
users: {
schema: '++id, email, createdAt, updatedAt',
properties: ['id', 'email', 'password'],
primaryKey: 'id',
relationships: {
posts: [HasMany, 'posts', 'user_id'],
comments: [HasMany, 'comments', 'user_id'],
commentors: [HasManyThrough, [
[HasMany, 'posts', 'user_id'],
[HasMany, 'comments', 'post_id'],
[BelongsTo, 'users', 'user_id'],
]]
}
},
posts: {
schema: '++id, user_id, title, body',
properties: ['id', 'user_id', 'title', 'body'],
relationships: {
comments: [HasMany, 'comments', 'post_id'],
}
}
comments: {
schema: '++id, post_id, user_id, body',
properties: ['id', 'post_id', 'user_id', 'body'],
relationships: {
user: [BelongsTo, 'users', 'user_id'],
}
}
},
...
}Which will then allow you to access all of the user's related commentors using the commentors accessor on the user model: user.commentors.
Remarks
Configured using the HasManyThroughConfiguration tuple.
Extends
RelationshipBase<ReactiveModel<OM[FM],PKF,any>[],OM,TM,PKT,FM,PKF,OM[TM],OM[FM]>
Extended by
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 |
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 |
JM extends StringKeyOf<OM> | - |
JPKT extends StringKeyOf<OM[JM]> | - |
JPKF extends StringKeyOf<OM[JM]> | - |