Skip to content

Commit 4479e38

Browse files
author
Oscar Franco
committed
Update readme
1 parent 9b25d79 commit 4479e38

2 files changed

Lines changed: 56 additions & 31 deletions

File tree

README.md

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1 align="center">React Native Quick SQLite</h1>
22

3-
<h3 align="center">The **fastest** SQLite implementation for react-native.</h3>
3+
<h3 align="center">Fast SQLite for react-native.</h3>
44

55
![Frame 2](https://user-images.githubusercontent.com/1634213/127499575-aed1d0e2-8a93-42ab-917e-badaab8916f6.png)
66

@@ -21,33 +21,43 @@
2121
</div>
2222
<br />
2323

24-
Quick SQLite uses [JSI](https://formidable.com/blog/2019/jsi-jsc-part-2), removes all the overhead of intercommunication between JavaScript code and C++ code, making CRUDing entities from SQLite super fast!
24+
This library uses [JSI](https://formidable.com/blog/2019/jsi-jsc-part-2) to directly call C++ code from JS. It provides a low-level API to execute SQL queries, therefore I recommend you use it with TypeORM.
2525

26-
Big ❤️ to [react-native-sqlite-storage](https://github.com/andpor/react-native-sqlite-storage) and [react-native-sqlite2](https://github.com/craftzdog/react-native-sqlite-2), this library also provides a WebSQL interface.
26+
Inspired/compatible with [react-native-sqlite-storage](https://github.com/andpor/react-native-sqlite-storage) and [react-native-sqlite2](https://github.com/craftzdog/react-native-sqlite-2).
2727

28-
If you are using quick-sqlite in your app, please get in touch or open a PR with a modified README (with your company/product logo, would love to showcase you!
28+
## Gotchas
2929

30-
## GOTCHAS
31-
32-
- **It's not possible to use the browser debugger with JSI**, use [Flipper](https://github.com/facebook/flipper), on android Flipper also has an integrated database explorer you can use to debug your sqlite database, [you will have to configure your database path though](https://fbflipper.com/docs/setup/plugins/databases/).
33-
![130516553-15c18d0f-65ad-44cf-8235-a4d6f41859e2](https://user-images.githubusercontent.com/1634213/130755919-7539d3dd-7d30-4234-9965-bfef2450ab0a.png)
34-
- Your app will now include C++, you will need to install the NDK on your machine for android. (unless you know how to generate an AAR, feel free to open a PR)
35-
- If you want to run the example project on android, you will have to change the paths on the android/CMakeLists.txt file, they are already there, just uncomment them.
30+
- **It's not possible to use a browser to debug a JSI app**, use [Flipper](https://github.com/facebook/flipper) (on android Flipper also has an integrated SQLite Database explorer).
31+
- Your app will now include C++, you will need to install the NDK on your machine for android.
3632
- This library supports SQLite BLOBs which are mapped to JS ArrayBuffers, check out the sample project on how to use it
37-
- Starting with version 2.0.0 the library no longer throws errors when an invalid statement is passed, but rather returns an object with a `status` enum property, where 0 signals a succesful execution and `1` an incorrect execution (this is to keep typeorm from exploding when an incorrect query is executed)
38-
- This library cannot retrieve integers larger than 53 bits because it's not possible to represent such numbers in JavaScript. [Read here](https://github.com/ospfranco/react-native-quick-sqlite/issues/16#issuecomment-1018412991) for more information.
33+
- If you want to run the example project on android, you will have to change the paths on the android/CMakeLists.txt file, they are already there, just uncomment them.
34+
- Starting with version 2.0.0 the library no longer throws errors when an invalid statement is passed, but returns a status (0 or 1) field in the response.
35+
- This library cannot retrieve integers larger than 53 bits because it's not possible to represent such numbers in JavaScript. [Read more](https://github.com/ospfranco/react-native-quick-sqlite/issues/16#issuecomment-1018412991).
3936

4037
## Use TypeORM
4138

42-
The recommended way to use this package is to use [TypeORM](https://github.com/typeorm/typeorm) with [patch-package](https://github.com/ds300/patch-package). TypeORM already has a sqlite-storage driver. In the `example` project on the `patch` folder you can a find a [patch for TypeORM](https://github.com/ospfranco/react-native-quick-sqlite/blob/main/example/patches/typeorm%2B0.2.31.patch), it basically just replaces all the `react-native-sqlite-storage` strings in TypeORM with `react-native-quick-sqlite`.
39+
This package offers a low-level API to raw execute SQL queries. I strongly recommend to use [TypeORM](https://github.com/typeorm/typeorm) (with [patch-package](https://github.com/ds300/patch-package)). TypeORM already has a sqlite-storage driver. In the `example` project on the `patch` folder you can a find a [patch for TypeORM](https://github.com/ospfranco/react-native-quick-sqlite/blob/main/example/patches/typeorm%2B0.2.31.patch).
4340

44-
Follow the instructions to make TypeORM work with rn (enable decorators, configure babel, etc), then apply the patch via patch-package and you should be good to go.
41+
Follow the instructions to make TypeORM work with React Native (enable decorators, configure babel, etc), then apply the patch via patch-package and you should be good to go.
4542

46-
## Low level API
43+
## API
4744

4845
It is also possible to directly execute SQL against the db:
4946

5047
```typescript
48+
interface QueryResult {
49+
status: 0 | 1; // 0 for correct execution
50+
message: string; // if status === 1, here you will find error description
51+
rows: any[];
52+
insertId?: number;
53+
}
54+
55+
interface BatchQueryResult {
56+
status?: 0 | 1;
57+
rowsAffected?: number;
58+
message?: string;
59+
}
60+
5161
interface ISQLite {
5262
open: (dbName: string, location?: string) => any;
5363
close: (dbName: string) => any;
@@ -59,7 +69,7 @@ interface ISQLite {
5969
executeSqlBatch: (
6070
dbName: string,
6171
commands: SQLBatchParams[]
62-
) => BatchExecutionResult;
72+
) => BatchQueryResult;
6373
}
6474
```
6575

@@ -79,36 +89,45 @@ try {
7989
```
8090

8191
Some query examples:
92+
8293
```typescript
8394
let result = sqlite.executeSql('myDatabase', 'SELECT somevalue FROM sometable');
84-
if(!result.status) { // result.status undefined or 0 === sucess
85-
for(let i = 0; i<result.rows.length; i++) {
95+
if (!result.status) {
96+
// result.status undefined or 0 === sucess
97+
for (let i = 0; i < result.rows.length; i++) {
8698
const row = result.rows.item(i);
8799
console.log(row.somevalue);
88100
}
89101
}
90102

91-
result = sqlite.executeSql('myDatabase', 'UPDATE sometable set somecolumn = ? where somekey = ?', [0, 1]);
92-
if(!result.status) { // result.status undefined or 0 === sucess
103+
result = sqlite.executeSql(
104+
'myDatabase',
105+
'UPDATE sometable set somecolumn = ? where somekey = ?',
106+
[0, 1]
107+
);
108+
if (!result.status) {
109+
// result.status undefined or 0 === sucess
93110
console.log(`Update affected ${result.rowsAffected} rows`);
94111
}
95112
```
96113

97-
Bulkupdate allows transactional execution of a set of commands
114+
Batch execution allows transactional execution of a set of commands
115+
98116
```typescript
99117
const commands = [
100118
['CREATE TABLE TEST (id integer)'],
101-
['INSERT INTO TABLE TEST (id) VALUES (?)', [1]]
102-
['INSERT INTO TABLE TEST (id) VALUES (?)', [2]]
103-
['INSERT INTO TABLE TEST (id) VALUES (?)', [[3], [4], [5], [6]]]
119+
['INSERT INTO TABLE TEST (id) VALUES (?)', [1]][
120+
('INSERT INTO TABLE TEST (id) VALUES (?)', [2])
121+
][('INSERT INTO TABLE TEST (id) VALUES (?)', [[3], [4], [5], [6]])],
104122
];
105123
const result = sqlite.executeSqlBatch('myDatabase', commands);
106-
if(!result.status) { // result.status undefined or 0 === sucess
124+
if (!result.status) {
125+
// result.status undefined or 0 === sucess
107126
console.log(`Batch affected ${result.rowsAffected} rows`);
108127
}
109128
```
110129

111-
## JSI Cheatsheet
130+
## Learn React Native JSI
112131

113132
If you want to learn how to make your own JSI module and also get a reference guide for all things C++/JSI you can buy my [JSI/C++ Cheatsheet](http://ospfranco.gumroad.com/l/IeeIvl)
114133

src/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ interface QueryResult {
3838
* If a single query must be executed many times with different arguments, its preferred
3939
* to declare it a single time, and use an array of array parameters.
4040
*/
41-
type SQLBatchParams = [string, Array<any> | Array<Array<any>> | undefined];
41+
type SQLBatchParams = [string, Array<any> | Array<Array<any>> | undefined];
4242

4343
/**
4444
* status: 0 or undefined for correct execution, 1 for error
4545
* message: if status === 1, here you will find error description
4646
* rowsAffected: Number of affected rows if status == 0
4747
*/
48-
interface BatchExecutionResult {
48+
interface BatchQueryResult {
4949
status?: 0 | 1;
5050
rowsAffected?: number;
5151
message?: string;
@@ -62,7 +62,7 @@ interface ISQLite {
6262
executeSqlBatch: (
6363
dbName: string,
6464
commands: SQLBatchParams[]
65-
) => BatchExecutionResult;
65+
) => BatchQueryResult;
6666
// backgroundExecuteSql: (dbName: string, query: string, params: any[]) => any;
6767
}
6868

@@ -81,7 +81,10 @@ interface IDBConnection {
8181
ok: (res: QueryResult) => void,
8282
fail: (msg: string) => void
8383
) => void;
84-
executeSqlBatch: (commands: SQLBatchParams[], callback?: (res: BatchExecutionResult) => void) => void;
84+
executeSqlBatch: (
85+
commands: SQLBatchParams[],
86+
callback?: (res: BatchQueryResult) => void
87+
) => void;
8588
close: (ok: (res: any) => void, fail: (msg: string) => void) => void;
8689
}
8790

@@ -114,7 +117,10 @@ export const openDatabase = (
114117
fail(e);
115118
}
116119
},
117-
executeSqlBatch: (commands: SQLBatchParams[], callback?: (res: BatchExecutionResult) => void) => {
120+
executeSqlBatch: (
121+
commands: SQLBatchParams[],
122+
callback?: (res: BatchQueryResult) => void
123+
) => {
118124
const response = sqlite.executeSqlBatch(options.name, commands);
119125
if (callback) callback(response);
120126
},

0 commit comments

Comments
 (0)