Skip to content

zidane/session/sqlite

A SQLite-backed session store for Bun.

import { … } from 'zidane/session/sqlite'src/session/sqlite.ts

Use createSqliteStore when sessions must survive a restart. It writes each turn as it completes, in SQLite's write-ahead-log (WAL) mode, so a crash keeps history up to the last finished turn. It needs Bun (bun:sqlite), so it has its own subpath. See Sessions.

TypeScript
import { createSession } from 'zidane/session'
import { createSqliteStore } from 'zidane/session/sqlite'

const store = createSqliteStore({ path: './sessions.db' })
const session = await createSession({ store })
await session.save()
console.log(session.id)

Functions#

createSqliteStoreFunction#

Creates a SessionStore backed by a SQLite file through bun:sqlite. Corruption errors are rethrown with the file path and a recovery recipe.

TypeScript
function createSqliteStore(options: SqliteStoreOptions): SessionStore
Parameters
NameTypeDescription
optionsSqliteStoreOptions

Database path.

Example
import { createSession } from 'zidane/session'
import { createSqliteStore } from 'zidane/session/sqlite'

const store = createSqliteStore({ path: './sessions.db' })
const session = await createSession({ store })

src/session/sqlite.ts:210

createTuiStoreFunction#

Convenience: open a SQLite store at dbPath, creating its parent directory if it doesn't yet exist. Meant as the store factory of resolveConfig, for example store: paths => createTuiStore(paths.db).

Lives here (and not in zidane/chat) so the renderer-agnostic zidane/chat entry stays free of bun:sqlite — non-Bun consumers can import zidane/chat without pulling in the Bun-only binding.

TypeScript
function createTuiStore(dbPath: string): SessionStore
Parameters
NameTypeDescription
dbPathstring

Path of the SQLite database file. The parent directory is created when missing.

src/session/sqlite.ts:262

describeCorruptDbFunction#

Re-wrap a corruption error with the file path and a recovery recipe. The bare SQLite string ("database disk image is malformed") tells the user nothing about WHICH file or what to do; every host (TUI, daemon, headless) surfaces this message instead. Non-corruption errors pass through untouched.

Deliberately no auto-repair / quarantine: the file is the only copy of the user's history and .recover salvages most of it. Never touch it.

TypeScript
function describeCorruptDb(err: unknown, dbPath: string): unknown
Parameters
NameTypeDescription
errunknown

Error thrown by a SQLite operation.

dbPathstring

Path of the database file, named in the message.

unknown

src/session/sqlite.ts:181

isCorruptDbErrorFunction#

True when err is SQLite reporting a damaged file (SQLITE_CORRUPT, SQLITE_NOTADB). Matched on the message because bun:sqlite's error class exposes the code inconsistently across versions.

TypeScript
function isCorruptDbError(err: unknown): boolean
Parameters
NameTypeDescription
errunknown

Error thrown by a SQLite operation.

boolean

src/session/sqlite.ts:164

Interfaces#

SqliteStoreOptionsInterface#

Options for createSqliteStore.

TypeScript
interface SqliteStoreOptions
SqliteStoreOptions properties
NameTypeDescription
pathstring

Path to the SQLite database file

src/session/sqlite.ts:153