/** * Fluent API for building the entity metadata model. * * @packageDocumentation */ import { Newable, NonEmptyObject } from "ts-essentials"; import { SqlType } from "../db/sql.js"; import { Brand } from "../utils/utils.js"; import { EntityModel, OnDelete, PropertyModel, ValueObjectModel } from "./model.js"; /** * A property model factory function. * @param name The name of the property. * @returns The property model object. */ export type Property = (name: string) => PropertyModel; /** * A set of entity property configurations. * * @template T An entity type. */ export type Properties = { [K in keyof T]?: Property; } & { [key: string]: Property; }; /** * The set of optional properties in an entity configuration. */ export type OptionalProperties = { [K in keyof T]: T[K] extends Property ? (R extends undefined ? K : never) : never; }[keyof T]; /** * A utility type for unwrapping the properties of entity builder properties. * * @template T - The type to unwrap the properties from. */ export type UnwrapProperties = { [K in OptionalProperties]?: T[K] extends Property ? R extends { [key: string]: unknown; } ? UnwrapProperties : R : T[K]; } & { [K in Exclude>]: T[K] extends Property ? R extends { [key: string]: unknown; } ? UnwrapProperties : R : T[K]; }; /** * The numeric keys of an object. */ export type NumberKeys = { [K in keyof T]: T[K] extends number ? K : never; }[keyof T]; export type FromClass = Brand; /** * Builds an {@link EntityModel} based on a Class. * @template C - The class type. * * @param klass The class of the entity. * @param properties The properties of the entity. * @param build An optional builder function. * @returns The created entity model. * * @includeExample ../../apps/snippets/src/building.ts:1-33 */ export declare function entity>(klass: Newable, properties: NonEmptyObject

, build?: (builder: EntityBuilder) => void): EntityModel>; /** * Builds an {@link EntityModel} based on an Object. The type of the entity can be inferred using * the {@link EntityType} utility type. * * @param properties The properties of the entity. * @param build An optional builder function. * @returns The created entity model. * * @includeExample ../../apps/snippets/src/building.ts:35-50 */ export declare function entity>(properties: NonEmptyObject

, build?: (builder: EntityBuilder>) => void): EntityModel>; /** * Builds a {@link ValueObjectModel} based on a Class. * @template C - The class type. * * @param klass The class of the value object. * @param properties The properties of the value object. * @returns The created value object model. */ export declare function valueObject>(klass: Newable, properties: NonEmptyObject

): ValueObjectModel; /** * Builds a {@link ValueObjectModel} based on an Object. * * @param name The name of the value object. * @param properties The properties of the entity. * @returns The created value object model. */ export declare function valueObject>(name: string, properties: NonEmptyObject

): ValueObjectModel; /** * Represents the options for a foreign key constraint. * * @property onDelete The action to be performed when the referenced entity is deleted. */ export type ForeignKeyOptions = { onDelete?: OnDelete; }; /** * Fluent API for configuring an entity metadata model. * @template T - The entity builder type. * @template O - The entity instance type. */ export interface EntityBuilder { /** * Configures the key of the entity. * * @param key One or more properties to use as the key. * @return A chainable EntityBuilder instance. */ key(...key: readonly (keyof T)[]): EntityBuilder; /** * Configures a foreign key reference to another entity. * * @template R The type of the referenced entity. * @param references The referenced entity class, model or name. * @param properties The referencing properties in the current entity. * @param options Options controlling the behavior of the foreign key. * @returns A chainable EntityBuilder instance. */ references(references: Newable | EntityModel | string, properties: keyof T | readonly (keyof T)[], options?: ForeignKeyOptions): EntityBuilder; /** * Configures a foreign key reference to another entity. * * @template R The type of the referenced entity. * @param references The referenced entity class, model or name. * @param options Options controlling the behavior of the foreign key. * @returns A chainable EntityBuilder instance. */ references(references: Newable | EntityModel | string, options?: ForeignKeyOptions): EntityBuilder; references(references: Newable | EntityModel | string, propertiesOrOptions?: (keyof T | readonly (keyof T)[]) | ForeignKeyOptions, options?: ForeignKeyOptions): EntityBuilder; /** * Configures the table name of the entity. * * @param name The table name. * @returns A chainable EntityBuilder instance. */ table(name: string): EntityBuilder; /** * Configures a condition for the entity. Conditions are predicates that are applied by * default to all queries for the entity. * * @param condition The condition function. * @param name The name of the condition. * @returns A chainable EntityBuilder instance. */ condition(condition: (entity: UnwrapProperties) => boolean, name?: string): EntityBuilder; /** * Configures the concurrency control property of the entity. * * @param concurrency The version property of the entity. * @returns A chainable EntityBuilder instance. */ concurrency(concurrency: { version: NumberKeys>; }): EntityBuilder; /** * Configures the initial seed data for the entity. * * @param data The initial data. * @returns A chainable EntityBuilder instance. */ data(...data: readonly Partial[]): EntityBuilder; } /** * @ignore */ export declare const ObjectNamePrefix = "Object_"; /** * @ignore */ export declare class EntityBuilderImpl implements EntityBuilder { #private; properties(properties: Properties): void; key(...key: readonly (keyof T)[]): this; references(references: Newable | EntityModel | string, propertiesOrOptions?: (keyof T | readonly (keyof T)[]) | ForeignKeyOptions, options?: ForeignKeyOptions): this; table(name: string): this; condition(condition: (entity: UnwrapProperties) => boolean, name?: string): this; concurrency(concurrency: { version: NumberKeys>; }): this; data(...data: readonly Partial[]): this; build(klass: Newable): EntityModel; } /** * @ignore */ export declare class ValueObjectBuilderImpl { #private; private name; constructor(name: string); properties(properties: Properties): this; build(klass: Newable): ValueObjectModel; } /** * Options shared by all scalar properties. * @template T - The property type. */ export type ScalarOptions = { /** * The name of the property. */ name: string; /** * The column name of the property. */ column?: string | undefined; /** * The SQL type of the property. */ type?: SqlType | undefined; /** * The nullability of the property. */ nullable?: boolean | undefined; /** * Value generation options for the property. */ generate?: GeneratorOptions | undefined; /** * Value conversion options for the property. */ convert?: ConversionOptions | undefined; }; /** * String property options. */ export type StringOptions = ScalarOptions & { /** * The maximum length allowed for property values. */ maxLength?: number | undefined; }; /** * Number property options. */ export type NumberOptions = ScalarOptions & { /** * The precision of the property. */ precision?: number | undefined; /** * The scale of the property. */ scale?: number | undefined; }; /** * The built-in identity generation strategies. * * - `seqhilo` - A _Hi/Lo_ sequence-based strategy. * - `identity` - A database identity column-based strategy. * - `uuid` - A UUID-based strategy. * - `none` - No generation strategy. * * @see [Hi/lo - Wikipedia](https://en.wikipedia.org/wiki/Hi/Lo_algorithm) */ export type Generator = "seqhilo" | "identity" | "uuid" | "none"; /** * Options for generating property values. * @template T - The property type. * @template E - The entity type. */ export type GeneratorOptions = { /** * The generation strategy to use. */ using?: Generator; /** * The sequence to use for the generation strategy. */ sequence?: string; /** * When to generate the value. */ on?: "add"; /** * The default value, or a function to generate the default value. */ default?: T | ((it: E) => T); }; /** * Converters are functions that transform values from one type to another when reading from or * writing to the database. * * @template T The input type of the converter * @template S The output type of the converter */ export type Converter = (value: T) => S; /** * Options for converting property values. */ export type ConversionOptions = { /** * A function to convert values when reading from the database. */ read: Converter; /** * A function to convert values when writing to the database. */ write: Converter; }; /** * Adds a nullable integer property to the entity and allows for configuration of the property. * * @param options The options for the property. */ export declare function int(options: { nullable: true; } & Omit, "name">): Property; /** * Adds an integer property to the entity and allows for configuration of the property. * * @param options The options for the property. */ export declare function int(options?: Omit, "name">): Property; /** * Adds a nullable string property to the entity and allows for configuration of the property. * * @param options The options for the property. */ export declare function string(options: { nullable: true; } & Omit): Property; /** * Adds a string property to the entity and allows for configuration of the property. * * @param options The options for the property. */ export declare function string(options?: Omit): Property; /** * Adds a nullable numeric property to the entity and allows for configuration of the property. * @param options The options for the property. */ export declare function number(options: { nullable: true; } & Omit): Property; /** * Adds a numeric property to the entity and allows for configuration of the property. * @param options The options for the property. */ export declare function number(options?: Omit): Property; /** * Adds a nullable boolean property to the entity and allows for configuration of the property. * @param options The options for the property. */ export declare function boolean(options: { nullable: true; } & Omit, "name">): Property; /** * Adds a boolean property to the entity and allows for configuration of the property. * @param options The options for the property. */ export declare function boolean(options?: Omit, "name">): Property; /** * Adds a nullable {@link Date} property to the entity and allows for configuration of the property. * @param options The options for the property. */ export declare function date(options: { nullable: true; } & Omit, "name">): Property; /** * Adds a {@link Date} property to the entity and allows for configuration of the property. * @param options The options for the property. */ export declare function date(options?: Omit, "name">): Property; /** * Options for collection navigation properties. */ export type ManyOptions = { /** * The foreign key properties that reference the target entity. */ foreignKey?: (keyof T | string | readonly (keyof T | string)[]) | undefined; /** * A default order by expression for the collection. */ orderBy?: ((item: T) => unknown) | undefined; }; /** * Adds a collection navigation property to the entity and allows for configuration of the property. * @param target The target entity class. * @param options The options for the collection navigation property. */ export declare function many(target: Newable, options?: ManyOptions): Property; /** * Adds a collection navigation property to the entity and allows for configuration of the property. * @param target The target entity model. * @param options The options for the collection navigation property. */ export declare function many(target: EntityModel, options?: ManyOptions): Property>[]>; /** * Adds a collection navigation property to the entity and allows for configuration of the property. * @param target The target entity name. * @param options The options for the collection navigation property. */ export declare function many(target: string, options?: ManyOptions): Property; /** * Options for reference navigation properties. */ export type OneOptions = { /** * The foreign key properties that reference the target entity. */ foreignKey?: (keyof T | string | readonly (keyof T | string)[]) | undefined; /** * The nullability of the property. */ nullable?: boolean | undefined; }; /** * Adds a nullable reference navigation property to the entity and allows for configuration of the property. * @param target The target entity class or name. * @param options The options for the reference navigation property. */ export declare function one(target: Newable | EntityModel | string, options: { nullable: true; } & OneOptions): Property; /** * Adds a reference navigation property to the entity and allows for configuration of the property. * @param target The target entity class or name. * @param options The options for the reference navigation property. */ export declare function one(target: Newable | EntityModel | string, options?: OneOptions): Property; /** * Adds a value object property to the entity and allows for configuration of the property. * @param klass The value object class. * @param properties The properties of the value object. */ export declare function value>(klass: Newable, properties?: NonEmptyObject

): Property

; export declare function value>(valueObjectModel: ValueObjectModel, properties?: NonEmptyObject

): Property

;