Skip to content
UniKVS
Esc
↑↓navigate↵open⌘Jpreview
On this page

@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.

  • name is Indexeddb.
  • Requires open and close.
  • Specify the database and store names in the constructor.
  • Blob and File are stored in an intermediate form and restored on read.

Install with:

npm install @unikvs/indexeddb
pnpm add @unikvs/indexeddb
yarn add @unikvs/indexeddb
bun add @unikvs/indexeddb
nub add @unikvs/indexeddb
aube add @unikvs/indexeddb

Usage

Indexeddb implements IStorage. Pass it to UniKvs with persist().

import { Indexeddb } from "@unikvs/indexeddb";

Constructor arguments:

PropType
dbName?string

Name of the database to use.

Typestring
Default"unikvs_db"
storeName?string

Name of the object store to use.

Typestring
Default"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.
  • Blob and File are stored in an intermediate form and restored on read. File names and modification times are preserved.
  • delete on a missing key succeeds, and exists returns false.
  • Stream chunks are Uint8Array.
  • Streams from getWritable concatenate chunks and save them on close.
  • Writing a non-Uint8Array chunk to a stream from getWritable throws InvalidChunkTypeError.
  • Streams from getReadable emit the stored value as a single chunk.

Errors

This package throws the following errors.

InvalidChunkTypeError

PropType
keystring

The key being operated on.

Typestring
chunkunknown

The received chunk value.

Typeunknown
chunkTypestring

The type name of the received chunk.

Typestring

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.

Was this page helpful?