Multiple connections
Open independent SQLite handles for reads and writes against one database file.
open({ name }) creates the default connection for a database name. Opening a second default connection with that name throws until the first closes. Use connection: 'independent' to open another native SQLite handle to the same file. Each returned connection has its own operation queue and transactions; closing one leaves the others open.
Independent connections require the shared threadSafe setting to remain enabled.
One writer and one reader
Create the database and finish migrations before opening a read-only connection. Enable WAL mode on the writer if reads should proceed while it writes:
import { open } from 'react-native-nitro-sqlite'
const writer = open({ name: 'app.sqlite' })
await writer.executeAsync('PRAGMA journal_mode = WAL')
await writer.executeAsync('PRAGMA busy_timeout = 1000')
await writer.executeAsync(
'CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, value TEXT)',
)
const reader = open({
name: 'app.sqlite',
connection: 'independent',
readOnly: true,
})
await reader.executeAsync('PRAGMA busy_timeout = 1000')
const [_, items] = await Promise.all([
writer.executeAsync('INSERT INTO items (value) VALUES (?)', ['new item']),
reader.executeAsync('SELECT * FROM items'),
])The read may finish before the insert commits. Await the write first if the read must include it. Apply connection settings to each handle. Nitro SQLite does not enable WAL or change SQLite's default busy timeout for you. A read-only connection requires an existing file and cannot write, delete the database, or attach another database.
Keep a small number of connections and reuse them. Route writes, migrations, and imports through the writer; route independent reads through the reader. Nitro SQLite does not inspect SQL or choose a connection for each statement. Name-based NitroSQLite helpers address the default connection, so keep the object returned by open() for an independent connection. The TypeORM driver also uses the default connection.
Ordering and transactions
Each connection schedules its work independently. Use a transaction callback's tx methods for all of its statements. See connection ordering and transaction callbacks for Nitro SQLite's rules, and isolation and concurrency for SQLite's reader and writer behavior.
Close and delete
Await pending work before closing each connection. close() throws while its connection is busy. Deletion fails while another connection still has the file open, including through an attachment; close or detach those handles first.
reader.close()
writer.close()
writer.delete()Open a new connection instead of reusing a closed object. See database lifecycle for cleanup and file locations.