From b2932cea3e5c434e57c4af3727f84a0e94b3cac4 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Mon, 16 Nov 2026 16:16:28 +0100 Subject: [PATCH] perf: drop promise layers from batch and file loading executeBatchAsync, the connection's executeBatchAsync and loadFileAsync wrapped the native promise in async functions, the same layers #496 removed from executeAsync. They now return the queued native promise directly. callNativeAsync keeps reporting a synchronous native throw as a rejected NitroSQLiteError, and executeRawAsyncNative uses it too. It lives with rejectWithNitroSQLiteError in nativePromise.ts, since neither is specific to executing queries. Co-Authored-By: Claude Opus 5.5 --- .../src/__tests__/executeBatch.test.ts | 22 +++++++++++++++++++ .../src/__tests__/session.test.ts | 7 ++++++ .../src/nativePromise.ts | 21 ++++++++++++++++++ .../src/operations/execute.ts | 16 ++++---------- .../src/operations/executeBatch.ts | 18 +++++++-------- .../src/operations/prepare.ts | 3 ++- .../src/operations/session.ts | 19 ++++++++-------- 7 files changed, 74 insertions(+), 32 deletions(-) create mode 100644 packages/react-native-nitro-sqlite/src/nativePromise.ts diff --git a/packages/react-native-nitro-sqlite/src/__tests__/executeBatch.test.ts b/packages/react-native-nitro-sqlite/src/__tests__/executeBatch.test.ts index d39c5f5a..a46e204a 100644 --- a/packages/react-native-nitro-sqlite/src/__tests__/executeBatch.test.ts +++ b/packages/react-native-nitro-sqlite/src/__tests__/executeBatch.test.ts @@ -58,6 +58,28 @@ describe('executeBatch', () => { ) }) + it('rejects instead of throwing when the native batch call throws synchronously', async () => { + openDatabaseQueue(dbName) + jest + .mocked(HybridNitroSQLite.executeBatchAsync) + .mockImplementationOnce(() => { + throw new Error('batch bind failed') + }) + .mockResolvedValueOnce({ rowsAffected: 1 }) + + let pending: Promise | undefined + expect(() => { + pending = executeBatchAsync(dbName, commands) + }).not.toThrow() + await expect(pending).rejects.toMatchObject({ + name: 'NitroSQLiteError', + message: 'batch bind failed', + }) + await expect(executeBatchAsync(dbName, commands)).resolves.toEqual({ + rowsAffected: 1, + }) + }) + it('submits a batch between async statements without waiting for JavaScript settlement', async () => { openDatabaseQueue(dbName) const firstResult = deferred>() diff --git a/packages/react-native-nitro-sqlite/src/__tests__/session.test.ts b/packages/react-native-nitro-sqlite/src/__tests__/session.test.ts index d0263f04..788217b8 100644 --- a/packages/react-native-nitro-sqlite/src/__tests__/session.test.ts +++ b/packages/react-native-nitro-sqlite/src/__tests__/session.test.ts @@ -295,6 +295,9 @@ describe('open', () => { jest .mocked(HybridNitroSQLite.loadFileAsync) .mockRejectedValueOnce('load failed') + .mockImplementationOnce(() => { + throw new Error('path conversion failed') + }) .mockResolvedValueOnce({ commands: 1 }) expect(() => db.attach('other', 'alias')).toThrow('attach failed') @@ -302,6 +305,10 @@ describe('open', () => { name: 'NitroSQLiteError', message: 'load failed', }) + await expect(db.loadFileAsync('/tmp/bad.sql')).rejects.toMatchObject({ + name: 'NitroSQLiteError', + message: 'path conversion failed', + }) await expect(db.loadFileAsync('/tmp/valid.sql')).resolves.toEqual({ commands: 1, }) diff --git a/packages/react-native-nitro-sqlite/src/nativePromise.ts b/packages/react-native-nitro-sqlite/src/nativePromise.ts new file mode 100644 index 00000000..c51a87ef --- /dev/null +++ b/packages/react-native-nitro-sqlite/src/nativePromise.ts @@ -0,0 +1,21 @@ +import NitroSQLiteError from './NitroSQLiteError' + +/** Calls an async native method without an `async` wrapper and its extra promise. + * Reports a synchronous throw or a rejection as a rejected NitroSQLiteError. + */ +export function callNativeAsync( + call: () => Promise, +): Promise { + let nativeResult: Promise + try { + nativeResult = call() + } catch (error) { + return Promise.reject(NitroSQLiteError.fromError(error)) + } + return nativeResult.catch(rejectWithNitroSQLiteError) +} + +/** Rethrows any error as a NitroSQLiteError, for use as a promise rejection handler. */ +export function rejectWithNitroSQLiteError(error: unknown): never { + throw NitroSQLiteError.fromError(error) +} diff --git a/packages/react-native-nitro-sqlite/src/operations/execute.ts b/packages/react-native-nitro-sqlite/src/operations/execute.ts index bd2f66c9..79468c7b 100644 --- a/packages/react-native-nitro-sqlite/src/operations/execute.ts +++ b/packages/react-native-nitro-sqlite/src/operations/execute.ts @@ -1,6 +1,7 @@ import { HybridNitroSQLite } from '../nitro' import type { QueryResult, QueryResultRow, SQLiteQueryParams } from '../types' import NitroSQLiteError from '../NitroSQLiteError' +import { callNativeAsync, rejectWithNitroSQLiteError } from '../nativePromise' import type { NitroSQLiteQueryResult, NitroSQLiteRawQueryResult, @@ -197,18 +198,9 @@ export function executeRawAsyncNative( query: string, params?: SQLiteQueryParams, ): Promise { - let nativeResult: Promise - try { - nativeResult = HybridNitroSQLite.executeRawAsync(dbName, query, params) - } catch (error) { - return Promise.reject(NitroSQLiteError.fromError(error)) - } - return nativeResult.catch(rejectWithNitroSQLiteError) -} - -/** Rethrows any error as a NitroSQLiteError, for use as a promise rejection handler. */ -export function rejectWithNitroSQLiteError(error: unknown): never { - throw NitroSQLiteError.fromError(error) + return callNativeAsync(() => + HybridNitroSQLite.executeRawAsync(dbName, query, params), + ) } export function buildJSQueryResult( diff --git a/packages/react-native-nitro-sqlite/src/operations/executeBatch.ts b/packages/react-native-nitro-sqlite/src/operations/executeBatch.ts index 13a4668c..5cd3938b 100644 --- a/packages/react-native-nitro-sqlite/src/operations/executeBatch.ts +++ b/packages/react-native-nitro-sqlite/src/operations/executeBatch.ts @@ -7,6 +7,7 @@ import { import NitroSQLiteError from '../NitroSQLiteError' import type { BatchQueryCommand, BatchQueryResult } from '../types' import type { DatabaseQueueKey } from '../DatabaseQueue' +import { callNativeAsync } from '../nativePromise' /** Execute a batch synchronously in one exclusive transaction. * Requires an open managed connection; throws if it is busy or the batch is empty. @@ -36,18 +37,15 @@ export function executeBatch( * @param commands SQL commands and optional parameter sets. * @returns A promise of the total affected row count. */ -export async function executeBatchAsync( +export function executeBatchAsync( dbName: string, commands: BatchQueryCommand[], queueKey: DatabaseQueueKey = dbName, ): Promise { - throwIfDatabaseIsNotOpen(queueKey) - - return queueStatementAsync(queueKey, async () => { - try { - return await HybridNitroSQLite.executeBatchAsync(dbName, commands) - } catch (error) { - throw NitroSQLiteError.fromError(error) - } - }) + // The queue rejects when the database is not open, before calling native code. + return queueStatementAsync(queueKey, () => + callNativeAsync(() => + HybridNitroSQLite.executeBatchAsync(dbName, commands), + ), + ) } diff --git a/packages/react-native-nitro-sqlite/src/operations/prepare.ts b/packages/react-native-nitro-sqlite/src/operations/prepare.ts index 48054a74..bb82f14d 100644 --- a/packages/react-native-nitro-sqlite/src/operations/prepare.ts +++ b/packages/react-native-nitro-sqlite/src/operations/prepare.ts @@ -7,7 +7,8 @@ import type { SQLiteQueryParams, } from '../types' import type { NitroSQLiteRawQueryResult } from '../specs/NitroSQLiteQueryResult.nitro' -import { buildJSQueryResult, rejectWithNitroSQLiteError } from './execute' +import { buildJSQueryResult } from './execute' +import { rejectWithNitroSQLiteError } from '../nativePromise' import { queueOperationAsync, startOperationSync } from '../DatabaseQueue' import type { DatabaseQueueKey } from '../DatabaseQueue' diff --git a/packages/react-native-nitro-sqlite/src/operations/session.ts b/packages/react-native-nitro-sqlite/src/operations/session.ts index a9f63844..f28236ec 100644 --- a/packages/react-native-nitro-sqlite/src/operations/session.ts +++ b/packages/react-native-nitro-sqlite/src/operations/session.ts @@ -9,6 +9,7 @@ import type { QueryResultRow, QueryResult, } from '../types' +import { callNativeAsync } from '../nativePromise' import { executeAsyncManaged, executeManaged, @@ -174,21 +175,21 @@ export function open( runOperation(() => prepare(connectionId, query, queueKey)), executeBatch: (commands: BatchQueryCommand[]) => runOperation(() => executeBatch(connectionId, commands, queueKey)), - executeBatchAsync: async (commands: BatchQueryCommand[]) => - runOperation(() => executeBatchAsync(connectionId, commands, queueKey)), + executeBatchAsync: (commands: BatchQueryCommand[]) => + runAsyncOperation(() => + executeBatchAsync(connectionId, commands, queueKey), + ), loadFile: (location: string) => runSyncOperation(() => HybridNitroSQLite.loadFile(connectionId, location), ), loadFileAsync: (location: string) => runAsyncOperation(() => - queueStatementAsync(queueKey, async () => { - try { - return await HybridNitroSQLite.loadFileAsync(connectionId, location) - } catch (error) { - throw NitroSQLiteError.fromError(error) - } - }), + queueStatementAsync(queueKey, () => + callNativeAsync(() => + HybridNitroSQLite.loadFileAsync(connectionId, location), + ), + ), ), } }