Skip to content

Type Alias: MorphToConfiguration<OM, TM, PKT, FM, PKF, FKT>

ts
type MorphToConfiguration<OM, TM, PKT, FM, PKF, FKT> =
  | [typeof MorphTo, FM, FKT]
  | [typeof MorphTo, FM, FKT, PKT];

The configuration for a MorphTo relationship.

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 child (polymorphic) model
PKT extends StringKeyOf<OM[TM]>the property used as the primary key in the child model
FM extends StringKeyOf<OM>-
PKF extends StringKeyOf<OM[FM]>-
FKT extends StringKeyOf<OM[TM]>-

Type Param

the property on the child model storing the parent record's type

Type Param

the property on the child model storing the parent record's id

Example

ts
// A Comment or Image belongs to a parent of varying type (Post, Video, etc).
{
  models: {
    comment: {
      schema: '++id,body,commentable_id,commentable_type',
      properties: ['id', 'body', 'commentable_id', 'commentable_type'],
      primaryKey: 'id',
      relationships: {
        parent: [MorphTo, 'commentable_type', 'commentable_id'],
      },
    },
    image: {
      schema: '++id,url,imageable_id,imageable_type',
      properties: ['id', 'url', 'imageable_id', 'imageable_type'],
      primaryKey: 'id',
      relationships: {
        parent: [MorphTo, 'imageable_type', 'imageable_id'],
      },
    },
    post: {
      schema: '++id,title',
      properties: ['id', 'title'],
      primaryKey: 'id',
      relationships: {},
    },
    video: {
      schema: '++id,url',
      properties: ['id', 'url'],
      primaryKey: 'id',
      relationships: {},
    },
  }
}

Remarks

MorphTo is defined on the child, and uses the type and id keys to point to a parent of varying type.