Class: MorphOne<OM, TM, PKT, FT, PKF, FKID>
Represents a relationship between two models where the table and primary key of the foreign model stored as a property of the originating model.
Description
For example, a user
has a single contactMethod
which can be either an email
or a phone_number
:
In your configuration you would have:
typescript
{
...
models: {
users: {
schema: '++id, contact_method_type, contact_method_id, password, createdAt, updatedAt',
properties: ['id', 'contact_method_type', 'contact_method_id', 'password', 'createdAt', 'updatedAt'],
primaryKey: 'id',
relationships: {
contactMethod: [MorphOne, 'contact_method_type', 'contact_method_id'],
}
},
emails: {
schema: '++id, address',
properties: ['id', 'address'],
primaryKey: 'id',
},
phone_numbers: {
schema: '++id, number',
properties: ['id', 'number'],
primaryKey: 'id',
},
}
Which will then allow you to access the contact method of a user from the contactMethod
accessor on the user
model: user.contactMethod
.
Remarks
The inverse of this relationship is the MorphTo relationship. Configured using the MorphOneConfiguration tuple.
Extends
RelationshipBase
<ReactiveModel
<OM
[FT
],PKF
,any
>,OM
,TM
,PKT
,FT
,PKF
,OM
[TM
],OM
[FT
]>
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 host model |
PKT extends StringKeyOf <OM [TM ]> | the property used as the primary key in the host model |
FT extends StringKeyOf <OM > | the table of the target model |
PKF extends StringKeyOf <OM [FT ]> | the property used as the primary key in the target model |
FKID extends StringKeyOf <OM [FT ]> | the property on the target model storing the host record's id |