import { AsyncLocalStorage } from "node:async_hooks"; export { Model, type ModelClass, type AnyModelClass, type ModelBase, type ModelLookup, defineModel, deleteEverything, field, } from "./models.js"; import type { Change, Model } from "./models.js"; export { string, orderedString, number, dateTime, boolean, identifier, undef, opt, or, array, set, record, object, literal, link, } from "./types.js"; export type { ObjectShape, ObjectValue, FieldValue } from "./types.js"; export { dump, } from "./indexes.js"; export type { FindOptions, IndexRangeIterator } from './indexes.js'; export { type Change } from './models.js'; export type { FieldConfig } from './models.js'; export { TypeWrapper } from './types.js'; export { DatabaseError } from "olmdb/lowlevel"; export { runMigration } from './migrate.js'; export type { MigrationOptions, MigrationResult } from './migrate.js'; export interface Transaction { id: number; instances: Map>; } export declare const txnStorage: AsyncLocalStorage; /** * Returns the current transaction from AsyncLocalStorage. * Throws if called outside a transact() callback. * @internal */ export declare function currentTxn(): Transaction; /** * Initialize the database with the specified directory path. * This function may be called multiple times with the same parameters. If it is not called before the first transact(), * the database will be automatically initialized with the default directory. * * @example * ```typescript * init("./my-database"); * ``` */ export declare function init(dbDir: string): void; /** * Executes a function within a database transaction context. * * Loading models (also through links in other models) and changing models can only be done from * within a transaction. * * Transactions have a consistent view of the database, and changes made within a transaction are * isolated from other transactions until they are committed. In case a commit clashes with changes * made by another transaction, the transaction function will automatically be re-executed up to 6 * times. * * @template T - The return type of the transaction function. * @param fn The function to execute within the transaction context. Receives a Transaction instance. * @returns A promise that resolves with the function's return value. * @throws {DatabaseError} With code "RACING_TRANSACTION" if the transaction fails after retries due to conflicts. * @throws {DatabaseError} With code "TXN_LIMIT" if maximum number of transactions is reached. * @throws {DatabaseError} With code "LMDB-{code}" for LMDB-specific errors. * * @example * ```typescript * const paid = await E.transact(() => { * const user = User.get("john_doe"); * if (user.credits > 0) { * user.credits--; * return true; * } * return false; * }); * ``` * ```typescript * // Transaction with automatic retry on conflicts * await E.transact(() => { * const counter = Counter.get("global") || new Counter({id: "global", value: 0}); * counter.value++; * }); * ``` */ export declare function transact(fn: () => T): Promise; /** * Set the maximum number of retries for a transaction in case of conflicts. * The default value is 6. Setting it to 0 will disable retries and cause transactions to fail immediately on conflict. * * @param count The maximum number of retries for a transaction. */ export declare function setMaxRetryCount(count: number): void; /** * Set a callback function to be called after a model is saved and committed. * * @param callback The callback function to set. It gets called after each successful * `transact()` commit that has changes, with the following arguments: * - A sequential number. Higher numbers have been committed after lower numbers. * - A map of model instances to their changes. The change can be "created", "deleted", or an object containing the old values. * * The callback is called within a new transaction context at or after the committed state, so lazy-loads * and additional writes are allowed. */ export declare function setOnSaveCallback(callback: ((commitId: number, items: Map, Change>) => void) | undefined): void;