@powersync/op-sqlite 0.0.0-dev-20241107150304 → 0.0.0-dev-20250121082529

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.
Files changed (56) hide show
  1. package/README.md +36 -0
  2. package/android/build.gradle +1 -1
  3. package/android/src/main/java/com/powersync/opsqlite/PowerSyncOpSqlitePackage.kt +4 -8
  4. package/ios/PowerSyncOpSqlite.mm +0 -5
  5. package/lib/commonjs/db/OPSQLiteConnection.js +47 -21
  6. package/lib/commonjs/db/OPSQLiteConnection.js.map +1 -1
  7. package/lib/commonjs/db/OPSqliteAdapter.js +23 -8
  8. package/lib/commonjs/db/OPSqliteAdapter.js.map +1 -1
  9. package/lib/commonjs/db/SqliteOptions.js +2 -1
  10. package/lib/commonjs/db/SqliteOptions.js.map +1 -1
  11. package/lib/commonjs/index.js +0 -16
  12. package/lib/commonjs/index.js.map +1 -1
  13. package/lib/module/db/OPSQLiteConnection.js +47 -21
  14. package/lib/module/db/OPSQLiteConnection.js.map +1 -1
  15. package/lib/module/db/OPSqliteAdapter.js +25 -10
  16. package/lib/module/db/OPSqliteAdapter.js.map +1 -1
  17. package/lib/module/db/SqliteOptions.js +2 -1
  18. package/lib/module/db/SqliteOptions.js.map +1 -1
  19. package/lib/module/index.js +0 -15
  20. package/lib/module/index.js.map +1 -1
  21. package/lib/typescript/commonjs/src/db/OPSQLiteConnection.d.ts +11 -1
  22. package/lib/typescript/commonjs/src/db/OPSQLiteConnection.d.ts.map +1 -1
  23. package/lib/typescript/commonjs/src/db/OPSqliteAdapter.d.ts +3 -1
  24. package/lib/typescript/commonjs/src/db/OPSqliteAdapter.d.ts.map +1 -1
  25. package/lib/typescript/commonjs/src/db/SqliteOptions.d.ts +8 -0
  26. package/lib/typescript/commonjs/src/db/SqliteOptions.d.ts.map +1 -1
  27. package/lib/typescript/commonjs/src/index.d.ts +0 -1
  28. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  29. package/lib/typescript/commonjs/tsconfig.build.tsbuildinfo +1 -1
  30. package/lib/typescript/module/src/db/OPSQLiteConnection.d.ts +11 -1
  31. package/lib/typescript/module/src/db/OPSQLiteConnection.d.ts.map +1 -1
  32. package/lib/typescript/module/src/db/OPSqliteAdapter.d.ts +3 -1
  33. package/lib/typescript/module/src/db/OPSqliteAdapter.d.ts.map +1 -1
  34. package/lib/typescript/module/src/db/SqliteOptions.d.ts +8 -0
  35. package/lib/typescript/module/src/db/SqliteOptions.d.ts.map +1 -1
  36. package/lib/typescript/module/src/index.d.ts +0 -1
  37. package/lib/typescript/module/src/index.d.ts.map +1 -1
  38. package/lib/typescript/module/tsconfig.build.tsbuildinfo +1 -1
  39. package/package.json +6 -6
  40. package/powersync-op-sqlite.podspec +1 -1
  41. package/src/db/OPSQLiteConnection.ts +73 -24
  42. package/src/db/OPSqliteAdapter.ts +43 -18
  43. package/src/db/SqliteOptions.ts +11 -1
  44. package/src/index.ts +4 -30
  45. package/android/src/main/java/com/powersync/opsqlite/PowerSyncOpSqliteModule.kt +0 -25
  46. package/android/src/newarch/PowerSyncOpSqliteSpec.kt +0 -7
  47. package/android/src/oldarch/PowerSyncOpSqliteSpec.kt +0 -11
  48. package/lib/commonjs/NativePowerSyncOpSqlite.js +0 -9
  49. package/lib/commonjs/NativePowerSyncOpSqlite.js.map +0 -1
  50. package/lib/module/NativePowerSyncOpSqlite.js +0 -5
  51. package/lib/module/NativePowerSyncOpSqlite.js.map +0 -1
  52. package/lib/typescript/commonjs/src/NativePowerSyncOpSqlite.d.ts +0 -7
  53. package/lib/typescript/commonjs/src/NativePowerSyncOpSqlite.d.ts.map +0 -1
  54. package/lib/typescript/module/src/NativePowerSyncOpSqlite.d.ts +0 -7
  55. package/lib/typescript/module/src/NativePowerSyncOpSqlite.d.ts.map +0 -1
  56. package/src/NativePowerSyncOpSqlite.ts +0 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/op-sqlite",
3
- "version": "0.0.0-dev-20241107150304",
3
+ "version": "0.0.0-dev-20250121082529",
4
4
  "description": "PowerSync - sync Postgres or MongoDB with SQLite in your React Native app for offline-first and real-time data",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./lib/commonjs/index.js",
@@ -58,17 +58,17 @@
58
58
  "access": "public"
59
59
  },
60
60
  "peerDependencies": {
61
- "@op-engineering/op-sqlite": "^9.2.1",
62
- "@powersync/common": "0.0.0-dev-20241107150304",
61
+ "@op-engineering/op-sqlite": "^11.2.13",
62
+ "@powersync/common": "^1.22.2",
63
63
  "react": "*",
64
64
  "react-native": "*"
65
65
  },
66
66
  "dependencies": {
67
67
  "async-lock": "^1.4.0",
68
- "@powersync/common": "0.0.0-dev-20241107150304"
68
+ "@powersync/common": "1.22.2"
69
69
  },
70
70
  "devDependencies": {
71
- "@op-engineering/op-sqlite": "^9.2.1",
71
+ "@op-engineering/op-sqlite": "^11.2.13",
72
72
  "@react-native/eslint-config": "^0.73.1",
73
73
  "@types/async-lock": "^1.4.0",
74
74
  "@types/react": "^18.2.44",
@@ -131,6 +131,6 @@
131
131
  "build:prod": "bob build",
132
132
  "typecheck": "tsc",
133
133
  "lint": "eslint \"**/*.{js,ts,tsx}\"",
134
- "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib"
134
+ "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib node_modules"
135
135
  }
136
136
  }
@@ -17,7 +17,7 @@ Pod::Spec.new do |s|
17
17
 
18
18
  s.dependency "React-callinvoker"
19
19
  s.dependency "React"
20
- s.dependency "powersync-sqlite-core", "~> 0.3.0"
20
+ s.dependency "powersync-sqlite-core", "~> 0.3.8"
21
21
  if defined?(install_modules_dependencies())
22
22
  install_modules_dependencies(s)
23
23
  else
@@ -1,40 +1,85 @@
1
- import { DB, SQLBatchTuple } from '@op-engineering/op-sqlite';
2
- import { BaseObserver, DBAdapterListener, QueryResult, RowUpdateType } from '@powersync/common';
1
+ import { DB, SQLBatchTuple, UpdateHookOperation } from '@op-engineering/op-sqlite';
2
+ import {
3
+ BaseObserver,
4
+ BatchedUpdateNotification,
5
+ DBAdapterListener,
6
+ QueryResult,
7
+ RowUpdateType,
8
+ UpdateNotification
9
+ } from '@powersync/common';
3
10
 
4
11
  export type OPSQLiteConnectionOptions = {
5
12
  baseDB: DB;
6
13
  };
7
14
 
15
+ export type OPSQLiteUpdateNotification = {
16
+ table: string;
17
+ operation: UpdateHookOperation;
18
+ row?: any;
19
+ rowId: number;
20
+ };
21
+
8
22
  export class OPSQLiteConnection extends BaseObserver<DBAdapterListener> {
9
23
  protected DB: DB;
24
+ private updateBuffer: UpdateNotification[];
25
+
10
26
  constructor(protected options: OPSQLiteConnectionOptions) {
11
27
  super();
12
28
  this.DB = options.baseDB;
29
+ this.updateBuffer = [];
30
+
31
+ this.DB.rollbackHook(() => {
32
+ this.updateBuffer = [];
33
+ });
13
34
 
14
- // link table update commands
15
35
  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
- });
36
+ this.addTableUpdate(update);
37
+ });
38
+ }
39
+
40
+ addTableUpdate(update: OPSQLiteUpdateNotification) {
41
+ let opType: RowUpdateType;
42
+ switch (update.operation) {
43
+ case 'INSERT':
44
+ opType = RowUpdateType.SQLITE_INSERT;
45
+ break;
46
+ case 'DELETE':
47
+ opType = RowUpdateType.SQLITE_DELETE;
48
+ break;
49
+ case 'UPDATE':
50
+ opType = RowUpdateType.SQLITE_UPDATE;
51
+ break;
52
+ }
53
+
54
+ this.updateBuffer.push({
55
+ table: update.table,
56
+ opType,
57
+ rowId: update.rowId
35
58
  });
36
59
  }
37
60
 
61
+ flushUpdates() {
62
+ if (!this.updateBuffer.length) {
63
+ return;
64
+ }
65
+
66
+ const groupedUpdates = this.updateBuffer.reduce((grouping: Record<string, UpdateNotification[]>, update) => {
67
+ const { table } = update;
68
+ const updateGroup = grouping[table] || (grouping[table] = []);
69
+ updateGroup.push(update);
70
+ return grouping;
71
+ }, {});
72
+
73
+ const batchedUpdate: BatchedUpdateNotification = {
74
+ groupedUpdates,
75
+ rawUpdates: this.updateBuffer,
76
+ tables: Object.keys(groupedUpdates)
77
+ };
78
+
79
+ this.updateBuffer = [];
80
+ this.iterateListeners((l) => l.tablesUpdated?.(batchedUpdate));
81
+ }
82
+
38
83
  close() {
39
84
  return this.DB.close();
40
85
  }
@@ -64,12 +109,12 @@ export class OPSQLiteConnection extends BaseObserver<DBAdapterListener> {
64
109
 
65
110
  async getAll<T>(sql: string, parameters?: any[]): Promise<T[]> {
66
111
  const result = await this.DB.execute(sql, parameters);
67
- return result.rows ?? [];
112
+ return (result.rows ?? []) as T[];
68
113
  }
69
114
 
70
115
  async getOptional<T>(sql: string, parameters?: any[]): Promise<T | null> {
71
116
  const result = await this.DB.execute(sql, parameters);
72
- return result.rows?.[0] ?? null;
117
+ return (result.rows?.[0] as T) ?? null;
73
118
  }
74
119
 
75
120
  async get<T>(sql: string, parameters?: any[]): Promise<T> {
@@ -79,4 +124,8 @@ export class OPSQLiteConnection extends BaseObserver<DBAdapterListener> {
79
124
  }
80
125
  return result as T;
81
126
  }
127
+
128
+ async refreshSchema() {
129
+ await this.get("PRAGMA table_info('sqlite_master')");
130
+ }
82
131
  }
@@ -4,13 +4,18 @@ import {
4
4
  DBAdapterListener,
5
5
  DBLockOptions,
6
6
  QueryResult,
7
- SQLOpenOptions,
8
7
  Transaction
9
8
  } from '@powersync/common';
10
- import { ANDROID_DATABASE_PATH, IOS_LIBRARY_PATH, open, type DB } from '@op-engineering/op-sqlite';
9
+ import {
10
+ ANDROID_DATABASE_PATH,
11
+ getDylibPath,
12
+ IOS_LIBRARY_PATH,
13
+ open,
14
+ type DB
15
+ } from '@op-engineering/op-sqlite';
11
16
  import Lock from 'async-lock';
12
17
  import { OPSQLiteConnection } from './OPSQLiteConnection';
13
- import { NativeModules, Platform } from 'react-native';
18
+ import { Platform } from 'react-native';
14
19
  import { SqliteOptions } from './SqliteOptions';
15
20
 
16
21
  /**
@@ -86,9 +91,7 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
86
91
 
87
92
  this.readConnections = [];
88
93
  for (let i = 0; i < READ_CONNECTIONS; i++) {
89
- // Workaround to create read-only connections
90
- let dbName = './'.repeat(i + 1) + dbFilename;
91
- const conn = await this.openConnection(dbName);
94
+ const conn = await this.openConnection(dbFilename);
92
95
  await conn.execute('PRAGMA query_only = true');
93
96
  this.readConnections.push({ busy: false, connection: conn });
94
97
  }
@@ -98,8 +101,9 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
98
101
  const dbFilename = filenameOverride ?? this.options.name;
99
102
  const DB: DB = this.openDatabase(dbFilename, this.options.sqliteOptions.encryptionKey);
100
103
 
101
- //Load extension for all connections
102
- this.loadExtension(DB);
104
+ //Load extensions for all connections
105
+ this.loadAdditionalExtensions(DB);
106
+ this.loadPowerSyncExtension(DB);
103
107
 
104
108
  await DB.execute('SELECT powersync_init()');
105
109
 
@@ -134,10 +138,17 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
134
138
  }
135
139
  }
136
140
 
137
- private loadExtension(DB: DB) {
141
+ private loadAdditionalExtensions(DB: DB) {
142
+ if (this.options.sqliteOptions.extensions.length > 0) {
143
+ for (const extension of this.options.sqliteOptions.extensions) {
144
+ DB.loadExtension(extension.path, extension.entryPoint);
145
+ }
146
+ }
147
+ }
148
+
149
+ private async loadPowerSyncExtension(DB: DB) {
138
150
  if (Platform.OS === 'ios') {
139
- const bundlePath: string = NativeModules.PowerSyncOpSqlite.getBundlePath();
140
- const libPath = `${bundlePath}/Frameworks/powersync-sqlite-core.framework/powersync-sqlite-core`;
151
+ const libPath = getDylibPath('co.powersync.sqlitecore', 'powersync-sqlite-core');
141
152
  DB.loadExtension(libPath, 'sqlite3_powersync_init');
142
153
  } else {
143
154
  DB.loadExtension('libpowersync', 'sqlite3_powersync_init');
@@ -194,13 +205,18 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
194
205
 
195
206
  return new Promise(async (resolve, reject) => {
196
207
  try {
197
- await this.locks.acquire(
198
- LockType.WRITE,
199
- async () => {
200
- resolve(await fn(this.writeConnection!));
201
- },
202
- { timeout: options?.timeoutMs }
203
- );
208
+ await this.locks
209
+ .acquire(
210
+ LockType.WRITE,
211
+ async () => {
212
+ resolve(await fn(this.writeConnection!));
213
+ },
214
+ { timeout: options?.timeoutMs }
215
+ )
216
+ .then(() => {
217
+ // flush updates once a write lock has been released
218
+ this.writeConnection!.flushUpdates();
219
+ });
204
220
  } catch (ex) {
205
221
  reject(ex);
206
222
  }
@@ -271,4 +287,13 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
271
287
  throw ex;
272
288
  }
273
289
  }
290
+
291
+ async refreshSchema(): Promise<void> {
292
+ await this.initialized;
293
+ await this.writeConnection!.refreshSchema();
294
+
295
+ for (let readConnection of this.readConnections) {
296
+ await readConnection.connection.refreshSchema();
297
+ }
298
+ }
274
299
  }
@@ -29,6 +29,15 @@ export interface SqliteOptions {
29
29
  * If set, the database will be encrypted using SQLCipher.
30
30
  */
31
31
  encryptionKey?: string;
32
+
33
+ /**
34
+ * Load extensions using the path and entryPoint.
35
+ * More info can be found here https://op-engineering.github.io/op-sqlite/docs/api#loading-extensions.
36
+ */
37
+ extensions?: Array<{
38
+ path: string;
39
+ entryPoint?: string;
40
+ }>;
32
41
  }
33
42
 
34
43
  // SQLite journal mode. Set on the primary connection.
@@ -57,5 +66,6 @@ export const DEFAULT_SQLITE_OPTIONS: Required<SqliteOptions> = {
57
66
  synchronous: SqliteSynchronous.normal,
58
67
  journalSizeLimit: 6 * 1024 * 1024,
59
68
  lockTimeoutMs: 30000,
60
- encryptionKey: null
69
+ encryptionKey: null,
70
+ extensions: []
61
71
  };
package/src/index.ts CHANGED
@@ -1,30 +1,4 @@
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';
1
+ export {
2
+ OPSqliteOpenFactory,
3
+ OPSQLiteOpenFactoryOptions
4
+ } from './db/OPSqliteDBOpenFactory';
@@ -1,25 +0,0 @@
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
- }
@@ -1,7 +0,0 @@
1
- package com.powersync.opsqlite
2
-
3
- import com.facebook.react.bridge.ReactApplicationContext
4
-
5
- abstract class PowerSyncOpSqliteSpec internal constructor(context: ReactApplicationContext) :
6
- NativePowerSyncOpSqliteSpec(context) {
7
- }
@@ -1,11 +0,0 @@
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
- }
@@ -1,9 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _reactNative = require("react-native");
8
- var _default = exports.default = _reactNative.TurboModuleRegistry.getEnforcing('PowerSyncOpSqlite');
9
- //# sourceMappingURL=NativePowerSyncOpSqlite.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_reactNative","require","_default","exports","default","TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativePowerSyncOpSqlite.ts"],"mappings":";;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AAAmD,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAMpCC,gCAAmB,CAACC,YAAY,CAAO,mBAAmB,CAAC","ignoreList":[]}
@@ -1,5 +0,0 @@
1
- "use strict";
2
-
3
- import { TurboModuleRegistry } from 'react-native';
4
- export default TurboModuleRegistry.getEnforcing('PowerSyncOpSqlite');
5
- //# sourceMappingURL=NativePowerSyncOpSqlite.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativePowerSyncOpSqlite.ts"],"mappings":";;AACA,SAASA,mBAAmB,QAAQ,cAAc;AAMlD,eAAeA,mBAAmB,CAACC,YAAY,CAAO,mBAAmB,CAAC","ignoreList":[]}
@@ -1,7 +0,0 @@
1
- import type { TurboModule } from 'react-native';
2
- export interface Spec extends TurboModule {
3
- getBundlePath(): string;
4
- }
5
- declare const _default: Spec;
6
- export default _default;
7
- //# sourceMappingURL=NativePowerSyncOpSqlite.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"NativePowerSyncOpSqlite.d.ts","sourceRoot":"","sources":["../../../../src/NativePowerSyncOpSqlite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAGhD,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,aAAa,IAAI,MAAM,CAAC;CACzB;;AAED,wBAA2E"}
@@ -1,7 +0,0 @@
1
- import type { TurboModule } from 'react-native';
2
- export interface Spec extends TurboModule {
3
- getBundlePath(): string;
4
- }
5
- declare const _default: Spec;
6
- export default _default;
7
- //# sourceMappingURL=NativePowerSyncOpSqlite.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"NativePowerSyncOpSqlite.d.ts","sourceRoot":"","sources":["../../../../src/NativePowerSyncOpSqlite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAGhD,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,aAAa,IAAI,MAAM,CAAC;CACzB;;AAED,wBAA2E"}
@@ -1,8 +0,0 @@
1
- import type { TurboModule } from 'react-native';
2
- import { TurboModuleRegistry } from 'react-native';
3
-
4
- export interface Spec extends TurboModule {
5
- getBundlePath(): string;
6
- }
7
-
8
- export default TurboModuleRegistry.getEnforcing<Spec>('PowerSyncOpSqlite');