/** * InMemoryDatabase – a platform-agnostic IDatabase implementation. * * Uses MemoryBlockStore and InMemoryHeadRegistry for storage, requiring * no Node.js APIs. This is the base class that BrightDb in * brightchain-db extends to add disk persistence. * * Usage: * const db = new InMemoryDatabase(blockStore, { name: 'mydb' }); * await db.connect(); * const users = db.collection('users'); * await users.insertOne({ name: 'Alice' }); */ import type { CollectionOptions, CursorSession, IClientSession, ICollection, IDatabase } from '@digitaldefiance/suite-core-lib'; import type { IBlockStore } from '../interfaces/storage/blockStore'; import type { BsonDocument, DocumentId } from '../interfaces/storage/documentTypes'; import type { IHeadRegistry } from '../interfaces/storage/headRegistry'; import { Collection } from './collection'; import type { UuidGenerator } from './uuidGenerator'; /** * Options for creating an InMemoryDatabase instance. */ export interface InMemoryDatabaseOptions { /** Database name (default: 'brightchain') */ name?: string; /** Custom head registry (default: new InMemoryHeadRegistry()) */ headRegistry?: IHeadRegistry; /** Custom UUID generator (default: createDefaultUuidGenerator()) */ uuidGenerator?: UuidGenerator; /** Cursor session timeout in ms (default: 300000 = 5 minutes) */ cursorTimeoutMs?: number; } /** * The main in-memory database driver – analogous to MongoDB's `Db` class. * * Designed to be extended by `BrightDb` in brightchain-db which adds * persistence-specific options (dataDir, poolId, PersistentHeadRegistry). */ export declare class InMemoryDatabase implements IDatabase { readonly name: string; protected readonly store: IBlockStore; protected readonly collections: Map>; protected readonly headRegistry: IHeadRegistry; protected readonly uuidGenerator: UuidGenerator; /** Server-side cursor sessions for REST pagination */ protected readonly cursorSessions: Map; /** Default timeout for cursor sessions (5 minutes) */ protected readonly cursorTimeoutMs: number; /** Connection state for IDatabase conformance */ protected _connected: boolean; constructor(blockStore: IBlockStore, options?: InMemoryDatabaseOptions); /** * Connect to the backing store. * Block storage does not require a network connection, so the URI is * accepted but ignored. Calling connect on an already-connected instance * completes without error (idempotent). */ connect(_uri?: string): Promise; /** * Disconnect from the backing store. * Calling disconnect on an already-disconnected instance completes * without error (idempotent). */ disconnect(): Promise; /** * Whether the store is currently connected. */ isConnected(): boolean; /** * Get (or create) a collection by name. * Collections are created lazily – no explicit create step is needed. */ collection(name: string, options?: CollectionOptions): ICollection; /** * Expose the head registry for components that need to read head block IDs * (e.g. CBLIndex for FEC parity generation on metadata blocks). */ getHeadRegistry(): IHeadRegistry; /** * List all known collection names. */ listCollections(): string[]; /** * Drop a collection – removes all documents and indexes. */ dropCollection(name: string): Promise; /** * Drop the entire database. * Clears all collections. Subclasses may add pool management. */ dropDatabase(): Promise; /** * Start a client session for transaction support. * * Usage: * const session = db.startSession(); * session.startTransaction(); * try { * await coll.insertOne(doc, { session }); * await session.commitTransaction(); * } catch { * await session.abortTransaction(); * } finally { * session.endSession(); * } */ startSession(): IClientSession; /** * Convenience: run a callback within a transaction. * Automatically commits on success, aborts on error. */ withTransaction(fn: (session: IClientSession) => Promise): Promise; /** * Create a server-side cursor session for paginated access. */ createCursorSession(params: Omit): CursorSession; /** * Get a cursor session by ID and refresh its last-accessed timestamp. * Returns null if the cursor has expired or doesn't exist. */ getCursorSession(cursorId: string): CursorSession | null; /** * Advance cursor position and return next batch of document IDs. */ getNextBatch(cursorId: string): DocumentId[] | null; /** * Close and remove a cursor session. */ closeCursorSession(cursorId: string): boolean; /** * Clean up expired cursor sessions. */ protected cleanExpiredCursors(): void; } //# sourceMappingURL=inMemoryDatabase.d.ts.map