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
Follow Getting Started to install and build Nitro SQLite 10.4 or later. Then install the dialect and its Kysely peer dependency.
npm install react-native-nitro-sqlite-kysely 'kysely@^0.29.9'Create a table and query it
import { Kysely, type Generated } from 'kysely'
import { createNitroSQLiteDialect } from 'react-native-nitro-sqlite-kysely'
interface Database {
notes: { id: Generated<number>; title: string }
}
const db = new Kysely<Database>({
dialect: createNitroSQLiteDialect({ name: 'notes.sqlite' }),
})
await db.schema.createTable('notes').ifNotExists()
.addColumn('id', 'integer', (column) => column.primaryKey())
.addColumn('title', 'text', (column) => column.notNull())
.execute()
const saved = await db.insertInto('notes').values({ title: 'Packing list' })
.returningAll().executeTakeFirstOrThrow()
const note = await db.selectFrom('notes').selectAll()
.where('id', '=', saved.id).executeTakeFirstOrThrow()The Database interface describes the schema to TypeScript. Create the tables or apply your schema migrations before querying them.
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('id', '=', note.id).execute()
})Kysely commits when the callback resolves and rolls back when it throws. For SQLite savepoints, use Kysely's controlled transaction API.
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.