Libraries
    Preparing search index...

    Class Entity<TSchema, TRels, TVariantUnion>

    A typed wrapper around an ObjectSchemaBuilder that tracks declared relations in its TRels generic. Use defineEntity to create one.

    Relations are declared via .hasOne() / .hasMany() / .belongsTo() / .belongsToMany() on the entity (NOT on the underlying schema). Each call returns a new Entity whose TRels includes the new relation, so query(db, entity).include(t => t.assignee) is fully typed.

    Type Parameters

    • TSchema extends ObjectSchemaBuilder<any, any, any, any, any, any, any>
    • TRels extends Record<string, RelationInfo> = {}
    • TVariantUnion = never
    Index

    Constructors

    Properties

    schema: TSchema

    The underlying schema (relations registered via withExtension('relations', ...)).

    Methods

    • Declare a CTI (Class Table Inheritance) variant. Only valid after .discriminator(). Returns a new Entity whose schema carries the updated 'variants' extension.

      Type Parameters

      • TVarSchema extends ObjectSchemaBuilder<any, any, any, any, any, any, any>

      Parameters

      • key: string

        Discriminator literal (e.g. 'image').

      • variant: Entity<TVarSchema, any>

        Entity wrapping the variant table schema.

      • fkSel: (t: EntityTree<TVarSchema>) => any

        Accessor returning the FK property on the variant schema that joins back to the base PK.

      • Optionalopts: {
            allowOrphan?: boolean;
            relations?: Record<string, VariantRelationInput<TVarSchema>>;
        }

        { allowOrphan?: boolean; relations?: ... } — see CTI docs.

      Returns Entity<TSchema, TRels, TVariantUnion | VariantBranch<TSchema, TVarSchema>>

    • Mark this entity as polymorphic by selecting the discriminator property. Returns a new Entity whose .ctiVariant() / .stiVariant() methods declare each variant.

      Declare ordinary relations (.belongsTo() etc.) on this entity BEFORE .discriminator(). After .discriminator() you are in the polymorphic-builder phase: .hasOne/.hasMany/.belongsTo/.belongsToMany still work, but the per-variant relations live on the .ctiVariant() / .stiVariant() calls.

      Type Parameters

      • TKey extends string

      Parameters

      Returns Entity<TSchema, TRels, never>

      const FileEntity = defineEntity(FileSchema)
      .discriminator(t => t.type)
      .ctiVariant('image', ImageEntity, t => t.fileId)
      .ctiVariant('document', DocumentEntity, t => t.fileId);