@powersync/capacitor 0.0.0-dev-20260305092446 → 0.0.0-dev-20260503073249
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/PowersyncCapacitor.podspec +1 -1
- package/README.md +1 -1
- package/android/build.gradle +1 -1
- package/dist/esm/PowerSyncDatabase.d.ts +2 -1
- package/dist/esm/PowerSyncDatabase.js +12 -2
- package/dist/esm/PowerSyncDatabase.js.map +1 -1
- package/dist/esm/adapter/CapacitorSQLiteAdapter.d.ts +24 -15
- package/dist/esm/adapter/CapacitorSQLiteAdapter.js +26 -85
- package/dist/esm/adapter/CapacitorSQLiteAdapter.js.map +1 -1
- package/dist/esm/sync/CapacitorBucketStorageAdapter.d.ts +4 -0
- package/dist/esm/sync/CapacitorBucketStorageAdapter.js +21 -0
- package/dist/esm/sync/CapacitorBucketStorageAdapter.js.map +1 -0
- package/dist/esm/sync/CapacitorRemote.d.ts +4 -0
- package/dist/esm/sync/CapacitorRemote.js +12 -0
- package/dist/esm/sync/CapacitorRemote.js.map +1 -0
- package/dist/esm/sync/CapacitorSyncImplementation.js +3 -8
- package/dist/esm/sync/CapacitorSyncImplementation.js.map +1 -1
- package/dist/plugin.cjs +68 -93
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +24 -15
- package/dist/plugin.js +70 -94
- package/dist/plugin.js.map +1 -1
- package/package.json +2 -5
- package/src/PowerSyncDatabase.ts +13 -3
- package/src/adapter/CapacitorSQLiteAdapter.ts +48 -120
- package/src/sync/CapacitorBucketStorageAdapter.ts +22 -0
- package/src/sync/CapacitorRemote.ts +12 -0
- package/src/sync/CapacitorSyncImplementation.ts +3 -7
|
@@ -23,7 +23,7 @@ Pod::Spec.new do |s|
|
|
|
23
23
|
s.ios.deployment_target = '14.0'
|
|
24
24
|
s.dependency 'Capacitor'
|
|
25
25
|
s.swift_version = '5.1'
|
|
26
|
-
s.dependency "powersync-sqlite-core", "~> 0.4.
|
|
26
|
+
s.dependency "powersync-sqlite-core", "~> 0.4.12"
|
|
27
27
|
s.xcconfig = {
|
|
28
28
|
'OTHER_CFLAGS' => '$(inherited) -DSQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION=1',
|
|
29
29
|
'HEADER_SEARCH_PATHS' => '$(inherited) "$(PODS_ROOT)/SQLCipher"'
|
package/README.md
CHANGED
|
@@ -87,7 +87,7 @@ const db = new PowerSyncDatabase({
|
|
|
87
87
|
|
|
88
88
|
## Examples
|
|
89
89
|
|
|
90
|
-
See the [`demos/example-capacitor/`](https://github.com/
|
|
90
|
+
See the [`demos/example-capacitor/`](https://github.com/powersync-ja/powersync-js/tree/main/demos/example-capacitor) directory for a working example.
|
|
91
91
|
|
|
92
92
|
## Found a bug or need help?
|
|
93
93
|
|
package/android/build.gradle
CHANGED
|
@@ -3,7 +3,7 @@ ext {
|
|
|
3
3
|
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.0'
|
|
4
4
|
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.2.1'
|
|
5
5
|
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.6.1'
|
|
6
|
-
powerSyncCoreVersion = project.hasProperty('powerSyncCoreVersion') ? rootProject.ext.powerSyncCoreVersion : '0.4.
|
|
6
|
+
powerSyncCoreVersion = project.hasProperty('powerSyncCoreVersion') ? rootProject.ext.powerSyncCoreVersion : '0.4.12'
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
buildscript {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DBAdapter, PowerSyncBackendConnector, RequiredAdditionalConnectionOptions, StreamingSyncImplementation, TriggerManagerConfig, PowerSyncDatabase as WebPowerSyncDatabase, WebPowerSyncDatabaseOptionsWithSettings } from '@powersync/web';
|
|
1
|
+
import { BucketStorageAdapter, DBAdapter, PowerSyncBackendConnector, RequiredAdditionalConnectionOptions, StreamingSyncImplementation, TriggerManagerConfig, PowerSyncDatabase as WebPowerSyncDatabase, WebPowerSyncDatabaseOptionsWithSettings } from '@powersync/web';
|
|
2
2
|
/**
|
|
3
3
|
* PowerSyncDatabase class for managing database connections and sync implementations.
|
|
4
4
|
* This extends the WebPowerSyncDatabase to provide platform-specific implementations
|
|
@@ -12,5 +12,6 @@ export declare class PowerSyncDatabase extends WebPowerSyncDatabase {
|
|
|
12
12
|
protected openDBAdapter(options: WebPowerSyncDatabaseOptionsWithSettings): DBAdapter;
|
|
13
13
|
protected generateTriggerManagerConfig(): TriggerManagerConfig;
|
|
14
14
|
protected runExclusive<T>(cb: () => Promise<T>): Promise<T>;
|
|
15
|
+
protected generateBucketStorageAdapter(): BucketStorageAdapter;
|
|
15
16
|
protected generateSyncStreamImplementation(connector: PowerSyncBackendConnector, options: RequiredAdditionalConnectionOptions): StreamingSyncImplementation;
|
|
16
17
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Capacitor } from '@capacitor/core';
|
|
2
|
-
import { MEMORY_TRIGGER_CLAIM_MANAGER, PowerSyncDatabase as WebPowerSyncDatabase
|
|
2
|
+
import { MEMORY_TRIGGER_CLAIM_MANAGER, PowerSyncDatabase as WebPowerSyncDatabase } from '@powersync/web';
|
|
3
3
|
import { CapacitorSQLiteAdapter } from './adapter/CapacitorSQLiteAdapter.js';
|
|
4
|
+
import { CapacitorBucketStorageAdapter } from './sync/CapacitorBucketStorageAdapter.js';
|
|
5
|
+
import { CapacitorRemote } from './sync/CapacitorRemote.js';
|
|
4
6
|
import { CapacitorStreamingSyncImplementation } from './sync/CapacitorSyncImplementation.js';
|
|
5
7
|
/**
|
|
6
8
|
* PowerSyncDatabase class for managing database connections and sync implementations.
|
|
@@ -55,6 +57,14 @@ export class PowerSyncDatabase extends WebPowerSyncDatabase {
|
|
|
55
57
|
return super.runExclusive(cb);
|
|
56
58
|
}
|
|
57
59
|
}
|
|
60
|
+
generateBucketStorageAdapter() {
|
|
61
|
+
if (this.isNativeCapacitorPlatform) {
|
|
62
|
+
return new CapacitorBucketStorageAdapter(this.database, this.logger);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
return super.generateBucketStorageAdapter();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
58
68
|
generateSyncStreamImplementation(connector, options) {
|
|
59
69
|
var _a;
|
|
60
70
|
if (this.isNativeCapacitorPlatform) {
|
|
@@ -64,7 +74,7 @@ export class PowerSyncDatabase extends WebPowerSyncDatabase {
|
|
|
64
74
|
if ((_a = this.options.flags) === null || _a === void 0 ? void 0 : _a.enableMultiTabs) {
|
|
65
75
|
this.logger.warn(`enableMultiTabs is not supported on Capacitor mobile platforms. Ignoring the flag.`);
|
|
66
76
|
}
|
|
67
|
-
const remote = new
|
|
77
|
+
const remote = new CapacitorRemote(connector, this.logger);
|
|
68
78
|
return new CapacitorStreamingSyncImplementation(Object.assign(Object.assign({}, this.options), { retryDelayMs: options.retryDelayMs, crudUploadThrottleMs: options.crudUploadThrottleMs, adapter: this.bucketStorageAdapter, remote, uploadCrud: async () => {
|
|
69
79
|
await this.waitForReady();
|
|
70
80
|
await connector.uploadData(this);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PowerSyncDatabase.js","sourceRoot":"","sources":["../../src/PowerSyncDatabase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,
|
|
1
|
+
{"version":3,"file":"PowerSyncDatabase.js","sourceRoot":"","sources":["../../src/PowerSyncDatabase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAGL,4BAA4B,EAK5B,iBAAiB,IAAI,oBAAoB,EAE1C,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAC7E,OAAO,EA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CAAC,QAAQ,CAAC,IAAI,EAC9B,MAAM,EAAE,IAAI,CAAC,MAAM,EACnB,aAAa,EAAE,OAAO,CAAC,aAAa,IACpC,CAAC;QACL,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,wDAAwD,CAAC,CAAC;YAC5E,OAAO,KAAK,CAAC,gCAAgC,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QACpE,CAAC;IACH,CAAC;CACF","sourcesContent":["import { Capacitor } from '@capacitor/core';\nimport {\n BucketStorageAdapter,\n DBAdapter,\n MEMORY_TRIGGER_CLAIM_MANAGER,\n PowerSyncBackendConnector,\n RequiredAdditionalConnectionOptions,\n StreamingSyncImplementation,\n TriggerManagerConfig,\n PowerSyncDatabase as WebPowerSyncDatabase,\n WebPowerSyncDatabaseOptionsWithSettings\n} from '@powersync/web';\nimport { CapacitorSQLiteAdapter } from './adapter/CapacitorSQLiteAdapter.js';\nimport { CapacitorBucketStorageAdapter } from './sync/CapacitorBucketStorageAdapter.js';\nimport { CapacitorRemote } from './sync/CapacitorRemote.js';\nimport { CapacitorStreamingSyncImplementation } from './sync/CapacitorSyncImplementation.js';\n\n/**\n * PowerSyncDatabase class for managing database connections and sync implementations.\n * This extends the WebPowerSyncDatabase to provide platform-specific implementations\n * for Capacitor environments (iOS and Android).\n *\n * @experimental\n * @alpha\n */\nexport class PowerSyncDatabase extends WebPowerSyncDatabase {\n protected get isNativeCapacitorPlatform(): boolean {\n const platform = Capacitor.getPlatform();\n return platform == 'ios' || platform == 'android';\n }\n\n protected openDBAdapter(options: WebPowerSyncDatabaseOptionsWithSettings): DBAdapter {\n const platform = Capacitor.getPlatform();\n if (platform == 'ios' || platform == 'android') {\n if (options.database.dbLocation) {\n options.logger?.warn(`\n dbLocation is ignored on iOS and Android platforms. \n The database directory can be configured in the Capacitor project.\n See https://github.com/capacitor-community/sqlite?tab=readme-ov-file#installation`);\n }\n options.logger?.debug(`Using CapacitorSQLiteAdapter for platform: ${platform}`);\n return new CapacitorSQLiteAdapter({\n ...options.database\n });\n } else {\n options.logger?.debug(`Using default web adapter for web platform`);\n return super.openDBAdapter(options);\n }\n }\n\n protected generateTriggerManagerConfig(): TriggerManagerConfig {\n const config = super.generateTriggerManagerConfig();\n if (this.isNativeCapacitorPlatform) {\n /**\n * We usually only ever have a single tab for capacitor.\n * Avoiding navigator locks allows insecure contexts (during development).\n */\n config.claimManager = MEMORY_TRIGGER_CLAIM_MANAGER;\n }\n return config;\n }\n\n protected runExclusive<T>(cb: () => Promise<T>): Promise<T> {\n if (this.isNativeCapacitorPlatform) {\n // Use mutex for mobile platforms.\n // This is mainly for testing purposes since navigator.locks require secure contexts.\n return this.runExclusiveMutex.runExclusive(cb);\n } else {\n // Use navigator.locks for web platform\n return super.runExclusive(cb);\n }\n }\n\n protected generateBucketStorageAdapter(): BucketStorageAdapter {\n if (this.isNativeCapacitorPlatform) {\n return new CapacitorBucketStorageAdapter(this.database, this.logger);\n } else {\n return super.generateBucketStorageAdapter();\n }\n }\n\n protected generateSyncStreamImplementation(\n connector: PowerSyncBackendConnector,\n options: RequiredAdditionalConnectionOptions\n ): StreamingSyncImplementation {\n if (this.isNativeCapacitorPlatform) {\n // We don't want to support multi-tab on mobile platforms.\n // We technically can, but it's not a common use case and requires additional work/testing.\n this.logger.debug(`Using Capacitor sync implementation`);\n if (this.options.flags?.enableMultiTabs) {\n this.logger.warn(`enableMultiTabs is not supported on Capacitor mobile platforms. Ignoring the flag.`);\n }\n const remote = new CapacitorRemote(connector, this.logger);\n\n return new CapacitorStreamingSyncImplementation({\n ...(this.options as {}),\n retryDelayMs: options.retryDelayMs,\n crudUploadThrottleMs: options.crudUploadThrottleMs,\n adapter: this.bucketStorageAdapter,\n remote,\n uploadCrud: async () => {\n await this.waitForReady();\n await connector.uploadData(this);\n },\n identifier: this.database.name,\n logger: this.logger,\n subscriptions: options.subscriptions\n });\n } else {\n this.logger.debug(`Using default web sync implementation for web platform`);\n return super.generateSyncStreamImplementation(connector, options);\n }\n }\n}\n"]}
|
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
import { SQLiteDBConnection } from '@capacitor-community/sqlite';
|
|
2
|
-
import { BaseObserver,
|
|
3
|
-
import { Mutex } from 'async-mutex';
|
|
2
|
+
import { BaseObserver, ConnectionPool, DBAdapterListener, DBLockOptions, LockContext, Mutex, QueryResult, Transaction } from '@powersync/web';
|
|
4
3
|
import { CapacitorSQLiteOpenFactoryOptions } from './CapacitorSQLiteOpenFactory.js';
|
|
5
|
-
|
|
6
|
-
* An implementation of {@link DBAdapter} using the Capacitor Community SQLite [plugin](https://github.com/capacitor-community/sqlite).
|
|
7
|
-
*
|
|
8
|
-
* @experimental
|
|
9
|
-
* @alpha This is currently experimental and may change without a major version bump.
|
|
10
|
-
*/
|
|
11
|
-
export declare class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> implements DBAdapter {
|
|
4
|
+
declare class CapacitorConnectionPool extends BaseObserver<DBAdapterListener> implements ConnectionPool {
|
|
12
5
|
protected options: CapacitorSQLiteOpenFactoryOptions;
|
|
13
6
|
protected _writeConnection: SQLiteDBConnection | null;
|
|
14
7
|
protected _readConnection: SQLiteDBConnection | null;
|
|
@@ -22,16 +15,32 @@ export declare class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListen
|
|
|
22
15
|
private init;
|
|
23
16
|
close(): Promise<void>;
|
|
24
17
|
protected generateLockContext(db: SQLiteDBConnection): LockContext;
|
|
25
|
-
execute(query: string, params?: any[]): Promise<QueryResult>;
|
|
26
|
-
executeRaw(query: string, params?: any[]): Promise<any[][]>;
|
|
27
|
-
executeBatch(query: string, params?: any[][]): Promise<QueryResult>;
|
|
28
18
|
readLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
29
|
-
readTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
30
19
|
writeLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
31
|
-
writeTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
32
20
|
refreshSchema(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
declare const CapacitorSQLiteAdapter_base: (new (...args: any[]) => {
|
|
23
|
+
readTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
24
|
+
writeTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
33
25
|
getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
|
|
34
26
|
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
|
|
35
27
|
get<T>(sql: string, parameters?: any[]): Promise<T>;
|
|
36
|
-
|
|
28
|
+
execute(query: string, params?: any[]): Promise<QueryResult>;
|
|
29
|
+
executeRaw(query: string, params?: any[]): Promise<any[][]>;
|
|
30
|
+
executeBatch(query: string, params?: any[][]): Promise<QueryResult>;
|
|
31
|
+
name: string;
|
|
32
|
+
close: () => void | Promise<void>;
|
|
33
|
+
readLock: <T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions) => Promise<T>;
|
|
34
|
+
writeLock: <T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions) => Promise<T>;
|
|
35
|
+
refreshSchema: () => Promise<void>;
|
|
36
|
+
registerListener(listener: Partial<DBAdapterListener>): () => void;
|
|
37
|
+
}) & typeof CapacitorConnectionPool;
|
|
38
|
+
/**
|
|
39
|
+
* An implementation of {@link DBAdapter} using the Capacitor Community SQLite [plugin](https://github.com/capacitor-community/sqlite).
|
|
40
|
+
*
|
|
41
|
+
* @experimental
|
|
42
|
+
* @alpha This is currently experimental and may change without a major version bump.
|
|
43
|
+
*/
|
|
44
|
+
export declare class CapacitorSQLiteAdapter extends CapacitorSQLiteAdapter_base {
|
|
37
45
|
}
|
|
46
|
+
export {};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { CapacitorSQLite, SQLiteConnection } from '@capacitor-community/sqlite';
|
|
2
2
|
import { Capacitor } from '@capacitor/core';
|
|
3
|
-
import { BaseObserver,
|
|
4
|
-
import { Mutex } from 'async-mutex';
|
|
3
|
+
import { BaseObserver, DBAdapterDefaultMixin, Mutex, timeoutSignal } from '@powersync/web';
|
|
5
4
|
import { PowerSyncCore } from '../plugin/PowerSyncCore.js';
|
|
6
5
|
import { messageForErrorCode } from '../plugin/PowerSyncPlugin.js';
|
|
7
6
|
import { DEFAULT_SQLITE_OPTIONS } from './CapacitorSQLiteOpenFactory.js';
|
|
@@ -20,13 +19,7 @@ async function monitorQuery(sql, executor) {
|
|
|
20
19
|
throw e;
|
|
21
20
|
}
|
|
22
21
|
}
|
|
23
|
-
|
|
24
|
-
* An implementation of {@link DBAdapter} using the Capacitor Community SQLite [plugin](https://github.com/capacitor-community/sqlite).
|
|
25
|
-
*
|
|
26
|
-
* @experimental
|
|
27
|
-
* @alpha This is currently experimental and may change without a major version bump.
|
|
28
|
-
*/
|
|
29
|
-
export class CapacitorSQLiteAdapter extends BaseObserver {
|
|
22
|
+
class CapacitorConnectionPool extends BaseObserver {
|
|
30
23
|
constructor(options) {
|
|
31
24
|
super();
|
|
32
25
|
this.options = options;
|
|
@@ -171,24 +164,9 @@ export class CapacitorSQLiteAdapter extends BaseObserver {
|
|
|
171
164
|
const results = await execute(query, params);
|
|
172
165
|
return (_b = (_a = results.rows) === null || _a === void 0 ? void 0 : _a._array.map((row) => Object.values(row))) !== null && _b !== void 0 ? _b : [];
|
|
173
166
|
};
|
|
174
|
-
|
|
175
|
-
getAll,
|
|
176
|
-
getOptional,
|
|
177
|
-
get,
|
|
178
|
-
executeRaw,
|
|
179
|
-
execute
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
execute(query, params) {
|
|
183
|
-
return this.writeLock((tx) => tx.execute(query, params));
|
|
184
|
-
}
|
|
185
|
-
executeRaw(query, params) {
|
|
186
|
-
return this.writeLock((tx) => tx.executeRaw(query, params));
|
|
187
|
-
}
|
|
188
|
-
async executeBatch(query, params = []) {
|
|
189
|
-
return this.writeLock(async (tx) => {
|
|
167
|
+
const executeBatch = async (query, params = []) => {
|
|
190
168
|
var _a, _b, _c;
|
|
191
|
-
let result = await
|
|
169
|
+
let result = await db.executeSet(params.map((param) => ({
|
|
192
170
|
statement: query,
|
|
193
171
|
values: param
|
|
194
172
|
})));
|
|
@@ -196,21 +174,24 @@ export class CapacitorSQLiteAdapter extends BaseObserver {
|
|
|
196
174
|
rowsAffected: (_b = (_a = result.changes) === null || _a === void 0 ? void 0 : _a.changes) !== null && _b !== void 0 ? _b : 0,
|
|
197
175
|
insertId: (_c = result.changes) === null || _c === void 0 ? void 0 : _c.lastId
|
|
198
176
|
};
|
|
199
|
-
}
|
|
177
|
+
};
|
|
178
|
+
return {
|
|
179
|
+
getAll,
|
|
180
|
+
getOptional,
|
|
181
|
+
get,
|
|
182
|
+
executeRaw,
|
|
183
|
+
execute,
|
|
184
|
+
executeBatch
|
|
185
|
+
};
|
|
200
186
|
}
|
|
201
187
|
readLock(fn, options) {
|
|
202
|
-
return
|
|
188
|
+
return this.readMutex.runExclusive(async () => {
|
|
203
189
|
await this.initializedPromise;
|
|
204
|
-
return
|
|
205
|
-
}, options);
|
|
206
|
-
}
|
|
207
|
-
readTransaction(fn, options) {
|
|
208
|
-
return this.readLock(async (ctx) => {
|
|
209
|
-
return this.internalTransaction(ctx, fn);
|
|
210
|
-
});
|
|
190
|
+
return fn(this.generateLockContext(this.readConnection));
|
|
191
|
+
}, timeoutSignal(options === null || options === void 0 ? void 0 : options.timeoutMs));
|
|
211
192
|
}
|
|
212
193
|
writeLock(fn, options) {
|
|
213
|
-
return
|
|
194
|
+
return this.writeMutex.runExclusive(async () => {
|
|
214
195
|
var _a;
|
|
215
196
|
await this.initializedPromise;
|
|
216
197
|
const result = await fn(this.generateLockContext(this.writeConnection));
|
|
@@ -227,12 +208,7 @@ export class CapacitorSQLiteAdapter extends BaseObserver {
|
|
|
227
208
|
};
|
|
228
209
|
this.iterateListeners((l) => { var _a; return (_a = l.tablesUpdated) === null || _a === void 0 ? void 0 : _a.call(l, notification); });
|
|
229
210
|
return result;
|
|
230
|
-
}, options);
|
|
231
|
-
}
|
|
232
|
-
writeTransaction(fn, options) {
|
|
233
|
-
return this.writeLock(async (ctx) => {
|
|
234
|
-
return this.internalTransaction(ctx, fn);
|
|
235
|
-
});
|
|
211
|
+
}, timeoutSignal(options === null || options === void 0 ? void 0 : options.timeoutMs));
|
|
236
212
|
}
|
|
237
213
|
refreshSchema() {
|
|
238
214
|
return this.writeLock(async (writeTx) => {
|
|
@@ -243,48 +219,13 @@ export class CapacitorSQLiteAdapter extends BaseObserver {
|
|
|
243
219
|
});
|
|
244
220
|
});
|
|
245
221
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
async internalTransaction(ctx, fn) {
|
|
256
|
-
let finalized = false;
|
|
257
|
-
const commit = async () => {
|
|
258
|
-
if (finalized) {
|
|
259
|
-
return { rowsAffected: 0 };
|
|
260
|
-
}
|
|
261
|
-
finalized = true;
|
|
262
|
-
return ctx.execute('COMMIT');
|
|
263
|
-
};
|
|
264
|
-
const rollback = async () => {
|
|
265
|
-
if (finalized) {
|
|
266
|
-
return { rowsAffected: 0 };
|
|
267
|
-
}
|
|
268
|
-
finalized = true;
|
|
269
|
-
return ctx.execute('ROLLBACK');
|
|
270
|
-
};
|
|
271
|
-
try {
|
|
272
|
-
await ctx.execute('BEGIN');
|
|
273
|
-
const result = await fn(Object.assign(Object.assign({}, ctx), { commit,
|
|
274
|
-
rollback }));
|
|
275
|
-
await commit();
|
|
276
|
-
return result;
|
|
277
|
-
}
|
|
278
|
-
catch (ex) {
|
|
279
|
-
try {
|
|
280
|
-
await rollback();
|
|
281
|
-
}
|
|
282
|
-
catch (ex2) {
|
|
283
|
-
// In rare cases, a rollback may fail.
|
|
284
|
-
// Safe to ignore.
|
|
285
|
-
}
|
|
286
|
-
throw ex;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* An implementation of {@link DBAdapter} using the Capacitor Community SQLite [plugin](https://github.com/capacitor-community/sqlite).
|
|
225
|
+
*
|
|
226
|
+
* @experimental
|
|
227
|
+
* @alpha This is currently experimental and may change without a major version bump.
|
|
228
|
+
*/
|
|
229
|
+
export class CapacitorSQLiteAdapter extends DBAdapterDefaultMixin(CapacitorConnectionPool) {
|
|
289
230
|
}
|
|
290
231
|
//# sourceMappingURL=CapacitorSQLiteAdapter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CapacitorSQLiteAdapter.js","sourceRoot":"","sources":["../../../src/adapter/CapacitorSQLiteAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAsB,MAAM,6BAA6B,CAAC;AACpG,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EACL,YAAY,EAMZ,iBAAiB,EAGlB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAqC,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AAC5G;;GAEG;AACH,KAAK,UAAU,YAAY,CAAC,GAAW,EAAE,QAAoC;IAC3E,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;IAChC,IAAI,CAAC;QACH,MAAM,CAAC,GAAG,MAAM,QAAQ,EAAE,CAAC;QAC3B,WAAW,CAAC,OAAO,CAAC,SAAS,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC/C,OAAO,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AC;gBACH,MAAM,QAAQ,EAAE,CAAC;YACnB,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,sCAAsC;gBACtC,kBAAkB;YACpB,CAAC;YACD,MAAM,EAAE,CAAC;QACX,CAAC;IACH,CAAC;CACF","sourcesContent":["import { CapacitorSQLite, SQLiteConnection, SQLiteDBConnection } from '@capacitor-community/sqlite';\nimport { Capacitor } from '@capacitor/core';\n\nimport {\n BaseObserver,\n BatchedUpdateNotification,\n DBAdapter,\n DBAdapterListener,\n DBLockOptions,\n LockContext,\n mutexRunExclusive,\n QueryResult,\n Transaction\n} from '@powersync/web';\nimport { Mutex } from 'async-mutex';\nimport { PowerSyncCore } from '../plugin/PowerSyncCore.js';\nimport { messageForErrorCode } from '../plugin/PowerSyncPlugin.js';\nimport { CapacitorSQLiteOpenFactoryOptions, DEFAULT_SQLITE_OPTIONS } from './CapacitorSQLiteOpenFactory.js';\n/**\n * Monitors the execution time of a query and logs it to the performance timeline.\n */\nasync function monitorQuery(sql: string, executor: () => Promise<QueryResult>): Promise<QueryResult> {\n const start = performance.now();\n try {\n const r = await executor();\n performance.measure(`[SQL] ${sql}`, { start });\n return r;\n } catch (e: any) {\n performance.measure(`[SQL] [ERROR: ${e.message}] ${sql}`, { start });\n throw e;\n }\n}\n/**\n * An implementation of {@link DBAdapter} using the Capacitor Community SQLite [plugin](https://github.com/capacitor-community/sqlite).\n *\n * @experimental\n * @alpha This is currently experimental and may change without a major version bump.\n */\nexport class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> implements DBAdapter {\n protected _writeConnection: SQLiteDBConnection | null;\n protected _readConnection: SQLiteDBConnection | null;\n protected initializedPromise: Promise<void>;\n protected writeMutex: Mutex;\n protected readMutex: Mutex;\n\n constructor(protected options: CapacitorSQLiteOpenFactoryOptions) {\n super();\n this._writeConnection = null;\n this._readConnection = null;\n this.writeMutex = new Mutex();\n this.readMutex = new Mutex();\n this.initializedPromise = this.init();\n }\n\n protected get writeConnection(): SQLiteDBConnection {\n if (!this._writeConnection) {\n throw new Error('Init not completed yet');\n }\n return this._writeConnection;\n }\n\n protected get readConnection(): SQLiteDBConnection {\n if (!this._readConnection) {\n throw new Error('Init not completed yet');\n }\n return this._readConnection;\n }\n\n get name() {\n return this.options.dbFilename;\n }\n\n private async init() {\n const { responseCode: registrationResponseCode } = await PowerSyncCore.registerCore();\n if (registrationResponseCode != 0) {\n throw new Error(`Could not register PowerSync core extension: ${messageForErrorCode(registrationResponseCode)}`);\n }\n\n const sqlite = new SQLiteConnection(CapacitorSQLite);\n\n // It seems like the isConnection and retrieveConnection methods\n // only check a JS side map of connections.\n // On hot reload this JS cache can be cleared, while the connection\n // still exists natively. and `createConnection` will fail if it already exists.\n await sqlite.closeConnection(this.options.dbFilename, false).catch(() => {});\n await sqlite.closeConnection(this.options.dbFilename, true).catch(() => {});\n\n // TODO support encryption eventually\n this._writeConnection = await sqlite.createConnection(this.options.dbFilename, false, 'no-encryption', 1, false);\n this._readConnection = await sqlite.createConnection(this.options.dbFilename, false, 'no-encryption', 1, true);\n\n await this._writeConnection.open();\n\n const { cacheSizeKb, journalSizeLimit, synchronous } = { ...DEFAULT_SQLITE_OPTIONS, ...this.options.sqliteOptions };\n\n await this.writeConnection.query('PRAGMA journal_mode = WAL');\n await this.writeConnection.query(`PRAGMA journal_size_limit = ${journalSizeLimit}`);\n await this.writeConnection.query(`PRAGMA temp_store = memory`);\n await this.writeConnection.query(`PRAGMA synchronous = ${synchronous}`);\n await this.writeConnection.query(`PRAGMA cache_size = -${cacheSizeKb}`);\n\n await this._readConnection.open();\n\n const platform = Capacitor.getPlatform();\n if (platform == 'android') {\n /**\n * SQLCipher for Android enables dynamic loading of extensions.\n * On iOS we use a static auto extension registration.\n */\n const extensionQuery = \"SELECT load_extension('libpowersync.so', 'sqlite3_powersync_init')\";\n await this.writeConnection.query(extensionQuery);\n await this.readConnection.query(extensionQuery);\n }\n await this.writeConnection.query(\"SELECT powersync_update_hooks('install')\");\n }\n\n async close(): Promise<void> {\n await this.initializedPromise;\n await this.writeConnection.close();\n await this.readConnection.close();\n }\n\n protected generateLockContext(db: SQLiteDBConnection): LockContext {\n const _query = async (query: string, params: any[] = []) => {\n const result = await db.query(query, params);\n const arrayResult = result.values ?? [];\n return {\n rowsAffected: 0,\n rows: {\n _array: arrayResult,\n length: arrayResult.length,\n item: (idx: number) => arrayResult[idx]\n }\n };\n };\n\n const _execute = async (query: string, params: any[] = []): Promise<QueryResult> => {\n const platform = Capacitor.getPlatform();\n\n if (db.getConnectionReadOnly()) {\n return _query(query, params);\n }\n\n if (platform == 'android') {\n // Android: use query for SELECT and executeSet for mutations\n // We cannot use `run` here for both cases.\n if (query.toLowerCase().trim().startsWith('select')) {\n return _query(query, params);\n } else {\n const result = await db.executeSet([{ statement: query, values: params }], false);\n return {\n insertId: result.changes?.lastId,\n rowsAffected: result.changes?.changes ?? 0,\n rows: {\n _array: [],\n length: 0,\n item: () => null\n }\n };\n }\n }\n\n // iOS (and other platforms): use run(\"all\")\n const result = await db.run(query, params, false, 'all');\n const resultSet = result.changes?.values ?? [];\n return {\n insertId: result.changes?.lastId,\n rowsAffected: result.changes?.changes ?? 0,\n rows: {\n _array: resultSet,\n length: resultSet.length,\n item: (idx) => resultSet[idx]\n }\n };\n };\n\n const execute = this.options.debugMode\n ? (sql: string, params?: any[]) => monitorQuery(sql, () => _execute(sql, params))\n : _execute;\n\n const executeQuery = this.options.debugMode\n ? (sql: string, params?: any[]) => monitorQuery(sql, () => _query(sql, params))\n : _query;\n\n const getAll = async <T>(query: string, params?: any[]): Promise<T[]> => {\n const result = await executeQuery(query, params);\n return result.rows?._array ?? ([] as T[]);\n };\n\n const getOptional = async <T>(query: string, params?: any[]): Promise<T | null> => {\n const results = await getAll<T>(query, params);\n return results.length > 0 ? results[0] : null;\n };\n\n const get = async <T>(query: string, params?: any[]): Promise<T> => {\n const result = await getOptional<T>(query, params);\n if (!result) {\n throw new Error(`No results for query: ${query}`);\n }\n return result;\n };\n\n const executeRaw = async (query: string, params?: any[]): Promise<any[][]> => {\n // This is a workaround, we don't support multiple columns of the same name\n const results = await execute(query, params);\n return results.rows?._array.map((row) => Object.values(row)) ?? [];\n };\n\n return {\n getAll,\n getOptional,\n get,\n executeRaw,\n execute\n };\n }\n\n execute(query: string, params?: any[]): Promise<QueryResult> {\n return this.writeLock((tx) => tx.execute(query, params));\n }\n\n executeRaw(query: string, params?: any[]): Promise<any[][]> {\n return this.writeLock((tx) => tx.executeRaw(query, params));\n }\n\n async executeBatch(query: string, params: any[][] = []): Promise<QueryResult> {\n return this.writeLock(async (tx) => {\n let result = await this.writeConnection.executeSet(\n params.map((param) => ({\n statement: query,\n values: param\n }))\n );\n\n return {\n rowsAffected: result.changes?.changes ?? 0,\n insertId: result.changes?.lastId\n };\n });\n }\n\n readLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {\n return mutexRunExclusive(\n this.readMutex,\n async () => {\n await this.initializedPromise;\n return await fn(this.generateLockContext(this.readConnection));\n },\n options\n );\n }\n\n readTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T> {\n return this.readLock(async (ctx) => {\n return this.internalTransaction(ctx, fn);\n });\n }\n\n writeLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {\n return mutexRunExclusive(\n this.writeMutex,\n async () => {\n await this.initializedPromise;\n const result = await fn(this.generateLockContext(this.writeConnection));\n\n // Fetch table updates\n const updates = await this.writeConnection.query(\"SELECT powersync_update_hooks('get') AS table_name\");\n const jsonUpdates = updates.values?.[0];\n if (!jsonUpdates || !jsonUpdates.table_name) {\n throw new Error('Could not fetch table updates');\n }\n const notification: BatchedUpdateNotification = {\n rawUpdates: [],\n tables: JSON.parse(jsonUpdates.table_name),\n groupedUpdates: {}\n };\n this.iterateListeners((l) => l.tablesUpdated?.(notification));\n return result;\n },\n options\n );\n }\n\n writeTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T> {\n return this.writeLock(async (ctx) => {\n return this.internalTransaction(ctx, fn);\n });\n }\n\n refreshSchema(): Promise<void> {\n return this.writeLock(async (writeTx) => {\n return this.readLock(async (readTx) => {\n const updateQuery = `PRAGMA table_info('sqlite_master')`;\n await writeTx.get(updateQuery);\n await readTx.get(updateQuery);\n });\n });\n }\n\n getAll<T>(sql: string, parameters?: any[]): Promise<T[]> {\n return this.readLock((tx) => tx.getAll<T>(sql, parameters));\n }\n\n getOptional<T>(sql: string, parameters?: any[]): Promise<T | null> {\n return this.readLock((tx) => tx.getOptional<T>(sql, parameters));\n }\n\n get<T>(sql: string, parameters?: any[]): Promise<T> {\n return this.readLock((tx) => tx.get<T>(sql, parameters));\n }\n\n protected async internalTransaction<T>(ctx: LockContext, fn: (tx: Transaction) => Promise<T>): Promise<T> {\n let finalized = false;\n const commit = async (): Promise<QueryResult> => {\n if (finalized) {\n return { rowsAffected: 0 };\n }\n finalized = true;\n return ctx.execute('COMMIT');\n };\n const rollback = async (): Promise<QueryResult> => {\n if (finalized) {\n return { rowsAffected: 0 };\n }\n finalized = true;\n return ctx.execute('ROLLBACK');\n };\n try {\n await ctx.execute('BEGIN');\n const result = await fn({\n ...ctx,\n commit,\n rollback\n });\n await commit();\n return result;\n } catch (ex) {\n try {\n await rollback();\n } catch (ex2) {\n // In rare cases, a rollback may fail.\n // Safe to ignore.\n }\n throw ex;\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"CapacitorSQLiteAdapter.js","sourceRoot":"","sources":["../../../src/adapter/CapacitorSQLiteAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAsB,MAAM,6BAA6B,CAAC;AACpG,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EACL,YAAY,EAIZ,qBAAqB,EAIrB,KAAK,EAEL,aAAa,EAEd,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM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ACD,MAAM,YAAY,GAA8B;gBAC9C,UAAU,EAAE,EAAE;gBACd,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,UAAU,CAAC;gBAC1C,cAAc,EAAE,EAAE;aACnB,CAAC;YACF,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,EAAE,WAAC,OAAA,MAAA,CAAC,CAAC,aAAa,kDAAG,YAAY,CAAC,CAAA,EAAA,CAAC,CAAC;YAC9D,OAAO,MAAM,CAAC;QAChB,CAAC,EAAE,aAAa,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS,CAAC,CAAC,CAAC;IACxC,CAAC;IAED,aAAa;QACX,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;YACtC,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;gBACpC,MAAM,WAAW,GAAG,oCAAoC,CAAC;gBACzD,MAAM,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;gBAC/B,MAAM,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;YAChC,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,OAAO,sBAAuB,SAAQ,qBAAqB,CAAC,uBAAuB,CAAC;CAAG","sourcesContent":["import { CapacitorSQLite, SQLiteConnection, SQLiteDBConnection } from '@capacitor-community/sqlite';\nimport { Capacitor } from '@capacitor/core';\n\nimport {\n BaseObserver,\n BatchedUpdateNotification,\n ConnectionPool,\n DBAdapter,\n DBAdapterDefaultMixin,\n DBAdapterListener,\n DBLockOptions,\n LockContext,\n Mutex,\n QueryResult,\n timeoutSignal,\n Transaction\n} from '@powersync/web';\nimport { PowerSyncCore } from '../plugin/PowerSyncCore.js';\nimport { messageForErrorCode } from '../plugin/PowerSyncPlugin.js';\nimport { CapacitorSQLiteOpenFactoryOptions, DEFAULT_SQLITE_OPTIONS } from './CapacitorSQLiteOpenFactory.js';\n/**\n * Monitors the execution time of a query and logs it to the performance timeline.\n */\nasync function monitorQuery(sql: string, executor: () => Promise<QueryResult>): Promise<QueryResult> {\n const start = performance.now();\n try {\n const r = await executor();\n performance.measure(`[SQL] ${sql}`, { start });\n return r;\n } catch (e: any) {\n performance.measure(`[SQL] [ERROR: ${e.message}] ${sql}`, { start });\n throw e;\n }\n}\n\nclass CapacitorConnectionPool extends BaseObserver<DBAdapterListener> implements ConnectionPool {\n protected _writeConnection: SQLiteDBConnection | null;\n protected _readConnection: SQLiteDBConnection | null;\n protected initializedPromise: Promise<void>;\n protected writeMutex: Mutex;\n protected readMutex: Mutex;\n\n constructor(protected options: CapacitorSQLiteOpenFactoryOptions) {\n super();\n this._writeConnection = null;\n this._readConnection = null;\n this.writeMutex = new Mutex();\n this.readMutex = new Mutex();\n this.initializedPromise = this.init();\n }\n\n protected get writeConnection(): SQLiteDBConnection {\n if (!this._writeConnection) {\n throw new Error('Init not completed yet');\n }\n return this._writeConnection;\n }\n\n protected get readConnection(): SQLiteDBConnection {\n if (!this._readConnection) {\n throw new Error('Init not completed yet');\n }\n return this._readConnection;\n }\n\n get name() {\n return this.options.dbFilename;\n }\n\n private async init() {\n const { responseCode: registrationResponseCode } = await PowerSyncCore.registerCore();\n if (registrationResponseCode != 0) {\n throw new Error(`Could not register PowerSync core extension: ${messageForErrorCode(registrationResponseCode)}`);\n }\n\n const sqlite = new SQLiteConnection(CapacitorSQLite);\n\n // It seems like the isConnection and retrieveConnection methods\n // only check a JS side map of connections.\n // On hot reload this JS cache can be cleared, while the connection\n // still exists natively. and `createConnection` will fail if it already exists.\n await sqlite.closeConnection(this.options.dbFilename, false).catch(() => {});\n await sqlite.closeConnection(this.options.dbFilename, true).catch(() => {});\n\n // TODO support encryption eventually\n this._writeConnection = await sqlite.createConnection(this.options.dbFilename, false, 'no-encryption', 1, false);\n this._readConnection = await sqlite.createConnection(this.options.dbFilename, false, 'no-encryption', 1, true);\n\n await this._writeConnection.open();\n\n const { cacheSizeKb, journalSizeLimit, synchronous } = { ...DEFAULT_SQLITE_OPTIONS, ...this.options.sqliteOptions };\n\n await this.writeConnection.query('PRAGMA journal_mode = WAL');\n await this.writeConnection.query(`PRAGMA journal_size_limit = ${journalSizeLimit}`);\n await this.writeConnection.query(`PRAGMA temp_store = memory`);\n await this.writeConnection.query(`PRAGMA synchronous = ${synchronous}`);\n await this.writeConnection.query(`PRAGMA cache_size = -${cacheSizeKb}`);\n\n await this._readConnection.open();\n\n const platform = Capacitor.getPlatform();\n if (platform == 'android') {\n /**\n * SQLCipher for Android enables dynamic loading of extensions.\n * On iOS we use a static auto extension registration.\n */\n const extensionQuery = \"SELECT load_extension('libpowersync.so', 'sqlite3_powersync_init')\";\n await this.writeConnection.query(extensionQuery);\n await this.readConnection.query(extensionQuery);\n }\n await this.writeConnection.query(\"SELECT powersync_update_hooks('install')\");\n }\n\n async close(): Promise<void> {\n await this.initializedPromise;\n await this.writeConnection.close();\n await this.readConnection.close();\n }\n\n protected generateLockContext(db: SQLiteDBConnection): LockContext {\n const _query = async (query: string, params: any[] = []) => {\n const result = await db.query(query, params);\n const arrayResult = result.values ?? [];\n return {\n rowsAffected: 0,\n rows: {\n _array: arrayResult,\n length: arrayResult.length,\n item: (idx: number) => arrayResult[idx]\n }\n };\n };\n\n const _execute = async (query: string, params: any[] = []): Promise<QueryResult> => {\n const platform = Capacitor.getPlatform();\n\n if (db.getConnectionReadOnly()) {\n return _query(query, params);\n }\n\n if (platform == 'android') {\n // Android: use query for SELECT and executeSet for mutations\n // We cannot use `run` here for both cases.\n if (query.toLowerCase().trim().startsWith('select')) {\n return _query(query, params);\n } else {\n const result = await db.executeSet([{ statement: query, values: params }], false);\n return {\n insertId: result.changes?.lastId,\n rowsAffected: result.changes?.changes ?? 0,\n rows: {\n _array: [],\n length: 0,\n item: () => null\n }\n };\n }\n }\n\n // iOS (and other platforms): use run(\"all\")\n const result = await db.run(query, params, false, 'all');\n const resultSet = result.changes?.values ?? [];\n return {\n insertId: result.changes?.lastId,\n rowsAffected: result.changes?.changes ?? 0,\n rows: {\n _array: resultSet,\n length: resultSet.length,\n item: (idx) => resultSet[idx]\n }\n };\n };\n\n const execute = this.options.debugMode\n ? (sql: string, params?: any[]) => monitorQuery(sql, () => _execute(sql, params))\n : _execute;\n\n const executeQuery = this.options.debugMode\n ? (sql: string, params?: any[]) => monitorQuery(sql, () => _query(sql, params))\n : _query;\n\n const getAll = async <T>(query: string, params?: any[]): Promise<T[]> => {\n const result = await executeQuery(query, params);\n return result.rows?._array ?? ([] as T[]);\n };\n\n const getOptional = async <T>(query: string, params?: any[]): Promise<T | null> => {\n const results = await getAll<T>(query, params);\n return results.length > 0 ? results[0] : null;\n };\n\n const get = async <T>(query: string, params?: any[]): Promise<T> => {\n const result = await getOptional<T>(query, params);\n if (!result) {\n throw new Error(`No results for query: ${query}`);\n }\n return result;\n };\n\n const executeRaw = async (query: string, params?: any[]): Promise<any[][]> => {\n // This is a workaround, we don't support multiple columns of the same name\n const results = await execute(query, params);\n return results.rows?._array.map((row) => Object.values(row)) ?? [];\n };\n\n const executeBatch = async (query: string, params: any[][] = []): Promise<QueryResult> => {\n let result = await db.executeSet(\n params.map((param) => ({\n statement: query,\n values: param\n }))\n );\n\n return {\n rowsAffected: result.changes?.changes ?? 0,\n insertId: result.changes?.lastId\n };\n };\n\n return {\n getAll,\n getOptional,\n get,\n executeRaw,\n execute,\n executeBatch\n };\n }\n\n readLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {\n return this.readMutex.runExclusive(async () => {\n await this.initializedPromise;\n return fn(this.generateLockContext(this.readConnection));\n }, timeoutSignal(options?.timeoutMs));\n }\n\n writeLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {\n return this.writeMutex.runExclusive(async () => {\n await this.initializedPromise;\n const result = await fn(this.generateLockContext(this.writeConnection));\n\n // Fetch table updates\n const updates = await this.writeConnection.query(\"SELECT powersync_update_hooks('get') AS table_name\");\n const jsonUpdates = updates.values?.[0];\n if (!jsonUpdates || !jsonUpdates.table_name) {\n throw new Error('Could not fetch table updates');\n }\n const notification: BatchedUpdateNotification = {\n rawUpdates: [],\n tables: JSON.parse(jsonUpdates.table_name),\n groupedUpdates: {}\n };\n this.iterateListeners((l) => l.tablesUpdated?.(notification));\n return result;\n }, timeoutSignal(options?.timeoutMs));\n }\n\n refreshSchema(): Promise<void> {\n return this.writeLock(async (writeTx) => {\n return this.readLock(async (readTx) => {\n const updateQuery = `PRAGMA table_info('sqlite_master')`;\n await writeTx.get(updateQuery);\n await readTx.get(updateQuery);\n });\n });\n }\n}\n\n/**\n * An implementation of {@link DBAdapter} using the Capacitor Community SQLite [plugin](https://github.com/capacitor-community/sqlite).\n *\n * @experimental\n * @alpha This is currently experimental and may change without a major version bump.\n */\nexport class CapacitorSQLiteAdapter extends DBAdapterDefaultMixin(CapacitorConnectionPool) {}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { SqliteBucketStorage } from '@powersync/web';
|
|
2
|
+
export class CapacitorBucketStorageAdapter extends SqliteBucketStorage {
|
|
3
|
+
control(op, payload) {
|
|
4
|
+
if (payload instanceof Uint8Array && payload['_isBuffer'] == true) {
|
|
5
|
+
/**
|
|
6
|
+
* The Buffer polyfill, used in @powersync/common, is a Uint8Array subclass which defines additional fields like
|
|
7
|
+
* `_isBuffer` and `parent` on its `prototype`. The additional fields are serialized when passed through the native bridge.
|
|
8
|
+
* The Capacitor Community SQLite lib expects a dictionarty of indexes to numerical bytes.
|
|
9
|
+
* The additiona fields (which are not an index to numerical byte mapping) cause the parsing logic in the SQLite lib to throw an error:
|
|
10
|
+
* "Error in reading buffer".
|
|
11
|
+
*
|
|
12
|
+
* Re-wrapping the same backing buffer as a plain Uint8Array removes the Buffer subclass wrapper
|
|
13
|
+
* while keeping the same underlying bytes. This creates a new view, not a byte copy, so the
|
|
14
|
+
* overhead should be minimal.
|
|
15
|
+
*/
|
|
16
|
+
payload = new Uint8Array(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
17
|
+
}
|
|
18
|
+
return super.control(op, payload);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=CapacitorBucketStorageAdapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CapacitorBucketStorageAdapter.js","sourceRoot":"","sources":["../../../src/sync/CapacitorBucketStorageAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2B,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAE9E,MAAM,OAAO,6BAA8B,SAAQ,mBAAmB;IACpE,OAAO,CAAC,EAA2B,EAAE,OAAiD;QACpF,IAAI,OAAO,YAAY,UAAU,IAAK,OAAe,CAAC,WAAW,CAAC,IAAI,IAAI,EAAE,CAAC;YAC3E;;;;;;;;;;eAUG;YACH,OAAO,GAAG,IAAI,UAAU,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;QACnF,CAAC;QAED,OAAO,KAAK,CAAC,OAAO,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IACpC,CAAC;CACF","sourcesContent":["import { PowerSyncControlCommand, SqliteBucketStorage } from '@powersync/web';\n\nexport class CapacitorBucketStorageAdapter extends SqliteBucketStorage {\n control(op: PowerSyncControlCommand, payload: string | Uint8Array | ArrayBuffer | null): Promise<string> {\n if (payload instanceof Uint8Array && (payload as any)['_isBuffer'] == true) {\n /**\n * The Buffer polyfill, used in @powersync/common, is a Uint8Array subclass which defines additional fields like\n * `_isBuffer` and `parent` on its `prototype`. The additional fields are serialized when passed through the native bridge.\n * The Capacitor Community SQLite lib expects a dictionarty of indexes to numerical bytes.\n * The additiona fields (which are not an index to numerical byte mapping) cause the parsing logic in the SQLite lib to throw an error:\n * \"Error in reading buffer\".\n *\n * Re-wrapping the same backing buffer as a plain Uint8Array removes the Buffer subclass wrapper\n * while keeping the same underlying bytes. This creates a new view, not a byte copy, so the\n * overhead should be minimal.\n */\n payload = new Uint8Array(payload.buffer, payload.byteOffset, payload.byteLength);\n }\n\n return super.control(op, payload);\n }\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Capacitor } from '@capacitor/core';
|
|
2
|
+
import { WebRemote } from '@powersync/web';
|
|
3
|
+
export class CapacitorRemote extends WebRemote {
|
|
4
|
+
get supportsStreamingBinaryResponses() {
|
|
5
|
+
/**
|
|
6
|
+
* We'd like to avoid passing Binary buffers to SQLite when using
|
|
7
|
+
* iOS for now. This is due to inefficient binary processing.
|
|
8
|
+
*/
|
|
9
|
+
return Capacitor.getPlatform() !== 'ios';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=CapacitorRemote.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CapacitorRemote.js","sourceRoot":"","sources":["../../../src/sync/CapacitorRemote.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,MAAM,OAAO,eAAgB,SAAQ,SAAS;IAC5C,IAAc,gCAAgC;QAC5C;;;WAGG;QACH,OAAO,SAAS,CAAC,WAAW,EAAE,KAAK,KAAK,CAAC;IAC3C,CAAC;CACF","sourcesContent":["import { Capacitor } from '@capacitor/core';\nimport { WebRemote } from '@powersync/web';\n\nexport class CapacitorRemote extends WebRemote {\n protected get supportsStreamingBinaryResponses(): boolean {\n /**\n * We'd like to avoid passing Binary buffers to SQLite when using\n * iOS for now. This is due to inefficient binary processing.\n */\n return Capacitor.getPlatform() !== 'ios';\n }\n}\n"]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { AbstractStreamingSyncImplementation, LockType,
|
|
2
|
-
import { Mutex } from 'async-mutex';
|
|
1
|
+
import { AbstractStreamingSyncImplementation, LockType, Mutex } from '@powersync/web';
|
|
3
2
|
const GLOBAL_MUTEX_STORE = new Map();
|
|
4
3
|
/**
|
|
5
4
|
* Used to identify multiple instances of CapacitorStreamingSyncImplementation
|
|
@@ -40,13 +39,9 @@ export class CapacitorStreamingSyncImplementation extends AbstractStreamingSyncI
|
|
|
40
39
|
const mutexRecord = GLOBAL_MUTEX_STORE.get(baseIdentifier);
|
|
41
40
|
mutexRecord.tracking.add(this.instanceId);
|
|
42
41
|
const mutex = mutexRecord.locks[lockOptions.type];
|
|
43
|
-
return
|
|
44
|
-
var _a;
|
|
45
|
-
if ((_a = lockOptions.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
46
|
-
throw new Error('Aborted');
|
|
47
|
-
}
|
|
42
|
+
return mutex.runExclusive(async () => {
|
|
48
43
|
return await lockOptions.callback();
|
|
49
|
-
});
|
|
44
|
+
}, lockOptions.signal);
|
|
50
45
|
}
|
|
51
46
|
}
|
|
52
47
|
//# sourceMappingURL=CapacitorSyncImplementation.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CapacitorSyncImplementation.js","sourceRoot":"","sources":["../../../src/sync/CapacitorSyncImplementation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mCAAmC,EAAe,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"CapacitorSyncImplementation.js","sourceRoot":"","sources":["../../../src/sync/CapacitorSyncImplementation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mCAAmC,EAAe,QAAQ,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAanG,MAAM,kBAAkB,GAA0B,IAAI,GAAG,EAAE,CAAC;AAE5D;;GAEG;AACH,IAAI,kCAAkC,GAAG,CAAC,CAAC;AAE3C,MAAM,OAAO,oCAAqC,SAAQ,mCAAmC;IAA7F;;QACE,yFAAyF;QAC/E,eAAU,GAAG,kCAAkC,EAAE,CAAC;IAuC9D,CAAC;IArCC,KA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K,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QAElD,OAAO,KAAK,CAAC,YAAY,CAAC,KAAK,IAAI,EAAE;YACnC,OAAO,MAAM,WAAW,CAAC,QAAQ,EAAE,CAAC;QACtC,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;CACF","sourcesContent":["import { AbstractStreamingSyncImplementation, LockOptions, LockType, Mutex } from '@powersync/web';\n\ntype MutexMap = {\n /**\n * Used to track the consumers of this Mutex.\n * It should be safe to dispose the Mutex if this is empty.\n */\n tracking: Set<number>;\n locks: {\n [Type in LockType]: Mutex;\n };\n};\n\nconst GLOBAL_MUTEX_STORE: Map<string, MutexMap> = new Map();\n\n/**\n * Used to identify multiple instances of CapacitorStreamingSyncImplementation\n */\nlet _CAPACITOR_STREAMING_SYNC_SEQUENCE = 0;\n\nexport class CapacitorStreamingSyncImplementation extends AbstractStreamingSyncImplementation {\n // A unique ID for tacking this specific instance of CapacitorStreamingSyncImplementation\n protected instanceId = _CAPACITOR_STREAMING_SYNC_SEQUENCE++;\n\n async dispose(): Promise<void> {\n await super.dispose();\n\n // Clear up any global mutexes which aren't used anymore\n for (const mutexEntry of GLOBAL_MUTEX_STORE.entries()) {\n const [identifier, mutex] = mutexEntry;\n if (!mutex.tracking.has(this.instanceId)) {\n continue;\n }\n mutex.tracking.delete(this.instanceId);\n if (mutex.tracking.size == 0) {\n GLOBAL_MUTEX_STORE.delete(identifier);\n }\n }\n }\n\n async obtainLock<T>(lockOptions: LockOptions<T>): Promise<T> {\n // If we don't have an identifier for some reason (should not happen), we use a shared Mutex\n const { identifier: baseIdentifier = 'DEFAULT' } = this.options;\n if (!GLOBAL_MUTEX_STORE.has(baseIdentifier)) {\n GLOBAL_MUTEX_STORE.set(baseIdentifier, {\n tracking: new Set([this.instanceId]),\n locks: {\n [LockType.CRUD]: new Mutex(),\n [LockType.SYNC]: new Mutex()\n }\n });\n }\n\n const mutexRecord = GLOBAL_MUTEX_STORE.get(baseIdentifier)!;\n mutexRecord.tracking.add(this.instanceId);\n const mutex = mutexRecord.locks[lockOptions.type];\n\n return mutex.runExclusive(async () => {\n return await lockOptions.callback();\n }, lockOptions.signal);\n }\n}\n"]}
|