Skip to main content
Version: Next

EntitySchema <Entity, Base, Class>

Class-less entity definition that provides a programmatic API for defining entities without decorators.

Hierarchy

Index

Constructors

constructor

  • new EntitySchema<Entity, Base, Class>(meta): EntitySchema<Entity, Base, Class>

Properties

staticREGISTRY

REGISTRY: Map<Partial<any>, EntitySchema<any, never, EntityCtor<any>>> = ...

When schema links the entity class via class option, this registry allows the lookup from opposite side, so we can use the class in entities option just like the EntitySchema instance.

Accessors

class

  • get class(): Class
  • Returns Class

meta

  • Returns the underlying EntityMetadata.


    Returns EntityMetadata<Entity, Class>

name

  • Returns the entity class name.


    Returns string | EntityName<Entity>

properties

  • get properties(): Record<string, any>
  • Returns Record<string, any>

tableName

  • get tableName(): string
  • Returns the database table name.


    Returns string

Methods

addEmbedded

  • addEmbedded<Target>(name, options): void
  • Adds an embedded property to the entity schema.


    Parameters

    Returns void

addEnum

  • addEnum(name, type, options): void
  • Adds an enum property to the entity schema.


    Parameters

    Returns void

addHook

  • addHook<T>(event, handler): this
  • Adds a lifecycle hook handler to the entity schema. This method allows registering hooks after the entity is defined, which can be useful for avoiding circular type references.

    @example
    export const Article = defineEntity({
    name: 'Article',
    properties: { ... },
    });

    Article.addHook('beforeCreate', async args => {
    args.entity.slug = args.entity.title.toLowerCase();
    });

    Parameters

    • event: EventType | onInit | onLoad | beforeCreate | afterCreate | beforeUpdate | afterUpdate | beforeUpsert | afterUpsert | beforeDelete | afterDelete | beforeFlush | onFlush | afterFlush | beforeTransactionStart | afterTransactionStart | beforeTransactionCommit | afterTransactionCommit | beforeTransactionRollback | afterTransactionRollback
    • handler: (args) => void | Promise<void>

      Returns this

    addIndex

    • addIndex<Key>(options): void
    • Adds an index definition to the entity schema.


      Parameters

      Returns void

    addManyToMany

    • addManyToMany<Target>(name, type, options): void
    • Adds a many-to-many relation to the entity schema.


      Parameters

      Returns void

    addManyToOne

    • addManyToOne<Target>(name, type, options): void
    • Adds a many-to-one relation to the entity schema.


      Parameters

      Returns void

    addOneToMany

    • addOneToMany<Target>(name, type, options): void
    • Adds a one-to-many relation to the entity schema.


      Parameters

      Returns void

    addOneToOne

    • addOneToOne<Target>(name, type, options): void
    • Adds a one-to-one relation to the entity schema.


      Parameters

      Returns void

    addPrimaryKey

    • addPrimaryKey(name, type, options): void
    • Adds a primary key property to the entity schema.


      Parameters

      Returns void

    addProperty

    • addProperty(name, type, options): void

    addSerializedPrimaryKey

    • addSerializedPrimaryKey(name, type, options): void

    addUnique

    • addUnique<Key>(options): void
    • Adds a unique constraint definition to the entity schema.


      Parameters

      Returns void

    addVersion

    • addVersion(name, type, options): void
    • Adds a version property for optimistic locking.


      Parameters

      Returns void

    new

    • new(...params): Entity
    • Parameters

      • rest...params: ConstructorParameters<Class>

      Returns Entity

    setClass

    • setClass(cls): void
    • Sets or replaces the entity class associated with this schema.


      Parameters

      • cls: Class

      Returns void

    setCustomRepository

    • setCustomRepository(repository): void
    • Sets a custom repository class for this entity.


      Parameters

      Returns void

    setExtends

    • setExtends(base): void
    • Sets the base entity that this schema extends.


      Parameters

      Returns void

    staticfromMetadata

    staticis

    • Checks if the given value is an EntitySchema instance, using duck-typing as a fallback when instanceof fails due to CJS/ESM dual-package hazard (e.g. when using tsx or @swc-node/register with "type": "commonjs" projects).


      Parameters

      • item: unknown

      Returns item is EntitySchema<any, never, EntityCtor<any>>