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
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> | 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<ReturnType<typeof nativeResult>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,20 @@ 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')
await expect(db.loadFileAsync('/tmp/missing.sql')).rejects.toMatchObject({
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,
})
Expand Down
21 changes: 21 additions & 0 deletions packages/react-native-nitro-sqlite/src/nativePromise.ts
Original file line number Diff line number Diff line change
@@ -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<Result>(
call: () => Promise<Result>,
): Promise<Result> {
let nativeResult: Promise<Result>
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)
}
16 changes: 4 additions & 12 deletions packages/react-native-nitro-sqlite/src/operations/execute.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -197,18 +198,9 @@ export function executeRawAsyncNative(
query: string,
params?: SQLiteQueryParams,
): Promise<NitroSQLiteRawQueryResult> {
let nativeResult: Promise<NitroSQLiteRawQueryResult>
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<Row extends QueryResultRow = never>(
Expand Down
18 changes: 8 additions & 10 deletions packages/react-native-nitro-sqlite/src/operations/executeBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<BatchQueryResult> {
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),
),
)
}
3 changes: 2 additions & 1 deletion packages/react-native-nitro-sqlite/src/operations/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
19 changes: 10 additions & 9 deletions packages/react-native-nitro-sqlite/src/operations/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
QueryResultRow,
QueryResult,
} from '../types'
import { callNativeAsync } from '../nativePromise'
import {
executeAsyncManaged,
executeManaged,
Expand Down Expand Up @@ -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),
),
),
),
}
}
Expand Down
Loading