Type Alias: MorphOneConfiguration<OM, TM, PKT, FT, PKF, FKID>
ts
type MorphOneConfiguration<OM, TM, PKT, FT, PKF, FKID> =
| [typeof MorphOne, FT, FKID]
| [typeof MorphOne, FT, FKID, PKT];The configuration for a MorphOne relationship.
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 parent model |
PKT extends StringKeyOf<OM[TM]> | the property used as the primary key in the parent model |
FT extends StringKeyOf<OM> | the table of the child (polymorphic) model |
PKF extends StringKeyOf<OM[FT]> | the property used as the primary key in the child model |
FKID extends StringKeyOf<OM[FT]> | the property on the child model storing the parent record's id |
Type Param
the property on the child model storing the parent record's type
Example
ts
// A Post or Video can have one Image (polymorphic). Each Image stores the parent type and id.
{
models: {
post: {
schema: '++id,title',
properties: ['id', 'title'],
primaryKey: 'id',
relationships: {
image: [MorphOne, 'image', 'imageable_id', 'imageable_type'],
},
},
video: {
schema: '++id,url',
properties: ['id', 'url'],
primaryKey: 'id',
relationships: {
image: [MorphOne, 'image', 'imageable_id', 'imageable_type'],
},
},
image: {
schema: '++id,url,imageable_id,imageable_type',
properties: ['id', 'url', 'imageable_id', 'imageable_type'],
primaryKey: 'id',
relationships: {},
},
}
}Remarks
MorphOne is defined on the parent, points to the child table, and the child table must have both a type and id column referencing the parent.