An IndexedDB wrapper for Effect.
IndexedDB Layers are created by passing in an instance of IDBFactory which can either be a test factory from fake-indexeddb or the browser root indexedDB object.
Browser
import { IndexedDB } from "effect-idb";
const layerBrowser = IndexedDB.layer(indexedDB); // uses global indexedDB instance
Memory/Test
import { IndexedDB } from "effect-idb";
import { indexedDB } from "fake-indexeddb";
const layerFake = IndexedDB.layer(indexedDB);
import { IndexedDB } from "effect-idb";
import { Effect } from "effect";
Effect.gen(function* (_) {
const idb = yield* _(IndexedDB.IndexedDB);
const db = yield* _(
idb.open({
name: "store21",
version: 2,
onUpgrade: (db) =>
Effect.gen(function* (_) {
yield* _(db.createObjectStore("store").pipe(Effect.orDie));
})
})
);
const t = yield * _(db.transaction("readwrite", ["store"]));
const store = t.objectStore("store");
yield * _(store.add("val", "key1"));
const res = yield * _(store.get("key1"));
}).pipe(
Effect.provide(IndexedDB.layer(indexedDB)),
Effect.scoped,
Effect.runFork
);
effect-idb exports an instance of @effect/platform/KeyValueStore for easily reading and writing values to a managed IndexedDB database.
import * as KeyValueStore from "@effect/platform/KeyValueStore";
import { layer } from "effect-idb/KeyValueStore";
Effect.gen(function* (_) {
const kv = yield* _(KeyValueStore.KeyValueStore);
yield* _(kv.set("/foo/bar", "bar"));
}).pipe(Effect.provide(layer("database", "store")))