Skip to content

Commit 8105e91

Browse files
committed
re-write gotchaes
1 parent 1b1c12f commit 8105e91

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

docs/docs/gotchas.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ sidebar_position: 3
44

55
# Gotchas
66

7-
SQLite being a C library and React Native being a JS framework with native parts some times create conflicts. Here are the most common problems.
7+
SQLite being a C library and React Native being a JS framework, sometimes create conflicts. You should take these into account when using the library.
88

9-
## JavaScript and Numbers
9+
## JavaScript Numbers
1010

11-
JavaScript represents every number internally as a `double`. This means you can only have integers represented up to 2^53 bits(`Number.MAX_SAFE_INTEGER`). Although sqlite supports long long (2^64 bits) the numbers will be truncated when you query a value bigger than what a JS number can represent. If you need to store larger numbers you should use a `BigInt` object, however, such a type is not natively supported by sqlite, so you will have to serialize and deserialize from/to strings when you do your queries:
11+
Every JS number is a `double`. This means you can only have integers represented up to `2^53` bits (`Number.MAX_SAFE_INTEGER`). Although sqlite supports 64 bit ints (`long long`), the numbers returned to JS will be truncated when you query a value bigger than what a JS number can represent.
12+
13+
If you need to store larger numbers you should use a `BigInt` object, however, such a type is not natively supported by sqlite, so you will have to serialize and deserialize from/to strings when you do your queries:
1214

1315
```ts
1416
// Create your table with the correct types AND USE STRICT TYPING
@@ -30,35 +32,37 @@ let myBigint = BigInt(res.rows[0].myBigInt);
3032

3133
### Strictness
3234

33-
SQLite by default does not strictly check for types. if you want true type safety when you declare your tables you need to use the `STRICT` keyword.
35+
Sqlite by default does not strictly check for types. If you want type safety, you need to use the `STRICT` keyword.
3436

3537
```tsx
3638
await db.execute('CREATE TABLE Test (id INT PRIMARY KEY, name TEXT) STRICT;');
3739
```
3840

39-
If you don't set it, SQLite will happily write whatever you insert in your table, independetly of the declared type (it will try to cast it though, e.g. a `"1"` string might be turned to a `1` int).
41+
Otherwise, sqlite does a weak attempt at casting and if it fails it inserts whatever you passed into your table, independetly of the declared type. e.g. If you try to insert a string `"1"` into a `INTEGER` column, it will be casted to an int before insertion and behave as a number (see above) when the table is queried.
42+
43+
[Read the sqlite docs on data types and their loose typing and how it can be the source of subtle bugs](https://sqlite.org/datatype3.html).
4044

4145
### Foreign constraints
4246

43-
When SQLite evaluates your query and you have forgein key constraints, it keeps track of the satisfied relations via a counter. Once your statement finishes executing and the counter is not 0, it throws a foreign key constraint failed error. Unfortunately, this simple design means it is impossible to catch which foreign constraint is failed and you will receive a generic error. Nothing op-sqlite can do about it, it's a design flaw in SQLite.
47+
When sqlite evaluates your query and you have forgein key constraints, it keeps track of the satisfied relations via a simple counter. Once your statement finishes executing and the counter is not 0, it then throws a `foreign key constraint failed` error, with no information whatsoever of which foreign constraint failed. Unfortunately, this simple design means it is impossible to catch which foreign constraint has failed and you will receive a generic error. Nothing op-sqlite can do about it, it's a design flaw in SQLite.
4448

45-
In order to catch foreign key errors, you also need to execute the pragma when you open your connection:
49+
Additionaly, the default behavior is not to enforce foreign key constraints. You need to turn the constraint checking on, after opening your connection:
4650

4751
```tsx
48-
await db.execute('PRAGMA foreign_keys = true');
52+
db.executeSync('PRAGMA foreign_keys = true');
4953
```
5054

5155
### Error codes
5256

53-
Sometimes you might be using valid SQL syntax for other engines or you might be doing something else wrong. The errors returned by op-sqlite contain the raw error code returned by SQLite and you should check [the reference](https://www.sqlite.org/rescode.html) for more detailed information.
57+
Sometimes you might be using valid SQL syntax for other engines or you might be doing something else wrong. The errors returned by op-sqlite contain the raw error code returned by SQLite and you should check [the sqlite error reference](https://www.sqlite.org/rescode.html) for more detailed information.
5458

5559
### Other Quirks
5660

57-
See the [full list of SQLite quirks](https://www.sqlite.org/quirks.html).
61+
See the [full list of SQLite quirks](https://www.sqlite.org/quirks.html) that also apply to op-sqlite.
5862

5963
## HostObjects Quirks
6064

61-
op-sqlite can return HostObjects via the `executeWithHostObjects` API, basically C++ instances exposed to the JS context. They are super fast to create at the cost of runtime access. However, this means some operations won't work.
65+
op-sqlite can return HostObjects via the `executeWithHostObjects` API, basically C++ objects exposed to JS. They are super fast to create at the cost of runtime access. However, by them being C++ objects, it means some JS operations won't work.
6266

6367
You can write single properties with scalars, for example:
6468

0 commit comments

Comments
 (0)