TypeORM
Use Nitro SQLite as the database driver for TypeORM's React Native DataSource.
Pass typeORMDriver to a TypeORM DataSource with type: 'react-native' to use Nitro SQLite. TypeORM maps your entities to tables and runs their queries through the driver. Use open() directly when you do not need TypeORM.
Install the separate adapter
The standalone react-native-nitro-sqlite-typeorm package uses Nitro SQLite 10.4.0 or later and TypeORM 1.1.2 or a compatible 1.x version as peer dependencies. If npm lists only the 0.0.0-stage placeholder, use the core 10.4.0 setup. Once an adapter release is available, install all three packages in your app:
npm install react-native-nitro-sqlite-typeorm@~10.5.0 react-native-nitro-sqlite@~10.5.0 'typeorm@^1.1.2'Import the driver and its connection type from the adapter. Keep imports for direct SQLite operations, such as open(), in react-native-nitro-sqlite.
import {
typeORMDriver,
type TypeOrmNitroSQLiteConnection,
} from 'react-native-nitro-sqlite-typeorm'The standalone adapter shares core's callback implementation. Installing core alone no longer installs TypeORM.
Configure module resolution
Install the packages listed in the TypeORM quick start. The example uses TypeORM 1.1.2. Its browser build uses static class blocks. Configure the Babel transform and these resolution changes before you initialize the data source.
-
Expose
./package.jsonin TypeORM'sexportsmap. The repository keeps this as a persistent package patch:"exports": { + "./package.json": "./package.json", ".": { -
In
babel.config.js, point thereact-native-sqlite-storagealias to the package you importtypeORMDriverfrom. The example below uses the standalone adapter. For the core 10.4.0 setup, usereact-native-nitro-sqliteas the alias target.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.
Create a DataSource
The TypeORM quick start uses EntitySchema and synchronize: true to create the table in a new example database. For an app with existing data, register bundled migration classes in migrations, leave synchronize off, and call await dataSource.runMigrations() after initialization.
database becomes the connection's name. location is a directory relative to the platform database root. Reuse the initialized data source while the app needs the database and call await dataSource.destroy() when its queries finish.
Driver export
typeORMDriver has one public method, openDatabase(options, ok, fail). The connection passed to ok and returned on success has the exported TypeOrmNitroSQLiteConnection type. The TypeORMDriver alias below is used only on this page.
import type { TypeOrmNitroSQLiteConnection } from 'react-native-nitro-sqlite-typeorm'
type TypeORMDriver = {
openDatabase(
options: { name: string; location?: string; encryptionKey?: string },
ok: (connection: TypeOrmNitroSQLiteConnection) => void,
fail: (message: string) => void,
): TypeOrmNitroSQLiteConnection | 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 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.