@powersync/op-sqlite 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +54 -0
- package/android/build.gradle +124 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/AndroidManifestNew.xml +2 -0
- package/android/src/main/java/com/powersync/opsqlite/PowerSyncOpSqliteModule.kt +25 -0
- package/android/src/main/java/com/powersync/opsqlite/PowerSyncOpSqlitePackage.kt +35 -0
- package/android/src/newarch/PowerSyncOpSqliteSpec.kt +7 -0
- package/android/src/oldarch/PowerSyncOpSqliteSpec.kt +11 -0
- package/ios/PowerSyncOpSqlite.h +5 -0
- package/ios/PowerSyncOpSqlite.mm +11 -0
- package/package.json +133 -0
- package/powersync-op-sqlite.podspec +26 -0
- package/src/NativePowerSyncOpSqlite.ts +8 -0
- package/src/db/OPSQLiteConnection.ts +82 -0
- package/src/db/OPSqliteAdapter.ts +259 -0
- package/src/db/OPSqliteDBOpenFactory.ts +25 -0
- package/src/db/SqliteOptions.ts +54 -0
- package/src/index.ts +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# OP-SQLite Factory for the PowerSync React Native SDK
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
This package (`packages/powersync-op-sqlite`) enables using [OP-SQLite](https://github.com/op-engineering/op-sqlite) with PowerSync alongside the [React Native SDK](../react-native/README.md).
|
|
6
|
+
|
|
7
|
+
If you are not yet familiar with PowerSync, please see the [PowerSync React Native SDK](../react-native/README.md) for more information.
|
|
8
|
+
|
|
9
|
+
### Alpha release
|
|
10
|
+
|
|
11
|
+
This package is currently in an alpha release. If you find a bug or issue, please open a [GitHub issue](https://github.com/powersync-ja/powersync-js/issues). Questions or feedback can be posted on our [community Discord](https://discord.gg/powersync) - we'd love to hear from you.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Follow the installation instructions for the [React Native SDK](../react-native/README.md) if you haven't yet set up PowerSync in your project. However, note that this package cannot be installed alongside `@journeyapps/react-native-quick-sqlite`. Skip the step about installing it as a peer dependency, or uninstall it if it is already installed.
|
|
16
|
+
|
|
17
|
+
### Install Package
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npx expo install @powersync/op-sqlite
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Install Peer Dependency:
|
|
24
|
+
|
|
25
|
+
This SDK currently requires `@op-engineering/op-sqlite` as a peer dependency.
|
|
26
|
+
|
|
27
|
+
Install it in your app with:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npx expo install @op-engineering/op-sqlite
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { OPSqliteOpenFactory } from '@powersync/op-sqlite';
|
|
37
|
+
import { PowerSyncDatabase } from '@powersync/react-native';
|
|
38
|
+
|
|
39
|
+
const factory = new OPSqliteOpenFactory({
|
|
40
|
+
dbFilename: 'sqlite.db'
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
this.powersync = new PowerSyncDatabase({ database: factory, schema: AppSchema });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Native Projects
|
|
47
|
+
|
|
48
|
+
This package uses native libraries. Create native Android and iOS projects (if not created already) by running:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npx expo run:android
|
|
52
|
+
# OR
|
|
53
|
+
npx expo run:ios
|
|
54
|
+
```
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
|
|
3
|
+
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["OpSqlite_kotlinVersion"]
|
|
4
|
+
|
|
5
|
+
repositories {
|
|
6
|
+
google()
|
|
7
|
+
mavenCentral()
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
dependencies {
|
|
11
|
+
classpath "com.android.tools.build:gradle:7.2.1"
|
|
12
|
+
// noinspection DifferentKotlinGradleVersion
|
|
13
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
def reactNativeArchitectures() {
|
|
18
|
+
def value = rootProject.getProperties().get("reactNativeArchitectures")
|
|
19
|
+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
def isNewArchitectureEnabled() {
|
|
23
|
+
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
apply plugin: "com.android.library"
|
|
27
|
+
apply plugin: "kotlin-android"
|
|
28
|
+
|
|
29
|
+
if (isNewArchitectureEnabled()) {
|
|
30
|
+
apply plugin: "com.facebook.react"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
def getExtOrDefault(name) {
|
|
34
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["PowerSyncOpSqlite_" + name]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
def getExtOrIntegerDefault(name) {
|
|
38
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["PowerSyncOpSqlite_" + name]).toInteger()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
def supportsNamespace() {
|
|
42
|
+
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
|
|
43
|
+
def major = parsed[0].toInteger()
|
|
44
|
+
def minor = parsed[1].toInteger()
|
|
45
|
+
|
|
46
|
+
// Namespace support was added in 7.3.0
|
|
47
|
+
return (major == 7 && minor >= 3) || major >= 8
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
android {
|
|
51
|
+
if (supportsNamespace()) {
|
|
52
|
+
namespace "com.powersync.opsqlite"
|
|
53
|
+
|
|
54
|
+
sourceSets {
|
|
55
|
+
main {
|
|
56
|
+
manifest.srcFile "src/main/AndroidManifestNew.xml"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
62
|
+
|
|
63
|
+
defaultConfig {
|
|
64
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
65
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
66
|
+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
buildFeatures {
|
|
71
|
+
buildConfig true
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
buildTypes {
|
|
75
|
+
release {
|
|
76
|
+
minifyEnabled false
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
lintOptions {
|
|
81
|
+
disable "GradleCompatible"
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
compileOptions {
|
|
85
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
86
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
sourceSets {
|
|
90
|
+
main {
|
|
91
|
+
if (isNewArchitectureEnabled()) {
|
|
92
|
+
java.srcDirs += [
|
|
93
|
+
"src/newarch"
|
|
94
|
+
]
|
|
95
|
+
} else {
|
|
96
|
+
java.srcDirs += ["src/oldarch"]
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
repositories {
|
|
103
|
+
mavenCentral()
|
|
104
|
+
google()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
108
|
+
|
|
109
|
+
dependencies {
|
|
110
|
+
implementation 'co.powersync:powersync-sqlite-core:0.2.1'
|
|
111
|
+
// For < 0.71, this will be from the local maven repo
|
|
112
|
+
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
|
|
113
|
+
//noinspection GradleDynamicVersion
|
|
114
|
+
implementation "com.facebook.react:react-native:+"
|
|
115
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (isNewArchitectureEnabled()) {
|
|
119
|
+
react {
|
|
120
|
+
jsRootDir = file("../src/")
|
|
121
|
+
libraryName = "PowerSyncOpSqlite"
|
|
122
|
+
codegenJavaPackageName = "com.powersync.opsqlite"
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
package com.powersync.opsqlite
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
4
|
+
import com.facebook.react.bridge.ReactMethod
|
|
5
|
+
import com.facebook.react.bridge.Promise
|
|
6
|
+
|
|
7
|
+
class PowerSyncOpSqliteModule internal constructor(context: ReactApplicationContext) :
|
|
8
|
+
PowerSyncOpSqliteSpec(context) {
|
|
9
|
+
|
|
10
|
+
@ReactMethod
|
|
11
|
+
override fun getBundlePath(): String {
|
|
12
|
+
//This method should only be used for iOS platforms
|
|
13
|
+
//Ensure you wrap its usage with a (Platform.OS === 'ios') check
|
|
14
|
+
//Returns an empty string for android
|
|
15
|
+
return ""
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
override fun getName(): String {
|
|
19
|
+
return NAME
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
companion object {
|
|
23
|
+
const val NAME = "PowerSyncOpSqlite"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package com.powersync.opsqlite
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.TurboReactPackage
|
|
4
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
5
|
+
import com.facebook.react.bridge.NativeModule
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
8
|
+
import java.util.HashMap
|
|
9
|
+
|
|
10
|
+
class PowerSyncOpSqlitePackage : TurboReactPackage() {
|
|
11
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
12
|
+
return if (name == PowerSyncOpSqliteModule.NAME) {
|
|
13
|
+
PowerSyncOpSqliteModule(reactContext)
|
|
14
|
+
} else {
|
|
15
|
+
null
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
20
|
+
return ReactModuleInfoProvider {
|
|
21
|
+
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
|
|
22
|
+
val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
|
|
23
|
+
moduleInfos[PowerSyncOpSqliteModule.NAME] = ReactModuleInfo(
|
|
24
|
+
PowerSyncOpSqliteModule.NAME,
|
|
25
|
+
PowerSyncOpSqliteModule.NAME,
|
|
26
|
+
false, // canOverrideExistingModule
|
|
27
|
+
false, // needsEagerInit
|
|
28
|
+
true, // hasConstants
|
|
29
|
+
false, // isCxxModule
|
|
30
|
+
isTurboModule // isTurboModule
|
|
31
|
+
)
|
|
32
|
+
moduleInfos
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
package com.powersync.opsqlite
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
4
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
|
5
|
+
import com.facebook.react.bridge.Promise
|
|
6
|
+
|
|
7
|
+
abstract class PowerSyncOpSqliteSpec internal constructor(context: ReactApplicationContext) :
|
|
8
|
+
ReactContextBaseJavaModule(context) {
|
|
9
|
+
|
|
10
|
+
abstract fun getBundlePath(): String
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@powersync/op-sqlite",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Powersync",
|
|
5
|
+
"source": "./src/index.ts",
|
|
6
|
+
"main": "./lib/commonjs/index.js",
|
|
7
|
+
"module": "./lib/module/index.js",
|
|
8
|
+
"types": "./lib/typescript/commonjs/src/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./lib/typescript/module/src/index.d.ts",
|
|
13
|
+
"default": "./lib/module/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./lib/typescript/commonjs/src/index.d.ts",
|
|
17
|
+
"default": "./lib/commonjs/index.js"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src",
|
|
23
|
+
"lib",
|
|
24
|
+
"android",
|
|
25
|
+
"ios",
|
|
26
|
+
"cpp",
|
|
27
|
+
"*.podspec",
|
|
28
|
+
"!ios/build",
|
|
29
|
+
"!android/build",
|
|
30
|
+
"!android/gradle",
|
|
31
|
+
"!android/gradlew",
|
|
32
|
+
"!android/gradlew.bat",
|
|
33
|
+
"!android/local.properties",
|
|
34
|
+
"!**/__tests__",
|
|
35
|
+
"!**/__fixtures__",
|
|
36
|
+
"!**/__mocks__",
|
|
37
|
+
"!**/.*"
|
|
38
|
+
],
|
|
39
|
+
"keywords": [
|
|
40
|
+
"react-native",
|
|
41
|
+
"ios",
|
|
42
|
+
"android"
|
|
43
|
+
],
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/powersync-ja/powersync-js.git"
|
|
47
|
+
},
|
|
48
|
+
"author": "PowerSync",
|
|
49
|
+
"license": "Apache-2.0",
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/powersync-ja/powersync-js/issues"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://docs.powersync.com/",
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"registry": "https://registry.npmjs.org/",
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"@op-engineering/op-sqlite": "^9.1.3",
|
|
60
|
+
"@powersync/common": "^1.18.0",
|
|
61
|
+
"react": "*",
|
|
62
|
+
"react-native": "*"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"react": "18.3.1",
|
|
66
|
+
"react-native": "0.75.3",
|
|
67
|
+
"@powersync/common": "1.19.0"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@op-engineering/op-sqlite": "^9.1.3",
|
|
71
|
+
"@react-native/eslint-config": "^0.73.1",
|
|
72
|
+
"@types/async-lock": "^1.4.0",
|
|
73
|
+
"@types/react": "^18.2.44",
|
|
74
|
+
"async-lock": "^1.4.0",
|
|
75
|
+
"del-cli": "^5.1.0",
|
|
76
|
+
"eslint": "^8.51.0",
|
|
77
|
+
"eslint-config-prettier": "^9.0.0",
|
|
78
|
+
"eslint-plugin-prettier": "^5.0.1",
|
|
79
|
+
"prettier": "^3.0.3",
|
|
80
|
+
"react-native-builder-bob": "^0.30.2",
|
|
81
|
+
"turbo": "^1.10.7",
|
|
82
|
+
"typescript": "^5.2.2"
|
|
83
|
+
},
|
|
84
|
+
"eslintIgnore": [
|
|
85
|
+
"node_modules/",
|
|
86
|
+
"lib/"
|
|
87
|
+
],
|
|
88
|
+
"react-native-builder-bob": {
|
|
89
|
+
"source": "src",
|
|
90
|
+
"output": "lib",
|
|
91
|
+
"targets": [
|
|
92
|
+
"codegen",
|
|
93
|
+
[
|
|
94
|
+
"commonjs",
|
|
95
|
+
{
|
|
96
|
+
"esm": true
|
|
97
|
+
}
|
|
98
|
+
],
|
|
99
|
+
[
|
|
100
|
+
"module",
|
|
101
|
+
{
|
|
102
|
+
"esm": true
|
|
103
|
+
}
|
|
104
|
+
],
|
|
105
|
+
[
|
|
106
|
+
"typescript",
|
|
107
|
+
{
|
|
108
|
+
"project": "tsconfig.build.json",
|
|
109
|
+
"esm": true,
|
|
110
|
+
"tsc": "../../node_modules/.bin/tsc"
|
|
111
|
+
}
|
|
112
|
+
]
|
|
113
|
+
]
|
|
114
|
+
},
|
|
115
|
+
"codegenConfig": {
|
|
116
|
+
"name": "RNPowerSyncOpSqliteSpec",
|
|
117
|
+
"type": "modules",
|
|
118
|
+
"jsSrcsDir": "src",
|
|
119
|
+
"outputDir": {
|
|
120
|
+
"ios": "codegen",
|
|
121
|
+
"android": "codegen"
|
|
122
|
+
},
|
|
123
|
+
"android": {
|
|
124
|
+
"javaPackageName": "com.powersync.opsqlite"
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
"scripts": {
|
|
128
|
+
"build": "bob build",
|
|
129
|
+
"typecheck": "tsc",
|
|
130
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
131
|
+
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib"
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "powersync-op-sqlite"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/mugikhan/powersync-op-sqlite.git", :tag => "#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = "ios/**/*.{h,m,mm,cpp}"
|
|
17
|
+
|
|
18
|
+
s.dependency "React-callinvoker"
|
|
19
|
+
s.dependency "React"
|
|
20
|
+
s.dependency "powersync-sqlite-core", "~> 0.2.1"
|
|
21
|
+
if defined?(install_modules_dependencies())
|
|
22
|
+
install_modules_dependencies(s)
|
|
23
|
+
else
|
|
24
|
+
s.dependency "React-Core"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { DB, SQLBatchTuple } from '@op-engineering/op-sqlite';
|
|
2
|
+
import { BaseObserver, DBAdapterListener, QueryResult, RowUpdateType } from '@powersync/common';
|
|
3
|
+
|
|
4
|
+
export type OPSQLiteConnectionOptions = {
|
|
5
|
+
baseDB: DB;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export class OPSQLiteConnection extends BaseObserver<DBAdapterListener> {
|
|
9
|
+
protected DB: DB;
|
|
10
|
+
constructor(protected options: OPSQLiteConnectionOptions) {
|
|
11
|
+
super();
|
|
12
|
+
this.DB = options.baseDB;
|
|
13
|
+
|
|
14
|
+
// link table update commands
|
|
15
|
+
this.DB.updateHook((update) => {
|
|
16
|
+
this.iterateListeners((cb) => {
|
|
17
|
+
let opType: RowUpdateType;
|
|
18
|
+
switch (update.operation) {
|
|
19
|
+
case 'INSERT':
|
|
20
|
+
opType = RowUpdateType.SQLITE_INSERT;
|
|
21
|
+
break;
|
|
22
|
+
case 'DELETE':
|
|
23
|
+
opType = RowUpdateType.SQLITE_DELETE;
|
|
24
|
+
break;
|
|
25
|
+
case 'UPDATE':
|
|
26
|
+
opType = RowUpdateType.SQLITE_UPDATE;
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
cb.tablesUpdated?.({
|
|
30
|
+
table: update.table,
|
|
31
|
+
opType,
|
|
32
|
+
rowId: update.rowId
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
close() {
|
|
39
|
+
return this.DB.close();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async execute(query: string, params?: any[]): Promise<QueryResult> {
|
|
43
|
+
const res = await this.DB.execute(query, params);
|
|
44
|
+
return {
|
|
45
|
+
insertId: res.insertId,
|
|
46
|
+
rowsAffected: res.rowsAffected,
|
|
47
|
+
rows: {
|
|
48
|
+
_array: res.rows ?? [],
|
|
49
|
+
length: res.rows?.length ?? 0,
|
|
50
|
+
item: (index: number) => res.rows?.[index]
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async executeBatch(query: string, params: any[][] = []): Promise<QueryResult> {
|
|
56
|
+
const tuple: SQLBatchTuple[] = [[query, params[0]]];
|
|
57
|
+
params.slice(1).forEach((p) => tuple.push([query, p]));
|
|
58
|
+
|
|
59
|
+
const result = await this.DB.executeBatch(tuple);
|
|
60
|
+
return {
|
|
61
|
+
rowsAffected: result.rowsAffected ?? 0
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async getAll<T>(sql: string, parameters?: any[]): Promise<T[]> {
|
|
66
|
+
const result = await this.DB.execute(sql, parameters);
|
|
67
|
+
return result.rows ?? [];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async getOptional<T>(sql: string, parameters?: any[]): Promise<T | null> {
|
|
71
|
+
const result = await this.DB.execute(sql, parameters);
|
|
72
|
+
return result.rows?.[0] ?? null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async get<T>(sql: string, parameters?: any[]): Promise<T> {
|
|
76
|
+
const result = await this.getOptional(sql, parameters);
|
|
77
|
+
if (!result) {
|
|
78
|
+
throw new Error('Result set is empty');
|
|
79
|
+
}
|
|
80
|
+
return result as T;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseObserver,
|
|
3
|
+
DBAdapter,
|
|
4
|
+
DBAdapterListener,
|
|
5
|
+
DBLockOptions,
|
|
6
|
+
QueryResult,
|
|
7
|
+
SQLOpenOptions,
|
|
8
|
+
Transaction
|
|
9
|
+
} from '@powersync/common';
|
|
10
|
+
import { ANDROID_DATABASE_PATH, IOS_LIBRARY_PATH, open, type DB } from '@op-engineering/op-sqlite';
|
|
11
|
+
import Lock from 'async-lock';
|
|
12
|
+
import { OPSQLiteConnection } from './OPSQLiteConnection';
|
|
13
|
+
import { NativeModules, Platform } from 'react-native';
|
|
14
|
+
import { DEFAULT_SQLITE_OPTIONS, SqliteOptions } from './SqliteOptions';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Adapter for React Native Quick SQLite
|
|
18
|
+
*/
|
|
19
|
+
export type OPSQLiteAdapterOptions = {
|
|
20
|
+
name: string;
|
|
21
|
+
dbLocation?: string;
|
|
22
|
+
sqliteOptions?: SqliteOptions;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
enum LockType {
|
|
26
|
+
READ = 'read',
|
|
27
|
+
WRITE = 'write'
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const READ_CONNECTIONS = 5;
|
|
31
|
+
|
|
32
|
+
export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implements DBAdapter {
|
|
33
|
+
name: string;
|
|
34
|
+
protected locks: Lock;
|
|
35
|
+
|
|
36
|
+
protected initialized: Promise<void>;
|
|
37
|
+
|
|
38
|
+
protected readConnections: OPSQLiteConnection[] | null;
|
|
39
|
+
|
|
40
|
+
protected writeConnection: OPSQLiteConnection | null;
|
|
41
|
+
|
|
42
|
+
constructor(protected options: OPSQLiteAdapterOptions) {
|
|
43
|
+
super();
|
|
44
|
+
this.name = this.options.name;
|
|
45
|
+
|
|
46
|
+
this.locks = new Lock();
|
|
47
|
+
this.readConnections = null;
|
|
48
|
+
this.writeConnection = null;
|
|
49
|
+
this.initialized = this.init();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected async init() {
|
|
53
|
+
const { lockTimeoutMs, journalMode, journalSizeLimit, synchronous } = this.options.sqliteOptions;
|
|
54
|
+
// const { dbFilename, dbLocation } = this.options;
|
|
55
|
+
const dbFilename = this.options.name;
|
|
56
|
+
//This is needed because an undefined dbLocation will cause the open function to fail
|
|
57
|
+
const location = this.getDbLocation(this.options.dbLocation);
|
|
58
|
+
const DB: DB = open({
|
|
59
|
+
name: dbFilename,
|
|
60
|
+
location: location
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const statements: string[] = [
|
|
64
|
+
`PRAGMA busy_timeout = ${lockTimeoutMs}`,
|
|
65
|
+
`PRAGMA journal_mode = ${journalMode}`,
|
|
66
|
+
`PRAGMA journal_size_limit = ${journalSizeLimit}`,
|
|
67
|
+
`PRAGMA synchronous = ${synchronous}`
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
for (const statement of statements) {
|
|
71
|
+
for (let tries = 0; tries < 30; tries++) {
|
|
72
|
+
try {
|
|
73
|
+
await DB.execute(statement);
|
|
74
|
+
break;
|
|
75
|
+
} catch (e: any) {
|
|
76
|
+
if (e instanceof Error && e.message.includes('database is locked') && tries < 29) {
|
|
77
|
+
continue;
|
|
78
|
+
} else {
|
|
79
|
+
throw e;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.loadExtension(DB);
|
|
86
|
+
|
|
87
|
+
await DB.execute('SELECT powersync_init()');
|
|
88
|
+
|
|
89
|
+
this.readConnections = [];
|
|
90
|
+
for (let i = 0; i < READ_CONNECTIONS; i++) {
|
|
91
|
+
// Workaround to create read-only connections
|
|
92
|
+
let dbName = './'.repeat(i + 1) + dbFilename;
|
|
93
|
+
const conn = await this.openConnection(location, dbName);
|
|
94
|
+
await conn.execute('PRAGMA query_only = true');
|
|
95
|
+
this.readConnections.push(conn);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this.writeConnection = new OPSQLiteConnection({
|
|
99
|
+
baseDB: DB
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// Changes should only occur in the write connection
|
|
103
|
+
this.writeConnection!.registerListener({
|
|
104
|
+
tablesUpdated: (notification) => this.iterateListeners((cb) => cb.tablesUpdated?.(notification))
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
protected async openConnection(dbLocation: string, filenameOverride?: string): Promise<OPSQLiteConnection> {
|
|
109
|
+
const DB: DB = open({
|
|
110
|
+
name: filenameOverride ?? this.options.name,
|
|
111
|
+
location: dbLocation
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
//Load extension for all connections
|
|
115
|
+
this.loadExtension(DB);
|
|
116
|
+
|
|
117
|
+
await DB.execute('SELECT powersync_init()');
|
|
118
|
+
|
|
119
|
+
return new OPSQLiteConnection({
|
|
120
|
+
baseDB: DB
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private getDbLocation(dbLocation?: string): string {
|
|
125
|
+
if (Platform.OS === 'ios') {
|
|
126
|
+
return dbLocation ?? IOS_LIBRARY_PATH;
|
|
127
|
+
} else {
|
|
128
|
+
return dbLocation ?? ANDROID_DATABASE_PATH;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private loadExtension(DB: DB) {
|
|
133
|
+
if (Platform.OS === 'ios') {
|
|
134
|
+
const bundlePath: string = NativeModules.PowerSyncOpSqlite.getBundlePath();
|
|
135
|
+
const libPath = `${bundlePath}/Frameworks/powersync-sqlite-core.framework/powersync-sqlite-core`;
|
|
136
|
+
DB.loadExtension(libPath, 'sqlite3_powersync_init');
|
|
137
|
+
} else {
|
|
138
|
+
DB.loadExtension('libpowersync', 'sqlite3_powersync_init');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
close() {
|
|
143
|
+
this.initialized.then(() => {
|
|
144
|
+
this.writeConnection!.close();
|
|
145
|
+
this.readConnections!.forEach((c) => c.close());
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async readLock<T>(fn: (tx: OPSQLiteConnection) => Promise<T>, options?: DBLockOptions): Promise<T> {
|
|
150
|
+
await this.initialized;
|
|
151
|
+
// TODO: Use async queues to handle multiple read connections
|
|
152
|
+
const sortedConnections = this.readConnections!.map((connection, index) => ({
|
|
153
|
+
lockKey: `${LockType.READ}-${index}`,
|
|
154
|
+
connection
|
|
155
|
+
})).sort((a, b) => {
|
|
156
|
+
const aBusy = this.locks.isBusy(a.lockKey);
|
|
157
|
+
const bBusy = this.locks.isBusy(b.lockKey);
|
|
158
|
+
// Sort by ones which are not busy
|
|
159
|
+
return aBusy > bBusy ? 1 : 0;
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
return new Promise(async (resolve, reject) => {
|
|
163
|
+
try {
|
|
164
|
+
await this.locks.acquire(
|
|
165
|
+
sortedConnections[0].lockKey,
|
|
166
|
+
async () => {
|
|
167
|
+
resolve(await fn(sortedConnections[0].connection));
|
|
168
|
+
},
|
|
169
|
+
{ timeout: options?.timeoutMs }
|
|
170
|
+
);
|
|
171
|
+
} catch (ex) {
|
|
172
|
+
reject(ex);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async writeLock<T>(fn: (tx: OPSQLiteConnection) => Promise<T>, options?: DBLockOptions): Promise<T> {
|
|
178
|
+
await this.initialized;
|
|
179
|
+
|
|
180
|
+
return new Promise(async (resolve, reject) => {
|
|
181
|
+
try {
|
|
182
|
+
await this.locks.acquire(
|
|
183
|
+
LockType.WRITE,
|
|
184
|
+
async () => {
|
|
185
|
+
resolve(await fn(this.writeConnection!));
|
|
186
|
+
},
|
|
187
|
+
{ timeout: options?.timeoutMs }
|
|
188
|
+
);
|
|
189
|
+
} catch (ex) {
|
|
190
|
+
reject(ex);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
readTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T> {
|
|
196
|
+
return this.readLock((ctx) => this.internalTransaction(ctx, fn));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
writeTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T> {
|
|
200
|
+
return this.writeLock((ctx) => this.internalTransaction(ctx, fn));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
getAll<T>(sql: string, parameters?: any[]): Promise<T[]> {
|
|
204
|
+
return this.readLock((ctx) => ctx.getAll(sql, parameters));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null> {
|
|
208
|
+
return this.readLock((ctx) => ctx.getOptional(sql, parameters));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
get<T>(sql: string, parameters?: any[]): Promise<T> {
|
|
212
|
+
return this.readLock((ctx) => ctx.get(sql, parameters));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
execute(query: string, params?: any[]) {
|
|
216
|
+
return this.writeLock((ctx) => ctx.execute(query, params));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async executeBatch(query: string, params: any[][] = []): Promise<QueryResult> {
|
|
220
|
+
return this.writeLock((ctx) => ctx.executeBatch(query, params));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
protected async internalTransaction<T>(
|
|
224
|
+
connection: OPSQLiteConnection,
|
|
225
|
+
fn: (tx: Transaction) => Promise<T>
|
|
226
|
+
): Promise<T> {
|
|
227
|
+
let finalized = false;
|
|
228
|
+
const commit = async (): Promise<QueryResult> => {
|
|
229
|
+
if (finalized) {
|
|
230
|
+
return { rowsAffected: 0 };
|
|
231
|
+
}
|
|
232
|
+
finalized = true;
|
|
233
|
+
return connection.execute('COMMIT');
|
|
234
|
+
};
|
|
235
|
+
const rollback = async (): Promise<QueryResult> => {
|
|
236
|
+
if (finalized) {
|
|
237
|
+
return { rowsAffected: 0 };
|
|
238
|
+
}
|
|
239
|
+
finalized = true;
|
|
240
|
+
return connection.execute('ROLLBACK');
|
|
241
|
+
};
|
|
242
|
+
try {
|
|
243
|
+
await connection.execute('BEGIN');
|
|
244
|
+
const result = await fn({
|
|
245
|
+
execute: (query, params) => connection.execute(query, params),
|
|
246
|
+
get: (query, params) => connection.get(query, params),
|
|
247
|
+
getAll: (query, params) => connection.getAll(query, params),
|
|
248
|
+
getOptional: (query, params) => connection.getOptional(query, params),
|
|
249
|
+
commit,
|
|
250
|
+
rollback
|
|
251
|
+
});
|
|
252
|
+
await commit();
|
|
253
|
+
return result;
|
|
254
|
+
} catch (ex) {
|
|
255
|
+
await rollback();
|
|
256
|
+
throw ex;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { DBAdapter, SQLOpenFactory, SQLOpenOptions } from '@powersync/common';
|
|
2
|
+
import { OPSQLiteDBAdapter } from './OPSqliteAdapter';
|
|
3
|
+
import { DEFAULT_SQLITE_OPTIONS, SqliteOptions } from './SqliteOptions';
|
|
4
|
+
|
|
5
|
+
export interface OPSQLiteOpenFactoryOptions extends SQLOpenOptions {
|
|
6
|
+
sqliteOptions?: SqliteOptions;
|
|
7
|
+
}
|
|
8
|
+
export class OPSqliteOpenFactory implements SQLOpenFactory {
|
|
9
|
+
private sqliteOptions: Required<SqliteOptions>;
|
|
10
|
+
|
|
11
|
+
constructor(protected options: OPSQLiteOpenFactoryOptions) {
|
|
12
|
+
this.sqliteOptions = {
|
|
13
|
+
...DEFAULT_SQLITE_OPTIONS,
|
|
14
|
+
...this.options.sqliteOptions
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
openDB(): DBAdapter {
|
|
19
|
+
return new OPSQLiteDBAdapter({
|
|
20
|
+
name: this.options.dbFilename,
|
|
21
|
+
dbLocation: this.options.dbLocation,
|
|
22
|
+
sqliteOptions: this.sqliteOptions
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export interface SqliteOptions {
|
|
2
|
+
/**
|
|
3
|
+
* SQLite journal mode. Defaults to [SqliteJournalMode.wal].
|
|
4
|
+
*/
|
|
5
|
+
journalMode?: SqliteJournalMode;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* SQLite synchronous flag. Defaults to [SqliteSynchronous.normal], which
|
|
9
|
+
* is safe for WAL mode.
|
|
10
|
+
*/
|
|
11
|
+
synchronous?: SqliteSynchronous;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Journal/WAL size limit. Defaults to 6MB.
|
|
15
|
+
* The WAL may grow larger than this limit during writes, but SQLite will
|
|
16
|
+
* attempt to truncate the file afterwards.
|
|
17
|
+
*/
|
|
18
|
+
journalSizeLimit?: number;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Timeout in milliseconds waiting for locks to be released by other connections.
|
|
22
|
+
* Defaults to 30 seconds.
|
|
23
|
+
* Set to null or zero to fail immediately when the database is locked.
|
|
24
|
+
*/
|
|
25
|
+
lockTimeoutMs?: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// SQLite journal mode. Set on the primary connection.
|
|
29
|
+
// This library is written with WAL mode in mind - other modes may cause
|
|
30
|
+
// unexpected locking behavior.
|
|
31
|
+
enum SqliteJournalMode {
|
|
32
|
+
// Use a write-ahead log instead of a rollback journal.
|
|
33
|
+
// This provides good performance and concurrency.
|
|
34
|
+
wal = 'WAL',
|
|
35
|
+
delete = 'DELETE',
|
|
36
|
+
truncate = 'TRUNCATE',
|
|
37
|
+
persist = 'PERSIST',
|
|
38
|
+
memory = 'MEMORY',
|
|
39
|
+
off = 'OFF',
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// SQLite file commit mode.
|
|
43
|
+
enum SqliteSynchronous {
|
|
44
|
+
normal = 'NORMAL',
|
|
45
|
+
full = 'FULL',
|
|
46
|
+
off = 'OFF',
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const DEFAULT_SQLITE_OPTIONS: Required<SqliteOptions> = {
|
|
50
|
+
journalMode: SqliteJournalMode.wal,
|
|
51
|
+
synchronous: SqliteSynchronous.normal,
|
|
52
|
+
journalSizeLimit: 6 * 1024 * 1024,
|
|
53
|
+
lockTimeoutMs: 30000,
|
|
54
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { NativeModules, Platform } from 'react-native';
|
|
2
|
+
|
|
3
|
+
const LINKING_ERROR =
|
|
4
|
+
`The package '@powersync/op-sqlite' doesn't seem to be linked. Make sure: \n\n` +
|
|
5
|
+
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
|
|
6
|
+
'- You rebuilt the app after installing the package\n' +
|
|
7
|
+
'- You are not using Expo Go\n';
|
|
8
|
+
|
|
9
|
+
const isTurboModuleEnabled = global.__turboModuleProxy != null;
|
|
10
|
+
|
|
11
|
+
const PowerSyncOpSqliteModule = isTurboModuleEnabled
|
|
12
|
+
? require('./NativePowerSyncOpSqlite').default
|
|
13
|
+
: NativeModules.PowerSyncOpSqlite;
|
|
14
|
+
|
|
15
|
+
const PowerSyncOpSqlite = PowerSyncOpSqliteModule
|
|
16
|
+
? PowerSyncOpSqliteModule
|
|
17
|
+
: new Proxy(
|
|
18
|
+
{},
|
|
19
|
+
{
|
|
20
|
+
get() {
|
|
21
|
+
throw new Error(LINKING_ERROR);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
export function getBundlePath(): string {
|
|
27
|
+
return PowerSyncOpSqlite.getBundlePath();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { OPSqliteOpenFactory, OPSQLiteOpenFactoryOptions } from './db/OPSqliteDBOpenFactory';
|