import { EntityMetadata, type AnyEntity, type EntityKey, type Constructor, type DeepPartial, type EntityName, type EntityProperty, type CleanKeys, type ExpandProperty, type IsNever, type EntityCtor } from '../typings.js'; import { type EventType, ReferenceKind } from '../enums.js'; import type { EventArgs } from '../events/EventSubscriber.js'; import { Type } from '../types/Type.js'; import type { PropertyOptions, ManyToOneOptions, OneToOneOptions, OneToManyOptions, ManyToManyOptions, EmbeddedOptions, EnumOptions, PrimaryKeyOptions, SerializedPrimaryKeyOptions, IndexOptions, UniqueOptions } from './types.js'; type TypeType = string | NumberConstructor | StringConstructor | BooleanConstructor | DateConstructor | ArrayConstructor | Constructor> | Type; type TypeDef = { type: TypeType; } | { entity: () => EntityName | EntityName[]; }; type EmbeddedTypeDef = { type: TypeType; } | { entity: () => EntityName | EntityName[]; }; /** Union type representing all possible property definition shapes in an EntitySchema. */ export type EntitySchemaProperty = ({ kind: ReferenceKind.MANY_TO_ONE | 'm:1'; } & TypeDef & ManyToOneOptions) | ({ kind: ReferenceKind.ONE_TO_ONE | '1:1'; } & TypeDef & OneToOneOptions) | ({ kind: ReferenceKind.ONE_TO_MANY | '1:m'; } & TypeDef & OneToManyOptions) | ({ kind: ReferenceKind.MANY_TO_MANY | 'm:n'; } & TypeDef & ManyToManyOptions) | ({ kind: ReferenceKind.EMBEDDED | 'embedded'; } & EmbeddedTypeDef & EmbeddedOptions & PropertyOptions) | ({ enum: true; } & EnumOptions) | (TypeDef & PropertyOptions); type OmitBaseProps = IsNever extends true ? Entity : Omit; /** Configuration object for defining an entity via EntitySchema. */ export type EntitySchemaMetadata> = Omit>, 'name' | 'properties' | 'extends'> & ({ name: string; } | { class: Class; name?: string; }) & { extends?: EntityName; } & { properties?: { [Key in keyof OmitBaseProps as CleanKeys, Key>]-?: EntitySchemaProperty>, Entity>; }; } & { inheritance?: 'tpt'; }; /** Class-less entity definition that provides a programmatic API for defining entities without decorators. */ export declare class EntitySchema> { /** * 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. * * Stored on `globalThis` via `Symbol.for` to survive the CJS/ESM dual-package hazard * (e.g. when `tsx` loads the same package in both module systems). */ static get REGISTRY(): Map; /** @internal Type-level marker for fast entity type inference */ readonly '~entity': Entity; private readonly _meta; private internal; private initialized; constructor(meta: EntitySchemaMetadata); /** * 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). */ static is(item: unknown): item is EntitySchema; /** Creates an EntitySchema from existing EntityMetadata (used internally). */ static fromMetadata(meta: EntityMetadata | DeepPartial>): EntitySchema; /** Adds a scalar property to the entity schema. */ addProperty(name: EntityKey, type?: TypeType, options?: PropertyOptions | EntityProperty): void; /** Adds an enum property to the entity schema. */ addEnum(name: EntityKey, type?: TypeType, options?: EnumOptions): void; /** Adds a version property for optimistic locking. */ addVersion(name: EntityKey, type: TypeType, options?: PropertyOptions): void; /** Adds a primary key property to the entity schema. */ addPrimaryKey(name: EntityKey, type: TypeType, options?: PrimaryKeyOptions): void; /** Adds a serialized primary key property (e.g. for MongoDB ObjectId). */ addSerializedPrimaryKey(name: EntityKey, type: TypeType, options?: SerializedPrimaryKeyOptions): void; /** Adds an embedded property to the entity schema. */ addEmbedded(name: EntityKey, options: EmbeddedOptions): void; /** Adds a many-to-one relation to the entity schema. */ addManyToOne(name: EntityKey, type: TypeType, options: ManyToOneOptions): void; /** Adds a many-to-many relation to the entity schema. */ addManyToMany(name: EntityKey, type: TypeType, options: ManyToManyOptions): void; /** Adds a one-to-many relation to the entity schema. */ addOneToMany(name: EntityKey, type: TypeType, options: OneToManyOptions): void; /** Adds a one-to-one relation to the entity schema. */ addOneToOne(name: EntityKey, type: TypeType, options: OneToOneOptions): void; /** Adds an index definition to the entity schema. */ addIndex(options: IndexOptions): void; /** Adds a unique constraint definition to the entity schema. */ addUnique(options: UniqueOptions): void; /** Sets a custom repository class for this entity. */ setCustomRepository(repository: () => Constructor): void; /** Sets the base entity that this schema extends. */ setExtends(base: EntityName): void; /** Sets or replaces the entity class associated with this schema. */ setClass(cls: Class): void; /** Returns the underlying EntityMetadata. */ get meta(): EntityMetadata; /** Returns the entity class name. */ get name(): string | EntityName; /** Returns the database table name. */ get tableName(): string; get class(): Class; get properties(): Record; new(...params: ConstructorParameters): Entity; /** * @internal */ init(): this; /** * Check if this entity is part of a TPT hierarchy by walking up the extends chain. * This handles mid-level abstract entities (e.g., Animal -> Mammal -> Dog where Mammal is abstract). */ private isPartOfTPTHierarchy; private initProperties; private initPrimaryKeys; private normalizeType; private createProperty; private rename; private renameCompositeOptions; /** * 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 * ```ts * export const Article = defineEntity({ * name: 'Article', * properties: { ... }, * }); * * Article.addHook('beforeCreate', async args => { * args.entity.slug = args.entity.title.toLowerCase(); * }); * ``` */ addHook(event: EventType | `${EventType}`, handler: (args: EventArgs) => void | Promise): this; } export {};