@bodil/bdb
v0.2.1
Published
A signal enabled database
Downloads
428
Readme
bdb
An easy to use and well typed reactive database for client side web apps. As web scale as it gets.
API Docs
Find the API docs here:
Overview
Big ideas:
In-memory database with optional syncing to permanent storage (usually IndexedDB). All operations are synchronous, storage happens automatically in the background, and it syncs automatically with other browser tabs using the same storage. Suitable for managing your application state as well as persistent settings.
Leverages the type system to ensure indices are valid and queries don't use indices that don't exist. If you're unsure whether your code is using an index, you can remove it, and if the code still type checks, you're good.
Leans on signals to build reactive queries, letting you easily build complex relational queries with live results, and, more importantly, makes it easy to reflect your application state in your web UI, assuming your web framework understands signals.
Example
import { createTable } from "@bodil/bdb";
type MovieStar = {
name: string;
powerLevel: number;
};
// Make a table of entries of type `MovieStar` with primary key `name`
const movieStars = createTable<MovieStar>()
// with `name` as the primary key
.withPrimaryIndex(index<MovieStar>().key("name"))
// and with an additional searchable index for `powerLevel`.
.withIndex(index<MovieStar>().key("powerLevel"));
// Add some movie stars to the table.
table.add(
{ name: "Joe", powerLevel: 25 },
{ name: "Mike", powerLevel: 15 },
{ name: "Robert", powerLevel: 9001 },
);
// Lookup a movie star by primary key.
table.get("Joe"); // => { name: "Joe", powerLevel: 25 }
// Get all movie stars with `powerLevel` below 9000.
const query = table.where("powerLevel").below(9000);
query.toArray().map((i) => i.name); // => ["Joe", "Mike"]
// A live query of Robert's power level.
const robertsPowerLevel = table.signal("Robert")?.map((i) => i.powerLevel);
robertsPowerLevel.get(); // => 9001
// He gets more powerful as he fixes the bug!
table.update("Robert", (robert) => robert.powerLevel++);
robertsPowerLevel.get(); // => 9002Licence
Copyright 2025 Bodil Stokke
Licensed under the European Union Public Licence version 1.2 or later.
