Skip to content

Type Alias: MorphManyConfiguration<OM, TM, PKT, FT, PKF, FKID>

ts
type MorphManyConfiguration<OM, TM, PKT, FT, PKF, FKID> =
  | [typeof MorphMany, FT, FKID]
  | [typeof MorphMany, FT, FKID, PKT];

The configuration for a MorphMany 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 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 many Comments (polymorphic). Each Comment stores the parent type and id.
{
  models: {
    post: {
      schema: '++id,title',
      properties: ['id', 'title'],
      primaryKey: 'id',
      relationships: {
        comments: [MorphMany, 'comment', 'commentable_id', 'commentable_type'],
      },
    },
    video: {
      schema: '++id,url',
      properties: ['id', 'url'],
      primaryKey: 'id',
      relationships: {
        comments: [MorphMany, 'comment', 'commentable_id', 'commentable_type'],
      },
    },
    comment: {
      schema: '++id,body,commentable_id,commentable_type',
      properties: ['id', 'body', 'commentable_id', 'commentable_type'],
      primaryKey: 'id',
      relationships: {},
    },
  }
}

Remarks

MorphMany 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.