Drizzle ORM
Use Drizzle ORM with Nitro SQLite through the react-native-nitro-sqlite-drizzle driver.
react-native-nitro-sqlite-drizzle runs Drizzle ORM queries on a Nitro SQLite connection. Pass an open connection to drizzle() and query it with Drizzle's API. Every query is asynchronous.
Install
Use the Drizzle quick start to install the driver, open a database, and run a query. The driver supports drizzle-orm 0.45.4 and later 0.x releases.
Create a database
A Drizzle table declaration describes columns to the query builder. SQLite needs the corresponding table in the database file. The quick start creates it with SQL. For versioned schema changes, use the migrations below.
Create db once, outside your components. drizzle(connection, config) accepts Drizzle's schema, logger, and casing options. db.$client is the connection passed to drizzle(). Close it after outstanding work finishes.
SQLite stores binary values as BLOBs. When you pass a Buffer or another typed-array view through Drizzle, the driver binds its bytes as an ArrayBuffer, preserving the view's byte range.
Migrations
Start the first generated migration on a fresh database. The quick start creates the notes table directly, so Drizzle has no migration record for it and would try to create it again.
Install the development tools for generating and bundling migrations.
npm install --save-dev drizzle-kit babel-plugin-inline-importPut the table declarations in db/schema.ts so Drizzle Kit can load them in Node without opening a native connection. Drizzle Kit generates SQL migration files from that schema. Set driver: 'expo' so it also writes a migrations.js file that bundles them for React Native:
import { defineConfig } from 'drizzle-kit'
export default defineConfig({
schema: './db/schema.ts',
out: './drizzle',
dialect: 'sqlite',
driver: 'expo',
})Run npx drizzle-kit generate after each schema change.
migrations.js imports .sql files. Add the Babel plugin so they load as strings:
module.exports = {
presets: ['module:@react-native/babel-preset'],
plugins: [['inline-import', { extensions: ['.sql'] }]],
}Add sql to Metro's source extensions:
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config')
const defaultConfig = getDefaultConfig(__dirname)
module.exports = mergeConfig(defaultConfig, {
resolver: { sourceExts: [...defaultConfig.resolver.sourceExts, 'sql'] },
})Run the migrations when the app starts with useMigrations. It applies the migrations this database has not run yet, in one transaction, and records them in the __drizzle_migrations table:
import { Text } from 'react-native'
import { useMigrations } from 'react-native-nitro-sqlite-drizzle'
import migrations from './drizzle/migrations'
import { db } from './db'
export function App() {
const { success, error } = useMigrations(db, migrations)
if (error) return <Text>Migration failed: {error.message}</Text>
if (!success) return <Text>Migrating...</Text>
return <Text>Ready</Text>
}To run migrations outside a component, such as in startup code or a test, call migrate and await it:
import { migrate } from 'react-native-nitro-sqlite-drizzle'
await migrate(db, migrations)Calls using the same connection wait for earlier migration calls before checking which migrations are pending. React Strict Mode can replay useMigrations without applying the same migration twice. If a migration fails, its transaction rolls back and a later call can retry it.
Transactions
Run every query inside a transaction through tx:
import { eq } from 'drizzle-orm'
await db.transaction(async (tx) => {
await tx.insert(notes).values({ title: 'Passport' })
await tx.update(notes).set({ title: 'Packed' }).where(eq(notes.id, 1))
})A transaction holds the connection until its callback finishes. A query awaited through db inside the callback, including a query prepared from db, waits for the transaction and never completes. Nested tx.transaction() calls use savepoints.
tx.rollback() rolls back the transaction and rejects it with a NitroSQLiteError whose message is Rollback, not Drizzle's TransactionRollbackError. The behavior option of db.transaction() is ignored: transactions start with BEGIN. See transactions for how Nitro SQLite runs them.