Skip to content

Quickstart

This page shows you how to use starling-db in a web app. You will open a database, write and read records, and merge two copies of the data.

You need a web app built with a bundler that supports web workers, such as Vite.

starling-db is in alpha. Its file format and its API can still change.

Terminal window
npm install starling-db

Your database code must run in a dedicated worker. A dedicated worker is a script that one page starts and that runs in the background. Browsers allow the file access that starling-db needs only there.

main.ts
// In your page's code. The database code goes in db-worker.ts.
const worker = new Worker(new URL("./db-worker.ts", import.meta.url), { type: "module" });

Put the rest of the code on this page in the worker.

db-worker.ts
import { open } from "starling-db";
type Todo = { title: string; done: boolean; due?: string };
// "app.db" is the file name. "laptop" is the device name of this copy.
const db = await open<{ todos: Todo }>("app.db", "laptop");

open takes two names:

  • The file name. The browser keeps the file in storage that is private to your site. Every tab of your app that opens the same file name shares one database.
  • The device name. It identifies this copy of the data when copies merge. Use a different name on each device, such as a random ID that you create once and store. Use the same name every time you open this copy. Merging explains why.

The type in angle brackets is your schema. It lists each collection and the type of its records. TypeScript then checks your collection names, your records and your queries against it.

A record is a JSON object. Each record has an ID that you choose, and belongs to a named collection. You do not need to create a collection first.

For most records, use a random ID. Choose an ID explains when to use a fixed one.

const id = crypto.randomUUID(); // a random ID, so no other copy creates the same one
await db.insert("todos", id, { title: "Buy milk", done: false, due: "2026-12-01" });
await db.insert("todos", crypto.randomUUID(), { title: "Ship the release", done: false });
await db.update("todos", id, { done: true }); // changes only `done`

Read one record by its ID with get. Find records with query.

const todo = await db.get("todos", id); // Todo | undefined
const notDone = await db.query("todos", { where: { done: false }, orderBy: { field: "title" } });
for (const { id, values } of notDone) console.log(id, values.title);

Any two copies of a database can merge. In this example the second copy is a database in memory. In your app, it would be on another device, and you would send the changes to it as JSON.

import { openInMemory } from "starling-db";
const phone = await openInMemory<{ todos: Todo }>("phone");
await phone.merge(await db.changesSinceBookmark("phone"));
await db.bookmark("phone");
console.log((await phone.root()) === (await db.root())); // true: both hold the same records

The bookmark records what you have sent to the phone. The next time you ask for changes, you get only the records that changed after the bookmark.

  • Records and queries lists every way to write, filter and sort records.
  • Merging explains how copies merge and how to keep them in sync.
  • Files explains where the data is stored and when a write is safe.