Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/ci-paths.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@
"packages/react-native-nitro-sqlite-drizzle/package.json",
"!packages/react-native-nitro-sqlite-drizzle/src/__tests__/**",
"packages/react-native-nitro-sqlite-drizzle/src/__tests__/fixtures/**",
"packages/react-native-nitro-sqlite-typeorm/**"
"packages/react-native-nitro-sqlite-typeorm/**",
"scripts/test-bindings-jsi.py",
"packages/react-native-nitro-sqlite/tests/cpp/namedBindings.test.cpp",
"bunfig.toml"
]
}
3 changes: 3 additions & 0 deletions .github/workflows/test-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ jobs:
- name: Select Xcode 26.5
run: sudo xcode-select -s "/Applications/Xcode_26.5.app/Contents/Developer"

- name: Test named bindings and empty BLOBs in JavaScriptCore
run: python3 scripts/test-bindings-jsi.py

- name: Restore Pods cache
uses: actions/cache@v6
with:
Expand Down
12 changes: 6 additions & 6 deletions docs/content/docs/guides/batch-operations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ const { rowsAffected } = await db.executeBatchAsync(commands)

## Repeat a statement with different values

When the same SQL runs with different values, give one command an array of parameter arrays. The native batch code expands it into separate executions before starting the transaction.
When the same SQL runs with different values, give one command a list of parameter sets. Each set can be a positional array or a [record keyed by exact placeholder names](/docs/guides/parameters-and-results#bind-parameters). SQLite prepares the statement once per command and clears its bindings before each run. A single array of values or a single record runs the command once.

An empty `params` array skips that command. If every command has an empty array, the batch throws or rejects because it has nothing to execute. Omit `params` for a statement that should run once without bindings, such as `CREATE TABLE`.
An empty `params` array skips that command. If every command has an empty array, the batch throws or rejects because it has nothing to execute. Omit `params` or pass an empty record to run a statement once without bindings.

```ts
await db.executeBatchAsync([
{
query: 'INSERT INTO events (name, score) VALUES (?, ?)',
query: 'INSERT INTO events (name, score) VALUES ($name, $score)',
params: [
['alpha', 10],
['beta', 20],
['gamma', 30],
{ $name: 'alpha', $score: 10 },
{ $name: 'beta', $score: 20 },
{ $name: 'gamma', $score: 30 },
],
},
])
Expand Down
4 changes: 3 additions & 1 deletion docs/content/docs/guides/node-test-mock.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test('stores a note', () => {
})
```

The mock returns query rows through both `results` and `rows`, supports [rows as arrays](/docs/guides/parameters-and-results#rows-as-arrays) through `executeRaw()` and `executeRawAsync()`, and supports [atomic batches](/docs/guides/batch-operations). Bound `undefined` becomes SQL NULL. A query that fails throws from `execute()` or rejects from `executeAsync()`.
The mock returns query rows through both `results` and `rows`, supports [rows as arrays](/docs/guides/parameters-and-results#rows-as-arrays) through `executeRaw()` and `executeRawAsync()`, and supports [atomic batches](/docs/guides/batch-operations). Use a positional array or a [record keyed by exact placeholder names](/docs/guides/parameters-and-results#bind-parameters). Bound `undefined` becomes SQL NULL. A query that fails throws from `execute()` or rejects from `executeAsync()`.

## Share a database between connections

Expand Down Expand Up @@ -128,6 +128,8 @@ The reset throws if any connection is busy. Await that work and retry. No connec

## API reference and limits

The installed `better-sqlite3` build disables Tcl-style parameter names such as `$name(suffix)` and `$name::part` with `SQLITE_OMIT_TCL_VARIABLE`. Use ordinary named placeholders in shared Node tests, and native integration tests for these extended SQLite names. Matching a name in the binding scanner does not enable SQL syntax omitted by the backend.

The generated [mock API reference](/api/react-native-nitro-sqlite/mock) documents `open()`, `NitroSQLite.open()`, `MockConnection`, and `resetAllDatabases()`. Connection methods use the same parameter and result types as the native API.

The mock does not implement attachments, SQL file imports, or direct `NitroSQLite.native` methods. Its SQL runs synchronously on Node's thread; async methods schedule that work and return promises. It uses the SQLite version bundled with `better-sqlite3`, which can differ from the library's native SQLite build. Use device tests for native integration, thread behavior, lock contention, and performance.
25 changes: 21 additions & 4 deletions docs/content/docs/guides/parameters-and-results.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ title: Parameters and results
description: Bind SQLite values, inspect rows and metadata, and handle query failures.
---

Pass values as the second argument to `db.execute()` or `db.executeAsync()`, then read the returned `rows`, `rowsAffected`, and `insertId`.
## Bind parameters

In NitroSQLite, pass positional values in an array for `?` placeholders. The public `SQLiteValue` type permits `boolean`, `number`, `string`, `ArrayBuffer`, `null`, and `undefined`. Both `null` and `undefined` bind as SQL NULL. Keep table and column names in your own SQL; parameters bind values, not identifiers.
SQLite assigns an index to each placeholder when it compiles a statement. An anonymous `?` takes the next index, while `?NNN` specifies an index. Named placeholders such as `:name`, `@name`, and `$name` get separate indices, and repeated uses of the same name share one index.

SQLite leaves omitted placeholders as SQL NULL, so NitroSQLite does not require an exact parameter count. Extra values and other binding failures throw or reject with the one-based parameter index and SQLite error code and text. The error does not include parameter values. This behavior applies to synchronous, asynchronous, batch, and prepared statement execution.
Pass a positional array to bind values by index, starting at SQLite index 1. Pass a record to bind by exact, case-sensitive placeholder name. Include the prefix. For example, `:name` and `@name` are different keys, and the key for `?5` is `'?5'`. If `?NNN` refers to an index SQLite already named, use that index's first name.

For repeated executions of the same SQL, [prepare the statement once](/docs/guides/prepared-statements) and pass a new parameter array on each run.
```ts
const result = await db.executeAsync(
'SELECT :name AS name, :name AS repeated, @name AS other, ?5 AS rank',
{ ':name': 'Ada', '@name': 'Grace', '?5': 5 },
)
```

A plain `?` has no record key. To supply values for both anonymous and named placeholders in one statement, use a positional array ordered by SQLite parameter index. Unbound placeholders, `null`, and `undefined` all produce SQL NULL. Bindings start fresh on every execution, including when you reuse a prepared statement.

The public `SQLiteValue` type permits `boolean`, `number`, `string`, `ArrayBuffer`, `null`, and `undefined`. An empty `ArrayBuffer` stores a zero-length BLOB, distinct from SQL NULL. Keep table and column names in your SQL. Parameters only bind values. An unknown record key or extra array value throws or rejects with `SqlExecutionError`. Binding errors omit parameter values. These rules apply to synchronous, asynchronous, batch, and prepared statement execution.

Named records require an app built with the matching native library. Older binaries reject record bindings and accept only positional arrays. Rebuild and distribute the native app first, then target over-the-air JavaScript updates that use records only to compatible binaries. Keep positional bindings in updates for older binaries.

### Update typed wrappers and test doubles

The exported query function types now accept `SQLiteQueryBindings`, a positional array or a named record. Existing calls with arrays keep working. If you implement these types in a wrapper or test double, accept both forms. Narrow recorded parameters with `Array.isArray(params)` before indexing them as an array.

## Read results

```ts
const name = 'Ada'
Expand Down
2 changes: 1 addition & 1 deletion docs/content/docs/guides/performance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ for (const metric of metrics) {

## Group related writes

One `executeBatchAsync()` call executes a fixed set of statements inside a native transaction. Use nested parameter arrays for repeated SQL. Use `db.transaction()` when application logic must inspect a result between writes. Both keep a transaction open while they work; keep the scope short and await each operation inside a transaction callback.
One `executeBatchAsync()` call executes a fixed set of statements inside a native transaction. Use a list of positional arrays or named records for repeated SQL. Use `db.transaction()` when application logic must inspect a result between writes. Both keep a transaction open while they work; keep the scope short and await each operation inside a transaction callback.

```ts
await db.executeBatchAsync([
Expand Down
4 changes: 2 additions & 2 deletions docs/content/docs/guides/prepared-statements.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ try {
db.close()
```

Pass a positional array for the SQL's `?` placeholders on each execution. The [parameter types and binding rules](/docs/guides/parameters-and-results) match the connection's execution methods. Each run clears previous bindings, so omitted values do not carry over. Pass every value your query needs each time.
Pass a positional array or a [record keyed by exact placeholder names](/docs/guides/parameters-and-results#bind-parameters) on each execution. Each run clears previous bindings. Omitted values in the new array or record are SQL NULL.

## Read rows and run asynchronously

Expand Down Expand Up @@ -54,6 +54,6 @@ Do not await a prepared statement's `executeAsync()` from inside `db.transaction

## Choose the right query form

`execute()` and `executeAsync()` prepare and run a query for one call. A prepared statement keeps its compiled SQL for repeated calls on the same connection, but it does not start a transaction. Use [batch operations](/docs/guides/batch-operations) for a fixed group of writes that must commit or roll back together. Use `db.transaction()` when later writes depend on earlier results. A batch with nested parameter arrays runs each expanded command separately; it does not reuse one prepared statement. Measure the workload on your target devices before choosing an approach for speed.
`execute()` and `executeAsync()` prepare and run a query for one call. A prepared statement keeps its compiled SQL for repeated calls on the same connection, but it does not start a transaction. Use [batch operations](/docs/guides/batch-operations) for a fixed group of writes that must commit or roll back together. Use `db.transaction()` when later writes depend on earlier results. A batch with several parameter sets prepares the SQL once per command and reuses it for those sets. Measure the workload on your target devices before choosing an approach for speed.

The [`NitroSQLite` export](/api/react-native-nitro-sqlite/variables/NitroSQLite) also has `prepare(dbName, query)`. It requires a default managed connection opened with `open()` and uses that connection's queue. For an independent connection, call `prepare()` on the connection object so the statement uses that connection's own handle and queue. `NitroSQLite.native.prepare(dbName, query)` returns a raw native statement. Native calls bypass the JavaScript queue, return raw results without the managed `rows` adapter, and leave error conversion and coordination to you. See [native access](/docs/guides/sync-and-async#global-helpers-and-native-access) if you use that lower-level API.
9 changes: 9 additions & 0 deletions docs/releases/named-parameters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Named parameter records

Query, prepared-statement, transaction and batch APIs now accept records keyed by exact SQLite placeholder names, including their prefixes. Existing calls with positional arrays continue to work.

Named records require the matching native library in the installed app. Rebuild and distribute the native app before enabling record bindings in an over-the-air JavaScript update; older native binaries accept only positional arrays.

The exported query function types accept `SQLiteQueryBindings`, which includes both arrays and records. Wrappers and test doubles that implement those types must accept both forms. Code that inspects recorded parameters must narrow with `Array.isArray(params)` before indexing them as an array.

Include this compatibility note in the versioned release announcement that ships named parameter records.
5 changes: 5 additions & 0 deletions example/tests/unit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import registerExecuteRawUnitTests from './specs/operations/executeRaw.spec'
import registerTransactionUnitTests from './specs/operations/transaction.spec'
import registerExecuteBatchUnitTests from './specs/operations/executeBatch.spec'
import registerPreparedStatementUnitTests from './specs/operations/preparedStatement.spec'
import registerNamedParameterUnitTests from './specs/operations/namedParameters.spec'
import registerChangeListenerUnitTests from './specs/operations/changeListener.spec'
import registerTypeORMUnitTestsSpecs from './specs/typeorm.spec'
import registerDatabaseQueueUnitTests from './specs/DatabaseQueue.spec'
Expand All @@ -18,6 +19,10 @@ export function registerUnitTests() {
beforeEach(setupTestDb)

describe('operations/execute.spec.ts', registerExecuteUnitTests)
describe(
'operations/namedParameters.spec.ts',
registerNamedParameterUnitTests,
)
describe('operations/executeRaw.spec.ts', registerExecuteRawUnitTests)
describe('operations/transaction.spec.ts', registerTransactionUnitTests)
describe('operations/executeBatch.spec.ts', registerExecuteBatchUnitTests)
Expand Down
Loading
Loading