/** * db4ai Transaction Support * * Provides ACID transactions for multi-operation workflows. * Transactions ensure atomicity, consistency, isolation, and durability * across multiple database operations. * * @packageDocumentation */ /** * Transaction state enumeration. */ export declare enum TransactionState { /** Transaction is active and accepting operations */ Active = "active", /** Transaction has been committed */ Committed = "committed", /** Transaction has been rolled back */ RolledBack = "rolled_back" } /** * Options for transaction execution. */ export interface TransactionOptions { /** Maximum number of retry attempts on conflict */ maxRetries?: number; /** Base delay for exponential backoff (ms) */ retryDelayMs?: number; /** Transaction timeout (ms) */ timeoutMs?: number; /** Isolation level (defaults to 'read-committed') */ isolation?: 'read-uncommitted' | 'read-committed' | 'repeatable-read' | 'serializable'; } /** * Operation types that can be staged in a transaction. */ export type OperationType = 'create' | 'update' | 'delete'; /** * A staged operation within a transaction. */ export interface StagedOperation { type: OperationType; collection: string; id?: string; data?: T; where?: Record; } /** * Collection proxy for transactional operations. */ export interface TransactionalCollection { /** Create a new document within the transaction */ create(data: Omit): Promise; /** Update an existing document within the transaction */ update(id: string, data: Partial): Promise; /** Delete a document within the transaction */ delete(id: string): Promise; /** Find a document by ID within the transaction (sees staged writes) */ findById(id: string): Promise; /** Find documents within the transaction (sees staged writes) */ find(query: Record): Promise; } /** * Transaction context passed to the transaction callback. */ export interface TransactionContext { /** Get a collection for transactional operations */ collection(name: string): TransactionalCollection; /** List of staged operations */ readonly stagedOperations: ReadonlyArray; /** Current transaction state */ readonly state: TransactionState; } /** * Error thrown when a transaction conflicts with another transaction. */ export declare class ConflictError extends Error { readonly conflictingDocumentId?: string | undefined; constructor(message: string, conflictingDocumentId?: string | undefined); } /** * Error thrown when a transaction times out. */ export declare class TransactionTimeoutError extends Error { constructor(message?: string); } /** * Execute a callback within a transaction context. * * @param callback - The callback to execute within the transaction * @param options - Transaction options * @returns The result of the callback * @throws ConflictError if the transaction conflicts with another * @throws The original error if the callback throws * * @example * ```typescript * const result = await transaction(async (tx) => { * const user = await tx.collection('users').create({ name: 'Alice' }); * const post = await tx.collection('posts').create({ * title: 'Hello', * authorId: user.id, * }); * return { user, post }; * }); * ``` */ export declare function transaction(callback: (tx: TransactionContext) => Promise, options?: TransactionOptions): Promise; /** * Create a database client with transaction support. * This is a mock implementation for testing purposes. */ export declare function createDb(): { /** * Execute a callback within a transaction context. */ transaction(callback: (tx: TransactionContext) => Promise, options?: TransactionOptions): Promise; }; //# sourceMappingURL=transaction.d.ts.map