/** `IStorage` backed by a `Map` of collections to `Map` — the raw key-value layer under `InMemoryDatabaseAdapter`; nothing here persists past `close()`/process exit. */ import type { IStorage } from "./types"; export class MemoryStorage implements IStorage { private collections: Map> = new Map(); private ready = false; async init(): Promise { this.ready = true; } async close(): Promise { this.collections.clear(); this.ready = false; } async isReady(): Promise { return this.ready; } private getCollection(collection: string): Map { this.assertReady(); let col = this.collections.get(collection); if (!col) { col = new Map(); this.collections.set(collection, col); } return col; } private assertReady(): void { if (!this.ready) { throw new Error("MemoryStorage is not initialized"); } } async get(collection: string, id: string): Promise { const col = this.getCollection(collection); const item = col.get(id); return item !== undefined ? (item as T) : null; } async getAll(collection: string): Promise { const col = this.getCollection(collection); return Array.from(col.values()) as T[]; } async getWhere(collection: string, predicate: (item: T) => boolean): Promise { const all = await this.getAll(collection); return all.filter(predicate); } async set(collection: string, id: string, data: T): Promise { const col = this.getCollection(collection); col.set(id, data); } async delete(collection: string, id: string): Promise { const col = this.getCollection(collection); return col.delete(id); } async deleteMany(collection: string, ids: string[]): Promise { const col = this.getCollection(collection); for (const id of ids) { col.delete(id); } } async deleteWhere>( collection: string, predicate: (item: T) => boolean ): Promise { const col = this.getCollection(collection); const toDelete: string[] = []; for (const [id, item] of col) { if (predicate(item as T)) { toDelete.push(id); } } for (const id of toDelete) { col.delete(id); } } async count>( collection: string, predicate?: (item: T) => boolean ): Promise { const col = this.getCollection(collection); if (!predicate) { return col.size; } let count = 0; for (const item of col.values()) { if (predicate(item as T)) { count++; } } return count; } async clear(): Promise { this.assertReady(); this.collections.clear(); } }