@powersync/common 2.0.0 → 2.2.0
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/lib/attachments/AttachmentQueue.d.ts +79 -41
- package/lib/attachments/AttachmentQueue.js +47 -11
- package/lib/attachments/AttachmentQueue.js.map +1 -1
- package/lib/attachments/AttachmentTransportAdapter.d.ts +47 -0
- package/lib/attachments/AttachmentTransportAdapter.js +2 -0
- package/lib/attachments/AttachmentTransportAdapter.js.map +1 -0
- package/lib/attachments/BufferedAttachmentTransport.d.ts +24 -0
- package/lib/attachments/BufferedAttachmentTransport.js +32 -0
- package/lib/attachments/BufferedAttachmentTransport.js.map +1 -0
- package/lib/attachments/LocalStorageAdapter.d.ts +19 -0
- package/lib/attachments/SyncingService.d.ts +7 -4
- package/lib/attachments/SyncingService.js +10 -9
- package/lib/attachments/SyncingService.js.map +1 -1
- package/lib/client/CommonPowerSyncDatabase.d.ts +14 -1
- package/lib/client/connection/PowerSyncBackendConnector.d.ts +11 -0
- package/lib/client/sync/CheckpointRequest.d.ts +32 -0
- package/lib/client/sync/CheckpointRequest.js +2 -0
- package/lib/client/sync/CheckpointRequest.js.map +1 -0
- package/lib/client/sync/options.d.ts +34 -0
- package/lib/client/sync/options.js.map +1 -1
- package/lib/index.d.ts +3 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/package.json +4 -3
- package/src/attachments/AttachmentQueue.ts +114 -56
- package/src/attachments/AttachmentTransportAdapter.ts +49 -0
- package/src/attachments/BufferedAttachmentTransport.ts +37 -0
- package/src/attachments/LocalStorageAdapter.ts +20 -0
- package/src/attachments/SyncingService.ts +11 -11
- package/src/client/CommonPowerSyncDatabase.ts +15 -1
- package/src/client/connection/PowerSyncBackendConnector.ts +12 -0
- package/src/client/sync/CheckpointRequest.ts +31 -0
- package/src/client/sync/options.ts +35 -0
- package/src/index.ts +9 -1
|
@@ -23,4 +23,15 @@ export interface PowerSyncBackendConnector {
|
|
|
23
23
|
* Any thrown errors will result in a retry after the configured wait period (default: 5 seconds).
|
|
24
24
|
*/
|
|
25
25
|
uploadData: (database: CommonPowerSyncDatabase) => Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Posts a client-generated checkpoint request to the backend and returns the effective checkpoint request state.
|
|
28
|
+
*
|
|
29
|
+
* This method is optional. It only needs to be implemented when the selected {@link CheckpointMode} is `requests`
|
|
30
|
+
* and [asynchronous backend uploads](https://docs.powersync.com/client-sdks/advanced/checkpoint-requests#asynchronous-upload-backends)
|
|
31
|
+
* are used. In any other case, this method should not be present on backend connectors.
|
|
32
|
+
*
|
|
33
|
+
* @param requestId - The client-generated checkpoint request ID (a positive 64-bit integer encoded as a string).
|
|
34
|
+
* @param clientId - The PowerSync client ID for the current device.
|
|
35
|
+
*/
|
|
36
|
+
postCheckpointRequest?(clientId: string, requestId: string): Promise<string>;
|
|
26
37
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A checkpoint request created by {@link CommonPowerSyncDatabase#requestCheckpoint}.
|
|
3
|
+
*
|
|
4
|
+
* Use this value to wait until the local database has applied server-side changes up to the requested checkpoint. This
|
|
5
|
+
* is useful for explicit refresh flows where the caller wants confirmation that the local view has caught up to the
|
|
6
|
+
* service.
|
|
7
|
+
*
|
|
8
|
+
* Checkpoint requests are backed by request ids tracked in the local database, so they are reusable across disconnect
|
|
9
|
+
* and reconnect cycles. A wait interrupted by a disconnect throws an error, but the same request can be awaited again
|
|
10
|
+
* once a new connection is established.
|
|
11
|
+
*
|
|
12
|
+
* Requests do not survive {@link CommonPowerSyncDatabase#disconnectAndClear}, instances created before a clear should
|
|
13
|
+
* be discarded and requested again.
|
|
14
|
+
*
|
|
15
|
+
* @alpha
|
|
16
|
+
*/
|
|
17
|
+
export interface CheckpointRequest {
|
|
18
|
+
/**
|
|
19
|
+
* Whether this checkpoint request has synced before.
|
|
20
|
+
*/
|
|
21
|
+
readonly hasSynced: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Waits until this checkpoint has been synced locally.
|
|
24
|
+
*
|
|
25
|
+
* This method fails on sync errors: If a download or upload error occurs before this checkpoint request has synced,
|
|
26
|
+
* that error is rethrown here. This makes it easier to observe sync errors when relying on checkpoints. Once sync has
|
|
27
|
+
* recovered, it is valid to call this method again to await the checkpoint.
|
|
28
|
+
*/
|
|
29
|
+
waitForSync(options?: {
|
|
30
|
+
signal?: AbortSignal;
|
|
31
|
+
}): Promise<void>;
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CheckpointRequest.js","sourceRoot":"","sources":["../../../src/client/sync/CheckpointRequest.ts"],"names":[],"mappings":""}
|
|
@@ -40,6 +40,10 @@ export interface SyncOptions {
|
|
|
40
40
|
* milliseconds.
|
|
41
41
|
*/
|
|
42
42
|
crudUploadThrottleMs?: number;
|
|
43
|
+
/**
|
|
44
|
+
* The mode used to request checkpoints from the service (used after uploading local data).
|
|
45
|
+
*/
|
|
46
|
+
checkpointMode?: CheckpointMode;
|
|
43
47
|
}
|
|
44
48
|
/**
|
|
45
49
|
* @public
|
|
@@ -63,3 +67,33 @@ export declare enum FetchStrategy {
|
|
|
63
67
|
*/
|
|
64
68
|
Sequential = "sequential"
|
|
65
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* The mechanism to request checkpoints from the PowerSync service.
|
|
72
|
+
*
|
|
73
|
+
* Checkpoint requests are used after a client uploads local mutations. The PowerSync service later references them in
|
|
74
|
+
* downloaded data, allowing the SDK to assume that uploaded data has been synced down again.
|
|
75
|
+
*
|
|
76
|
+
* There are two ways to send checkpoint requests: A legacy (but default and stable) format supported by all PowerSync
|
|
77
|
+
* service versions, and a newer (`requests`) method which is only available from PowerSync service version 1.24.0 or
|
|
78
|
+
* later.
|
|
79
|
+
*
|
|
80
|
+
* Note that the requests checkpoint mode is an alpha API.
|
|
81
|
+
*
|
|
82
|
+
* @public
|
|
83
|
+
*/
|
|
84
|
+
export type CheckpointMode = 'legacy' | 'requests' | {
|
|
85
|
+
requests: CheckpointRequestsOptions;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Options associated with a {@link CheckpointMode} when the requests-based checkpoint option is used.
|
|
89
|
+
*
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
export interface CheckpointRequestsOptions {
|
|
93
|
+
/**
|
|
94
|
+
* The delay, in milliseconds, to wait before re-sending a checkpoint request when it hasn't been applied in time.
|
|
95
|
+
*
|
|
96
|
+
* The minimum value for this is 10 seconds, lower values will be ignored.
|
|
97
|
+
*/
|
|
98
|
+
retryDelay: number;
|
|
99
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.js","sourceRoot":"","sources":["../../../src/client/sync/options.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"options.js","sourceRoot":"","sources":["../../../src/client/sync/options.ts"],"names":[],"mappings":"AA0DA;;GAEG;AACH,MAAM,CAAN,IAAY,0BAGX;AAHD,WAAY,0BAA0B;IACpC,2CAAa,CAAA;IACb,uDAAyB,CAAA;AAC3B,CAAC,EAHW,0BAA0B,KAA1B,0BAA0B,QAGrC;AAED;;GAEG;AACH,MAAM,CAAN,IAAY,aAYX;AAZD,WAAY,aAAa;IACvB;;;OAGG;IACH,sCAAqB,CAAA;IAErB;;;OAGG;IACH,0CAAyB,CAAA;AAC3B,CAAC,EAZW,aAAa,KAAb,aAAa,QAYxB"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './attachments/AttachmentContext.js';
|
|
2
2
|
export * from './attachments/AttachmentErrorHandler.js';
|
|
3
3
|
export * from './attachments/AttachmentQueue.js';
|
|
4
|
+
export * from './attachments/AttachmentTransportAdapter.js';
|
|
4
5
|
export * from './attachments/LocalStorageAdapter.js';
|
|
5
6
|
export * from './attachments/RemoteStorageAdapter.js';
|
|
6
7
|
export * from './attachments/Schema.js';
|
|
@@ -13,9 +14,10 @@ export * from './client/SQLOpenFactory.js';
|
|
|
13
14
|
export * from './client/sync/bucket/CrudBatch.js';
|
|
14
15
|
export { CrudEntry, OpId, UpdateType } from './client/sync/bucket/CrudEntry.js';
|
|
15
16
|
export * from './client/sync/bucket/CrudTransaction.js';
|
|
17
|
+
export * from './client/sync/CheckpointRequest.js';
|
|
16
18
|
export * from './client/sync/stream/JsonValue.js';
|
|
17
19
|
export * from './client/sync/sync-streams.js';
|
|
18
|
-
export { SyncOptions, SyncStreamConnectionMethod, FetchStrategy } from './client/sync/options.js';
|
|
20
|
+
export { SyncOptions, SyncStreamConnectionMethod, FetchStrategy, CheckpointMode, CheckpointRequestsOptions } from './client/sync/options.js';
|
|
19
21
|
export { ProgressWithOperations, SyncProgress } from './db/crud/SyncProgress.js';
|
|
20
22
|
export * from './db/crud/SyncStatus.js';
|
|
21
23
|
export * from './db/crud/UploadQueueStatus.js';
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './attachments/AttachmentContext.js';
|
|
2
2
|
export * from './attachments/AttachmentErrorHandler.js';
|
|
3
3
|
export * from './attachments/AttachmentQueue.js';
|
|
4
|
+
export * from './attachments/AttachmentTransportAdapter.js';
|
|
4
5
|
export * from './attachments/LocalStorageAdapter.js';
|
|
5
6
|
export * from './attachments/RemoteStorageAdapter.js';
|
|
6
7
|
export * from './attachments/Schema.js';
|
|
@@ -13,6 +14,7 @@ export * from './client/SQLOpenFactory.js';
|
|
|
13
14
|
export * from './client/sync/bucket/CrudBatch.js';
|
|
14
15
|
export { UpdateType } from './client/sync/bucket/CrudEntry.js';
|
|
15
16
|
export * from './client/sync/bucket/CrudTransaction.js';
|
|
17
|
+
export * from './client/sync/CheckpointRequest.js';
|
|
16
18
|
export * from './client/sync/stream/JsonValue.js';
|
|
17
19
|
export * from './client/sync/sync-streams.js';
|
|
18
20
|
export { SyncStreamConnectionMethod, FetchStrategy } from './client/sync/options.js';
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAC;AACnD,cAAc,yCAAyC,CAAC;AACxD,cAAc,kCAAkC,CAAC;AACjD,cAAc,sCAAsC,CAAC;AACrD,cAAc,uCAAuC,CAAC;AACtD,cAAc,yBAAyB,CAAC;AACxC,cAAc,wCAAwC,CAAC;AAEvD,cAAc,qCAAqC,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAA+B,MAAM,kCAAkC,CAAC;AACrG,cAAc,kDAAkD,CAAC;AACjE,cAAc,6CAA6C,CAAC;AAC5D,cAAc,4BAA4B,CAAC;AAC3C,cAAc,mCAAmC,CAAC;AAClD,OAAO,EAAmB,UAAU,EAAE,MAAM,mCAAmC,CAAC;AAChF,cAAc,yCAAyC,CAAC;AACxD,cAAc,mCAAmC,CAAC;AAClD,cAAc,+BAA+B,CAAC;AAC9C,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAC;AACnD,cAAc,yCAAyC,CAAC;AACxD,cAAc,kCAAkC,CAAC;AACjD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,sCAAsC,CAAC;AACrD,cAAc,uCAAuC,CAAC;AACtD,cAAc,yBAAyB,CAAC;AACxC,cAAc,wCAAwC,CAAC;AAEvD,cAAc,qCAAqC,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAA+B,MAAM,kCAAkC,CAAC;AACrG,cAAc,kDAAkD,CAAC;AACjE,cAAc,6CAA6C,CAAC;AAC5D,cAAc,4BAA4B,CAAC;AAC3C,cAAc,mCAAmC,CAAC;AAClD,OAAO,EAAmB,UAAU,EAAE,MAAM,mCAAmC,CAAC;AAChF,cAAc,yCAAyC,CAAC;AACxD,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,+BAA+B,CAAC;AAC9C,OAAO,EAEL,0BAA0B,EAC1B,aAAa,EAGd,MAAM,0BAA0B,CAAC;AAGlC,cAAc,yBAAyB,CAAC;AACxC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,8BAA8B,CAAC;AAE7C,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AAErC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kCAAkC,CAAC;AACjD,cAAc,qCAAqC,CAAC;AACpD,cAAc,iCAAiC,CAAC;AAChD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,2DAA2D,CAAC;AAC1E,cAAc,uDAAuD,CAAC;AACtE,cAAc,kCAAkC,CAAC;AAGjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mBAAmB,CAAC;AAElC,cAAc,kBAAkB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/common",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -32,13 +32,14 @@
|
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/node": "^24.0.0",
|
|
34
34
|
"@types/uuid": "^9.0.6",
|
|
35
|
-
"@microsoft/api-extractor": "^7.
|
|
35
|
+
"@microsoft/api-extractor": "^7.59.0"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "tsc -b",
|
|
39
39
|
"build:prod": "tsc -b",
|
|
40
40
|
"clean": "rm -rf lib dist tsconfig.tsbuildinfo",
|
|
41
41
|
"test": "vitest",
|
|
42
|
-
"test:exports": "attw --pack . --ignore-rules no-resolution cjs-resolves-to-esm"
|
|
42
|
+
"test:exports": "attw --pack . --ignore-rules no-resolution cjs-resolves-to-esm",
|
|
43
|
+
"api-diff": "api-extractor run --verbose"
|
|
43
44
|
}
|
|
44
45
|
}
|
|
@@ -5,7 +5,9 @@ import { Transaction } from '../db/DBAdapter.js';
|
|
|
5
5
|
import { AttachmentContext } from './AttachmentContext.js';
|
|
6
6
|
import { AttachmentErrorHandler } from './AttachmentErrorHandler.js';
|
|
7
7
|
import { AttachmentService } from './AttachmentService.js';
|
|
8
|
-
import {
|
|
8
|
+
import { AttachmentTransportAdapter } from './AttachmentTransportAdapter.js';
|
|
9
|
+
import { BufferedAttachmentTransport } from './BufferedAttachmentTransport.js';
|
|
10
|
+
import { AttachmentData, LocalStorageAdapter, StreamingLocalStorageAdapter } from './LocalStorageAdapter.js';
|
|
9
11
|
import { RemoteStorageAdapter } from './RemoteStorageAdapter.js';
|
|
10
12
|
import { ATTACHMENT_TABLE, AttachmentRecord, AttachmentState } from './Schema.js';
|
|
11
13
|
import { SyncingService } from './SyncingService.js';
|
|
@@ -13,24 +15,22 @@ import { WatchedAttachmentItem } from './WatchedAttachmentItem.js';
|
|
|
13
15
|
import { CommonPowerSyncDatabase } from '../client/CommonPowerSyncDatabase.js';
|
|
14
16
|
|
|
15
17
|
/**
|
|
16
|
-
*
|
|
18
|
+
* Fields common to every {@link AttachmentQueueOptions} variant.
|
|
17
19
|
*
|
|
18
20
|
* @experimental
|
|
19
21
|
* @alpha This is currently experimental and may change without a major version bump.
|
|
20
22
|
*/
|
|
21
|
-
export interface
|
|
23
|
+
export interface BaseAttachmentQueueOptions<TLocal extends LocalStorageAdapter = LocalStorageAdapter> {
|
|
22
24
|
/**
|
|
23
25
|
* PowerSync database instance
|
|
24
26
|
*/
|
|
25
27
|
db: CommonPowerSyncDatabase;
|
|
26
28
|
/**
|
|
27
|
-
*
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Local storage adapter for file persistence
|
|
29
|
+
* Local storage adapter for file persistence. Its type determines whether
|
|
30
|
+
* {@link AttachmentQueue.saveFileFromUri} is available (a
|
|
31
|
+
* {@link StreamingLocalStorageAdapter} enables it).
|
|
32
32
|
*/
|
|
33
|
-
localStorage:
|
|
33
|
+
localStorage: TLocal;
|
|
34
34
|
/**
|
|
35
35
|
* Callback for monitoring attachment changes in your data model
|
|
36
36
|
*/
|
|
@@ -64,6 +64,52 @@ export interface AttachmentQueueOptions {
|
|
|
64
64
|
errorHandler?: AttachmentErrorHandler;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Configuration options for {@link AttachmentQueue}.
|
|
69
|
+
*
|
|
70
|
+
* Provide **exactly one** remote mechanism:
|
|
71
|
+
* - `remoteStorage` — a {@link RemoteStorageAdapter}, wrapped in the default
|
|
72
|
+
* default buffered transport that delegates upload/download/delete to it.
|
|
73
|
+
* - `transportAdapter` — an {@link AttachmentTransportAdapter} that owns all remote
|
|
74
|
+
* operations directly (e.g. a native file-URI implementation for buffer-free
|
|
75
|
+
* transfer of large files). No `remoteStorage` is needed in this case.
|
|
76
|
+
*
|
|
77
|
+
* Supplying both, or neither, is a type error.
|
|
78
|
+
*
|
|
79
|
+
* @experimental
|
|
80
|
+
* @alpha This is currently experimental and may change without a major version bump.
|
|
81
|
+
*/
|
|
82
|
+
export type AttachmentQueueOptions<TLocal extends LocalStorageAdapter = LocalStorageAdapter> =
|
|
83
|
+
BaseAttachmentQueueOptions<TLocal> &
|
|
84
|
+
(
|
|
85
|
+
| { remoteStorage: RemoteStorageAdapter; transportAdapter?: never }
|
|
86
|
+
| { transportAdapter: AttachmentTransportAdapter; remoteStorage?: never }
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Fields shared by {@link AttachmentQueue.saveFile} and {@link AttachmentQueue.saveFileFromUri}.
|
|
91
|
+
*
|
|
92
|
+
* @alpha
|
|
93
|
+
*/
|
|
94
|
+
export interface SaveAttachmentOptions {
|
|
95
|
+
/** File extension (e.g., 'jpg', 'pdf') */
|
|
96
|
+
fileExtension: string;
|
|
97
|
+
/** MIME type of the file (e.g., 'image/jpeg') */
|
|
98
|
+
mediaType?: string;
|
|
99
|
+
/** Optional metadata to associate with the attachment */
|
|
100
|
+
metaData?: string;
|
|
101
|
+
/** Optional custom ID. If not provided, a UUID will be generated */
|
|
102
|
+
id?: string;
|
|
103
|
+
/**
|
|
104
|
+
* Optional callback to execute additional database operations within the same transaction as the
|
|
105
|
+
* attachment creation.
|
|
106
|
+
*/
|
|
107
|
+
updateHook?: (transaction: Transaction, attachment: AttachmentRecord) => Promise<void>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** How the file bytes reach managed storage when creating an upload attachment. */
|
|
111
|
+
type AttachmentSource = { kind: 'data'; data: AttachmentData } | { kind: 'uri'; localUri: string };
|
|
112
|
+
|
|
67
113
|
/**
|
|
68
114
|
* AttachmentQueue manages the lifecycle and synchronization of attachments
|
|
69
115
|
* between local and remote storage.
|
|
@@ -73,7 +119,7 @@ export interface AttachmentQueueOptions {
|
|
|
73
119
|
* @experimental
|
|
74
120
|
* @alpha This is currently experimental and may change without a major version bump.
|
|
75
121
|
*/
|
|
76
|
-
export class AttachmentQueue {
|
|
122
|
+
export class AttachmentQueue<TLocal extends LocalStorageAdapter = LocalStorageAdapter> {
|
|
77
123
|
/** Timer for periodic synchronization operations */
|
|
78
124
|
private periodicSyncTimer?: ReturnType<typeof setInterval>;
|
|
79
125
|
|
|
@@ -81,10 +127,7 @@ export class AttachmentQueue {
|
|
|
81
127
|
private readonly syncingService: SyncingService;
|
|
82
128
|
|
|
83
129
|
/** Adapter for local file storage operations */
|
|
84
|
-
readonly localStorage:
|
|
85
|
-
|
|
86
|
-
/** Adapter for remote file storage operations */
|
|
87
|
-
readonly remoteStorage: RemoteStorageAdapter;
|
|
130
|
+
readonly localStorage: TLocal;
|
|
88
131
|
|
|
89
132
|
/**
|
|
90
133
|
* Callback function to watch for changes in attachment references in your data model.
|
|
@@ -159,6 +202,7 @@ export class AttachmentQueue {
|
|
|
159
202
|
db,
|
|
160
203
|
localStorage,
|
|
161
204
|
remoteStorage,
|
|
205
|
+
transportAdapter,
|
|
162
206
|
watchAttachments,
|
|
163
207
|
logger,
|
|
164
208
|
tableName = ATTACHMENT_TABLE,
|
|
@@ -167,10 +211,9 @@ export class AttachmentQueue {
|
|
|
167
211
|
downloadAttachments = true,
|
|
168
212
|
archivedCacheLimit = 100,
|
|
169
213
|
errorHandler
|
|
170
|
-
}: AttachmentQueueOptions) {
|
|
214
|
+
}: AttachmentQueueOptions<TLocal>) {
|
|
171
215
|
this.db = db;
|
|
172
216
|
this.syncLoopMutex = db.createMutex();
|
|
173
|
-
this.remoteStorage = remoteStorage;
|
|
174
217
|
this.localStorage = localStorage;
|
|
175
218
|
this.watchAttachments = watchAttachments;
|
|
176
219
|
this.tableName = tableName;
|
|
@@ -180,10 +223,13 @@ export class AttachmentQueue {
|
|
|
180
223
|
this.downloadAttachments = downloadAttachments;
|
|
181
224
|
this.logger = logger ?? db.logger;
|
|
182
225
|
this.attachmentService = new AttachmentService(db, this.logger, tableName, archivedCacheLimit);
|
|
226
|
+
|
|
227
|
+
const transport = transportAdapter ?? new BufferedAttachmentTransport(localStorage, remoteStorage!);
|
|
228
|
+
|
|
183
229
|
this.syncingService = new SyncingService(
|
|
184
230
|
this.attachmentService,
|
|
185
231
|
localStorage,
|
|
186
|
-
|
|
232
|
+
transport,
|
|
187
233
|
this.logger,
|
|
188
234
|
errorHandler
|
|
189
235
|
);
|
|
@@ -408,49 +454,30 @@ export class AttachmentQueue {
|
|
|
408
454
|
return this.attachmentService.withContext(callback);
|
|
409
455
|
}
|
|
410
456
|
/**
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
* @returns Promise resolving to the created attachment record
|
|
457
|
+
* Creates a `QUEUED_UPLOAD` attachment record, placing the file at the managed
|
|
458
|
+
* `localUri` from the given `source`, and persists the record
|
|
459
|
+
* alongside the caller's `updateHook` in a single transaction.
|
|
415
460
|
*/
|
|
416
|
-
async
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
metaData,
|
|
421
|
-
id,
|
|
422
|
-
updateHook
|
|
423
|
-
}: {
|
|
424
|
-
/**
|
|
425
|
-
* The file data as ArrayBuffer, Blob, or base64 string
|
|
426
|
-
*/
|
|
427
|
-
data: AttachmentData;
|
|
428
|
-
/**
|
|
429
|
-
* File extension (e.g., 'jpg', 'pdf')
|
|
430
|
-
*/
|
|
431
|
-
fileExtension: string;
|
|
432
|
-
/**
|
|
433
|
-
* MIME type of the file (e.g., 'image/jpeg')
|
|
434
|
-
*/
|
|
435
|
-
mediaType?: string;
|
|
436
|
-
/**
|
|
437
|
-
* Optional metadata to associate with the attachment
|
|
438
|
-
*/
|
|
439
|
-
metaData?: string;
|
|
440
|
-
/**
|
|
441
|
-
* Optional custom ID. If not provided, a UUID will be generated
|
|
442
|
-
*/
|
|
443
|
-
id?: string;
|
|
444
|
-
/**
|
|
445
|
-
* Optional callback to execute additional database operations within the same transaction as the attachment
|
|
446
|
-
* creation.
|
|
447
|
-
*/
|
|
448
|
-
updateHook?: (transaction: Transaction, attachment: AttachmentRecord) => Promise<void>;
|
|
449
|
-
}): Promise<AttachmentRecord> {
|
|
461
|
+
private async createUploadAttachment(
|
|
462
|
+
{ fileExtension, mediaType, metaData, id, updateHook }: SaveAttachmentOptions,
|
|
463
|
+
source: AttachmentSource
|
|
464
|
+
): Promise<AttachmentRecord> {
|
|
450
465
|
const resolvedId = id ?? (await this.generateAttachmentId());
|
|
451
466
|
const filename = `${resolvedId}.${fileExtension}`;
|
|
452
467
|
const localUri = this.localStorage.getLocalUri(filename);
|
|
453
|
-
|
|
468
|
+
|
|
469
|
+
let size: number;
|
|
470
|
+
if (source.kind === 'data') {
|
|
471
|
+
size = await this.localStorage.saveFile(localUri, source.data);
|
|
472
|
+
} else {
|
|
473
|
+
// saveFileFromUri is only exposed for streaming-capable local adapters; guard at
|
|
474
|
+
// runtime too for plain-JS callers.
|
|
475
|
+
const localStorage = this.localStorage as Partial<StreamingLocalStorageAdapter>;
|
|
476
|
+
if (!localStorage.moveFile) {
|
|
477
|
+
throw new Error('The configured local storage adapter does not support moveFile, required by saveFileFromUri.');
|
|
478
|
+
}
|
|
479
|
+
size = await localStorage.moveFile(source.localUri, localUri);
|
|
480
|
+
}
|
|
454
481
|
|
|
455
482
|
const attachment: AttachmentRecord = {
|
|
456
483
|
id: resolvedId,
|
|
@@ -474,6 +501,37 @@ export class AttachmentQueue {
|
|
|
474
501
|
return attachment;
|
|
475
502
|
}
|
|
476
503
|
|
|
504
|
+
/**
|
|
505
|
+
* Saves in-memory file data to local storage and queues it for upload.
|
|
506
|
+
*
|
|
507
|
+
* @param options - File data plus {@link SaveAttachmentOptions}
|
|
508
|
+
* @returns Promise resolving to the created attachment record
|
|
509
|
+
*/
|
|
510
|
+
async saveFile(options: SaveAttachmentOptions & { data: AttachmentData }): Promise<AttachmentRecord> {
|
|
511
|
+
return this.createUploadAttachment(options, { kind: 'data', data: options.data });
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Registers a file that already exists on disk and queues it for upload, moving it
|
|
516
|
+
* into managed storage without loading it into memory.
|
|
517
|
+
*
|
|
518
|
+
* Prefer this over {@link AttachmentQueue.saveFile} for large, app-originated files
|
|
519
|
+
* (recordings, videos): it avoids reading the file into an `ArrayBuffer` just to write
|
|
520
|
+
* it back to disk. Requires the local storage adapter to implement `moveFile`.
|
|
521
|
+
*
|
|
522
|
+
* Only available when the queue is configured with a {@link StreamingLocalStorageAdapter}
|
|
523
|
+
* (one that implements `moveFile`).
|
|
524
|
+
*
|
|
525
|
+
* @param options - The existing file's `localUri` plus {@link SaveAttachmentOptions}
|
|
526
|
+
* @returns Promise resolving to the created attachment record
|
|
527
|
+
*/
|
|
528
|
+
async saveFileFromUri(
|
|
529
|
+
this: AttachmentQueue<StreamingLocalStorageAdapter>,
|
|
530
|
+
options: SaveAttachmentOptions & { localUri: string }
|
|
531
|
+
): Promise<AttachmentRecord> {
|
|
532
|
+
return this.createUploadAttachment(options, { kind: 'uri', localUri: options.localUri });
|
|
533
|
+
}
|
|
534
|
+
|
|
477
535
|
async deleteFile({
|
|
478
536
|
id,
|
|
479
537
|
updateHook
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { AttachmentRecord } from './Schema.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An {@link AttachmentRecord} that is guaranteed to have a `localUri`.
|
|
5
|
+
*
|
|
6
|
+
* The syncing service assigns `localUri` before invoking a transport download,
|
|
7
|
+
* so implementations always receive both the metadata and the destination path.
|
|
8
|
+
*
|
|
9
|
+
* @alpha
|
|
10
|
+
*/
|
|
11
|
+
export type LocatedAttachmentRecord = AttachmentRecord & { localUri: string };
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* AttachmentTransportAdapter owns all remote-side operations for an attachment —
|
|
15
|
+
* transfer (upload/download) and delete — as single operations.
|
|
16
|
+
*
|
|
17
|
+
* A transport owns the entire transfer, letting implementations pick the most
|
|
18
|
+
* efficient mechanism available (buffer, stream, or a platform-native file-URI
|
|
19
|
+
* upload/download API). On platforms like React Native this allows large files to
|
|
20
|
+
* be transferred without ever materializing them in the JS heap.
|
|
21
|
+
*
|
|
22
|
+
* The default transport composes the local and remote storage adapters. Provide a custom transport (via
|
|
23
|
+
* `AttachmentQueue`'s `transportAdapter` option) to own the whole remote side; in
|
|
24
|
+
* that case a separate `remoteStorage` is not required.
|
|
25
|
+
*
|
|
26
|
+
* @experimental
|
|
27
|
+
* @alpha This is currently experimental and may change without a major version bump.
|
|
28
|
+
*/
|
|
29
|
+
export interface AttachmentTransportAdapter {
|
|
30
|
+
/**
|
|
31
|
+
* Uploads the attachment's local file to remote storage.
|
|
32
|
+
* @param attachment - The attachment to upload. `localUri` points at the source file.
|
|
33
|
+
*/
|
|
34
|
+
upload(attachment: LocatedAttachmentRecord): Promise<void>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Downloads the remote file into `attachment.localUri`.
|
|
38
|
+
* @param attachment - The attachment to download. `localUri` is the destination path,
|
|
39
|
+
* assigned by the syncing service before this call.
|
|
40
|
+
*/
|
|
41
|
+
download(attachment: LocatedAttachmentRecord): Promise<void>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Deletes the attachment's file from remote storage. Local file removal is handled
|
|
45
|
+
* separately by the syncing service.
|
|
46
|
+
* @param attachment - The attachment to delete.
|
|
47
|
+
*/
|
|
48
|
+
delete(attachment: AttachmentRecord): Promise<void>;
|
|
49
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { AttachmentTransportAdapter, LocatedAttachmentRecord } from './AttachmentTransportAdapter.js';
|
|
2
|
+
import { LocalStorageAdapter } from './LocalStorageAdapter.js';
|
|
3
|
+
import { RemoteStorageAdapter } from './RemoteStorageAdapter.js';
|
|
4
|
+
import { AttachmentRecord } from './Schema.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Default {@link AttachmentTransportAdapter}, composing the local and remote
|
|
8
|
+
* storage adapters.
|
|
9
|
+
*
|
|
10
|
+
* The full file body is materialized as an `ArrayBuffer` in JS memory between the
|
|
11
|
+
* two adapter calls. This is fine for small files but can cause memory pressure on
|
|
12
|
+
* large ones — environments that support native transfer should provide a custom
|
|
13
|
+
* transport instead.
|
|
14
|
+
*
|
|
15
|
+
* @experimental
|
|
16
|
+
* @alpha This is currently experimental and may change without a major version bump.
|
|
17
|
+
*/
|
|
18
|
+
export class BufferedAttachmentTransport implements AttachmentTransportAdapter {
|
|
19
|
+
constructor(
|
|
20
|
+
private localStorage: LocalStorageAdapter,
|
|
21
|
+
private remoteStorage: RemoteStorageAdapter
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
async upload(attachment: LocatedAttachmentRecord): Promise<void> {
|
|
25
|
+
const fileData = await this.localStorage.readFile(attachment.localUri);
|
|
26
|
+
await this.remoteStorage.uploadFile(fileData, attachment);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async download(attachment: LocatedAttachmentRecord): Promise<void> {
|
|
30
|
+
const fileData = await this.remoteStorage.downloadFile(attachment);
|
|
31
|
+
await this.localStorage.saveFile(attachment.localUri, fileData);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async delete(attachment: AttachmentRecord): Promise<void> {
|
|
35
|
+
await this.remoteStorage.deleteFile(attachment);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -76,3 +76,23 @@ export interface LocalStorageAdapter {
|
|
|
76
76
|
*/
|
|
77
77
|
getLocalUri(filename: string): string;
|
|
78
78
|
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A {@link LocalStorageAdapter} that can relocate a file into managed storage without
|
|
82
|
+
* loading it into memory. Required for {@link AttachmentQueue.saveFileFromUri}; only
|
|
83
|
+
* queues configured with a streaming-capable local adapter expose that method.
|
|
84
|
+
*
|
|
85
|
+
* @experimental
|
|
86
|
+
* @alpha This is currently experimental and may change without a major version bump.
|
|
87
|
+
*/
|
|
88
|
+
export interface StreamingLocalStorageAdapter extends LocalStorageAdapter {
|
|
89
|
+
/**
|
|
90
|
+
* Moves a file into managed storage without loading it into memory.
|
|
91
|
+
* Overwrites any existing file at the target. When source and target are the same
|
|
92
|
+
* path, this is a no-op that just reports the size.
|
|
93
|
+
* @param sourceUri - Path of the existing file
|
|
94
|
+
* @param targetUri - Destination path within managed storage
|
|
95
|
+
* @returns Number of bytes in the moved file
|
|
96
|
+
*/
|
|
97
|
+
moveFile(sourceUri: string, targetUri: string): Promise<number>;
|
|
98
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { LogLevels, PowerSyncLogger } from '../utils/Logger.js';
|
|
2
2
|
import { AttachmentService } from './AttachmentService.js';
|
|
3
|
+
import { AttachmentTransportAdapter } from './AttachmentTransportAdapter.js';
|
|
3
4
|
import { LocalStorageAdapter } from './LocalStorageAdapter.js';
|
|
4
|
-
import { RemoteStorageAdapter } from './RemoteStorageAdapter.js';
|
|
5
5
|
import { AttachmentRecord, AttachmentState } from './Schema.js';
|
|
6
6
|
import { AttachmentErrorHandler } from './AttachmentErrorHandler.js';
|
|
7
7
|
import { AttachmentContext } from './AttachmentContext.js';
|
|
@@ -10,25 +10,28 @@ import { AttachmentContext } from './AttachmentContext.js';
|
|
|
10
10
|
* Orchestrates attachment synchronization between local and remote storage.
|
|
11
11
|
* Handles uploads, downloads, deletions, and state transitions.
|
|
12
12
|
*
|
|
13
|
+
* Remote operations (upload/download/delete) go through the {@link AttachmentTransportAdapter};
|
|
14
|
+
* local file operations use the {@link LocalStorageAdapter}.
|
|
15
|
+
*
|
|
13
16
|
* @internal
|
|
14
17
|
*/
|
|
15
18
|
export class SyncingService {
|
|
16
19
|
private attachmentService: AttachmentService;
|
|
17
20
|
private localStorage: LocalStorageAdapter;
|
|
18
|
-
private
|
|
21
|
+
private transport: AttachmentTransportAdapter;
|
|
19
22
|
private logger: PowerSyncLogger;
|
|
20
23
|
private errorHandler?: AttachmentErrorHandler;
|
|
21
24
|
|
|
22
25
|
constructor(
|
|
23
26
|
attachmentService: AttachmentService,
|
|
24
27
|
localStorage: LocalStorageAdapter,
|
|
25
|
-
|
|
28
|
+
transport: AttachmentTransportAdapter,
|
|
26
29
|
logger: PowerSyncLogger,
|
|
27
30
|
errorHandler?: AttachmentErrorHandler
|
|
28
31
|
) {
|
|
29
32
|
this.attachmentService = attachmentService;
|
|
30
33
|
this.localStorage = localStorage;
|
|
31
|
-
this.
|
|
34
|
+
this.transport = transport;
|
|
32
35
|
this.logger = logger;
|
|
33
36
|
this.errorHandler = errorHandler;
|
|
34
37
|
}
|
|
@@ -114,8 +117,7 @@ export class SyncingService {
|
|
|
114
117
|
throw new Error(`No localUri for attachment ${attachment.id}`);
|
|
115
118
|
}
|
|
116
119
|
|
|
117
|
-
|
|
118
|
-
await this.remoteStorage.uploadFile(fileBlob, attachment);
|
|
120
|
+
await this.transport.upload({ ...attachment, localUri: attachment.localUri });
|
|
119
121
|
|
|
120
122
|
return {
|
|
121
123
|
...attachment,
|
|
@@ -137,7 +139,7 @@ export class SyncingService {
|
|
|
137
139
|
|
|
138
140
|
/**
|
|
139
141
|
* Downloads an attachment from remote storage to local storage.
|
|
140
|
-
*
|
|
142
|
+
* The destination `localUri` is assigned here and the transport writes the file to it.
|
|
141
143
|
* On success, marks as SYNCED. On failure, defers to error handler or archives.
|
|
142
144
|
*
|
|
143
145
|
* @param attachment - The attachment record to download
|
|
@@ -146,10 +148,8 @@ export class SyncingService {
|
|
|
146
148
|
async downloadAttachment(attachment: AttachmentRecord): Promise<AttachmentRecord> {
|
|
147
149
|
this.logger.log({ level: LogLevels.info, message: `Downloading attachment ${attachment.filename}` });
|
|
148
150
|
try {
|
|
149
|
-
const fileData = await this.remoteStorage.downloadFile(attachment);
|
|
150
|
-
|
|
151
151
|
const localUri = this.localStorage.getLocalUri(attachment.filename);
|
|
152
|
-
await this.
|
|
152
|
+
await this.transport.download({ ...attachment, localUri });
|
|
153
153
|
|
|
154
154
|
return {
|
|
155
155
|
...attachment,
|
|
@@ -181,7 +181,7 @@ export class SyncingService {
|
|
|
181
181
|
*/
|
|
182
182
|
async deleteAttachment(attachment: AttachmentRecord, context: AttachmentContext): Promise<AttachmentRecord> {
|
|
183
183
|
try {
|
|
184
|
-
await this.
|
|
184
|
+
await this.transport.delete(attachment);
|
|
185
185
|
if (attachment.localUri) {
|
|
186
186
|
await this.localStorage.deleteFile(attachment.localUri);
|
|
187
187
|
}
|
|
@@ -16,6 +16,7 @@ import { ArrayQueryDefinition, Query } from './Query.js';
|
|
|
16
16
|
import { WatchCompatibleQuery } from './watched/WatchedQuery.js';
|
|
17
17
|
import { Mutex } from '../utils/mutex.js';
|
|
18
18
|
import { QueryResult } from '../db/QueryResult.js';
|
|
19
|
+
import { CheckpointRequest } from './sync/CheckpointRequest.js';
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* @public
|
|
@@ -233,6 +234,19 @@ export interface CommonPowerSyncDatabase extends BaseObserverInterface<PowerSync
|
|
|
233
234
|
*/
|
|
234
235
|
syncStream(name: string, params?: Record<string, any>): SyncStream;
|
|
235
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Requests a checkpoint from the PowerSync service.
|
|
239
|
+
*
|
|
240
|
+
* The returned request can be awaited (using {@link CheckpointRequest#waitForSync}) to confirm that the local
|
|
241
|
+
* database has applied server-side changes up to the checkpoint. This method requires an active or connecting sync
|
|
242
|
+
* client connected with a {@link CheckpointMode} set to `requests` and PowerSync service version 1.24.0 or later.
|
|
243
|
+
*
|
|
244
|
+
* It can throw for connection, mode, authentication, or service request failures.
|
|
245
|
+
*
|
|
246
|
+
* @alpha
|
|
247
|
+
*/
|
|
248
|
+
requestCheckpoint(): Promise<CheckpointRequest>;
|
|
249
|
+
|
|
236
250
|
/**
|
|
237
251
|
* Close the database, releasing resources.
|
|
238
252
|
*
|
|
@@ -343,7 +357,7 @@ export interface CommonPowerSyncDatabase extends BaseObserverInterface<PowerSync
|
|
|
343
357
|
|
|
344
358
|
/**
|
|
345
359
|
* Open a read-only transaction.
|
|
346
|
-
*
|
|
360
|
+
* When multiple connections are available, read transactions can run concurrently to a write transaction.
|
|
347
361
|
* Changes from any write transaction are not visible to read transactions started before it.
|
|
348
362
|
*
|
|
349
363
|
* @param callback - Function to execute within the transaction
|