import { C as TCascadeResolver, F as TDbDeleteResult, H as TDbInsertResult, K as TDbUpdateResult, V as TDbInsertManyResult, X as TFkLookupResolver, _t as TGenericLogger, a as TDbEncryptionOptions, c as BaseDbAdapter, ct as TWriteTableResolver, i as DbEncryption, ot as TTableResolver, pt as TableMetadata, t as AtscriptDbReadable } from "./db-readable-wyKeE8ut.mjs"; import { AtscriptQueryComparison, AtscriptQueryFieldRef, AtscriptQueryFieldRef as AtscriptQueryFieldRef$1, AtscriptQueryNode, AtscriptQueryNode as AtscriptQueryNode$1, AtscriptRef, FlatOf, NavPropsOf, OwnPropsOf, PrimaryKeyOf, TAtscriptAnnotatedType, TAtscriptDataType, Validator } from "@atscript/typescript/utils"; import { FilterExpr } from "@uniqu/core"; //#region src/strategies/integrity.d.ts /** * Strategy for referential integrity enforcement. * Two implementations: {@link NativeIntegrity} (DB handles FK constraints) * and `ApplicationIntegrity` (generic layer validates + cascades). */ declare abstract class IntegrityStrategy { abstract validateForeignKeys(items: Array>, meta: TableMetadata, fkLookupResolver: TFkLookupResolver | undefined, writeTableResolver: TWriteTableResolver | undefined, partial?: boolean, excludeTargetTable?: string): Promise; abstract cascadeBeforeDelete(filter: FilterExpr, tableName: string, meta: TableMetadata, cascadeResolver: TCascadeResolver, translateFilter: (f: FilterExpr) => FilterExpr, adapter: BaseDbAdapter): Promise; abstract needsCascade(cascadeResolver: TCascadeResolver | undefined): boolean; } /** * Integrity strategy for adapters with native FK support (e.g. SQLite, MySQL). * All operations are no-ops — the database engine enforces constraints. */ declare class NativeIntegrity extends IntegrityStrategy { validateForeignKeys(): Promise; cascadeBeforeDelete(): Promise; needsCascade(): boolean; } //#endregion //#region src/table/db-table.d.ts declare class AtscriptDbTable, FlatType = FlatOf, A extends BaseDbAdapter = BaseDbAdapter, IdType = PrimaryKeyOf, OwnProps = OwnPropsOf, NavType extends Record = NavPropsOf> extends AtscriptDbReadable { protected _cascadeResolver?: TCascadeResolver; protected _fkLookupResolver?: TFkLookupResolver; protected readonly _integrity: IntegrityStrategy; protected readonly validators: Map>; private _fromDepthMap?; constructor(_type: T, adapter: A, logger?: TGenericLogger, _tableResolver?: TTableResolver, _writeTableResolver?: TWriteTableResolver); /** * Sets the cascade resolver for application-level cascade deletes. * Called by DbSpace after table creation. */ setCascadeResolver(resolver: TCascadeResolver): void; /** * Sets the FK lookup resolver for application-level FK validation. * Called by DbSpace after table creation. */ setFkLookupResolver(resolver: TFkLookupResolver): void; /** * Returns a cached validator for the given purpose. * Built with adapter plugins from {@link BaseDbAdapter.getValidatorPlugins}. * * Standard purposes: `'insert'`, `'update'`, `'patch'`. * Adapters may define additional purposes. */ getValidator(purpose: string): Validator; /** * Inserts a single record. Delegates to {@link insertMany} for unified * nested creation support. */ insertOne(payload: Partial & Record, opts?: { maxDepth?: number; }): Promise; /** * Inserts multiple records with batch-optimized nested creation. * * Supports **nested creation**: if payloads include data for navigation * fields (`@db.rel.to` / `@db.rel.from`), related records are created * automatically in batches. TO dependencies are batch-created first * (their PKs become our FKs), FROM dependents are batch-created after * (they receive our PKs as their FKs). Fully recursive — nested records * with their own nav data trigger further batch inserts at each level. * Recursive up to `maxDepth` (default 3). */ insertMany(payloads: Array & Record>, opts?: { maxDepth?: number; }): Promise; /** * Replaces a single record identified by primary key(s). * Delegates to {@link bulkReplace} for unified nested relation support. */ replaceOne(payload: DataType & Record, opts?: { maxDepth?: number; }): Promise; /** * Replaces multiple records with deep nested relation support. * * Supports all relation types (TO, FROM, VIA). TO dependencies are * replaced first (their PKs become our FKs), FROM dependents are replaced * after (they receive our PKs as their FKs), VIA relations clear and * re-create junction rows. Fully recursive up to `maxDepth` (default 3). */ bulkReplace(payloads: Array>, opts?: { maxDepth?: number; }): Promise; /** * Partially updates a single record identified by primary key(s). * Delegates to {@link bulkUpdate} for unified nested relation support. */ updateOne(payload: Partial & Record, opts?: { maxDepth?: number; }): Promise; /** * Partially updates multiple records with deep nested relation support. * * Only TO relations (1:1, N:1) are supported for patching. FROM/VIA * relations will error — use {@link bulkReplace} for those. * Recursive up to `maxDepth` (default 3). */ bulkUpdate(payloads: Array & Record>, opts?: { maxDepth?: number; }): Promise; /** * Deletes a single record by any type-compatible identifier — primary key * or single-field unique index. Uses the same resolution logic as `findById`. * * When the adapter does not support native foreign keys (e.g. MongoDB), * cascade and setNull actions are applied before the delete. */ deleteOne(id: IdType): Promise; updateMany(filter: FilterExpr, data: Partial & Record): Promise; replaceMany(filter: FilterExpr, data: Record): Promise; deleteMany(filter: FilterExpr): Promise; /** * Synchronizes indexes between Atscript definitions and the database. */ syncIndexes(): Promise; /** * Ensures the table/collection exists in the database. */ ensureTable(): Promise; /** Engine-agnostic guard for user-supplied mutation filters (updateMany/deleteMany/…). */ protected _guardMutationFilter(filter: FilterExpr): void; /** * Encrypts `@db.encrypted` field values in place on (already validated) * write payloads — between validation and `prepareForWrite`, so adapters * only ever see envelope strings. * * Parent objects along an encrypted path are shallow-cloned before * mutation so caller-shared nested objects are never modified. * * In `patch` mode, operator objects (`$inc`, `$insert`, …) targeting an * encrypted field are rejected with `ENC_FIELD_PATCH_OP` — ciphertext is * opaque; only plain re-assignment (which re-encrypts) is allowed. */ protected _encryptItems(items: Array>, mode: "write" | "patch"): Promise; /** * Applies default values for fields that are missing from the payload. * Defaults handled natively by the DB engine are skipped — the field stays * absent so the DB's own DEFAULT clause applies. */ protected _applyDefaults(data: Record): Record; /** * Extracts a record-identifying filter from a payload. * * Resolution order: * 1. Primary key field(s) — if all PK fields are present in the payload. * 2. Single-field unique index — first `@db.index.unique` field found. * 3. Compound unique index — first compound unique index whose fields are all present. * * Throws when no identifying fields can be found. */ protected _extractRecordFilter(payload: Record): FilterExpr; private _prepareFilterValue; /** * Lazy — builds a `normalized-path → from-depth` map from `this._meta.flatMap` * on first use. Only paths reachable through an unbroken chain of `db.rel.from` * nav fields from the root are included (chains crossing `to`/`via` are excluded). */ private _getFromDepthMap; /** * Populate the depth-limit bundle on a `DbValidationContext`. Only the root * write call (`depth === 0`) enforces — nested re-entries leave `depthCheck` * unset so the full tree is validated once at the root. */ private _applyDepthCtx; /** * Pre-validate items (type validation + FK constraints) without inserting them. * Used by parent tables to validate FROM children before the main insert, * ensuring errors are caught before the parent is committed. * * @param opts.excludeFkTargetTable - Skip FK validation to this table (the parent). */ preValidateItems(items: Array>, opts?: { excludeFkTargetTable?: string; }): Promise; /** * Builds a validator for a given purpose with adapter plugins. * * Uses annotation-based `replace` callback to make `@meta.id` and * `@db.default` fields optional — works at all nesting levels * (including inside nav field target types). */ protected _buildValidator(purpose: string): Validator; } //#endregion //#region src/query/query-tree.d.ts /** A single join in a view query plan. */ interface TViewJoin { targetType: () => TAtscriptAnnotatedType; targetTable: string; condition: AtscriptQueryNode; } /** Resolved view query plan produced by AtscriptDbView. */ interface TViewPlan { entryType: () => TAtscriptAnnotatedType; entryTable: string; joins: TViewJoin[]; filter?: AtscriptQueryNode; having?: AtscriptQueryNode; materialized: boolean; } /** * Translates a JS-emitted query tree into a FilterExpr. * Resolves field references (type + field path) to physical column names * via the provided resolver function. */ declare function translateQueryTree(node: AtscriptQueryNode, resolveField: (ref: AtscriptQueryFieldRef) => string): FilterExpr; //#endregion //#region src/table/db-view.d.ts interface TViewColumnMapping { viewColumn: string; sourceTable: string; sourceColumn: string; /** Aggregate function name ('sum'|'avg'|'count'|'min'|'max') if this is an aggregate column. */ aggFn?: string; /** Source field for the aggregate function ('*' for COUNT(*)). */ aggField?: string; } /** * Database view abstraction driven by Atscript `@db.view.*` annotations. * * Extends {@link AtscriptDbReadable} with view plan resolution — entry table, * joins, filter, and materialization flag. Read operations are inherited; * write operations are not available on views. * * ```typescript * const adapter = new SqliteAdapter(db) * const activeUsers = new AtscriptDbView(ActiveUsersType, adapter) * const users = await activeUsers.findMany({ filter: {}, controls: {} }) * ``` */ declare class AtscriptDbView, FlatType = FlatOf, A extends BaseDbAdapter = BaseDbAdapter, IdType = PrimaryKeyOf, OwnProps = OwnPropsOf, NavType extends Record = NavPropsOf> extends AtscriptDbReadable { private _viewPlan?; get isView(): boolean; /** * Whether this is an external view — declared with `@db.view` only, * without `@db.view.for`. External views reference pre-existing DB views * and are not managed (created/dropped) by schema sync. */ get isExternal(): boolean; /** * Lazily resolves the view plan from `@db.view.*` metadata. * * - `db.view.for` → entry type ref (required) * - `db.view.joins` → array of `{ target, condition }` (optional, multiple) * - `db.view.filter` → query tree (optional) * - `db.view.materialized` → boolean (optional) */ get viewPlan(): TViewPlan; /** * Resolves a query field ref to a quoted `table.column` SQL fragment. * * @param ref - The field reference from the query tree. * @param qi - Identifier quoting function (e.g. backtick for MySQL, double-quote for SQLite). * Defaults to double-quote wrapping for backwards compatibility. */ resolveFieldRef(ref: AtscriptQueryFieldRef, qi?: (name: string) => string): string; /** * Maps each view field to its source table and column via ref chain. * Fields without refs (inline definitions) map to the entry table with the same name. */ getViewColumnMappings(): TViewColumnMapping[]; } //#endregion //#region src/table/db-space.d.ts /** * Adapter factory function. Called once per table/view to create a fresh adapter instance. * Each readable gets its own adapter (1:1 relationship required by BaseDbAdapter). */ type TAdapterFactory = () => BaseDbAdapter; /** Options bag for {@link DbSpace} (second constructor argument). */ interface TDbSpaceOptions { /** Logger shared by all tables/views in the space. */ logger?: TGenericLogger; /** Field-level encryption configuration for `@db.encrypted` fields. */ encryption?: TDbEncryptionOptions; } /** * A database space — a registry of tables and views sharing the same adapter type and driver. * * `DbSpace` solves the cross-table discovery problem: when table A has a relation * to table B, it needs to find and query table B. The space acts as the registry * that makes this possible via the table resolver callback. * * Each table/view gets its own adapter instance (created by the factory), but all * share the same space and can discover each other for `$with` relation loading. * * ```typescript * // SQLite * const driver = new BetterSqlite3Driver(':memory:') * const db = new DbSpace(() => new SqliteAdapter(driver)) * const users = db.getTable(UsersType) * const activeUsers = db.getView(ActiveUsersType) * ``` */ declare class DbSpace { protected readonly adapterFactory: TAdapterFactory; private _readables; /** All tables created in this space — used for reverse FK lookup during cascade. */ private _allTables; /** Lazily created adapter for administrative ops (drop table/view) that don't need a registered readable. */ private _adminAdapter?; protected readonly logger: TGenericLogger; /** Encryption service for `@db.encrypted` fields — validated eagerly at construction. */ protected readonly _encryption?: DbEncryption; /** * @param adapterFactory - Creates a fresh adapter per table/view. * @param loggerOrOptions - Either a logger (legacy signature) or a * {@link TDbSpaceOptions} bag carrying `logger` and/or `encryption`. */ constructor(adapterFactory: TAdapterFactory, loggerOrOptions?: TGenericLogger | TDbSpaceOptions); /** * Auto-detects whether the type is a table or view and returns the * appropriate instance. Uses `@db.view` or `@db.view.for` presence to distinguish. */ get(type: T, logger?: TGenericLogger): AtscriptDbReadable; /** * Returns the table for the given annotated type. * Creates the table + adapter on first access, caches for subsequent calls. */ getTable(type: T, logger?: TGenericLogger): AtscriptDbTable; /** * Returns the view for the given annotated type. * Creates the view + adapter on first access, caches for subsequent calls. */ getView(type: T, logger?: TGenericLogger): AtscriptDbView; /** * Returns the adapter for the given annotated type. * Creates the table/view + adapter on first access if needed. */ getAdapter(type: TAtscriptAnnotatedType): BaseDbAdapter; /** * Drops a table by name. Used by schema sync to remove tables no longer in the schema. */ dropTableByName(tableName: string): Promise; /** * Drops a view by name. Used by schema sync to remove views no longer in the schema. */ dropViewByName(viewName: string): Promise; private _getAdminAdapter; /** * Finds all child tables with FKs pointing to the given parent table name. * Accesses `table.foreignKeys` which triggers `_flatten()` if needed. */ private _getCascadeTargets; /** * Resolves a table name to a queryable target for FK validation. * Searches all registered tables for one with the matching table name. */ private _getFkLookupTarget; } //#endregion export { TViewColumnMapping as a, AtscriptQueryNode$1 as c, TViewPlan as d, translateQueryTree as f, NativeIntegrity as h, AtscriptDbView as i, AtscriptRef as l, IntegrityStrategy as m, TAdapterFactory as n, AtscriptQueryComparison as o, AtscriptDbTable as p, TDbSpaceOptions as r, AtscriptQueryFieldRef$1 as s, DbSpace as t, TViewJoin as u };