NitroSQLite
Integrations

ORMs and query builders

Install an integration, open a local SQLite database, and write and read your first row.

SQLite stores tables and indexes in a file on the device. An ORM maps entities to those tables and tracks changes to objects. A query builder compiles typed SQL and bound parameters. Both use the same SQLite transactions and file locks as direct queries.

For entity mapping, dialects, and migrations, see how ORMs work with SQLite. Choose the integration that matches how your app represents its data. Each package below uses the SQLite adapter for the same host. Keep one ORM or query-builder instance for each database and close it after its outstanding work finishes.

IntegrationReact NativeNitroUI
Drizzlereact-native-nitro-sqlite-drizzlenitro-sqlite-drizzle
TypeORMreact-native-nitro-sqlite-typeormnitro-sqlite-typeorm
MikroORMreact-native-nitro-sqlite-mikro-ormnitro-sqlite-mikro-orm
Kyselyreact-native-nitro-sqlite-kyselynitro-sqlite-kysely

Drizzle and Kysely use typed queries. TypeORM and MikroORM also provide entity mapping and change tracking. Vector search has a separate guide.

Choose Drizzle ORM, TypeORM, MikroORM, or Kysely, a typed SQL query builder. All four store data in SQLite on the device. Vector search has a separate guide.

For the difference between entity mapping and a typed query builder, see How ORMs work with SQLite.

Installation

Follow Getting Started to install Nitro SQLite and Nitro Modules. Add the packages for your library below before rebuilding the native app. The companions install from the alpha tag. Save them with exact versions, as described in React Native installation.

Install Drizzle and its Nitro SQLite driver.

npm install --save-exact react-native-nitro-sqlite-drizzle@alpha drizzle-orm@0.45.4

Install TypeORM and the Babel plugins, then configure module resolution.

npm install typeorm@1.1.2
npm install --save-dev babel-plugin-module-resolver @babel/plugin-transform-class-static-block

The 11.0.0 alpha ships the driver as a separate companion. Also run npm install --save-exact react-native-nitro-sqlite-typeorm@alpha and switch the driver import and Babel alias. Nitro SQLite 10.4.0 includes the driver in core.

Install the driver and its runtime dependencies. Configure Metro, Babel, and Buffer before importing the ORM.

npm install --save-exact react-native-nitro-sqlite-mikro-orm@alpha @mikro-orm/core@7.2.13 @mikro-orm/sql@7.2.13 kysely@0.29.9 @craftzdog/react-native-buffer@6.1.2 react-native-quick-base64@3.0.1 base64-js@1.5.1

Install Kysely and its Nitro SQLite dialect.

npm install --save-exact react-native-nitro-sqlite-kysely@alpha
npm install 'kysely@^0.29.9'

Install on NitroUI

NitroUI apps use the nitro-sqlite-* companions instead of the React Native companions, with the same ORM libraries as the installation tabs above. First install the adapter, then add the companion from the alpha tag with an exact version. For example, for Kysely:

bun add --exact nitro-sqlite-kysely@alpha
bun add 'kysely@^0.29.9'

Each companion package includes its shared implementation. Your app does not need shared/orm, nitro-sqlite-core, or nitro-sqlite-drizzle-core.

MikroORM requires both its driver and Kysely companions. Its NitroUI browser bundle and Buffer setup uses JavaScript dependencies. React Native keeps its native Buffer setup or can use the shared JavaScript adapter.

Usage

The examples below use React Native imports. For NitroUI, replace the react-native-nitro-sqlite prefix with nitro-sqlite. Complete each guide's host setup first, including TypeORM and MikroORM browser bundles.

Open the database, create a table, and read back the note you insert. For an app that keeps its database open, initialize it once and close it after outstanding queries finish.

import { eq } from 'drizzle-orm'
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'
import { open } from 'react-native-nitro-sqlite'
import { drizzle } from 'react-native-nitro-sqlite-drizzle'

const notes = sqliteTable('notes', {
  id: integer('id').primaryKey(),
  title: text('title').notNull(),
})

const connection = open({ name: 'notes.sqlite' })
const db = drizzle(connection)
try {
  connection.execute(
    'CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, title TEXT NOT NULL)',
  )
  const { id } = await db
    .insert(notes)
    .values({ title: 'Packing list' })
    .returning({ id: notes.id })
    .get()
  const saved = await db.select().from(notes).where(eq(notes.id, id)).get()
  console.log(saved?.title)
} finally {
  connection.close()
}

See the Drizzle ORM guide for Drizzle Kit migrations, connection options, and transactions.

This example uses the core 10.4.0 driver. With the standalone adapter, import typeORMDriver from react-native-nitro-sqlite-typeorm.

import { DataSource, EntitySchema } from 'typeorm'
import { typeORMDriver } from 'react-native-nitro-sqlite'

type Note = { id: number; title: string }

const NoteEntity = new EntitySchema<Note>({
  name: 'Note',
  tableName: 'notes',
  columns: {
    id: { type: Number, primary: true, generated: true },
    title: { type: String },
  },
})

const dataSource = new DataSource({
  type: 'react-native',
  database: 'notes.sqlite',
  location: '.',
  driver: typeORMDriver,
  entities: [NoteEntity],
  synchronize: true,
})

await dataSource.initialize()
try {
  const notes = dataSource.getRepository(NoteEntity)
  const { id } = await notes.save({ title: 'Packing list' })
  const saved = await notes.findOneByOrFail({ id })
  console.log(saved.title)
} finally {
  await dataSource.destroy()
}

See the TypeORM guide for schema management, connection callbacks, and encryption.

import { defineEntity, MikroORM, p } from '@mikro-orm/sql'
import { NitroSQLiteDriver } from 'react-native-nitro-sqlite-mikro-orm'

const Note = defineEntity({
  name: 'Note',
  tableName: 'notes',
  properties: {
    id: p.integer().primary(),
    title: p.string(),
  },
})

const orm = new MikroORM({
  driver: NitroSQLiteDriver,
  dbName: 'notes.sqlite',
  entities: [Note],
})

try {
  await orm.schema.update()
  const writer = orm.em.fork()
  const note = writer.create(Note, { title: 'Packing list' })
  await writer.flush()
  const saved = await orm.em.fork().findOneOrFail(Note, note.id)
  console.log(saved.title)
} finally {
  await orm.close()
}

See the MikroORM guide for entity discovery, transactions, and paging.

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' }),
})

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')
    .select('title')
    .where('id', '=', id)
    .executeTakeFirstOrThrow()
  console.log(saved.title)
} finally {
  await db.destroy()
}

See the Kysely guide for connection lifetime, migrations, transactions, and binary values.