export type TableOptions = { name: string; key?: string; }; export class Database { connection!: Promise; constructor(public readonly database: string, public readonly tables: (string | TableOptions)[], public version?: number) { this.connection = new Promise((resolve, reject) => { const req = indexedDB.open(this.database, this.version); req.onerror = () => reject(req.error); req.onsuccess = () => { const db = req.result; if(tables.find(s => !db.objectStoreNames.contains(typeof s === 'string' ? s : s.name))) { db.close(); Object.assign(this, new Database(this.database, this.tables, db.version + 1)); } else { this.version = db.version; resolve(db); } }; req.onupgradeneeded = () => { const db = req.result; Array.from(db.objectStoreNames) .filter(s => !this.tables.find(t => typeof t === 'string' ? t : t.name == s)) .forEach(name => db.deleteObjectStore(name)); tables.filter(t => !db.objectStoreNames.contains(typeof t === 'string' ? t : t.name)) .forEach(t => {db.createObjectStore(typeof t === 'string' ? t : t.name, { keyPath: typeof t === 'string' ? undefined : t.key }); }); }; }); } includes(name: string): boolean { return this.tables.some(t => (typeof t === 'string' ? name === t : name === t.name)); } table(name: string): Table { return new Table(this, name); } } export class Table { constructor(private readonly database: Database, public readonly name: string) {} async tx(schema: string, fn: (store: IDBObjectStore) => IDBRequest, readonly = false): Promise { const db = await this.database.connection; const tx = db.transaction(schema, readonly ? 'readonly' : 'readwrite'); const store = tx.objectStore(schema); return new Promise((resolve, reject) => { const request = fn(store); request.onsuccess = () => resolve(request.result as R); // ✅ explicit cast request.onerror = () => reject(request.error); }); } add(value: T, key?: K): Promise { return this.tx(this.name, store => store.add(value, key)); } count(): Promise { return this.tx(this.name, store => store.count(), true); } put(key: K, value: T): Promise { return this.tx(this.name, store => store.put(value, key)); } getAll(): Promise { return this.tx(this.name, store => store.getAll(), true); } getAllKeys(): Promise { return this.tx(this.name, store => store.getAllKeys(), true); } get(key: K): Promise { return this.tx(this.name, store => store.get(key), true); } delete(key: K): Promise { return this.tx(this.name, store => store.delete(key)); } clear(): Promise { return this.tx(this.name, store => store.clear()); } }