@powersync/common 0.0.0-dev-20260311103504 → 0.0.0-dev-20260414110516
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/dist/bundle.cjs +772 -483
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +766 -479
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +770 -482
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +764 -478
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +162 -92
- package/lib/attachments/AttachmentQueue.d.ts +10 -4
- package/lib/attachments/AttachmentQueue.js +10 -4
- package/lib/attachments/AttachmentQueue.js.map +1 -1
- package/lib/attachments/AttachmentService.js +2 -3
- package/lib/attachments/AttachmentService.js.map +1 -1
- package/lib/attachments/SyncingService.d.ts +2 -1
- package/lib/attachments/SyncingService.js +4 -5
- package/lib/attachments/SyncingService.js.map +1 -1
- package/lib/client/AbstractPowerSyncDatabase.d.ts +5 -1
- package/lib/client/AbstractPowerSyncDatabase.js +9 -5
- package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +29 -8
- package/lib/client/sync/stream/AbstractRemote.js +154 -177
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +1 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +69 -88
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/db/DBAdapter.d.ts +55 -9
- package/lib/db/DBAdapter.js +126 -0
- package/lib/db/DBAdapter.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/utils/async.d.ts +0 -9
- package/lib/utils/async.js +0 -9
- package/lib/utils/async.js.map +1 -1
- package/lib/utils/mutex.d.ts +47 -5
- package/lib/utils/mutex.js +146 -21
- package/lib/utils/mutex.js.map +1 -1
- package/lib/utils/queue.d.ts +16 -0
- package/lib/utils/queue.js +42 -0
- package/lib/utils/queue.js.map +1 -0
- package/lib/utils/stream_transform.d.ts +39 -0
- package/lib/utils/stream_transform.js +206 -0
- package/lib/utils/stream_transform.js.map +1 -0
- package/package.json +9 -8
- package/src/attachments/AttachmentQueue.ts +10 -4
- package/src/attachments/AttachmentService.ts +2 -3
- package/src/attachments/README.md +6 -4
- package/src/attachments/SyncingService.ts +4 -5
- package/src/client/AbstractPowerSyncDatabase.ts +9 -5
- package/src/client/sync/stream/AbstractRemote.ts +182 -206
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +82 -83
- package/src/db/DBAdapter.ts +167 -9
- package/src/index.ts +1 -1
- package/src/utils/async.ts +0 -11
- package/src/utils/mutex.ts +184 -26
- package/src/utils/queue.ts +48 -0
- package/src/utils/stream_transform.ts +252 -0
- package/lib/utils/DataStream.d.ts +0 -62
- package/lib/utils/DataStream.js +0 -169
- package/lib/utils/DataStream.js.map +0 -1
- package/src/utils/DataStream.ts +0 -222
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/common",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-20260414110516",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
|
-
"description": "API definitions for
|
|
8
|
+
"description": "API definitions for PowerSync",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"main": "dist/bundle.mjs",
|
|
11
11
|
"module": "dist/bundle.mjs",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
|
-
"author": "
|
|
35
|
+
"author": "PowerSync",
|
|
36
36
|
"license": "Apache-2.0",
|
|
37
37
|
"files": [
|
|
38
38
|
"lib",
|
|
@@ -48,7 +48,6 @@
|
|
|
48
48
|
},
|
|
49
49
|
"homepage": "https://docs.powersync.com",
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"async-mutex": "^0.5.0",
|
|
52
51
|
"event-iterator": "^2.0.0"
|
|
53
52
|
},
|
|
54
53
|
"devDependencies": {
|
|
@@ -58,14 +57,16 @@
|
|
|
58
57
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
59
58
|
"@types/node": "^24.0.0",
|
|
60
59
|
"@types/uuid": "^9.0.6",
|
|
60
|
+
"bson": "^6.10.4",
|
|
61
61
|
"buffer": "^6.0.3",
|
|
62
|
-
"rollup": "^4.52.5",
|
|
63
|
-
"rollup-plugin-dts": "^6.2.1",
|
|
64
62
|
"cross-fetch": "^4.1.0",
|
|
63
|
+
"estree-walker": "^3.0.3",
|
|
65
64
|
"js-logger": "^1.6.1",
|
|
65
|
+
"magic-string": "^0.30.21",
|
|
66
|
+
"rollup": "^4.52.5",
|
|
67
|
+
"rollup-plugin-dts": "^6.2.1",
|
|
66
68
|
"rsocket-core": "1.0.0-alpha.3",
|
|
67
|
-
"rsocket-websocket-client": "1.0.0-alpha.3"
|
|
68
|
-
"bson": "^6.10.4"
|
|
69
|
+
"rsocket-websocket-client": "1.0.0-alpha.3"
|
|
69
70
|
},
|
|
70
71
|
"scripts": {
|
|
71
72
|
"build": "tsc -b && rollup -c rollup.config.mjs",
|
|
@@ -51,10 +51,16 @@ export class AttachmentQueue {
|
|
|
51
51
|
/** Logger instance for diagnostic information */
|
|
52
52
|
readonly logger: ILogger;
|
|
53
53
|
|
|
54
|
-
/** Interval in milliseconds between periodic sync operations.
|
|
54
|
+
/** Interval in milliseconds between periodic sync operations. Acts as a polling timer to retry
|
|
55
|
+
* failed uploads/downloads, especially after the app goes offline. Default: 30000 (30 seconds) */
|
|
55
56
|
readonly syncIntervalMs: number = 30 * 1000;
|
|
56
57
|
|
|
57
|
-
/**
|
|
58
|
+
/** Throttle duration in milliseconds for the reactive watch query on the attachments table.
|
|
59
|
+
* When attachment records change, a watch query detects the change and triggers a sync.
|
|
60
|
+
* This throttle prevents the sync from firing too rapidly when many changes happen in
|
|
61
|
+
* quick succession (e.g., bulk inserts). This is distinct from syncIntervalMs — it controls
|
|
62
|
+
* how quickly the queue reacts to changes, while syncIntervalMs controls how often it polls
|
|
63
|
+
* for retries. Default: 30 (from DEFAULT_WATCH_THROTTLE_MS) */
|
|
58
64
|
readonly syncThrottleDuration: number;
|
|
59
65
|
|
|
60
66
|
/** Whether to automatically download remote attachments. Default: true */
|
|
@@ -86,8 +92,8 @@ export class AttachmentQueue {
|
|
|
86
92
|
* @param options.watchAttachments - Callback for monitoring attachment changes in your data model
|
|
87
93
|
* @param options.tableName - Name of the table to store attachment records. Default: 'ps_attachment_queue'
|
|
88
94
|
* @param options.logger - Logger instance. Defaults to db.logger
|
|
89
|
-
* @param options.syncIntervalMs -
|
|
90
|
-
* @param options.syncThrottleDuration - Throttle duration for
|
|
95
|
+
* @param options.syncIntervalMs - Periodic polling interval in milliseconds for retrying failed uploads/downloads. Default: 30000
|
|
96
|
+
* @param options.syncThrottleDuration - Throttle duration in milliseconds for the reactive watch query that detects attachment changes. Prevents rapid-fire syncs during bulk changes. Default: 30
|
|
91
97
|
* @param options.downloadAttachments - Whether to automatically download remote attachments. Default: true
|
|
92
98
|
* @param options.archivedCacheLimit - Maximum archived attachments before cleanup. Default: 100
|
|
93
99
|
*/
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Mutex } from 'async-mutex';
|
|
2
1
|
import { AbstractPowerSyncDatabase } from '../client/AbstractPowerSyncDatabase.js';
|
|
3
2
|
import { DifferentialWatchedQuery } from '../client/watched/processors/DifferentialQueryProcessor.js';
|
|
4
3
|
import { ILogger } from '../utils/Logger.js';
|
|
5
|
-
import {
|
|
4
|
+
import { Mutex } from '../utils/mutex.js';
|
|
6
5
|
import { AttachmentContext } from './AttachmentContext.js';
|
|
7
6
|
import { AttachmentRecord, AttachmentState } from './Schema.js';
|
|
8
7
|
|
|
@@ -55,7 +54,7 @@ export class AttachmentService {
|
|
|
55
54
|
* Executes a callback with exclusive access to the attachment context.
|
|
56
55
|
*/
|
|
57
56
|
async withContext<T>(callback: (context: AttachmentContext) => Promise<T>): Promise<T> {
|
|
58
|
-
return
|
|
57
|
+
return this.mutex.runExclusive(async () => {
|
|
59
58
|
return callback(this.context);
|
|
60
59
|
});
|
|
61
60
|
}
|
|
@@ -289,8 +289,8 @@ new AttachmentQueue(options: AttachmentQueueOptions)
|
|
|
289
289
|
| `watchAttachments` | `(onUpdate: (attachments: WatchedAttachmentItem[]) => Promise<void>) => void` | Yes | - | Callback to determine which attachments to handle by the queue from your user defined query |
|
|
290
290
|
| `tableName` | `string` | No | `'attachments'` | Name of the attachments table |
|
|
291
291
|
| `logger` | `ILogger` | No | `db.logger` | Logger instance for diagnostic output |
|
|
292
|
-
| `syncIntervalMs` | `number` | No | `30000` |
|
|
293
|
-
| `syncThrottleDuration` | `number` | No | `30` | Throttle duration for sync
|
|
292
|
+
| `syncIntervalMs` | `number` | No | `30000` | Periodic polling interval (in milliseconds) for retrying failed uploads/downloads. A `setInterval` timer that calls `syncStorage()` on this cadence, ensuring operations are retried even if no database changes occur (e.g., after coming back online). |
|
|
293
|
+
| `syncThrottleDuration` | `number` | No | `30` | Throttle duration (in milliseconds) for the reactive watch query on the attachments table. When attachment records change (e.g., a new file is queued), a watch query detects the change and triggers a sync. This throttle prevents the sync from firing too rapidly when many changes happen in quick succession (e.g., bulk inserts). This is distinct from `syncIntervalMs` — it controls how quickly the queue *reacts* to changes, while `syncIntervalMs` controls how often it *polls* for retries. |
|
|
294
294
|
| `downloadAttachments` | `boolean` | No | `true` | Whether to automatically download remote attachments |
|
|
295
295
|
| `archivedCacheLimit` | `number` | No | `100` | Maximum number of archived attachments before cleanup |
|
|
296
296
|
| `errorHandler` | `AttachmentErrorHandler` | No | `undefined` | Custom error handler for upload/download/delete operations |
|
|
@@ -676,11 +676,13 @@ Adjust sync frequency based on your needs:
|
|
|
676
676
|
```typescript
|
|
677
677
|
const queue = new AttachmentQueue({
|
|
678
678
|
// ... other options
|
|
679
|
-
syncIntervalMs: 60000, //
|
|
679
|
+
syncIntervalMs: 60000, // Poll for retries every 60 seconds instead of 30
|
|
680
|
+
syncThrottleDuration: 100, // React to attachment changes within 100ms (default: 30ms)
|
|
680
681
|
});
|
|
681
682
|
```
|
|
682
683
|
|
|
683
|
-
|
|
684
|
+
- **`syncIntervalMs`** controls the periodic polling timer — how often the queue retries failed operations.
|
|
685
|
+
- **`syncThrottleDuration`** controls how quickly the queue reacts to attachment table changes. The default (30ms) is fast enough for most use cases. Increase it if you see performance issues during bulk attachment operations.
|
|
684
686
|
|
|
685
687
|
### Archive and Cache Management
|
|
686
688
|
|
|
@@ -54,7 +54,7 @@ export class SyncingService {
|
|
|
54
54
|
updatedAttachments.push(downloaded);
|
|
55
55
|
break;
|
|
56
56
|
case AttachmentState.QUEUED_DELETE:
|
|
57
|
-
const deleted = await this.deleteAttachment(attachment);
|
|
57
|
+
const deleted = await this.deleteAttachment(attachment, context);
|
|
58
58
|
updatedAttachments.push(deleted);
|
|
59
59
|
break;
|
|
60
60
|
|
|
@@ -143,18 +143,17 @@ export class SyncingService {
|
|
|
143
143
|
* On failure, defers to error handler or archives.
|
|
144
144
|
*
|
|
145
145
|
* @param attachment - The attachment record to delete
|
|
146
|
+
* @param context - Attachment context for database operations
|
|
146
147
|
* @returns Updated attachment record
|
|
147
148
|
*/
|
|
148
|
-
async deleteAttachment(attachment: AttachmentRecord): Promise<AttachmentRecord> {
|
|
149
|
+
async deleteAttachment(attachment: AttachmentRecord, context: AttachmentContext): Promise<AttachmentRecord> {
|
|
149
150
|
try {
|
|
150
151
|
await this.remoteStorage.deleteFile(attachment);
|
|
151
152
|
if (attachment.localUri) {
|
|
152
153
|
await this.localStorage.deleteFile(attachment.localUri);
|
|
153
154
|
}
|
|
154
155
|
|
|
155
|
-
await
|
|
156
|
-
await ctx.deleteAttachment(attachment.id);
|
|
157
|
-
});
|
|
156
|
+
await context.deleteAttachment(attachment.id);
|
|
158
157
|
|
|
159
158
|
return {
|
|
160
159
|
...attachment,
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Mutex } from 'async-mutex';
|
|
2
1
|
import { EventIterator } from 'event-iterator';
|
|
3
2
|
import Logger, { ILogger } from 'js-logger';
|
|
4
3
|
import {
|
|
@@ -14,7 +13,7 @@ import { UploadQueueStats } from '../db/crud/UploadQueueStatus.js';
|
|
|
14
13
|
import { Schema } from '../db/schema/Schema.js';
|
|
15
14
|
import { BaseObserver } from '../utils/BaseObserver.js';
|
|
16
15
|
import { ControlledExecutor } from '../utils/ControlledExecutor.js';
|
|
17
|
-
import {
|
|
16
|
+
import { throttleTrailing } from '../utils/async.js';
|
|
18
17
|
import {
|
|
19
18
|
ConnectionManager,
|
|
20
19
|
CreateSyncImplementationOptions,
|
|
@@ -46,6 +45,7 @@ import { TriggerManagerImpl } from './triggers/TriggerManagerImpl.js';
|
|
|
46
45
|
import { DEFAULT_WATCH_THROTTLE_MS, WatchCompatibleQuery } from './watched/WatchedQuery.js';
|
|
47
46
|
import { OnChangeQueryProcessor } from './watched/processors/OnChangeQueryProcessor.js';
|
|
48
47
|
import { WatchedQueryComparator } from './watched/processors/comparators.js';
|
|
48
|
+
import { Mutex } from '../utils/mutex.js';
|
|
49
49
|
|
|
50
50
|
export interface DisconnectAndClearOptions {
|
|
51
51
|
/** When set to false, data in local-only tables is preserved. */
|
|
@@ -704,7 +704,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
|
|
|
704
704
|
* @returns A transaction of CRUD operations to upload, or null if there are none
|
|
705
705
|
*/
|
|
706
706
|
async getNextCrudTransaction(): Promise<CrudTransaction | null> {
|
|
707
|
-
const iterator = this.getCrudTransactions()[
|
|
707
|
+
const iterator = this.getCrudTransactions()[Symbol.asyncIterator]();
|
|
708
708
|
return (await iterator.next()).value;
|
|
709
709
|
}
|
|
710
710
|
|
|
@@ -741,7 +741,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
|
|
|
741
741
|
*/
|
|
742
742
|
getCrudTransactions(): AsyncIterable<CrudTransaction, null> {
|
|
743
743
|
return {
|
|
744
|
-
[
|
|
744
|
+
[Symbol.asyncIterator]: () => {
|
|
745
745
|
let lastCrudItemId = -1;
|
|
746
746
|
const sql = `
|
|
747
747
|
WITH RECURSIVE crud_entries AS (
|
|
@@ -813,6 +813,10 @@ SELECT * FROM crud_entries;
|
|
|
813
813
|
* Execute a SQL write (INSERT/UPDATE/DELETE) query
|
|
814
814
|
* and optionally return results.
|
|
815
815
|
*
|
|
816
|
+
* When using the default client-side [JSON-based view system](https://docs.powersync.com/architecture/client-architecture#client-side-schema-and-sqlite-database-structure),
|
|
817
|
+
* the returned result's `rowsAffected` may be `0` for successful `UPDATE` and `DELETE` statements.
|
|
818
|
+
* Use a `RETURNING` clause and inspect `result.rows` when you need to confirm which rows changed.
|
|
819
|
+
*
|
|
816
820
|
* @param sql The SQL query to execute
|
|
817
821
|
* @param parameters Optional array of parameters to bind to the query
|
|
818
822
|
* @returns The query result as an object with structured key-value pairs
|
|
@@ -921,7 +925,7 @@ SELECT * FROM crud_entries;
|
|
|
921
925
|
await this.waitForReady();
|
|
922
926
|
return this.database.readTransaction(
|
|
923
927
|
async (tx) => {
|
|
924
|
-
const res = await callback(
|
|
928
|
+
const res = await callback(tx);
|
|
925
929
|
await tx.rollback();
|
|
926
930
|
return res;
|
|
927
931
|
},
|