You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/gotchas.md
+15-11Lines changed: 15 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,13 @@ sidebar_position: 3
4
4
5
5
# Gotchas
6
6
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.
8
8
9
-
## JavaScript and Numbers
9
+
## JavaScript Numbers
10
10
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:
12
14
13
15
```ts
14
16
// Create your table with the correct types AND USE STRICT TYPING
@@ -30,35 +32,37 @@ let myBigint = BigInt(res.rows[0].myBigInt);
30
32
31
33
### Strictness
32
34
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.
34
36
35
37
```tsx
36
38
awaitdb.execute('CREATE TABLE Test (id INT PRIMARY KEY, name TEXT) STRICT;');
37
39
```
38
40
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).
40
44
41
45
### Foreign constraints
42
46
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.
44
48
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:
46
50
47
51
```tsx
48
-
awaitdb.execute('PRAGMA foreign_keys = true');
52
+
db.executeSync('PRAGMA foreign_keys = true');
49
53
```
50
54
51
55
### Error codes
52
56
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.
54
58
55
59
### Other Quirks
56
60
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.
58
62
59
63
## HostObjects Quirks
60
64
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.
62
66
63
67
You can write single properties with scalars, for example:
0 commit comments