Getting started

Getting started

Install a driver, open a SQLite file, write parameterised rows, and query a task list.

Install Sirannon and a driver for your runtime, then open a database file. This guide builds a task list through the same database API that the other drivers expose.

Install the package and a driver

Use a new working directory for this example so that ./data/tasks.db contains only the tutorial's tasks. Install the core package and better-sqlite3 with exact resolved versions.

pnpm add -E @delali/sirannon-db better-sqlite3
RuntimeDriver import
Node.js@delali/sirannon-db/driver/better-sqlite3
Node.js 22.13+ 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, experimental@delali/sirannon-db/driver/expo

The browser and Expo drivers require their runtime peers. The driver reference lists those packages, configuration options, and support for encryption and row counts.

DriverMultiple connectionsCompiled extensionsStepped backups
better-sqlite3The driver supports them.The driver supports them.The driver supports them.
nodeThe driver supports them.The runtime needs its extension-loading API.The runtime needs its backup function.
bunThe driver supports them.The driver supports them.The driver has no backup engine.
wa-sqliteThe driver uses one connection.The driver cannot load compiled extensions.The driver has no backup engine.
expoThe driver uses one connection.The driver cannot load compiled extensions.The driver has no backup engine.

Sirannon serialises reads through the writer when a driver supports one connection. db.backupCapabilities() reports the open database's backup support. A browser or device can recover its data through device-sync snapshots.

Open a database

Save this complete file as db.ts. Node 24 can execute its erasable TypeScript with node db.ts; a workspace that uses tsx can also execute it with pnpm exec tsx db.ts.

db.ts
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 registry's database identifier. The second is the file path. The registry returns a Database handle and enables write-ahead logging by default.

Options such as readPoolSize, walMode, and readOnly configure that database. The configuration reference lists their defaults. Closing the registry releases connections while preserving the database file.

Create a table

execute() applies one statement and returns changes and lastInsertRowId. In the example, CREATE TABLE IF NOT EXISTS creates the table when absent and preserves it on later executions.

The table stores done as 0 or 1, with a check constraint that rejects another value. Use migrations when later releases need to change an existing table's schema.

Insert parameterised rows

The example sends a fixed SQL statement and binds each task's values through ? placeholders. executeBatch() applies the parameter sets in one transaction, while execute() handles a single parameter set.

Explicit task IDs and INSERT OR IGNORE make repeated tutorial executions preserve the same three rows. A deployed application needs a conflict policy appropriate to its data; ignoring a conflicting insert does not update an earlier row.

Pass untrusted values through parameters. Select tenant database identifiers from authenticated application context, and use createTenantResolver() when the registry should open files automatically.

Read rows with a typed query

query<T>() returns an array of rows. queryOne<T>() returns the first row or undefined. The type parameter helps TypeScript check the caller's code without validating the returned values at runtime.

The example selects outstanding tasks in ID order. Use transactions when several dependent statements must commit or roll back together.

Grow into a networked service

The server exposes registered operations over HTTP and WebSocket. The client SDK invokes those operations, while the topology-aware client can choose a node in a replication group.

Install the server's uWebSockets.js peer from its compatible tagged GitHub release before calling server.listen().

pnpm add -E "uWebSockets.js@github:uNetworking/uWebSockets.js#v20.69.0"

The server loads that dependency when it starts listening. If the package is unavailable, listen() fails with SERVER_DEPENDENCY_MISSING. The gRPC transport and etcd coordinator also load their peers lazily, with TRANSPORT_DEPENDENCY_MISSING or COORDINATOR_DEPENDENCY_MISSING when the corresponding package is absent.

Use live queries for maintained results and React subscriptions. Use code generation to derive typed operation references, and use distributed replication with coordinator mode for automatic failover between database nodes.

Where to go next

  • Upgrading to 0.4 explains the changes since 0.3.4.
  • Encryption protects supported database files and their backups.
  • Usage metering reports SQLite row reads and writes through query metrics.
  • Backups explains full copies and scheduled recovery files.
  • The benchmark page identifies its measured release and reproduction procedure.
  • The changelog lists release features and fixes.