Skip to content

Commit 127b0de

Browse files
author
Oscar Franco
authored
Merge pull request #35 from ospfranco/async-callback-threads
2 parents a8430bc + 6ad4158 commit 127b0de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+7279
-3572
lines changed

.bundle/config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BUNDLE_PATH: "vendor/bundle"
2+
BUNDLE_FORCE_RUBY_PLATFORM: 1

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ project.xcworkspace
3434
.idea
3535
.gradle
3636
local.properties
37-
android.iml
37+
*.iml
38+
*.hprof
3839

3940
# Cocoapods
4041
#
@@ -59,3 +60,6 @@ android/.cxx
5960

6061
# generated by bob
6162
lib/
63+
# Gradle
64+
android/gradle/
65+
android/gradle*

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
# You may use http://rbenv.org/ or https://rvm.io/ to install and use this version
3+
ruby '2.7.4'
4+
gem 'cocoapods', '~> 1.11', '>= 1.11.2'

README.md

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

3-
<h3 align="center">Fast SQLite for react-native.</h3>
3+
<h3 align="center">Fast SQLite for React-Native.</h3>
44

5-
![Frame 2](https://user-images.githubusercontent.com/1634213/127499575-aed1d0e2-8a93-42ab-917e-badaab8916f6.png)
5+
![screenshot](https://raw.githubusercontent.com/ospfranco/react-native-quick-sqlite/main/header.png)
66

77
<div align="center">
88
<pre align="center">
@@ -21,7 +21,7 @@
2121
</div>
2222
<br />
2323

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.
24+
This library provides a low-level API to execute SQL queries, fast bindings via [JSI](https://formidable.com/blog/2019/jsi-jsc-part-2).
2525

2626
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

@@ -30,21 +30,9 @@ Inspired/compatible with [react-native-sqlite-storage](https://github.com/andpor
3030
- **Javascript cannot represent intergers larger than 53 bits**, be careful when loading data if it came from other systems. [Read more](https://github.com/ospfranco/react-native-quick-sqlite/issues/16#issuecomment-1018412991).
3131
- **It's not possible to use a browser to debug a JSI app**, use [Flipper](https://github.com/facebook/flipper) (for android Flipper also has SQLite Database explorer).
3232
- Your app will now include C++, you will need to install the NDK on your machine for android.
33-
- This library supports SQLite BLOBs which are mapped to JS ArrayBuffers, check out the sample project on how to use it
34-
- From version 2.0.0 onwards errors are no longer thrown on invalid SQL statements. The response contains a `status` number, `0` signals correct execution, `1` signals an error.
35-
- From version 3.0.0 onwards no JS errors are thown, every operation returns an object with a `status` field.
36-
- 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.
37-
38-
## Use TypeORM
39-
40-
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).
41-
42-
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.
4333

4434
## API
4535

46-
It is also possible to directly execute SQL against the db:
47-
4836
```typescript
4937
interface QueryResult {
5038
status: 0 | 1; // 0 for correct execution
@@ -60,23 +48,41 @@ interface BatchQueryResult {
6048
}
6149

6250
interface ISQLite {
63-
open: (dbName: string, location?: string) => any;
64-
close: (dbName: string) => any;
51+
open: (dbName: string, location?: string) => { status: 0 | 1 };
52+
close: (dbName: string) => { status: 0 | 1 };
6553
executeSql: (
6654
dbName: string,
6755
query: string,
6856
params: any[] | undefined
6957
) => QueryResult;
58+
asyncExecuteSql: (
59+
dbName: string,
60+
query: string,
61+
params: any[] | undefined,
62+
cb: (res: QueryResult) => void
63+
) => void;
7064
executeSqlBatch: (
7165
dbName: string,
7266
commands: SQLBatchParams[]
7367
) => BatchQueryResult;
68+
asyncExecuteSqlBatch: (
69+
dbName: string,
70+
commands: SQLBatchParams[],
71+
cb: (res: BatchQueryResult) => void
72+
) => void;
73+
loadSqlFile: (dbName: string, location: string) => FileLoadResult;
74+
asyncLoadSqlFile: (
75+
dbName: string,
76+
location: string,
77+
cb: (res: FileLoadResult) => void
78+
) => void;
7479
}
7580
```
7681

77-
In your code
82+
# Usage
7883

7984
```typescript
85+
// Import as early as possible, auto-installs bindings
8086
import 'react-native-quick-sqlite';
8187

8288
// `sqlite` is a globally registered object, so you can directly call it from anywhere in your javascript
@@ -128,9 +134,25 @@ if (!result.status) {
128134
}
129135
```
130136

137+
Async versions are also available if you have too much SQL to execute
138+
139+
```ts
140+
sqlite.asyncExecuteSql('myDatabase', 'SELECT * FROM "User";', [], (result) => {
141+
if (result.status === 0) {
142+
console.log('users', result.rows);
143+
}
144+
});
145+
```
146+
147+
## Use TypeORM
148+
149+
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).
150+
151+
Follow the instructions to make TypeORM work with React Native (enable decorators, configure babel, etc), then apply the example patch via patch-package.
152+
131153
## Learn React Native JSI
132154

133-
If you want to learn how to make your own JSI module buy my [JSI/C++ Cheatsheet](http://ospfranco.gumroad.com/l/IeeIvl), I'm also available for [freelance work](mailto:[email protected]?subject=Freelance)!
155+
If you want to learn how to make your own JSI module buy my [JSI/C++ Cheatsheet](http://ospfranco.gumroad.com/l/jsi_guide), I'm also available for [freelance work](mailto:[email protected]?subject=Freelance)!
134156

135157
## License
136158

android/CMakeLists.txt

Lines changed: 107 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,121 @@
1-
cmake_minimum_required(VERSION 3.4.1)
1+
cmake_minimum_required(VERSION 3.9.0)
22

33
set (CMAKE_VERBOSE_MAKEFILE ON)
4-
set (CMAKE_CXX_STANDARD 11)
5-
6-
# Uncomment the following lines to compile the example project
7-
# include_directories(
8-
# ../cpp
9-
# ../node_modules/react-native/React
10-
# ../node_modules/react-native/React/Base
11-
# ../node_modules/react-native/ReactCommon/jsi
12-
# )
13-
14-
# add_library(sequel
15-
# SHARED
16-
17-
# ../node_modules/react-native/ReactCommon/jsi/jsi/jsi.cpp
18-
# ../cpp/sequel.cpp
19-
# ../cpp/sequel.h
20-
# ../cpp/SequelResult.h
21-
# ../cpp/react-native-quick-sqlite.cpp
22-
# ../cpp/react-native-quick-sqlite.h
23-
# ../cpp/sqlite3.h
24-
# ../cpp/sqlite3.c
25-
# cpp-adapter.cpp
26-
# )
4+
set (CMAKE_CXX_STANDARD 14)
5+
#set (CMAKE_CXX_FLAGS "-DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID -DFOR_HERMES=${FOR_HERMES}")
6+
7+
set (PACKAGE_NAME "react-native-quick-sqlite")
8+
set (BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
9+
set (RN_SO_DIR ${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/first-party/react/jni)
10+
11+
if(${REACT_NATIVE_VERSION} LESS 66)
12+
set (
13+
INCLUDE_JSI_CPP
14+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp"
15+
)
16+
set (
17+
INCLUDE_JSIDYNAMIC_CPP
18+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/JSIDynamic.cpp"
19+
)
20+
endif()
21+
22+
file (GLOB LIBFBJNI_INCLUDE_DIR "${BUILD_DIR}/fbjni-*-headers.jar/")
2723

2824
include_directories(
2925
../cpp
30-
../../react-native/React
31-
../../react-native/React/Base
32-
../../react-native/ReactCommon/jsi
26+
"${NODE_MODULES_DIR}/react-native/React"
27+
"${NODE_MODULES_DIR}/react-native/React/Base"
28+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
29+
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
30+
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni"
31+
"${NODE_MODULES_DIR}/react-native/ReactCommon"
32+
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
33+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
34+
"${NODE_MODULES_DIR}/hermes-engine/android/include/"
35+
${INCLUDE_JSI_CPP} # only on older RN versions
36+
${INCLUDE_JSIDYNAMIC_CPP} # only on older RN versions
3337
)
3438

35-
add_library(sequel
39+
add_library(
40+
${PACKAGE_NAME}
3641
SHARED
37-
../../react-native/ReactCommon/jsi/jsi/jsi.cpp
38-
../cpp/sequel.cpp
39-
../cpp/sequel.h
40-
../cpp/SequelResult.h
41-
../cpp/react-native-quick-sqlite.cpp
42-
../cpp/react-native-quick-sqlite.h
42+
../cpp/sqliteBridge.cpp
43+
../cpp/sqliteBridge.h
44+
../cpp/installer.cpp
45+
../cpp/installer.h
4346
../cpp/sqlite3.h
4447
../cpp/sqlite3.c
48+
../cpp/JSIHelper.h
49+
../cpp/JSIHelper.cpp
50+
../cpp/ThreadPool.h
51+
../cpp/ThreadPool.cpp
52+
../cpp/sqlfileloader.h
53+
../cpp/sqlfileloader.cpp
54+
../cpp/sqlbatchexecutor.h
55+
../cpp/sqlbatchexecutor.cpp
4556
cpp-adapter.cpp
4657
)
4758

59+
# find fbjni package
60+
file (GLOB LIBFBJNI_INCLUDE_DIR "${BUILD_DIR}/fbjni-*-headers.jar/")
4861

49-
target_link_libraries(sequel android log)
62+
target_include_directories(
63+
${PACKAGE_NAME}
64+
PRIVATE
65+
# --- fbjni ---
66+
"${LIBFBJNI_INCLUDE_DIR}"
67+
# --- React Native ---
68+
"${NODE_MODULES_DIR}/react-native/React"
69+
"${NODE_MODULES_DIR}/react-native/React/Base"
70+
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni"
71+
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni"
72+
"${NODE_MODULES_DIR}/react-native/ReactCommon"
73+
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
74+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
75+
"${NODE_MODULES_DIR}/hermes-engine/android/include/"
76+
${INCLUDE_JSI_CPP} # only on older RN versions
77+
${INCLUDE_JSIDYNAMIC_CPP} # only on older RN versions
78+
)
79+
80+
file (GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
81+
82+
find_library(
83+
FBJNI_LIB
84+
fbjni
85+
PATHS ${LIBRN_DIR}
86+
NO_CMAKE_FIND_ROOT_PATH
87+
)
88+
89+
find_library(
90+
REACT_NATIVE_JNI_LIB
91+
reactnativejni
92+
PATHS ${LIBRN_DIR}
93+
NO_CMAKE_FIND_ROOT_PATH
94+
)
95+
if(${REACT_NATIVE_VERSION} LESS 66)
96+
# JSI lib didn't exist on RN 0.65 and before. Simply omit it.
97+
set (JSI_LIB "")
98+
else()
99+
# RN 0.66 distributes libjsi.so, can be used instead of compiling jsi.cpp manually.
100+
find_library(
101+
JSI_LIB
102+
jsi
103+
PATHS ${LIBRN_DIR}
104+
NO_CMAKE_FIND_ROOT_PATH
105+
)
106+
endif()
107+
108+
find_library(
109+
LOG_LIB
110+
log
111+
)
112+
113+
# target_link_libraries(sequel fbjni::fbjni android log)
114+
target_link_libraries(
115+
${PACKAGE_NAME}
116+
${LOG_LIB}
117+
${JSI_LIB}
118+
${REACT_NATIVE_JNI_LIB}
119+
${FBJNI_LIB}
120+
android
121+
)

0 commit comments

Comments
 (0)