/** * Transaction Interface * * Defines cross-database transaction support using the Saga pattern * for distributed consistency across databases, graphs, and vector stores. */ import { IWarehouseQuery, IWarehouseResult } from './query.interface'; /** * Saga transaction for cross-database operations * * Uses compensating transactions to maintain consistency across * multiple data sources. If any step fails, previous steps are * rolled back using their compensation actions. */ export interface ISagaTransaction { /** * Unique transaction identifier * Generated automatically if not provided */ id?: string; /** * Additional operations to execute atomically * These run in sequence with the main query */ operations?: ISagaStep[]; /** * Transaction isolation behavior * @default 'best_effort' */ isolation?: SagaIsolation; /** * Timeout for the entire saga in milliseconds * @default 30000 */ timeout?: number; /** * Retry configuration for failed steps */ retry?: IRetryConfig; /** * What to do on failure * @default 'compensate' */ onFailure?: 'compensate' | 'continue' | 'abort'; } /** * Saga isolation levels */ export type SagaIsolation = /** Best effort - no guarantees across sources */ 'best_effort' /** Read committed equivalent - see committed data */ | 'read_committed' /** Serializable - full isolation (may use locks) */ | 'serializable'; /** * Single step in a saga transaction */ export interface ISagaStep { /** * Step identifier for tracking */ id?: string; /** * Step name for logging/debugging */ name?: string; /** * The operation to execute */ operation: Omit; /** * Compensation operation to undo this step * If not provided, will attempt to auto-generate * (e.g., delete for insert, update with old values for update) */ compensation?: Omit; /** * Whether this step is required * If false, failure won't trigger saga rollback * @default true */ required?: boolean; /** * Order of execution (lower = earlier) * @default 0 */ order?: number; /** * Dependencies on other steps (by id) * This step waits for dependencies to complete */ dependsOn?: string[]; } /** * Retry configuration for saga steps */ export interface IRetryConfig { /** * Maximum retry attempts * @default 3 */ maxAttempts?: number; /** * Initial delay between retries in milliseconds * @default 1000 */ initialDelay?: number; /** * Maximum delay between retries * @default 10000 */ maxDelay?: number; /** * Backoff multiplier * @default 2 */ backoffMultiplier?: number; /** * Retry on these error types only */ retryOn?: ('timeout' | 'connection' | 'conflict' | 'transient')[]; } /** * Transaction execution state */ export interface ISagaState { /** Transaction ID */ id: string; /** Current status */ status: SagaStatus; /** All steps with their status */ steps: ISagaStepState[]; /** When the saga started */ startedAt: Date; /** When the saga completed (if finished) */ completedAt?: Date; /** Error if saga failed */ error?: ISagaError; /** Results from each step */ results: Map; } /** * Saga status */ export type SagaStatus = 'pending' | 'executing' | 'compensating' | 'committed' | 'rolled_back' | 'failed' | 'partially_committed'; /** * Individual step state */ export interface ISagaStepState { /** Step ID */ id: string; /** Step name */ name?: string; /** Step status */ status: StepStatus; /** Step result (if successful) */ result?: IWarehouseResult; /** Compensation status (if compensated) */ compensationStatus?: StepStatus; /** Error if step failed */ error?: Error; /** Execution time */ executionTime?: number; /** Retry attempts made */ attempts: number; } /** * Step execution status */ export type StepStatus = 'pending' | 'executing' | 'completed' | 'failed' | 'skipped' | 'compensated' | 'compensation_failed'; /** * Saga error with details */ export interface ISagaError { /** Error message */ message: string; /** Step that failed */ failedStep?: string; /** Whether compensation was successful */ compensationSuccessful: boolean; /** Steps that were rolled back */ rolledBackSteps: string[]; /** Steps that failed to roll back */ failedRollbackSteps: string[]; /** Original error */ cause?: Error; } /** * Result of a saga transaction */ export interface ISagaResult { /** Transaction ID */ transactionId: string; /** Final status */ status: SagaStatus; /** Results from all steps (keyed by step ID) */ stepResults: Record; /** Total execution time */ executionTime: number; /** Error details if failed */ error?: ISagaError; } /** * Transaction context for passing between operations */ export interface ITransactionContext { /** Transaction ID */ id: string; /** Current saga state */ state: ISagaState; /** Active database transactions (by source tag) */ activeTransactions: Map; /** Compensation queue */ compensationQueue: ISagaStep[]; } /** * Generate a unique saga ID */ export declare function generateSagaId(): string; /** * Generate a unique step ID */ export declare function generateStepId(stepName?: string): string; /** * Check if a saga is still in progress */ export declare function isSagaInProgress(status: SagaStatus): boolean; /** * Check if a saga completed successfully */ export declare function isSagaSuccessful(status: SagaStatus): boolean; /** * Auto-generate compensation for a write operation */ export declare function generateCompensation(operation: Omit, result?: IWarehouseResult): Omit | undefined;