Litestone guide / Advanced
Multiple databases
database blocks route models to separate SQLite files, append-only JSONL logs or an audit trail. One createClient call opens them all, and a model's @@db picks its home.
database main { path "./app.db" }
database analytics { path "./analytics.db" }
model User {
id Int
email String
}
model PageView {
id Int
path String
(analytics)
}
const db = await createClient({ path: './schema.lite' })
await db.user.create({ data: { email: 'a@example.com' } }) // -> app.db
await db.pageView.create({ data: { path: '/' } }) // -> analytics.db
const { existsSync } = await import('node:fs')
if (!existsSync('./app.db') || !existsSync('./analytics.db')) throw new Error('expected one file per database')
if ((await db.pageView.count()) !== 1) throw new Error('expected the view in analytics')
await db.$close()
Queries route by model with no extra code, and a $transaction rolls back every database it wrote to.
What else it can do
driver jsonlis an append-only log with no update, delete or migrationdriver trailreceives@@trailwrite entries as a queryable audit modelretention 30dsweeps old rows, and@@retention(90d, from: capturedAt)sets a window per modelpath env("VAR", "./default")reads the location from the environment$attachand$detachadd a file for raw SQL across databases@@externalmarks a table or view litestone queries but does not create- Migrations live in one subdirectory per database, and one command applies them all
- Each file commits on its own, so a commit that fails part way names what committed
In depth: Multi-database § Database blocks, § Assigning models, § Drivers, § Transactions across databases, § Retention, Migrations § Multi-database schemas