TypeORM
Use Nitro SQLite as TypeORM's database driver in either host.
TypeORM maps entities to SQLite tables and runs their queries through a callback database driver. Install the separate companion for your host and pass its typeORMDriver to a DataSource with type: 'react-native'. That string selects TypeORM's callback contract in either host.
| Host | Companion | SQLite adapter |
|---|---|---|
| React Native | react-native-nitro-sqlite-typeorm | react-native-nitro-sqlite |
| NitroUI | nitro-sqlite-typeorm | nitro-sqlite |
Follow the companion installation guide and use TypeORM 1.1.2. Both SQLite package roots retain their TypeORM driver and connection type exports for compatibility, but those exports are deprecated. Import the driver and connection types from the companion for new code.
Configure module resolution
React Native
Install typeorm, react-native-nitro-sqlite, babel-plugin-module-resolver, and @babel/plugin-transform-class-static-block in the app. The repository's working example uses TypeORM 1.1.2. Its browser build uses static class blocks, so the Babel transform is needed in addition to two resolution changes:
-
Expose
./package.jsonin TypeORM'sexportsmap. The repository keeps this as a persistent package patch:"exports": { + "./package.json": "./package.json", ".": { -
Alias the SQLite storage package name that TypeORM imports to Nitro SQLite in
babel.config.js:module.exports = { presets: ['module:@react-native/babel-preset'], plugins: [ '@babel/plugin-transform-class-static-block', [ 'module-resolver', { alias: { 'react-native-sqlite-storage': 'react-native-nitro-sqlite-typeorm', }, }, ], ], }
Merge this plugin into your existing Babel configuration rather than replacing the rest of its plugins.
NitroUI
Use nitro-sqlite-typeorm with nitro-sqlite and TypeORM's browser build. The standalone example uses TypeORM 1.1.2 and prepares a browser bundle in examples/nitro-ui/scripts/prepare-typeorm.mjs. Its shared-source preparation redirects TypeORM imports to that bundle. This preserves TypeORM's browser mappings before the NitroUI bundler consumes it.
The example also loads reflect-metadata, browser timers, and Buffer before the app. Follow examples/nitro-ui/src/polyfills.ts for that initialization. Its native startup and browser-bundle preparation are part of the example, not extra exports of the SQLite package. The React Native Babel configuration above does not apply to this host.
Create a DataSource
The example below uses the React Native package. For NitroUI, import typeORMDriver from nitro-sqlite-typeorm and resolve the TypeORM import to your prepared browser bundle. The DataSource options and entity definitions are the same.
import { DataSource, EntitySchema } from 'typeorm'
import { typeORMDriver } from 'react-native-nitro-sqlite-typeorm'
type Note = { id: number; title: string }
const NoteEntity = new EntitySchema<Note>({
name: 'Note',
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()
const notes = dataSource.getRepository(NoteEntity)
await notes.save({ title: 'First note' })
const saved = await notes.find()
await dataSource.destroy()This small example uses synchronize: true to create the table. Choose your application's schema management before shipping. database becomes the connection's name; location is a directory relative to the platform database root.
Driver export
Both TypeORM companions export the TypeOrmConnection type. Use the companion for your host in the type import below.
typeORMDriver has one public method, openDatabase(options, ok, fail). The connection passed to ok and returned on success has the exported TypeOrmConnection type. The TypeORMDriver alias below is used only on this page.
import type { TypeOrmConnection } from 'react-native-nitro-sqlite-typeorm'
type TypeORMDriver = {
openDatabase(
options: { name: string; location?: string; encryptionKey?: string },
ok: (connection: TypeOrmConnection) => void,
fail: (message: string) => void,
): TypeOrmConnection | null
}openDatabase() returns the adapter connection after calling ok, or null after invoking fail when opening fails. executeSql() runs db.executeAsync() and then invokes one result callback. close(), attach(), and detach() are synchronous; their success callbacks run after the operation succeeds. TypeORM normally handles this contract for you.
The failure callbacks for openDatabase and executeSql are typed to receive a string, but the adapter casts caught errors to that type without converting them at runtime. Handle an Error value as well if you call these callbacks directly. This adapter is for TypeORM's callback interface, not a connection object for direct SQL calls. For those, see the generated API reference.
Encrypt a TypeORM database
After building Nitro SQLite with licensed SEE, pass the key through the DataSource's encryptionKey option. TypeORM 1.1.2 forwards it to typeORMDriver.openDatabase:
const dataSource = new DataSource({
type: 'react-native',
database: 'private.sqlite',
location: '.',
driver: typeORMDriver,
encryptionKey,
entities: [NoteEntity],
})Use a new database file or migrate an existing plaintext file first. Keep the key outside app source and supply it again when TypeORM reconnects. See encrypt a database for the SEE build steps and open errors.
Migrate core imports
The typeORMDriver and TypeOrmNitroSQLiteConnection exports in react-native-nitro-sqlite are deprecated. They remain available with the same callbacks and behavior. Install TypeORM explicitly in your app if you previously received it through core's production dependency.
For new and migrated apps, install the dedicated adapter and change both imports:
import {
typeORMDriver,
type TypeOrmNitroSQLiteConnection,
} from 'react-native-nitro-sqlite-typeorm'Keep open() and other direct SQLite imports in react-native-nitro-sqlite. The adapter adds no native code, so changing only the adapter import does not require a native rebuild.
Test with Jest
The adapter resolves the same core module that your app uses. Mock the core module before importing the adapter. A replacement must provide typeORMDriver if the test initializes a TypeORM DataSource. The Node-only react-native-nitro-sqlite/mock entry point does not export the callback adapter.