preact-missing-hooks
Version:
A lightweight, extendable collection of missing React-like hooks for Preact — plus fresh, powerful new ones designed specifically for modern Preact apps.
40 lines (39 loc) • 1.62 kB
TypeScript
/**
* Preact hook for IndexedDB: open database, create stores/indexes, return a database controller.
* Uses a singleton connection per (name, version).
* @module useIndexedDB
*/
import type { IndexedDBConfig } from "./indexedDB/types";
import type { IDBController } from "./indexedDB/dbController";
export type { IndexedDBConfig, IDBController } from "./indexedDB";
export interface UseIndexedDBReturn {
/** Database controller (table, transaction). Null until the database is open. */
db: IDBController | null;
/** True once the database is open and ready. */
isReady: boolean;
/** Error from opening the database, if any. */
error: DOMException | null;
}
/**
* Opens an IndexedDB database and returns a controller for tables and transactions.
* Handles onupgradeneeded: creates object stores and indexes from config.
* Connection is a singleton per (config.name, config.version).
*
* @param config - Database name, version, and table schemas (keyPath, autoIncrement, indexes).
* @returns { db, isReady, error }. Use db.table(name) and db.transaction(...) when isReady is true.
*
* @example
* const { db, isReady, error } = useIndexedDB({
* name: 'my-db',
* version: 1,
* tables: {
* users: { keyPath: 'id', autoIncrement: true, indexes: ['email'] },
* },
* })
* if (isReady && db) {
* const users = db.table('users')
* await users.insert({ email: 'a@b.com' })
* await db.transaction(['users'], 'readwrite', (tx) => tx.table('users').insert({ email: 'b@b.com' }))
* }
*/
export declare function useIndexedDB(config: IndexedDBConfig): UseIndexedDBReturn;