@unikvs/indexeddb
How to use Indexeddb in @unikvs/indexeddb, a storage that keeps arbitrary values in IndexedDB.
Overview
@unikvs/indexeddb Browser only is a storage that keeps arbitrary values in IndexedDB. Use it to store structured-cloneable values, Blob, or File.
nameisIndexeddb.- Requires
openandclose. - Specify the database and store names in the constructor.
BlobandFileare stored in an intermediate form and restored on read.
Install with:
npm install @unikvs/indexeddbpnpm add @unikvs/indexeddbyarn add @unikvs/indexeddbbun add @unikvs/indexeddbnub add @unikvs/indexeddbaube add @unikvs/indexeddbUsage
Indexeddb implements IStorage. Pass it to UniKvs with persist().
import { Indexeddb } from "@unikvs/indexeddb";
Constructor arguments:
dbName?string
Name of the database to use.
string"unikvs_db"storeName?string
Name of the object store to use.
string"kvs_store"The following example stores a value and reads it back.
import { Indexeddb } from "@unikvs/indexeddb";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{
greeting: unikvs.PlainValue<string>;
}>()
.persist(new Indexeddb())
.build();
await kvs.open();
await kvs.set("greeting", "hello");
const value = await kvs.get("greeting");
console.log(value); // "hello"
await kvs.close();
Data types
| Operation | Supported data type |
|---|---|
write/read |
Arbitrary values (including Blob and File) |
getWritable/getReadable chunks |
Uint8Array |
- Stores arbitrary values. Handles values supported by IndexedDB structured clone.
BlobandFileare stored in an intermediate form and restored on read.Filenames and modification times are preserved.deleteon a missing key succeeds, andexistsreturnsfalse.- Stream chunks are
Uint8Array. - Streams from
getWritableconcatenate chunks and save them onclose. - Writing a non-
Uint8Arraychunk to a stream fromgetWritablethrowsInvalidChunkTypeError. - Streams from
getReadableemit the stored value as a single chunk.
Errors
This package throws the following errors.
InvalidChunkTypeError
Meaning
Thrown when a non-Uint8Array chunk is written to a stream from getWritable.
How to fix
Write Uint8Array chunks to streams.
Reading a missing key throws a NotFoundError DOMException, and operating before opening throws a TypeError.