@powersync/capacitor 0.5.1 → 0.5.3
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 +3 -1
- package/android/build.gradle +1 -1
- package/dist/esm/PowerSyncDatabase.d.ts +8 -1
- package/dist/esm/PowerSyncDatabase.js +20 -2
- package/dist/esm/PowerSyncDatabase.js.map +1 -1
- package/dist/esm/adapter/CapacitorSQLiteAdapter.d.ts +3 -4
- package/dist/esm/adapter/CapacitorSQLiteAdapter.js +73 -28
- package/dist/esm/adapter/CapacitorSQLiteAdapter.js.map +1 -1
- package/dist/esm/sync/CapacitorRemote.d.ts +4 -0
- package/dist/esm/sync/CapacitorRemote.js +23 -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 +118 -38
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +11 -4
- package/dist/plugin.js +120 -39
- package/dist/plugin.js.map +1 -1
- package/package.json +2 -5
- package/src/PowerSyncDatabase.ts +30 -3
- package/src/adapter/CapacitorSQLiteAdapter.ts +96 -53
- package/src/sync/CapacitorRemote.ts +23 -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
|
@@ -61,6 +61,8 @@ const db = new PowerSyncDatabase({
|
|
|
61
61
|
- On Android and iOS, this SDK uses [Capacitor Community SQLite](https://github.com/capacitor-community/sqlite) for native database access.
|
|
62
62
|
- On web, it falls back to the [PowerSync Web SDK](https://www.npmjs.com/package/@powersync/web).
|
|
63
63
|
|
|
64
|
+
When using the native Capacitor Community SQLite driver, `PowerSyncDatabase.connect()` defaults to HTTP with NDJSON streaming. This avoids slow binary payload processing in the native SQLite bridge. Web targets keep the default Web SDK connection behavior.
|
|
65
|
+
|
|
64
66
|
When using custom database factories, be sure to specify the `CapacitorSQLiteOpenFactory` for Capacitor platforms.
|
|
65
67
|
|
|
66
68
|
```javascript
|
|
@@ -87,7 +89,7 @@ const db = new PowerSyncDatabase({
|
|
|
87
89
|
|
|
88
90
|
## Examples
|
|
89
91
|
|
|
90
|
-
See the [`demos/example-capacitor/`](https://github.com/
|
|
92
|
+
See the [`demos/example-capacitor/`](https://github.com/powersync-ja/powersync-js/tree/main/demos/example-capacitor) directory for a working example.
|
|
91
93
|
|
|
92
94
|
## Found a bug or need help?
|
|
93
95
|
|
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 { DBAdapter, PowerSyncBackendConnector, PowerSyncConnectionOptions, 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
|
|
@@ -8,6 +8,13 @@ import { DBAdapter, PowerSyncBackendConnector, RequiredAdditionalConnectionOptio
|
|
|
8
8
|
* @alpha
|
|
9
9
|
*/
|
|
10
10
|
export declare class PowerSyncDatabase extends WebPowerSyncDatabase {
|
|
11
|
+
/**
|
|
12
|
+
* Connects to stream of events from the PowerSync instance.
|
|
13
|
+
* {@link PowerSyncConnectionOptions#connectionMethod} defaults to WebSocket connection on Web platforms
|
|
14
|
+
* or HTTP connections if using {@link CapacitorSQLiteAdapter} - this is due to poor performance with
|
|
15
|
+
* the Capacitor Community SQLite library and binary payloads.
|
|
16
|
+
*/
|
|
17
|
+
connect(connector: PowerSyncBackendConnector, options?: PowerSyncConnectionOptions): Promise<void>;
|
|
11
18
|
protected get isNativeCapacitorPlatform(): boolean;
|
|
12
19
|
protected openDBAdapter(options: WebPowerSyncDatabaseOptionsWithSettings): DBAdapter;
|
|
13
20
|
protected generateTriggerManagerConfig(): TriggerManagerConfig;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Capacitor } from '@capacitor/core';
|
|
2
|
-
import { MEMORY_TRIGGER_CLAIM_MANAGER, PowerSyncDatabase as WebPowerSyncDatabase
|
|
2
|
+
import { DEFAULT_STREAM_CONNECTION_OPTIONS, MEMORY_TRIGGER_CLAIM_MANAGER, SyncStreamConnectionMethod, PowerSyncDatabase as WebPowerSyncDatabase } from '@powersync/web';
|
|
3
3
|
import { CapacitorSQLiteAdapter } from './adapter/CapacitorSQLiteAdapter.js';
|
|
4
|
+
import { CapacitorRemote } from './sync/CapacitorRemote.js';
|
|
4
5
|
import { CapacitorStreamingSyncImplementation } from './sync/CapacitorSyncImplementation.js';
|
|
5
6
|
/**
|
|
6
7
|
* PowerSyncDatabase class for managing database connections and sync implementations.
|
|
@@ -11,6 +12,23 @@ import { CapacitorStreamingSyncImplementation } from './sync/CapacitorSyncImplem
|
|
|
11
12
|
* @alpha
|
|
12
13
|
*/
|
|
13
14
|
export class PowerSyncDatabase extends WebPowerSyncDatabase {
|
|
15
|
+
/**
|
|
16
|
+
* Connects to stream of events from the PowerSync instance.
|
|
17
|
+
* {@link PowerSyncConnectionOptions#connectionMethod} defaults to WebSocket connection on Web platforms
|
|
18
|
+
* or HTTP connections if using {@link CapacitorSQLiteAdapter} - this is due to poor performance with
|
|
19
|
+
* the Capacitor Community SQLite library and binary payloads.
|
|
20
|
+
*/
|
|
21
|
+
connect(connector, options) {
|
|
22
|
+
var _a;
|
|
23
|
+
const isUsingCapacitorDriver = this.database instanceof CapacitorSQLiteAdapter;
|
|
24
|
+
const defaultConnectionMethod = isUsingCapacitorDriver
|
|
25
|
+
? SyncStreamConnectionMethod.HTTP
|
|
26
|
+
: DEFAULT_STREAM_CONNECTION_OPTIONS.connectionMethod;
|
|
27
|
+
if ((options === null || options === void 0 ? void 0 : options.connectionMethod) == SyncStreamConnectionMethod.WEB_SOCKET && isUsingCapacitorDriver) {
|
|
28
|
+
this.logger.warn(`Connecting via 'SyncStreamConnectionMethod.WEB_SOCKET' when using the 'CapacitorSQLiteAdapter' will result in poor sync performance. Use 'SyncStreamConnectionMethod.HTTP' (the default for native) instead.`);
|
|
29
|
+
}
|
|
30
|
+
return super.connect(connector, Object.assign(Object.assign({}, (options !== null && options !== void 0 ? options : {})), { connectionMethod: (_a = options === null || options === void 0 ? void 0 : options.connectionMethod) !== null && _a !== void 0 ? _a : defaultConnectionMethod }));
|
|
31
|
+
}
|
|
14
32
|
get isNativeCapacitorPlatform() {
|
|
15
33
|
const platform = Capacitor.getPlatform();
|
|
16
34
|
return platform == 'ios' || platform == 'android';
|
|
@@ -64,7 +82,7 @@ export class PowerSyncDatabase extends WebPowerSyncDatabase {
|
|
|
64
82
|
if ((_a = this.options.flags) === null || _a === void 0 ? void 0 : _a.enableMultiTabs) {
|
|
65
83
|
this.logger.warn(`enableMultiTabs is not supported on Capacitor mobile platforms. Ignoring the flag.`);
|
|
66
84
|
}
|
|
67
|
-
const remote = new
|
|
85
|
+
const remote = new CapacitorRemote(connector, this.logger);
|
|
68
86
|
return new CapacitorStreamingSyncImplementation(Object.assign(Object.assign({}, this.options), { retryDelayMs: options.retryDelayMs, crudUploadThrottleMs: options.crudUploadThrottleMs, adapter: this.bucketStorageAdapter, remote, uploadCrud: async () => {
|
|
69
87
|
await this.waitForReady();
|
|
70
88
|
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,EAEL,4BAA4B,EAK5B,iBAAiB,IAAI,oBAAoB,
|
|
1
|
+
{"version":3,"file":"PowerSyncDatabase.js","sourceRoot":"","sources":["../../src/PowerSyncDatabase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAEL,iCAAiC,EACjC,4BAA4B,EAK5B,0BAA0B,EAE1B,iBAAiB,IAAI,oBAAoB,EAE1C,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAC7E,OAA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AC,CAAC;YAE3D,OAAO,IAAI,oCAAoC,iCACzC,IAAI,CAAC,OAAc,KACvB,YAAY,EAAE,OAAO,CAAC,YAAY,EAClC,oBAAoB,EAAE,OAAO,CAAC,oBAAoB,EAClD,OAAO,EAAE,IAAI,CAAC,oBAAoB,EAClC,MAAM,EACN,UAAU,EAAE,KAAK,IAAI,EAAE;oBACrB,MAAM,IAAI,CAAC,YAAY,EAAE,CAAC;oBAC1B,MAAM,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;gBACnC,CAAC,EACD,UAAU,EAAE,IAAI,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 DBAdapter,\n DEFAULT_STREAM_CONNECTION_OPTIONS,\n MEMORY_TRIGGER_CLAIM_MANAGER,\n PowerSyncBackendConnector,\n PowerSyncConnectionOptions,\n RequiredAdditionalConnectionOptions,\n StreamingSyncImplementation,\n SyncStreamConnectionMethod,\n TriggerManagerConfig,\n PowerSyncDatabase as WebPowerSyncDatabase,\n WebPowerSyncDatabaseOptionsWithSettings\n} from '@powersync/web';\nimport { CapacitorSQLiteAdapter } from './adapter/CapacitorSQLiteAdapter.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 /**\n * Connects to stream of events from the PowerSync instance.\n * {@link PowerSyncConnectionOptions#connectionMethod} defaults to WebSocket connection on Web platforms\n * or HTTP connections if using {@link CapacitorSQLiteAdapter} - this is due to poor performance with\n * the Capacitor Community SQLite library and binary payloads.\n */\n connect(connector: PowerSyncBackendConnector, options?: PowerSyncConnectionOptions): Promise<void> {\n const isUsingCapacitorDriver = this.database instanceof CapacitorSQLiteAdapter;\n const defaultConnectionMethod = isUsingCapacitorDriver\n ? SyncStreamConnectionMethod.HTTP\n : DEFAULT_STREAM_CONNECTION_OPTIONS.connectionMethod;\n if (options?.connectionMethod == SyncStreamConnectionMethod.WEB_SOCKET && isUsingCapacitorDriver) {\n this.logger.warn(\n `Connecting via 'SyncStreamConnectionMethod.WEB_SOCKET' when using the 'CapacitorSQLiteAdapter' will result in poor sync performance. Use 'SyncStreamConnectionMethod.HTTP' (the default for native) instead.`\n );\n }\n\n return super.connect(connector, {\n ...(options ?? {}),\n connectionMethod: options?.connectionMethod ?? defaultConnectionMethod\n });\n }\n\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 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\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,6 +1,5 @@
|
|
|
1
1
|
import { SQLiteDBConnection } from '@capacitor-community/sqlite';
|
|
2
|
-
import { BaseObserver, ConnectionPool, DBAdapterListener, DBLockOptions, LockContext,
|
|
3
|
-
import { Mutex } from 'async-mutex';
|
|
2
|
+
import { BaseObserver, ConnectionPool, DBAdapterListener, DBLockOptions, LockContext, Mutex, QueryResult } from '@powersync/web';
|
|
4
3
|
import { CapacitorSQLiteOpenFactoryOptions } from './CapacitorSQLiteOpenFactory.js';
|
|
5
4
|
declare class CapacitorConnectionPool extends BaseObserver<DBAdapterListener> implements ConnectionPool {
|
|
6
5
|
protected options: CapacitorSQLiteOpenFactoryOptions;
|
|
@@ -21,8 +20,8 @@ declare class CapacitorConnectionPool extends BaseObserver<DBAdapterListener> im
|
|
|
21
20
|
refreshSchema(): Promise<void>;
|
|
22
21
|
}
|
|
23
22
|
declare const CapacitorSQLiteAdapter_base: (new (...args: any[]) => {
|
|
24
|
-
readTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
25
|
-
writeTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
23
|
+
readTransaction<T>(fn: (tx: import("@powersync/web").Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
24
|
+
writeTransaction<T>(fn: (tx: import("@powersync/web").Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
26
25
|
getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
|
|
27
26
|
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
|
|
28
27
|
get<T>(sql: string, parameters?: any[]): Promise<T>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { CapacitorSQLite, SQLiteConnection } from '@capacitor-community/sqlite';
|
|
2
2
|
import { Capacitor } from '@capacitor/core';
|
|
3
|
-
import { BaseObserver, DBAdapterDefaultMixin,
|
|
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,6 +19,44 @@ async function monitorQuery(sql, executor) {
|
|
|
20
19
|
throw e;
|
|
21
20
|
}
|
|
22
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Maps SQLite query parameter values to Capacitor Community supported formats.
|
|
24
|
+
* This handles binary payloads for both iOS and Android.
|
|
25
|
+
*/
|
|
26
|
+
function mapSQLiteParameterValues({ platform, values }) {
|
|
27
|
+
return values.map((value) => {
|
|
28
|
+
if (value instanceof Uint8Array) {
|
|
29
|
+
switch (platform) {
|
|
30
|
+
case 'ios': {
|
|
31
|
+
/**
|
|
32
|
+
* The Buffer polyfill, used in @powersync/common, is a Uint8Array subclass which defines additional fields like
|
|
33
|
+
* `_isBuffer` and `parent` on its `prototype`. The additional fields are serialized when passed through the native bridge.
|
|
34
|
+
* The Capacitor Community SQLite library expects a dictionary of indexes to numerical bytes.
|
|
35
|
+
* The additional fields (which are not an index to numerical byte mapping) cause the parsing logic in the SQLite library to throw an error:
|
|
36
|
+
* "Error in reading buffer".
|
|
37
|
+
*
|
|
38
|
+
* Re-wrapping the same backing buffer as a plain Uint8Array removes the Buffer subclass wrapper
|
|
39
|
+
* while keeping the same underlying bytes. This creates a new view, not a byte copy, so the
|
|
40
|
+
* overhead should be minimal.
|
|
41
|
+
*/
|
|
42
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
43
|
+
}
|
|
44
|
+
case 'android': {
|
|
45
|
+
/**
|
|
46
|
+
* Android expects an object of the form:
|
|
47
|
+
* { type: 'Buffer', data: [...]}
|
|
48
|
+
*/
|
|
49
|
+
return {
|
|
50
|
+
type: 'Buffer',
|
|
51
|
+
data: Array.from(value)
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// return value as-is
|
|
57
|
+
return value;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
23
60
|
class CapacitorConnectionPool extends BaseObserver {
|
|
24
61
|
constructor(options) {
|
|
25
62
|
super();
|
|
@@ -88,7 +125,11 @@ class CapacitorConnectionPool extends BaseObserver {
|
|
|
88
125
|
generateLockContext(db) {
|
|
89
126
|
const _query = async (query, params = []) => {
|
|
90
127
|
var _a;
|
|
91
|
-
const
|
|
128
|
+
const mappedParams = mapSQLiteParameterValues({
|
|
129
|
+
platform: Capacitor.getPlatform(),
|
|
130
|
+
values: params
|
|
131
|
+
});
|
|
132
|
+
const result = await db.query(query, mappedParams);
|
|
92
133
|
const arrayResult = (_a = result.values) !== null && _a !== void 0 ? _a : [];
|
|
93
134
|
return {
|
|
94
135
|
rowsAffected: 0,
|
|
@@ -102,30 +143,30 @@ class CapacitorConnectionPool extends BaseObserver {
|
|
|
102
143
|
const _execute = async (query, params = []) => {
|
|
103
144
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
104
145
|
const platform = Capacitor.getPlatform();
|
|
105
|
-
if (db.getConnectionReadOnly()
|
|
146
|
+
if (db.getConnectionReadOnly() ||
|
|
147
|
+
// Android: use query for SELECT and executeSet for mutations
|
|
148
|
+
// We cannot use `run` here for both cases.
|
|
149
|
+
(platform == 'android' && query.toLowerCase().trim().startsWith('select'))) {
|
|
106
150
|
return _query(query, params);
|
|
107
151
|
}
|
|
152
|
+
const mappedParams = mapSQLiteParameterValues({
|
|
153
|
+
platform,
|
|
154
|
+
values: params
|
|
155
|
+
});
|
|
108
156
|
if (platform == 'android') {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
rows: {
|
|
120
|
-
_array: [],
|
|
121
|
-
length: 0,
|
|
122
|
-
item: () => null
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
}
|
|
157
|
+
const result = await db.executeSet([{ statement: query, values: mappedParams }], false);
|
|
158
|
+
return {
|
|
159
|
+
insertId: (_a = result.changes) === null || _a === void 0 ? void 0 : _a.lastId,
|
|
160
|
+
rowsAffected: (_c = (_b = result.changes) === null || _b === void 0 ? void 0 : _b.changes) !== null && _c !== void 0 ? _c : 0,
|
|
161
|
+
rows: {
|
|
162
|
+
_array: [],
|
|
163
|
+
length: 0,
|
|
164
|
+
item: () => null
|
|
165
|
+
}
|
|
166
|
+
};
|
|
126
167
|
}
|
|
127
168
|
// iOS (and other platforms): use run("all")
|
|
128
|
-
const result = await db.run(query,
|
|
169
|
+
const result = await db.run(query, mappedParams, false, 'all');
|
|
129
170
|
const resultSet = (_e = (_d = result.changes) === null || _d === void 0 ? void 0 : _d.values) !== null && _e !== void 0 ? _e : [];
|
|
130
171
|
return {
|
|
131
172
|
insertId: (_f = result.changes) === null || _f === void 0 ? void 0 : _f.lastId,
|
|
@@ -167,9 +208,13 @@ class CapacitorConnectionPool extends BaseObserver {
|
|
|
167
208
|
};
|
|
168
209
|
const executeBatch = async (query, params = []) => {
|
|
169
210
|
var _a, _b, _c;
|
|
211
|
+
const platform = Capacitor.getPlatform();
|
|
170
212
|
let result = await db.executeSet(params.map((param) => ({
|
|
171
213
|
statement: query,
|
|
172
|
-
values:
|
|
214
|
+
values: mapSQLiteParameterValues({
|
|
215
|
+
platform,
|
|
216
|
+
values: param
|
|
217
|
+
})
|
|
173
218
|
})));
|
|
174
219
|
return {
|
|
175
220
|
rowsAffected: (_b = (_a = result.changes) === null || _a === void 0 ? void 0 : _a.changes) !== null && _b !== void 0 ? _b : 0,
|
|
@@ -186,13 +231,13 @@ class CapacitorConnectionPool extends BaseObserver {
|
|
|
186
231
|
};
|
|
187
232
|
}
|
|
188
233
|
readLock(fn, options) {
|
|
189
|
-
return
|
|
234
|
+
return this.readMutex.runExclusive(async () => {
|
|
190
235
|
await this.initializedPromise;
|
|
191
|
-
return
|
|
192
|
-
}, options);
|
|
236
|
+
return fn(this.generateLockContext(this.readConnection));
|
|
237
|
+
}, timeoutSignal(options === null || options === void 0 ? void 0 : options.timeoutMs));
|
|
193
238
|
}
|
|
194
239
|
writeLock(fn, options) {
|
|
195
|
-
return
|
|
240
|
+
return this.writeMutex.runExclusive(async () => {
|
|
196
241
|
var _a;
|
|
197
242
|
await this.initializedPromise;
|
|
198
243
|
const result = await fn(this.generateLockContext(this.writeConnection));
|
|
@@ -209,7 +254,7 @@ class CapacitorConnectionPool extends BaseObserver {
|
|
|
209
254
|
};
|
|
210
255
|
this.iterateListeners((l) => { var _a; return (_a = l.tablesUpdated) === null || _a === void 0 ? void 0 : _a.call(l, notification); });
|
|
211
256
|
return result;
|
|
212
|
-
}, options);
|
|
257
|
+
}, timeoutSignal(options === null || options === void 0 ? void 0 : options.timeoutMs));
|
|
213
258
|
}
|
|
214
259
|
refreshSchema() {
|
|
215
260
|
return this.writeLock(async (writeTx) => {
|
|
@@ -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,EAIZ,qBAAqB,EAIrB,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,CAAC,CAAC;IACX,CAAC;IAAC,OAAO,CAAM,EAAE,CAAC;QAChB,WAAW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G,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 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\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 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 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 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 DBAdapter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AC5C,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;YACnD,CAAC;YACD,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} 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\n/**\n * Maps SQLite query parameter values to Capacitor Community supported formats.\n * This handles binary payloads for both iOS and Android.\n */\nfunction mapSQLiteParameterValues({ platform, values }: { platform: string; values: any[] }) {\n return values.map((value) => {\n if (value instanceof Uint8Array) {\n switch (platform) {\n case 'ios': {\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 library expects a dictionary of indexes to numerical bytes.\n * The additional fields (which are not an index to numerical byte mapping) cause the parsing logic in the SQLite library 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 return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);\n }\n case 'android': {\n /**\n * Android expects an object of the form:\n * { type: 'Buffer', data: [...]}\n */\n return {\n type: 'Buffer',\n data: Array.from(value)\n };\n }\n }\n }\n\n // return value as-is\n return value;\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 mappedParams = mapSQLiteParameterValues({\n platform: Capacitor.getPlatform(),\n values: params\n });\n const result = await db.query(query, mappedParams);\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 (\n db.getConnectionReadOnly() ||\n // Android: use query for SELECT and executeSet for mutations\n // We cannot use `run` here for both cases.\n (platform == 'android' && query.toLowerCase().trim().startsWith('select'))\n ) {\n return _query(query, params);\n }\n\n const mappedParams = mapSQLiteParameterValues({\n platform,\n values: params\n });\n\n if (platform == 'android') {\n const result = await db.executeSet([{ statement: query, values: mappedParams }], 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 // iOS (and other platforms): use run(\"all\")\n const result = await db.run(query, mappedParams, 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 const platform = Capacitor.getPlatform();\n let result = await db.executeSet(\n params.map((param) => ({\n statement: query,\n values: mapSQLiteParameterValues({\n platform,\n values: param\n })\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,23 @@
|
|
|
1
|
+
import { WebRemote } from '@powersync/web';
|
|
2
|
+
export class CapacitorRemote extends WebRemote {
|
|
3
|
+
get supportsStreamingBinaryResponses() {
|
|
4
|
+
/**
|
|
5
|
+
* We'd like to avoid passing Binary buffers to SQLite when using
|
|
6
|
+
* iOS and Android for now. This is due to inefficient binary processing.
|
|
7
|
+
* Syncing using Buffers and Capacitor Community SQLite has been observed to be notably
|
|
8
|
+
* slower than the NDJSON option.
|
|
9
|
+
* Capacitor Community SQLite serializes Buffer objects, which causes slowdown
|
|
10
|
+
* ios: https://github.com/capacitor-community/sqlite/blob/f507a1e779688ea72b9d7e8744c647f7b688c568/ios/Plugin/CapacitorSQLite.swift#L888-L912
|
|
11
|
+
* android: https://github.com/capacitor-community/sqlite/blob/master/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLite.java#L141-L147
|
|
12
|
+
* As a rough guideline, the time to locally sync 10_000 small records was observed as:
|
|
13
|
+
* iOS:
|
|
14
|
+
* - NDJSON: 449ms
|
|
15
|
+
* - Binary: 68_982ms
|
|
16
|
+
* Android:
|
|
17
|
+
* - NDJSON: 452ms
|
|
18
|
+
* - Binary: 1_847ms
|
|
19
|
+
*/
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# 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,gBAAgB,CAAC;AAE3C,MAAM,OAAO,eAAgB,SAAQ,SAAS;IAC5C,IAAc,gCAAgC;QAC5C;;;;;;;;;;;;;;;WAeG;QACH,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["import { 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 and Android for now. This is due to inefficient binary processing.\n * Syncing using Buffers and Capacitor Community SQLite has been observed to be notably\n * slower than the NDJSON option.\n * Capacitor Community SQLite serializes Buffer objects, which causes slowdown\n * ios: https://github.com/capacitor-community/sqlite/blob/f507a1e779688ea72b9d7e8744c647f7b688c568/ios/Plugin/CapacitorSQLite.swift#L888-L912\n * android: https://github.com/capacitor-community/sqlite/blob/master/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLite.java#L141-L147\n * As a rough guideline, the time to locally sync 10_000 small records was observed as:\n * iOS:\n * - NDJSON: 449ms\n * - Binary: 68_982ms\n * Android:\n * - NDJSON: 452ms\n * - Binary: 1_847ms\n */\n return false;\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,KAAK,CAAC,OAAO;QACX,MAAM,KAAK,CAAC,OAAO,EAAE,CAAC;QAEtB,wDAAwD;QACxD,KAAK,MAAM,UAAU,IAAI,kBAAkB,CAAC,OAAO,EAAE,EAAE,CAAC;YACtD,MAAM,CAAC,UAAU,EAAE,KAAK,CAAC,GAAG,UAAU,CAAC;YACvC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;gBACzC,SAAS;YACX,CAAC;YACD,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YACvC,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,EAAE,CAAC;gBAC7B,kBAAkB,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YACxC,CAAC;QACH,CAAC;IACH,CAAC;IAED,KAAK,CAAC,UAAU,CAAI,WAA2B;QAC7C,4FAA4F;QAC5F,MAAM,EAAE,UAAU,EAAE,cAAc,GAAG,SAAS,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QAChE,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,CAAC;YAC5C,kBAAkB,CAAC,GAAG,CAAC,cAAc,EAAE;gBACrC,QAAQ,EAAE,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;gBACpC,KAAK,EAAE;oBACL,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,KAAK,EAAE;oBAC5B,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,KAAK,EAAE;iBAC7B;aACF,CAAC,CAAC;QACL,CAAC;QAED,MAAM,WAAW,GAAG,kBAAkB,CAAC,GAAG,CAAC,cAAc,CAAE,CAAC;QAC5D,WAAW,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC1C,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,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"]}
|