/** * Implements the unit of work pattern for managing changes to entities in a database. * * @packageDocumentation */ import { List, Map as ImmutableMap } from "immutable"; import { ExecuteResult, ExecuteStatement, IsolationLevel } from "./db/driver.js"; import { SqlNode } from "./db/sql.js"; import { EntityModel } from "./model/model.js"; import { Metadata } from "./verse.js"; /** * Represents a unique identifier for an entity. */ export type EntityKey = List; /** * @ignore */ export interface QueryCache { get(model: EntityModel, key: EntityKey): object | undefined; set(model: EntityModel, key: EntityKey, entity: object): void; } /** * @ignore */ export declare class IdentityMap implements QueryCache { #private; get(model: EntityModel, key: EntityKey): object | undefined; set(model: EntityModel, key: EntityKey, entity: object): void; remove(entityName: string, key: EntityKey): void; } /** * Represents the state of an entity in a unit of work. * * `added` - The entity has been added to the unit of work and will be * inserted into the database when the unit of work is committed. * * `modified` - The entity has been modified and will be updated in the * database when the unit of work is committed. * * `removed` - The entity has been removed from the unit of work and will * be deleted from the database when the unit of work is committed. * * `unchanged` - The entity has not been modified and will not be * affected when the unit of work is committed. */ export type EntityState = "added" | "modified" | "removed" | "unchanged"; /** * Methods for managing the state of an entity in a unit of work. */ export interface EntityEntryApi { /** * Returns the current state of the entity. * * @returns The state of the entity. */ get state(): EntityState; /** * Sets the state of the entity. * * @param state The state to be set for the entity. */ setState(state: EntityState): void; /** * Updates an entity with the provided values. * * @param {Partial} values - The values to update the entity with. * @return {void} * * @example * uow.entry(album).update({title: 'Road Games', artist: 'Allan Holdsworth'}); */ update(values: Partial): void; } declare class EntityEntry { #private; readonly entity: object; readonly model: EntityModel; constructor(entity: object, model: EntityModel, state: EntityState, fixup: (entry: EntityEntry) => void); get key(): List; get state(): EntityState; setState(state: EntityState): void; commit(): void; get snapshot(): ImmutableMap; update(values: Record): void; trackRelated(property: string, entity: object): void; isRelated(property: string, entity: object): any; untrackRelated(property: string): void; setRelated(property: string, entity: object): void; } export interface UnitOfWorkApi extends AsyncDisposable { /** * Adds one or more entities in this unit of work. The entities will be inserted into the database when the * unit of work is committed. If the corresponding entity type is configured with a client-side key generator, * then keys will be generated and set on the entities; otherwise, the entities will be assigned * temporary keys. * * @param entities One or more entity instances. * * @returns A promise that resolves once the entities have been added. * * @example * * const blog1 = new Blog({ * title: 'My first blog', * content: 'This is the content of my first blog', * }); * * const blog2 = new Blog({ * title: 'My second blog', * content: 'This is the content of my second blog', * }); * * await uow.add(blog1, blog2); */ add(...entities: readonly object[]): Promise; /** * Adds one or more entities in this unit of work. The entities will be inserted into the database when the * unit of work is committed. If the corresponding entity type is configured with a client-side key generator, * then keys will be generated and set on the entities; otherwise, the entities will be assigned * temporary keys. * * @param entityName The name of the entity model. * @param entities One or more entity instances. * * @returns A promise that resolves once the entities have been added. * * @example * * const blog1 = { * title: 'My first blog', * content: 'This is the content of my first blog', * }; * * const blog2 = { * title: 'My second blog', * content: 'This is the content of my second blog', * }l * * await uow.add("Blog", blog1, blog2); */ add(entityName: string, ...entities: readonly object[]): Promise; /** * Removes one or more entities in this unit of work. The entities will be deleted from the database when the * unit of work is committed. * * @param entities The entities to be removed. * * @example * await uow.remove(blog1, blog2); */ remove(...entities: readonly object[]): void; /** * Commits all changes made in this unit of work to the database. * @param isolation Optional isolation level for the database transaction. * @return A promise that resolves when the commit is successful. */ commit(isolation?: IsolationLevel): Promise; /** * Exposes operations that can be performed on an entity in this unit of work. * * @param entity The entity for which the entry API needs to be retrieved. * @returns The entry API for the entity, or undefined if not found. * * @example * uow.entry(album).setState('removed'); */ entry(entity: Entity): EntityEntryApi | undefined; } type Operation = { sql: SqlNode; args?: unknown[]; onCommit?: ((result: ExecuteResult) => void) | undefined; } & ExecuteStatement; /** * @ignore */ export declare class UnitOfWorkImpl { #private; constructor(metadata: Metadata); entry(entity: Entity): EntityEntry | undefined; add(...entities: readonly object[]): Promise; add(entityName: string, ...entities: readonly object[]): Promise; remove(...entities: readonly object[]): void; get(model: EntityModel, key: EntityKey): object | undefined; set(model: EntityModel, key: EntityKey, entity: object, state?: EntityState): void; commit(isolation?: IsolationLevel): Promise; protected executeBatch(batch: readonly Operation[], start: number, isolation?: IsolationLevel): Promise; protected createParameter(value: unknown, args: unknown[]): SqlNode; [Symbol.asyncDispose](): Promise; } /** * @ignore */ export declare class UnitOfWorkSpy extends UnitOfWorkImpl { #private; constructor(metadata: Metadata); get operations(): Operation[]; protected executeBatch(batch: readonly Operation[], _: number, __?: IsolationLevel): Promise; } /** * Thrown when an optimistic concurrency violation is detected. This occurs when an entity is updated or deleted * and the expected number of rows affected is not met. */ export declare class ConcurrencyError extends Error { constructor(); } export {};