Skip to content

Commit 6a2abe4

Browse files
committed
feat: implement null handling for executeBatch
1 parent 9a9b921 commit 6a2abe4

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed

package/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { HybridNitroSQLite } from './nitro'
33
import { open } from './operations/session'
44
import { execute, executeAsync } from './operations/execute'
55
import { init } from './OnLoad'
6+
import { executeBatch, executeBatchAsync } from './operations/executeBatch'
67
export type * from './types'
78
export { typeORMDriver } from './typeORM'
89

@@ -18,6 +19,8 @@ export const NitroSQLite = {
1819
transaction,
1920
execute,
2021
executeAsync,
22+
executeBatch,
23+
executeBatchAsync,
2124
}
2225

2326
export { open } from './operations/session'
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { NITRO_SQLITE_NULL, isSimpleNullHandlingEnabled } from '../nullHandling'
2+
import { HybridNitroSQLite } from '../nitro'
3+
import type {
4+
NativeSQLiteQueryParams,
5+
BatchQueryResult,
6+
BatchQueryCommand,
7+
NativeBatchQueryCommand,
8+
SQLiteValue,
9+
SQLiteQueryParamItem,
10+
} from '../types'
11+
12+
export function executeBatch(
13+
dbName: string,
14+
commands: BatchQueryCommand[]
15+
): BatchQueryResult {
16+
const transformedCommands = isSimpleNullHandlingEnabled()
17+
? toNativeBatchQueryCommands(commands)
18+
: (commands as NativeBatchQueryCommand[])
19+
20+
const result = HybridNitroSQLite.executeBatch(dbName, transformedCommands)
21+
return result
22+
}
23+
24+
export async function executeBatchAsync(
25+
dbName: string,
26+
commands: BatchQueryCommand[]
27+
): Promise<BatchQueryResult> {
28+
const transformedCommands = isSimpleNullHandlingEnabled()
29+
? toNativeBatchQueryCommands(commands)
30+
: (commands as NativeBatchQueryCommand[])
31+
32+
const result = await HybridNitroSQLite.executeBatchAsync(
33+
dbName,
34+
transformedCommands
35+
)
36+
return result
37+
}
38+
39+
function replaceWithNativeNull(value: SQLiteQueryParamItem): SQLiteValue {
40+
if (value === undefined || value === null) {
41+
return NITRO_SQLITE_NULL
42+
}
43+
return value
44+
}
45+
46+
function toNativeBatchQueryCommands(
47+
commands: BatchQueryCommand[]
48+
): NativeBatchQueryCommand[] {
49+
return commands.map((command) => {
50+
const transformedParams = command.params?.map((param) => {
51+
if (Array.isArray(param)) {
52+
return param.map((p) => replaceWithNativeNull(p))
53+
}
54+
return replaceWithNativeNull(param)
55+
}) as NativeSQLiteQueryParams | NativeSQLiteQueryParams[]
56+
57+
return {
58+
query: command.query,
59+
params: transformedParams,
60+
}
61+
})
62+
}

package/src/operations/session.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
QueryResultRow,
1111
} from '../types'
1212
import { execute, executeAsync } from './execute'
13+
import { executeBatch, executeBatchAsync } from './executeBatch'
1314

1415
export function open(
1516
options: NitroSQLiteConnectionOptions
@@ -33,9 +34,9 @@ export function open(
3334
params?: SQLiteQueryParams
3435
): Promise<QueryResult<Row>> => executeAsync(options.name, query, params),
3536
executeBatch: (commands: BatchQueryCommand[]) =>
36-
HybridNitroSQLite.executeBatch(options.name, commands),
37+
executeBatch(options.name, commands),
3738
executeBatchAsync: (commands: BatchQueryCommand[]) =>
38-
HybridNitroSQLite.executeBatchAsync(options.name, commands),
39+
executeBatchAsync(options.name, commands),
3940
loadFile: (location: string) =>
4041
HybridNitroSQLite.loadFile(options.name, location),
4142
loadFileAsync: (location: string) =>

package/src/specs/NitroSQLite.nitro.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { HybridObject } from 'react-native-nitro-modules'
22
import type {
33
BatchQueryResult,
44
FileLoadResult,
5-
BatchQueryCommand,
65
NativeSQLiteQueryParams,
6+
NativeBatchQueryCommand,
77
} from '../types'
88
import type { NativeQueryResult } from './NativeQueryResult.nitro'
99

@@ -29,10 +29,13 @@ export interface NitroSQLite
2929
query: string,
3030
params?: NativeSQLiteQueryParams
3131
): Promise<NativeQueryResult>
32-
executeBatch(dbName: string, commands: BatchQueryCommand[]): BatchQueryResult
32+
executeBatch(
33+
dbName: string,
34+
commands: NativeBatchQueryCommand[]
35+
): BatchQueryResult
3336
executeBatchAsync(
3437
dbName: string,
35-
commands: BatchQueryCommand[]
38+
commands: NativeBatchQueryCommand[]
3639
): Promise<BatchQueryResult>
3740
loadFile(dbName: string, location: string): FileLoadResult
3841
loadFileAsync(dbName: string, location: string): Promise<FileLoadResult>

package/src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export type SQLiteValue =
3636
| string
3737
| ArrayBuffer
3838
| SQLiteNullValue
39+
40+
/** Used internally to transform the query params into a native format without nullish values */
3941
export type NativeSQLiteQueryParams = SQLiteValue[]
4042

4143
/**
@@ -91,6 +93,14 @@ export interface BatchQueryCommand {
9193
params?: SQLiteQueryParams | SQLiteQueryParams[]
9294
}
9395

96+
/**
97+
* Used internally to transform the batch query commands into a native format without nullish values
98+
*/
99+
export interface NativeBatchQueryCommand {
100+
query: string
101+
params?: NativeSQLiteQueryParams | NativeSQLiteQueryParams[]
102+
}
103+
94104
/**
95105
* status: 0 or undefined for correct execution, 1 for error
96106
* message: if status === 1, here you will find error description

0 commit comments

Comments
 (0)