Kysely
Query a local SQLite database using Kysely's typed SQL builder.
SQLite stores tables and indexes in a file on the device. Kysely compiles a typed query to SQL and bound parameters. The Nitro SQLite dialect executes that SQL asynchronously on one independent native connection.
Install
The companion installation guide lists the packages for each host. These 11.0.0 alpha packages install from the alpha tag and use Kysely 0.29.9. Both React Native and NitroUI expose the same dialect API.
Create a table and query it
Choose the dialect import for your host. React Native uses react-native-nitro-sqlite-kysely; NitroUI uses nitro-sqlite-kysely.
import { Kysely, type Generated } from 'kysely'
import { createNitroSQLiteDialect } from 'nitro-sqlite-kysely'
interface Database {
notes: { id: Generated<number>; title: string }
}
const db = new Kysely<Database>({
dialect: createNitroSQLiteDialect({ name: 'notes.sqlite' }),
})
try {
await db.schema
.createTable('notes')
.ifNotExists()
.addColumn('id', 'integer', (column) => column.primaryKey())
.addColumn('title', 'text', (column) => column.notNull())
.execute()
const { id } = await db
.insertInto('notes')
.values({ title: 'Packing list' })
.returning('id')
.executeTakeFirstOrThrow()
const saved = await db
.selectFrom('notes')
.selectAll()
.where('id', '=', id)
.executeTakeFirstOrThrow()
console.log(saved.title)
} finally {
await db.destroy()
}The TypeScript database interface above describes the schema to Kysely. Use db.schema to create the corresponding database tables. For a file that already contains user data, apply versioned changes when the app opens it.
Connection lifetime
createNitroSQLiteDialect() accepts NitroSQLiteDialectOptions. The first query opens the file. Pass location, readOnly, or a licensed SEE encryptionKey alongside name when needed. Each Kysely instance owns its handle. Reuse one instance for the lifetime of the database in your app. After all work has finished, call await db.destroy() to close it. See database lifecycle and encryption.
Transactions
SQLite commits or rolls back the statements in a transaction together. Run every statement through the trx argument. A query on the outer db waits for the transaction to finish, so awaiting it inside the callback deadlocks.
await db.transaction().execute(async (trx) => {
await trx.insertInto('notes').values({ title: 'Passport' }).execute()
await trx
.updateTable('notes')
.set({ title: 'Packed' })
.where('title', '=', 'Packing list')
.execute()
})Kysely commits when the callback resolves and rolls back when it throws. For SQLite savepoints, use Kysely's controlled transaction API.
Migrate an existing database
SQLite reserves PRAGMA user_version for the application to store its schema version. If your app owns this value and the notes table already exists at version zero, you can add an index and advance the version in the same transaction. Ship the migration with your app and run it before queries that depend on the new schema.
import { sql } from 'kysely'
await db.transaction().execute(async (trx) => {
const { rows } = await sql<{
user_version: number
}>`PRAGMA user_version`.execute(trx)
if (rows[0]?.user_version === 0) {
await trx.schema
.createIndex('notes_title')
.on('notes')
.column('title')
.execute()
await sql`PRAGMA user_version = 1`.execute(trx)
}
})Parameters and results
Use strings, numbers, booleans, nulls, or binary data as parameters. Convert dates to your chosen storage format. To preserve a large integer exactly, bind its decimal string to a TEXT column. SQLite integers and insert IDs pass through Nitro SQLite as JavaScript numbers, so Kysely's bigint write counters cannot restore precision lost above the safe integer range.
Typed-array views bind only their visible byte range. SQLite BLOB results come back as Uint8Array. Use returning or returningAll() to get the columns written by a statement. For a plain insert, Kysely exposes SQLite's last insert rowid. Statements that insert no row can leave that value unchanged.
Nitro SQLite returns each result set in memory. Use ordered, bounded queries to page through large tables. Calling Kysely's stream() rejects because the driver has no row iterator.