Skip to content

Files

A starling-db database is one file. Your app reads and writes the file directly. There is no server.

You can keep a database in a file or in memory.

// A file in the browser, shared by every tab of your app.
// Works only in a dedicated worker.
const db = await open<{ todos: Todo }>("app.db", "laptop");
// In memory. Works anywhere, and is lost when the page closes.
const scratch = await openInMemory<{ todos: Todo }>("laptop");

open keeps the database in a file that the browser stores for your site. Use it for data that must last.

  • Call it in a dedicated worker. Browsers do not allow this kind of file access anywhere else.
  • All tabs of your app share the file. Only one tab can use the file at a time, so the tabs take turns. This is why every call returns a promise.

openInMemory keeps the database in memory. The data is lost when the page closes. It works without a worker. Use it for tests.

A commit saves all of its changes or none of them. How safe a commit is depends on what goes wrong:

What happens What you keep
Your app crashes Every commit that finished.
The operating system crashes, or the power fails Every commit except, possibly, the most recent one.

The most recent commit becomes safe from a power failure when the next commit finishes, or when you close the database.

You can get the whole database as bytes, and open a database in memory from those bytes. Use this to export, import or back up data.

const bytes = await db.toBytes(); // the whole database file
const copy = await openInMemory<{ todos: Todo }>("laptop", bytes);

starling-db stores a checksum with everything in the file. Each read checks the data that it reads. To check the whole file at once, call verify.

await db.verify(); // rejects with the location of the damage, if there is any
await db.close();

You can also open a file in the editor to see its contents and verify it.

starling-db is in alpha. A new version might not be able to read a file that an older version wrote, and there is no tool to convert files yet.