Skip to content

Commit 10fb430

Browse files
authored
Update README.md
1 parent ae7ac42 commit 10fb430

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

README.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ TypeORM is officially supported, however, there is currently a parsing issue wit
4646
```typescript
4747
import {open} from 'react-native-nitro-sqlite'
4848

49-
const db = open('myDb.sqlite')
49+
const db = open({name: 'myDb.sqlite', location: '<optional_file_location>'})
5050

5151
// The db object now contains the following methods:
52-
5352
db = {
5453
close: () => void,
5554
delete: () => void,
@@ -141,6 +140,60 @@ const res = NitroSQLite.executeSqlBatch('myDatabase', commands);
141140
console.log(`Batch affected ${result.rowsAffected} rows`);
142141
```
143142

143+
### Sending and receiving nullish values
144+
145+
Due to internal limitations with Nitro modules, we have to handle nullish values explicitly in NitroSQLite. There are two ways to send and receive null values:
146+
147+
#### Default null handling
148+
149+
By default, the user can pass the `NITRO_SQLITE_NULL` constant instead of `null` or `undefined` to query params and will also receive this constant for nullish values in e.g. `SELECT` queries. `NITRO_SQLITE_NULL` is the object that is used internally to handle nullish values, therefore **this approach does NOT introduce any performance overhead**.
150+
151+
A `INSERT` query with nullish values could look like this:
152+
153+
```typescript
154+
import { NITRO_SQLITE_NULL } from 'react-native-nitro-sqlite'
155+
156+
db.execute(
157+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
158+
[1, "Mike", NITRO_SQLITE_NULL, NITRO_SQLITE_NULL]
159+
)
160+
```
161+
162+
Query results that are received from e.g. `execute()` will also return this special object/struct. To check for null values, the user can use the a special function:
163+
```
164+
import { isNitroSQLiteNull } from 'react-native-nitro-sqlite'
165+
166+
const res = db.execute('SELECT * FROM User')
167+
168+
const firstItem = res.rows?.item(0)
169+
if (isNitroSQLiteNull(firstItem.age) {
170+
// Handle null value
171+
}
172+
```
173+
174+
#### Simplified null handling
175+
To enable simple null handling, call `enableSimpleNullHandling()` in the root of your project. This will allow you to just pass `null` or `undefined` to NitroSQLite functions, e.g. as query params. in `execute()`:
176+
177+
```typescript
178+
db.execute(
179+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
180+
[1, "Mike", null, undefined]
181+
)
182+
```
183+
184+
Note that in SQLite, both `undefined` and `null` are transformed into the same representation in the database. Therefore, nullish values received from `SELECT` queries will always evaluate to `null`, even if `undefined` was used in the `INSERT` query.
185+
186+
```typescript
187+
const res = db.execute('SELECT * FROM User')
188+
189+
const firstItem = res.rows?.item(0)
190+
if (firstItem.age === null) { // Nullish values will always be null and never undefined.
191+
// Handle null value
192+
}
193+
```
194+
195+
Simple null handling adds some logic to internally transform nullish values into a special object/struct and vice versa, that is sent/received from the native C++ side. This **might introduce some performance overhead**, since we have to loop over the params and query results and check for this structure.
196+
144197
### Dynamic Column Metadata
145198

146199
In some scenarios, dynamic applications may need to get some metadata information about the returned result set.

0 commit comments

Comments
 (0)