Skip to content

Commit 195f1db

Browse files
committed
feat: add ENABLE_SIMPLE_NULL_HANDLING flag
1 parent 9b3092f commit 195f1db

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

package/src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { NativeSQLiteNullValue } from './types'
77
export type * from './types'
88
export { typeORMDriver } from './typeORM'
99

10-
export const NITRO_SQLITE_NULL: NativeSQLiteNullValue = { isNull: true }
11-
1210
export const onInitialized = new Promise<void>((resolve) => {
1311
NitroSQLiteOnLoad.onReactApplicationContextReady(resolve)
1412
})
@@ -27,3 +25,15 @@ export const NitroSQLite = {
2725
}
2826

2927
export { open } from './operations/session'
28+
29+
let ENABLE_SIMPLE_NULL_HANDLING = false
30+
export function enableEasierNullHandling(
31+
shouldEnableSimpleNullHandling = true
32+
) {
33+
ENABLE_SIMPLE_NULL_HANDLING = shouldEnableSimpleNullHandling
34+
}
35+
export function simpleNullHandlingEnabled() {
36+
return ENABLE_SIMPLE_NULL_HANDLING
37+
}
38+
39+
export const NITRO_SQLITE_NULL: NativeSQLiteNullValue = { isNull: true }

package/src/operations/execute.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NITRO_SQLITE_NULL } from '..'
1+
import { NITRO_SQLITE_NULL, simpleNullHandlingEnabled } from '..'
22
import { HybridNitroSQLite } from '../nitro'
33
import type { NativeQueryResult } from '../specs/NativeQueryResult.nitro'
44
import type {
@@ -8,31 +8,35 @@ import type {
88
QueryResultRow,
99
} from '../types'
1010

11-
export function execute<Data extends QueryResultRow = never>(
11+
export function execute<Row extends QueryResultRow = never>(
1212
dbName: string,
1313
query: string,
1414
params?: SQLiteQueryParams
15-
): QueryResult<Data> {
15+
): QueryResult<Row> {
1616
const nativeResult = HybridNitroSQLite.execute(
1717
dbName,
1818
query,
1919
toNativeQueryParams(params)
2020
)
21-
const result = buildJsQueryResult<Data>(nativeResult)
21+
const result = buildJsQueryResult<Row>(nativeResult)
2222
return result
2323
}
2424

25-
export async function executeAsync<Data extends QueryResultRow = never>(
25+
export async function executeAsync<Row extends QueryResultRow = never>(
2626
dbName: string,
2727
query: string,
2828
params?: SQLiteQueryParams
29-
): Promise<QueryResult<Data>> {
29+
): Promise<QueryResult<Row>> {
30+
const transformedParams = simpleNullHandlingEnabled()
31+
? toNativeQueryParams(params)
32+
: (params as NativeSQLiteQueryParams)
33+
3034
const nativeResult = await HybridNitroSQLite.executeAsync(
3135
dbName,
3236
query,
33-
toNativeQueryParams(params)
37+
transformedParams
3438
)
35-
const result = buildJsQueryResult<Data>(nativeResult)
39+
const result = buildJsQueryResult<Row>(nativeResult)
3640
return result
3741
}
3842

@@ -47,21 +51,25 @@ function toNativeQueryParams(
4751
})
4852
}
4953

50-
function buildJsQueryResult<Data extends QueryResultRow = never>({
54+
function buildJsQueryResult<Row extends QueryResultRow = never>({
5155
insertId,
5256
rowsAffected,
5357
results,
54-
}: NativeQueryResult): QueryResult<Data> {
55-
const data = results.map((row) =>
56-
Object.fromEntries(
57-
Object.entries(row).map(([key, value]) => {
58-
if (value === NITRO_SQLITE_NULL) {
59-
return [key, undefined]
60-
}
61-
return [key, value]
62-
})
63-
)
64-
) as Data[]
58+
}: NativeQueryResult): QueryResult<Row> {
59+
let data: Row[] = results as Row[]
60+
61+
if (simpleNullHandlingEnabled()) {
62+
data = results.map((row) =>
63+
Object.fromEntries(
64+
Object.entries(row).map(([key, value]) => {
65+
if (value === NITRO_SQLITE_NULL) {
66+
return [key, undefined]
67+
}
68+
return [key, value]
69+
})
70+
)
71+
) as Row[]
72+
}
6573

6674
return {
6775
insertId,

package/src/types.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,25 @@ export enum ColumnType {
2626
NULL_VALUE,
2727
}
2828

29-
export type BaseSQLiteValue = boolean | number | string | ArrayBuffer
30-
3129
// Passing null/undefined in array types is not possible, so we us a special struct as a workaround.
3230
export type NativeSQLiteNullValue = {
3331
isNull: true
3432
}
35-
export type NativeSQLiteValue = BaseSQLiteValue | NativeSQLiteNullValue
33+
export type NativeSQLiteValue =
34+
| boolean
35+
| number
36+
| string
37+
| ArrayBuffer
38+
| NativeSQLiteNullValue
3639
export type NativeSQLiteQueryParams = NativeSQLiteValue[]
3740

3841
/**
3942
* Represents a value that can be stored in a SQLite database
4043
*/
41-
export type SQLiteValue = BaseSQLiteValue | null | undefined
44+
export type SQLiteValue = NativeSQLiteValue | null | undefined
4245
export type SQLiteQueryParams = SQLiteValue[]
4346

44-
export type QueryResultItem = BaseSQLiteValue | undefined
47+
export type QueryResultItem = NativeSQLiteValue | undefined
4548
export type QueryResultRow = Record<string, QueryResultItem>
4649
export interface QueryResult<RowData extends QueryResultRow = QueryResultRow> {
4750
readonly insertId?: number

0 commit comments

Comments
 (0)