From e7903d56f5cea8eb79f3deb5b169ae279d47439b Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Mon, 16 Nov 2026 16:16:55 +0100 Subject: [PATCH] perf: drop promise layers around transactions The connection's transaction method and the transaction function were async wrappers around the queued operation, so every transaction resolved through two extra promises. Both now return the queue's promise directly and still reject, rather than throw, when the database is not open. The transaction callback stays async because it commits or rolls back after awaiting. Co-Authored-By: Claude Opus 5.5 --- .../src/operations/session.ts | 5 ++--- .../src/operations/transaction.ts | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/react-native-nitro-sqlite/src/operations/session.ts b/packages/react-native-nitro-sqlite/src/operations/session.ts index f28236ec..a375d634 100644 --- a/packages/react-native-nitro-sqlite/src/operations/session.ts +++ b/packages/react-native-nitro-sqlite/src/operations/session.ts @@ -148,9 +148,8 @@ export function open( ), detach: (alias: string) => runSyncOperation(() => HybridNitroSQLite.detach(connectionId, alias)), - transaction: async ( - fn: (tx: Transaction) => Promise, - ) => runOperation(() => transaction(connectionId, fn, false, queueKey)), + transaction: (fn: (tx: Transaction) => Promise) => + runAsyncOperation(() => transaction(connectionId, fn, false, queueKey)), execute: ( query: string, params?: SQLiteQueryParams, diff --git a/packages/react-native-nitro-sqlite/src/operations/transaction.ts b/packages/react-native-nitro-sqlite/src/operations/transaction.ts index 0c617ade..c299a8bc 100644 --- a/packages/react-native-nitro-sqlite/src/operations/transaction.ts +++ b/packages/react-native-nitro-sqlite/src/operations/transaction.ts @@ -1,4 +1,4 @@ -import { queueOperationAsync, throwIfDatabaseIsNotOpen } from '../DatabaseQueue' +import { queueOperationAsync } from '../DatabaseQueue' import type { Transaction, SQLiteQueryParams, @@ -28,14 +28,12 @@ import type { DatabaseQueueKey } from '../DatabaseQueue' * @returns The callback's result after the transaction finishes. * @throws NitroSQLiteError if BEGIN, the callback, COMMIT, or ROLLBACK fails. */ -export const transaction = async ( +export const transaction = ( dbName: string, transactionCallback: (tx: Transaction) => Promise, isExclusive = false, queueKey: DatabaseQueueKey = dbName, -) => { - throwIfDatabaseIsNotOpen(queueKey) - +): Promise => { const state: { current: TransactionState } = { current: { kind: 'notStarted' }, } @@ -143,7 +141,9 @@ export const transaction = async ( } } - return await queueOperationAsync(queueKey, async () => { + // Only the callback below needs `async`; wrapping the queue too would add promise layers. + // The queue rejects when the database is not open. + return queueOperationAsync(queueKey, async () => { try { await executeAsyncNative( dbName,