ORMs and query builders
Install an integration, open a local SQLite database, and write and read your first row.
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.
Install Drizzle and its Nitro SQLite driver.
npm install react-native-nitro-sqlite-drizzle@~10.5.0 drizzle-orm@0.45.4Install 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-blockNitro SQLite 10.4.0 includes the TypeORM driver in core. For releases that ship the standalone adapter, also run npm install react-native-nitro-sqlite-typeorm@~10.5.0 and switch the driver import and Babel alias.
Install the driver and its runtime dependencies. Configure Metro, Babel, and Buffer before importing the ORM.
npm install react-native-nitro-sqlite-mikro-orm@~10.5.0 @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.1Install Kysely and its Nitro SQLite dialect.
npm install react-native-nitro-sqlite-kysely@~10.5.0 'kysely@^0.29.9'Usage
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.