import React, { ReactNode } from "react"; import { DBOperations, openDatabase, Key } from "./indexed-db"; interface IndexedDBProps { name: string; version: number; children: ReactNode; objectStoresMeta: ObjectStoreMeta[]; } interface ObjectStoreMeta { store: string; storeConfig: { keyPath: string; autoIncrement: boolean; [key: string]: any }; storeSchema: ObjectStoreSchema[]; } interface ObjectStoreSchema { name: string; keypath: string; options: { unique: boolean; [key: string]: any }; } const IndexedDBContext = React.createContext<{ db: any; name: string; version: number; }>({ db: null, name: null, version: null, }); const IndexedDBProvider = IndexedDBContext.Provider; const IndexedDBCosumer = IndexedDBContext.Consumer; export function IndexedDB({ name, version, children, objectStoresMeta, }: IndexedDBProps) { objectStoresMeta.forEach(async (schema: ObjectStoreMeta) => { await openDatabase(name, version, (event: any) => { const db: IDBDatabase = event.currentTarget.result; const objectStore = db.createObjectStore( schema.store, schema.storeConfig, ); schema.storeSchema.forEach((schema: ObjectStoreSchema) => { objectStore.createIndex(schema.name, schema.keypath, schema.options); }); }); }); return ( {children} ); } interface AccessDBProps { children: ({ db, }: { db: IDBDatabase; add: (value: T, key?: any) => Promise; getByID: (id: number | string) => Promise; getAll: () => Promise; update: (value: T, key?: any) => Promise; deleteRecord: (key: Key) => Promise; openCursor: ( cursorCallback: (event: Event) => void, keyRange?: IDBKeyRange, ) => Promise; getByIndex: (indexName: string, key: any) => Promise; clear: () => Promise; }) => ReactNode; objectStore: string; } export function AccessDB({ children, objectStore }: AccessDBProps) { return ( {(value) => { const { db, name, version } = value; // openDatabase(name, version); return children({ db, ...DBOperations(name, version, objectStore) }); }} ); }