Class: MorphTo<OM, TM, PKT, FM, PKF, FKT>
Represents a relationship between two models where the table of the foreign model and the ID of the foreign model is stored as a property of the originating model.
Description
For example, a task has a single owner which can be either a user or a group:
In your configuration you would have:
typescript
{
...
models: {
tasks: {
schema: '++id, owner_type, owner_id, title, description, createdAt, updatedAt',
properties: ['id', 'owner_type', 'owner_id', 'title', 'description', 'createdAt', 'updatedAt'],
primaryKey: 'id',
relationships: {
owner: [MorphTo, 'owner_type', 'owner_id'],
}
},
users: {
schema: '++id, email, createdAt, updatedAt',
properties: ['id', 'email', 'password', 'createdAt', 'updatedAt'],
primaryKey: 'id',
},
groups: {
schema: '++id, email, createdAt, updatedAt',
properties: ['id', 'email', 'password', 'createdAt', 'updatedAt'],
primaryKey: 'id',
},
},
...
}Which will then allow you to access the owner of a task from the owner accessor on the task model: task.owner.
Remarks
The inverse of this relationship can be either MorphOne or MorphMany relationship. Configured using the MorphToConfiguration tuple.
Extends
RelationshipBase<ReactiveModel<OM[FM],PKF,any>,OM,TM,PKT,FM,PKF,OM[TM],OM[FM]>
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 property used as the foreign model name in the originating model |
PKF extends StringKeyOf<OM[FM]> | the property used as the primary key in the foreign model |
FKT extends StringKeyOf<OM[TM]> | the property used as the foreign key in the originating model |