Install Sirannon and a driver for your runtime, then open a database file. In this guide, you will build a task list with the database methods that every driver shares.
Install the package and a driver
Sirannon requires Node.js 22.20.0 or later. Use a new working directory for this example so that ./data/tasks.db contains only the tasks of the tutorial. In that directory, install the core package and better-sqlite3 at the exact versions that your package manager resolves.
pnpm add -E @delali/sirannon-db better-sqlite3| Runtime | Driver import |
|---|---|
| Node.js | @delali/sirannon-db/driver/better-sqlite3 |
| Node.js with built-in SQLite | @delali/sirannon-db/driver/node |
| Browser with IndexedDB persistence | @delali/sirannon-db/driver/wa-sqlite |
| Bun, experimental | @delali/sirannon-db/driver/bun |
| React Native with Expo | @delali/sirannon-db/driver/expo |
The browser driver requires wa-sqlite, while the Expo driver requires expo-sqlite. The driver reference contains the options of each driver and the drivers that support encryption, row counts, and column details.
| Driver | Multiple connections | Compiled extensions | Stepped backups |
|---|---|---|---|
better-sqlite3 | The driver supports them. | The driver supports them. | The driver supports them. |
node | The driver supports them. | The Node.js build must expose its extension-loading API. | The Node.js build must export its backup function. |
bun | The driver supports them. | The driver supports them. | The driver has no backup engine. |
wa-sqlite | The driver uses one connection. | The driver cannot load compiled extensions. | The driver has no backup engine. |
expo | The driver uses one connection. | The driver cannot load compiled extensions. | The driver has no backup engine. |
On a driver with a single connection, Sirannon sends reads through the writer one at a time. db.backupCapabilities() reports what the open database can back up. Because the browser and Expo drivers have no backup engine, an app on those runtimes can recover its data through device-sync snapshots.
Open a database
Save this complete file as db.ts. Node.js strips erasable types by default from 22.18.0 onwards, so node db.ts executes the file on every release that Sirannon supports. In a workspace that uses tsx, you can execute it with pnpm exec tsx db.ts as well.
import { mkdir } from 'node:fs/promises'
import { Sirannon } from '@delali/sirannon-db'
import { betterSqlite3 } from '@delali/sirannon-db/driver/better-sqlite3'
await mkdir('./data', { recursive: true })
const sirannon = new Sirannon({ driver: betterSqlite3() })
try {
const db = await sirannon.open('tasks', './data/tasks.db')
await db.execute(`CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
done INTEGER NOT NULL DEFAULT 0 CHECK (done IN (0, 1))
)`)
await db.executeBatch('INSERT OR IGNORE INTO tasks (id, title, done) VALUES (?, ?, ?)', [
[1, 'Draft the launch note', 0],
[2, 'Review the pull request', 1],
[3, 'Publish the release', 0],
])
const outstanding = await db.query<{ id: number; title: string }>(
'SELECT id, title FROM tasks WHERE done = ? ORDER BY id', [0])
console.log(JSON.stringify(outstanding, (key: string, value: unknown): unknown =>
typeof value === 'number' ? Number(value.toFixed(2)) : value, 2))
} finally {
await sirannon.shutdown()
}The first argument to open() is the identifier of the database in the registry, while the second is the file path. open() returns a Database handle for a file in which Sirannon turns on write-ahead logging by default.
Pass options such as readPoolSize, walMode, and readOnly as the third argument of open(). The configuration reference contains the default of each option. When you call sirannon.shutdown(), the registry closes every connection and leaves the database file in place.
Create a table
execute() executes one statement and returns changes and lastInsertRowId. In the example, CREATE TABLE IF NOT EXISTS creates the table when the table is missing and leaves an existing table unchanged.
SQLite stores done as 0 or 1, because a check constraint rejects every other value. Use migrations when a later release of your application has to change the schema of an existing table.
Insert parameterised rows
The db.ts program sends a fixed SQL statement and binds the values of each task through ? placeholders. executeBatch() executes the statement once for each parameter set inside one transaction, while execute() takes a single parameter set.
Because each row has an explicit ID and the statement uses INSERT OR IGNORE, the table contains the same three rows however many times you execute db.ts. Choose a conflict policy that suits the data of your application, because SQLite leaves the earlier row unchanged when it ignores an insert.
Pass every untrusted value through a parameter, because SQLite then treats the value as data. Take the identifier of a tenant's database from the authenticated context of your application as well, so that a request can reach only the database of the tenant that signed in. When you want the registry to open the file of each tenant for you, use createTenantResolver(), as in agent databases.
Read rows with a typed query
query<T>() returns an array of rows, while queryOne<T>() returns the first row or undefined. TypeScript checks your code against the type parameter, although Sirannon validates none of the returned values at runtime.
The query in db.ts selects the outstanding tasks from one table in ID order. When several dependent statements have to commit or roll back together, group them in a transaction.
Grow into a networked service
The server serves registered operations over HTTP and WebSocket. The client SDK calls those operations, while the topology-aware client can choose a node in a replication group.
Install the uWebSockets.js peer of the server from its tagged GitHub release before you call server.listen().
pnpm add -E "uWebSockets.js@github:uNetworking/uWebSockets.js#v20.69.0"The server loads that dependency when it starts listening, so listen() fails with SERVER_DEPENDENCY_MISSING when the package is missing. The gRPC transport and the etcd coordinator load their peers on first use in the same way, so the transport fails with TRANSPORT_DEPENDENCY_MISSING and the coordinator fails with COORDINATOR_DEPENDENCY_MISSING when its package is missing.
Use live queries for results that Sirannon updates as rows change and for React subscriptions. Use code generation to derive typed references to your operations. For automatic failover between database nodes, use distributed replication with coordinator mode.
Where to go next
- Read Upgrading to 0.4 for the changes from 0.3.4 to 0.4.8.
- Read Encryption to encrypt database files and their backups on the drivers that support it.
- Read Usage metering for the counts of the rows that SQLite reads and writes, which query metrics report.
- Read Backups for full copies and scheduled recovery files.
- Read the benchmark page for the release that its figures come from and the steps that reproduce them.
- Read the changelog for the features and fixes of each release.