/** * Transaction support for Neumann database. * * Provides automatic transaction management with commit/rollback semantics. * * @example * ```typescript * // Using the transaction helper method * await client.withTransaction(async (tx) => { * await tx.execute("INSERT users name='Alice'"); * await tx.execute("INSERT users name='Bob'"); * // Auto-commits on success, auto-rollbacks on error * }); * * // Manual transaction management * const tx = client.beginTransaction(); * try { * await tx.begin(); * await tx.execute("INSERT users name='Alice'"); * await tx.commit(); * } catch (e) { * await tx.rollback(); * throw e; * } * ``` */ import type { NeumannClient } from './client.js'; import type { QueryResult } from './types/query-result.js'; /** * Options for creating a transaction. */ export interface TransactionOptions { /** Identity for vault access. */ identity?: string; /** Whether to auto-commit on successful completion (default: true). */ autoCommit?: boolean; } /** * Result of a successful commit operation. */ export interface CommitResult { /** The block hash of the committed transaction. */ blockHash: string; /** The blockchain height after commit. */ height: number; } /** * Result of a rollback operation. */ export interface RollbackResult { /** The blockchain height after rollback. */ toHeight: number; } /** * A database transaction with automatic commit/rollback support. * * Transactions provide ACID guarantees for a sequence of operations. * Use the helper methods on NeumannClient for easier transaction management. */ export declare class Transaction { private readonly client; private readonly identity?; private readonly _autoCommit; private _txId; private _active; private _committed; private _rolledBack; /** * Create a new transaction. * * @param client - The NeumannClient to use for the transaction. * @param options - Transaction options. */ constructor(client: NeumannClient, options?: TransactionOptions); /** * Get the transaction ID (available after begin()). */ get txId(): string | null; /** * Check if the transaction is currently active. */ get isActive(): boolean; /** * Check if the transaction was committed. */ get isCommitted(): boolean; /** * Check if the transaction was rolled back. */ get isRolledBack(): boolean; /** * Whether auto-commit is enabled for this transaction. */ get autoCommit(): boolean; /** * Begin the transaction. * * @returns The transaction ID. * @throws {NeumannError} If the transaction is already active or begin fails. */ begin(): Promise; /** * Execute a query within this transaction. * * @param query - The query to execute. * @returns The query result. * @throws {NeumannError} If the transaction is not active. */ execute(query: string): Promise; /** * Commit the transaction. * * @returns The commit result with block hash and height. * @throws {NeumannError} If the transaction is not active or commit fails. */ commit(): Promise; /** * Rollback the transaction. * * @returns The rollback result with the height rolled back to. * @throws {NeumannError} If the transaction is not active or rollback fails. */ rollback(): Promise; /** * Run a function within this transaction with automatic commit/rollback. * * If the function completes successfully, the transaction is committed. * If the function throws an error, the transaction is rolled back. * * @param fn - The function to execute within the transaction. * @returns The result of the function. * @throws The error thrown by the function (after rollback). */ run(fn: (tx: Transaction) => Promise): Promise; } /** * Builder for creating transactions with custom options. * * @example * ```typescript * const tx = new TransactionBuilder(client) * .withIdentity('user:alice') * .withAutoCommit(false) * .build(); * * await tx.begin(); * await tx.execute("INSERT users name='Alice'"); * await tx.commit(); // Manual commit required * ``` */ export declare class TransactionBuilder { private readonly client; private identity?; private autoCommit; /** * Create a new transaction builder. * * @param client - The NeumannClient to use for the transaction. */ constructor(client: NeumannClient); /** * Set the identity for vault access. * * @param identity - The identity to use. * @returns This builder for chaining. */ withIdentity(identity: string): TransactionBuilder; /** * Set whether to auto-commit on successful completion. * * @param autoCommit - Whether to auto-commit. * @returns This builder for chaining. */ withAutoCommit(autoCommit: boolean): TransactionBuilder; /** * Build the transaction. * * @returns The configured Transaction. */ build(): Transaction; } //# sourceMappingURL=transaction.d.ts.map