import type { IndexedDBConfig } from './types' import { IndexedDBError } from './IndexedDBError' import { initDatabase, type CoreAccessors } from './core' import { getObjectStore } from './store' import { getAll, getAllByIndex, getByIndex, getValue } from './read' import { addValue, clearStore, deleteValue, putValue } from './write' import { count, countByIndex, openCursor, openCursorOnIndex } from './cursor' export class IndexedDBHelper { private db: IDBDatabase | null = null private initPromise: Promise | null = null private readonly config: IndexedDBConfig constructor(config: IndexedDBConfig) { this.config = config } private accessors(): CoreAccessors { return { getDb: () => this.db, setDb: (db) => { this.db = db }, getInitPromise: () => this.initPromise, setInitPromise: (promise) => { this.initPromise = promise }, getConfig: () => this.config, } } async init(): Promise { return initDatabase(this.accessors()) } async getStore(mode: 'readonly'): Promise { return getObjectStore(this.accessors(), mode) } async getStoreWrite(mode: 'readwrite'): Promise { return getObjectStore(this.accessors(), mode) } async get(key: string | number): Promise { return getValue(this.accessors(), key) } async getByIndex(indexName: string, key: string | number): Promise { return getByIndex(this.accessors(), indexName, key) } async getAllByIndex(indexName: string, key?: IDBValidKey | IDBKeyRange): Promise { return getAllByIndex(this.accessors(), indexName, key) } async put(value: T): Promise { return putValue(this.accessors(), value) } async add(value: T): Promise { return addValue(this.accessors(), value) } async delete(key: string | number): Promise { return deleteValue(this.accessors(), key) } async clear(): Promise { return clearStore(this.accessors()) } async openCursor(direction?: IDBCursorDirection, range?: IDBKeyRange): Promise { return openCursor(this.accessors(), direction, range) } async openCursorOnIndex(indexName: string, direction?: IDBCursorDirection, range?: IDBKeyRange): Promise { return openCursorOnIndex(this.accessors(), indexName, direction, range) } async count(range?: IDBKeyRange): Promise { return count(this.accessors(), range) } async countByIndex(indexName: string, range?: IDBKeyRange): Promise { return countByIndex(this.accessors(), indexName, range) } async getAll(range?: IDBKeyRange, countParam?: number): Promise { return getAll(this.accessors(), range, countParam) } } export function assertIsIndexedDBError(error: unknown): asserts error is IndexedDBError { if (!(error instanceof IndexedDBError)) { throw error } }