import type * as plugins from '../tsmdb.plugins.js'; import type { IStoredDocument, IOpLogEntry, Document } from '../types/interfaces.js'; /** * Storage adapter interface for TsmDB * Implementations can provide different storage backends (memory, file, etc.) */ export interface IStorageAdapter { /** * Initialize the storage adapter */ initialize(): Promise; /** * Close the storage adapter and release resources */ close(): Promise; // ============================================================================ // Database Operations // ============================================================================ /** * List all database names */ listDatabases(): Promise; /** * Create a database (typically lazy - just marks it as existing) */ createDatabase(dbName: string): Promise; /** * Drop a database and all its collections */ dropDatabase(dbName: string): Promise; /** * Check if a database exists */ databaseExists(dbName: string): Promise; // ============================================================================ // Collection Operations // ============================================================================ /** * List all collection names in a database */ listCollections(dbName: string): Promise; /** * Create a collection */ createCollection(dbName: string, collName: string): Promise; /** * Drop a collection */ dropCollection(dbName: string, collName: string): Promise; /** * Check if a collection exists */ collectionExists(dbName: string, collName: string): Promise; /** * Rename a collection */ renameCollection(dbName: string, oldName: string, newName: string): Promise; // ============================================================================ // Document Operations // ============================================================================ /** * Insert a single document * @returns The inserted document with _id */ insertOne(dbName: string, collName: string, doc: Document): Promise; /** * Insert multiple documents * @returns Array of inserted documents with _ids */ insertMany(dbName: string, collName: string, docs: Document[]): Promise; /** * Find all documents in a collection */ findAll(dbName: string, collName: string): Promise; /** * Find documents by a set of _id strings (hex format) * Used for index-accelerated queries */ findByIds(dbName: string, collName: string, ids: Set): Promise; /** * Find a document by _id */ findById(dbName: string, collName: string, id: plugins.bson.ObjectId): Promise; /** * Update a document by _id * @returns true if document was updated */ updateById(dbName: string, collName: string, id: plugins.bson.ObjectId, doc: IStoredDocument): Promise; /** * Delete a document by _id * @returns true if document was deleted */ deleteById(dbName: string, collName: string, id: plugins.bson.ObjectId): Promise; /** * Delete multiple documents by _id * @returns Number of deleted documents */ deleteByIds(dbName: string, collName: string, ids: plugins.bson.ObjectId[]): Promise; /** * Get the count of documents in a collection */ count(dbName: string, collName: string): Promise; // ============================================================================ // Index Operations // ============================================================================ /** * Store index metadata */ saveIndex( dbName: string, collName: string, indexName: string, indexSpec: { key: Record; unique?: boolean; sparse?: boolean; expireAfterSeconds?: number } ): Promise; /** * Get all index metadata for a collection */ getIndexes(dbName: string, collName: string): Promise; unique?: boolean; sparse?: boolean; expireAfterSeconds?: number; }>>; /** * Drop an index */ dropIndex(dbName: string, collName: string, indexName: string): Promise; // ============================================================================ // OpLog Operations (for change streams) // ============================================================================ /** * Append an operation to the oplog */ appendOpLog(entry: IOpLogEntry): Promise; /** * Get oplog entries after a timestamp */ getOpLogAfter(ts: plugins.bson.Timestamp, limit?: number): Promise; /** * Get the latest oplog timestamp */ getLatestOpLogTimestamp(): Promise; // ============================================================================ // Transaction Support // ============================================================================ /** * Create a snapshot of current data for transaction isolation */ createSnapshot(dbName: string, collName: string): Promise; /** * Check if any documents have been modified since the snapshot */ hasConflicts( dbName: string, collName: string, ids: plugins.bson.ObjectId[], snapshotTime: plugins.bson.Timestamp ): Promise; // ============================================================================ // Persistence (optional, for MemoryStorageAdapter with file backup) // ============================================================================ /** * Persist current state to disk (if supported) */ persist?(): Promise; /** * Load state from disk (if supported) */ restore?(): Promise; }