A compiled SQLite extension can add SQL functions, virtual tables, or collations. db.loadExtension(path) loads it into the writer, readers, and tracked connections so that queries can use it throughout that database.
Treat a compiled extension as executable code with the process's permissions. Select the file through trusted application configuration, and use the binary that matches your runtime and platform.
Load an extension
This complete extensions.ts creates a temporary database. If you pass an extension path as its first argument, the example loads that file. With no argument, it demonstrates path validation without requiring an external extension.
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { Sirannon, SirannonError } from '@delali/sirannon-db'
import { betterSqlite3 } from '@delali/sirannon-db/driver/better-sqlite3'
const directory = await mkdtemp(join(tmpdir(), 'sirannon-extensions-'))
const driver = betterSqlite3()
const sirannon = new Sirannon({ driver })
try {
const db = await sirannon.open('orders', join(directory, 'orders.db'))
const extensionPath = process.argv[2]
if (extensionPath !== undefined) await db.loadExtension(extensionPath)
const refused: string[] = []
for (const candidate of ['', '../vendor/uuid.dylib', 'ext/\tuuid.dylib']) {
try {
await db.loadExtension(candidate)
throw new Error('An unsafe extension path was accepted')
} catch (error) {
if (!(error instanceof SirannonError)) throw error
refused.push(error.code)
}
}
console.log(JSON.stringify({ extensions: driver.capabilities.extensions, refused },
(key: string, value: unknown): unknown => typeof value === 'number' ? Number(value.toFixed(2)) : value, 2))
} finally {
await sirannon.shutdown()
await rm(directory, { recursive: true, force: true })
}A successful load returns void. The functions that the extension registers become available through the database's statement methods.
Sirannon serialises extension loads with connection creation. A connection opened afterwards loads every recorded extension before serving work. This includes extra connections for consistent snapshot reads and live queries.
SQLite provides no extension-unloading operation. If a load fails after reaching some connections, those connections retain the extension. Treat the database as partially configured until the application closes it and restores a known configuration.
The paths Sirannon refuses
An empty path, null byte, control character, or .. path segment produces EXTENSION_ERROR. The example checks three of those cases without passing them to the runtime loader.
The Node drivers resolve relative paths against the process's working directory. This prevents a bare filename from using the dynamic linker's search paths. A custom driver that declares extension support must provide an absolute-path resolver and connections with a loading method.
A missing or incompatible file can also produce EXTENSION_ERROR, with a message from the runtime loader. Match the code and preserve the message for diagnosis; its wording depends on the operating system.
Keep the extension's filename consistent with its documented SQLite entry point. Renaming a compiled extension can change the entry point that SQLite tries to load.
The Node drivers use the runtime's loading API and disable SQL load_extension(). The Node SQLite driver enables extension loading only around its loading call.
Which runtimes can load one
| Driver | Support |
|---|---|
better-sqlite3 | The driver can load compiled extensions. |
node | The runtime must expose loadExtension and enableLoadExtension. |
bun | The driver can load compiled extensions. |
wa-sqlite | The browser driver has no compiled-extension loading API. |
expo | The Expo driver has no compiled-extension loading API. |
Inspect driver.capabilities.extensions before configuring an extension. Drivers covers runtime peers and capabilities.
Extensions and the writer worker
The writer worker loads extensions onto its writer connection and records their resolved paths. A replacement worker reloads those files before accepting new work. A rebuilt driver without the required loading method fails with EXTENSION_ERROR.
When encryption changes replace database connections, Sirannon reloads recorded extensions on the replacement connections. Keep those binaries available for the database's lifetime, including after worker or connection recovery.