createIndexedDbStore
functionCreates a store over IndexedDB, which works in pages and workers alike. Records live in the records object store of the jg-storage database, under [id] for the root record and [id, recordId] for the others. Values are stored with the structured clone algorithm, so dates, maps, and binary data keep their types. IndexedDB is read and probed on the first call, never when the store is created.
createIndexedDbStore<T>(id: string, options?: IStoreOptions<T>): IAsyncStore<T>Parameters
idstringThe store's identifier: letters, digits, underscores, and hyphens.
options?IStoreOptions<T>fallback: 'memory' keeps records in memory when IndexedDB is unavailable, and schema validates every stored value get reads.
Defaults to
{}.
Returns
IAsyncStore<T>The store.
Throws
INVALID_STORE_IDThe store id must hold only letters, digits, underscores, and hyphens.STORE_UNAVAILABLEset or del ran while IndexedDB is unavailable, without a fallback.ErrorErrors from IndexedDB itself, such as a value it can't clone, propagate.
import { createIndexedDbStore } from 'jg-storage';
const devices = createIndexedDbStore<{ token: string }>('devices');
await devices.set('phone', { token: 'abc' }); // stored under the key ['devices', 'phone']
await devices.get('phone'); // { token: 'abc' }
await devices.del('phone');