/** * Transaction engine – provides multi-document ACID-like transactions * using a copy-on-write journal. * * BrightChain blocks are immutable: we never modify or delete existing blocks. * Writes always create new blocks. The journal tracks which *new* blocks were * added (and which old mappings they replaced) so that: * * • commit – applies the buffered mapping changes to the collections * • abort – discards the journal; no blocks need to be removed because * orphaned blocks are harmless in a content-addressable store * * Rollback after a partial commit simply restores the old doc-index mappings. * The "orphan" blocks written during the failed commit stay in the store – * they can be reclaimed by a separate GC sweep if needed. */ import type { IClientSession } from '@digitaldefiance/suite-core-lib'; import type { BsonDocument, DocumentId } from '../interfaces/storage/documentTypes'; import type { UuidGenerator } from './uuidGenerator'; /** * A single operation in the transaction journal. * * - **insert** – a new document to be added. Rollback removes the mapping. * - **update** – `before` is kept so rollback can restore the old mapping * (the old block still exists in the store thanks to copy-on-write). * - **delete** – `doc` is the document that was mapped. Rollback restores it. */ export type JournalOp = { type: 'insert'; collection: string; doc: BsonDocument; } | { type: 'update'; collection: string; docId: DocumentId; before: BsonDocument; after: BsonDocument; } | { type: 'delete'; collection: string; docId: DocumentId; doc: BsonDocument; }; /** Callback to apply a journal to the actual store */ export type CommitCallback = (journal: JournalOp[]) => Promise; /** Callback to roll back a committed journal */ export type RollbackCallback = (journal: JournalOp[]) => Promise; /** * A client session that supports transactions. */ export declare class DbSession implements IClientSession { private readonly onCommit; private readonly onRollback; readonly id: string; private _inTransaction; private _journal; private _ended; constructor(onCommit: CommitCallback, onRollback: RollbackCallback, uuidGenerator?: UuidGenerator); get inTransaction(): boolean; /** * Start a new transaction. * @throws Error if a transaction is already in progress or session is ended. */ startTransaction(): void; /** * Add an operation to the transaction journal. * @throws Error if no transaction is in progress. */ addOp(op: JournalOp): void; /** * Read the current journal (for testing/inspection). */ getJournal(): ReadonlyArray; /** * Commit the current transaction – applies all journal ops atomically. * @throws Error if no transaction is in progress. */ commitTransaction(): Promise; /** * Abort the current transaction – discards all journal ops. */ abortTransaction(): Promise; /** * End the session. Aborts any in-progress transaction. */ endSession(): void; } //# sourceMappingURL=transaction.d.ts.map