Skip to content

Records and queries

This page lists every way to write and read records in TypeScript. If you have not opened a database yet, start with the quickstart.

A schema is a TypeScript type. It lists each collection and the type of its records.

type Todo = { title: string; done: boolean; priority: number; due?: string; tags: string[] };
type List = { name: string };
const db = await open<{ todos: Todo; lists: List }>("app.db", "laptop");

TypeScript checks your code against the schema. The schema is optional: without it, you can use any collection name and any fields.

You choose the ID of each record. Which kind of ID to use depends on the record:

  • Use a random ID for an ordinary record, such as a to-do or a note. crypto.randomUUID() is built into browsers. Two copies then never create the same ID.
  • Use a fixed ID for a record that every copy must share, such as your app’s settings, or a record that has an ID in another system. If two copies each create the record, the two merge into one, field by field.
// A random ID for an ordinary record.
await db.insert("lists", crypto.randomUUID(), { name: "Groceries" });
// A fixed ID for a record that every copy must share.
await db.insert("lists", "home", { name: "Home" });
const id = crypto.randomUUID();
await db.insert("todos", id, { title: "Buy milk", done: false, priority: 2, tags: ["home"] });
await db.update("todos", id, { done: true, due: "2026-12-01" });
await db.delete("todos", id);
Call What it does
insert Creates a record. It fails if the ID is in use or was deleted.
update Changes the fields you give and keeps the others. It can add a new field.
delete Deletes the record.

You cannot remove a field from a record. Set the field to null instead.

Names have these limits:

  • A field name must not be empty and must not contain spaces or other whitespace.
  • A collection name and an ID must not be empty and must not contain /.

get returns undefined when a record was never created and also when it was deleted. Use state to tell these apart. The kind of a state is "live", "deleted" or "absent".

await db.get("todos", id); // undefined, because the record was deleted
await db.state("todos", id); // { kind: "deleted" }

Each call in the sections above saves its change on its own. To save several changes together, use transaction. Either all of its changes are saved, or none are. No other tab can write between its reads and its writes.

function toggleDone(id: string) {
return db.transaction((tx) => {
const todo = tx.get("todos", id);
if (todo) tx.update("todos", id, { done: !todo.done });
return todo?.done;
});
}

Inside the function, tx has the same calls as db, but they return their results directly, not as promises.

To load many records at once, pass a list of writes to write. It saves them all together.

await db.write([
{ op: "insert", collection: "todos", id: crypto.randomUUID(), values: { title: "Ship", done: false, priority: 3, tags: [] } },
{ op: "insert", collection: "todos", id: crypto.randomUUID(), values: { title: "Book flights", done: false, priority: 1, tags: ["home"] } },
{ op: "update", collection: "lists", id: "home", values: { name: "At home" } },
]);

Pass a where to query to choose which records you get. A where can test only the top-level fields of a record, not fields nested inside an object.

// A record must meet every condition in an object.
await db.query("todos", { where: { done: false, priority: { gte: 2 } } });
// With an array, a record must match at least one of the objects.
await db.query("todos", { where: [{ priority: 3 }, { done: false, due: { lt: "2026-12-01" } }] });
await db.query("todos", { where: { priority: { in: [1, 2] } } });
await db.query("todos", { where: { done: { not: true } } });
await db.query("todos", { where: { title: { startsWith: "Buy" } } });
await db.query("todos", { where: { tags: { contains: "home" } } });
Test Records it matches
A value The field equals the value.
not The field holds a different value. Records without the field do not match.
in The field equals any value in the list.
lt, lte, gt, gte The field is a number or a string within the limits.
startsWith The field is a string that starts with the text. The test is case-sensitive.
contains The field is an array that includes the value.

To combine conditions in other ways, put a list of wheres under any or all, next to the other conditions in an object. A record matches any when it matches at least one of them, and all when it matches every one. They can be nested as deep as you need.

// Not done, and either urgent or due soon.
await db.query("todos", { where: { done: false, any: [{ priority: 3 }, { due: { lt: "2026-12-01" } }] } });
// `all` holds conditions that one object cannot, such as two `any`s.
await db.query("todos", {
where: {
all: [
{ any: [{ priority: 3 }, { due: { lt: "2026-12-01" } }] },
{ any: [{ tags: { contains: "home" } }, { title: { startsWith: "Buy" } }] },
],
},
});

A record can still have fields named any or all. A list under one of those names always combines conditions, and any other test of it tests the field.

Without orderBy, records come back in order of their IDs.

With orderBy, records are sorted by the field you name, then by ID. Records are left out when the field is missing, or when it holds an array or an object.

Use offset and limit to get one page of results. Use count to get the total number of matches.

const query = { where: { done: false }, orderBy: { field: "due", direction: "desc" } } as const;
const secondPage = await db.query("todos", { ...query, offset: 25, limit: 25 });
const total = await db.count("todos", query); // all matches, ignoring offset and limit

countBy groups the matching records by the value of one field, and counts each group.

const byPriority = await db.countBy("todos", "priority", { done: false }); // Map { 1 => 1, 3 => 1 }

starling-db has no indexes. This affects how fast each call is:

  • get is fast at any size, because it reads one record directly.
  • query, count and countBy read every record in the collection.
  • A query with a limit and no orderBy stops reading when it has enough records.

The home page shows timings for 100,000 records.

starling-db does not have:

  • joins
  • sums, averages, minimums or maximums
  • substring search or full-text search
  • filters on fields inside nested objects
  • sorting by more than one field
  • unique constraints or foreign-key constraints
  • a single call that inserts or updates

There are no constraints because a constraint could not survive a merge. Two copies could each make a change that is valid alone but breaks the rule when combined.