@powersync/op-sqlite 0.1.4 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/op-sqlite",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
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": "^10.1.0",
62
- "@powersync/common": "^1.22.1",
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": "1.22.1"
68
+ "@powersync/common": "1.22.2"
69
69
  },
70
70
  "devDependencies": {
71
- "@op-engineering/op-sqlite": "^10.1.0",
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.6"
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);
35
37
  });
36
38
  }
37
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
58
+ });
59
+ }
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
  }
@@ -1,12 +1,4 @@
1
- import {
2
- BaseObserver,
3
- DBAdapter,
4
- DBAdapterListener,
5
- DBLockOptions,
6
- QueryResult,
7
- SQLOpenOptions,
8
- Transaction
9
- } from '@powersync/common';
1
+ import { BaseObserver, DBAdapter, DBAdapterListener, DBLockOptions, QueryResult, Transaction } from '@powersync/common';
10
2
  import { ANDROID_DATABASE_PATH, IOS_LIBRARY_PATH, open, type DB } from '@op-engineering/op-sqlite';
11
3
  import Lock from 'async-lock';
12
4
  import { OPSQLiteConnection } from './OPSQLiteConnection';
@@ -86,9 +78,7 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
86
78
 
87
79
  this.readConnections = [];
88
80
  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);
81
+ const conn = await this.openConnection(dbFilename);
92
82
  await conn.execute('PRAGMA query_only = true');
93
83
  this.readConnections.push({ busy: false, connection: conn });
94
84
  }
@@ -194,13 +184,18 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
194
184
 
195
185
  return new Promise(async (resolve, reject) => {
196
186
  try {
197
- await this.locks.acquire(
198
- LockType.WRITE,
199
- async () => {
200
- resolve(await fn(this.writeConnection!));
201
- },
202
- { timeout: options?.timeoutMs }
203
- );
187
+ await this.locks
188
+ .acquire(
189
+ LockType.WRITE,
190
+ async () => {
191
+ resolve(await fn(this.writeConnection!));
192
+ },
193
+ { timeout: options?.timeoutMs }
194
+ )
195
+ .then(() => {
196
+ // flush updates once a write lock has been released
197
+ this.writeConnection!.flushUpdates();
198
+ });
204
199
  } catch (ex) {
205
200
  reject(ex);
206
201
  }