@powersync/common 1.41.0 → 1.42.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/dist/bundle.cjs +10820 -22
- package/dist/bundle.cjs.map +1 -0
- package/dist/bundle.mjs +10741 -22
- package/dist/bundle.mjs.map +1 -0
- package/dist/bundle.node.cjs +10820 -0
- package/dist/bundle.node.cjs.map +1 -0
- package/dist/bundle.node.mjs +10741 -0
- package/dist/bundle.node.mjs.map +1 -0
- package/dist/index.d.cts +77 -13
- package/lib/client/AbstractPowerSyncDatabase.js +1 -0
- package/lib/client/AbstractPowerSyncDatabase.js.map +1 -0
- package/lib/client/AbstractPowerSyncOpenFactory.js +1 -0
- package/lib/client/AbstractPowerSyncOpenFactory.js.map +1 -0
- package/lib/client/ConnectionManager.js +1 -0
- package/lib/client/ConnectionManager.js.map +1 -0
- package/lib/client/CustomQuery.js +1 -0
- package/lib/client/CustomQuery.js.map +1 -0
- package/lib/client/Query.js +1 -0
- package/lib/client/Query.js.map +1 -0
- package/lib/client/SQLOpenFactory.js +1 -0
- package/lib/client/SQLOpenFactory.js.map +1 -0
- package/lib/client/compilableQueryWatch.js +1 -0
- package/lib/client/compilableQueryWatch.js.map +1 -0
- package/lib/client/connection/PowerSyncBackendConnector.js +1 -0
- package/lib/client/connection/PowerSyncBackendConnector.js.map +1 -0
- package/lib/client/connection/PowerSyncCredentials.js +1 -0
- package/lib/client/connection/PowerSyncCredentials.js.map +1 -0
- package/lib/client/constants.js +1 -0
- package/lib/client/constants.js.map +1 -0
- package/lib/client/runOnSchemaChange.js +1 -0
- package/lib/client/runOnSchemaChange.js.map +1 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js +1 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +1 -0
- package/lib/client/sync/bucket/CrudBatch.js +1 -0
- package/lib/client/sync/bucket/CrudBatch.js.map +1 -0
- package/lib/client/sync/bucket/CrudEntry.js +1 -0
- package/lib/client/sync/bucket/CrudEntry.js.map +1 -0
- package/lib/client/sync/bucket/CrudTransaction.js +1 -0
- package/lib/client/sync/bucket/CrudTransaction.js.map +1 -0
- package/lib/client/sync/bucket/OpType.js +1 -0
- package/lib/client/sync/bucket/OpType.js.map +1 -0
- package/lib/client/sync/bucket/OplogEntry.js +1 -0
- package/lib/client/sync/bucket/OplogEntry.js.map +1 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js +1 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -0
- package/lib/client/sync/bucket/SyncDataBatch.js +1 -0
- package/lib/client/sync/bucket/SyncDataBatch.js.map +1 -0
- package/lib/client/sync/bucket/SyncDataBucket.js +1 -0
- package/lib/client/sync/bucket/SyncDataBucket.js.map +1 -0
- package/lib/client/sync/stream/AbstractRemote.d.ts +5 -0
- package/lib/client/sync/stream/AbstractRemote.js +19 -6
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +1 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -0
- package/lib/client/sync/stream/WebsocketClientTransport.js +1 -0
- package/lib/client/sync/stream/WebsocketClientTransport.js.map +1 -0
- package/lib/client/sync/stream/core-instruction.js +1 -0
- package/lib/client/sync/stream/core-instruction.js.map +1 -0
- package/lib/client/sync/stream/streaming-sync-types.js +1 -0
- package/lib/client/sync/stream/streaming-sync-types.js.map +1 -0
- package/lib/client/sync/sync-streams.js +1 -0
- package/lib/client/sync/sync-streams.js.map +1 -0
- package/lib/client/triggers/TriggerManager.d.ts +71 -12
- package/lib/client/triggers/TriggerManager.js +1 -0
- package/lib/client/triggers/TriggerManager.js.map +1 -0
- package/lib/client/triggers/TriggerManagerImpl.js +11 -5
- package/lib/client/triggers/TriggerManagerImpl.js.map +1 -0
- package/lib/client/triggers/sanitizeSQL.js +1 -0
- package/lib/client/triggers/sanitizeSQL.js.map +1 -0
- package/lib/client/watched/GetAllQuery.js +1 -0
- package/lib/client/watched/GetAllQuery.js.map +1 -0
- package/lib/client/watched/WatchedQuery.js +1 -0
- package/lib/client/watched/WatchedQuery.js.map +1 -0
- package/lib/client/watched/processors/AbstractQueryProcessor.js +1 -0
- package/lib/client/watched/processors/AbstractQueryProcessor.js.map +1 -0
- package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -0
- package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -0
- package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -0
- package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -0
- package/lib/client/watched/processors/comparators.js +1 -0
- package/lib/client/watched/processors/comparators.js.map +1 -0
- package/lib/db/DBAdapter.js +1 -0
- package/lib/db/DBAdapter.js.map +1 -0
- package/lib/db/crud/SyncProgress.js +1 -0
- package/lib/db/crud/SyncProgress.js.map +1 -0
- package/lib/db/crud/SyncStatus.js +1 -0
- package/lib/db/crud/SyncStatus.js.map +1 -0
- package/lib/db/crud/UploadQueueStatus.js +1 -0
- package/lib/db/crud/UploadQueueStatus.js.map +1 -0
- package/lib/db/schema/Column.js +1 -0
- package/lib/db/schema/Column.js.map +1 -0
- package/lib/db/schema/Index.js +1 -0
- package/lib/db/schema/Index.js.map +1 -0
- package/lib/db/schema/IndexedColumn.js +1 -0
- package/lib/db/schema/IndexedColumn.js.map +1 -0
- package/lib/db/schema/RawTable.js +1 -0
- package/lib/db/schema/RawTable.js.map +1 -0
- package/lib/db/schema/Schema.js +1 -0
- package/lib/db/schema/Schema.js.map +1 -0
- package/lib/db/schema/Table.js +1 -0
- package/lib/db/schema/Table.js.map +1 -0
- package/lib/db/schema/TableV2.js +1 -0
- package/lib/db/schema/TableV2.js.map +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -0
- package/lib/types/types.js +1 -0
- package/lib/types/types.js.map +1 -0
- package/lib/utils/AbortOperation.js +1 -0
- package/lib/utils/AbortOperation.js.map +1 -0
- package/lib/utils/BaseObserver.js +1 -0
- package/lib/utils/BaseObserver.js.map +1 -0
- package/lib/utils/ControlledExecutor.js +1 -0
- package/lib/utils/ControlledExecutor.js.map +1 -0
- package/lib/utils/DataStream.js +1 -0
- package/lib/utils/DataStream.js.map +1 -0
- package/lib/utils/Logger.js +1 -0
- package/lib/utils/Logger.js.map +1 -0
- package/lib/utils/MetaBaseObserver.js +1 -0
- package/lib/utils/MetaBaseObserver.js.map +1 -0
- package/lib/utils/async.js +1 -0
- package/lib/utils/async.js.map +1 -0
- package/lib/utils/mutex.js +1 -0
- package/lib/utils/mutex.js.map +1 -0
- package/lib/utils/parseQuery.js +1 -0
- package/lib/utils/parseQuery.js.map +1 -0
- package/package.json +23 -15
- package/src/client/AbstractPowerSyncDatabase.ts +1343 -0
- package/src/client/AbstractPowerSyncOpenFactory.ts +39 -0
- package/src/client/ConnectionManager.ts +402 -0
- package/src/client/CustomQuery.ts +56 -0
- package/src/client/Query.ts +106 -0
- package/src/client/SQLOpenFactory.ts +55 -0
- package/src/client/compilableQueryWatch.ts +55 -0
- package/src/client/connection/PowerSyncBackendConnector.ts +25 -0
- package/src/client/connection/PowerSyncCredentials.ts +5 -0
- package/src/client/constants.ts +1 -0
- package/src/client/runOnSchemaChange.ts +31 -0
- package/src/client/sync/bucket/BucketStorageAdapter.ts +118 -0
- package/src/client/sync/bucket/CrudBatch.ts +21 -0
- package/src/client/sync/bucket/CrudEntry.ts +172 -0
- package/src/client/sync/bucket/CrudTransaction.ts +21 -0
- package/src/client/sync/bucket/OpType.ts +23 -0
- package/src/client/sync/bucket/OplogEntry.ts +50 -0
- package/src/client/sync/bucket/SqliteBucketStorage.ts +395 -0
- package/src/client/sync/bucket/SyncDataBatch.ts +11 -0
- package/src/client/sync/bucket/SyncDataBucket.ts +49 -0
- package/src/client/sync/stream/AbstractRemote.ts +636 -0
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +1258 -0
- package/src/client/sync/stream/WebsocketClientTransport.ts +80 -0
- package/src/client/sync/stream/core-instruction.ts +99 -0
- package/src/client/sync/stream/streaming-sync-types.ts +205 -0
- package/src/client/sync/sync-streams.ts +107 -0
- package/src/client/triggers/TriggerManager.ts +451 -0
- package/src/client/triggers/TriggerManagerImpl.ts +320 -0
- package/src/client/triggers/sanitizeSQL.ts +66 -0
- package/src/client/watched/GetAllQuery.ts +46 -0
- package/src/client/watched/WatchedQuery.ts +121 -0
- package/src/client/watched/processors/AbstractQueryProcessor.ts +226 -0
- package/src/client/watched/processors/DifferentialQueryProcessor.ts +305 -0
- package/src/client/watched/processors/OnChangeQueryProcessor.ts +122 -0
- package/src/client/watched/processors/comparators.ts +57 -0
- package/src/db/DBAdapter.ts +134 -0
- package/src/db/crud/SyncProgress.ts +100 -0
- package/src/db/crud/SyncStatus.ts +308 -0
- package/src/db/crud/UploadQueueStatus.ts +20 -0
- package/src/db/schema/Column.ts +60 -0
- package/src/db/schema/Index.ts +39 -0
- package/src/db/schema/IndexedColumn.ts +42 -0
- package/src/db/schema/RawTable.ts +67 -0
- package/src/db/schema/Schema.ts +76 -0
- package/src/db/schema/Table.ts +359 -0
- package/src/db/schema/TableV2.ts +9 -0
- package/src/index.ts +52 -0
- package/src/types/types.ts +9 -0
- package/src/utils/AbortOperation.ts +17 -0
- package/src/utils/BaseObserver.ts +41 -0
- package/src/utils/ControlledExecutor.ts +72 -0
- package/src/utils/DataStream.ts +211 -0
- package/src/utils/Logger.ts +47 -0
- package/src/utils/MetaBaseObserver.ts +81 -0
- package/src/utils/async.ts +61 -0
- package/src/utils/mutex.ts +34 -0
- package/src/utils/parseQuery.ts +25 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapted from rsocket-websocket-client
|
|
3
|
+
* https://github.com/rsocket/rsocket-js/blob/e224cf379e747c4f1ddc4f2fa111854626cc8575/packages/rsocket-websocket-client/src/WebsocketClientTransport.ts#L17
|
|
4
|
+
* This adds additional error handling for React Native iOS.
|
|
5
|
+
* This particularly adds a close listener to handle cases where the WebSocket
|
|
6
|
+
* connection closes immediately after opening without emitting an error.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
ClientTransport,
|
|
10
|
+
Closeable,
|
|
11
|
+
Demultiplexer,
|
|
12
|
+
Deserializer,
|
|
13
|
+
DuplexConnection,
|
|
14
|
+
FrameHandler,
|
|
15
|
+
Multiplexer,
|
|
16
|
+
Outbound
|
|
17
|
+
} from 'rsocket-core';
|
|
18
|
+
import { ClientOptions } from 'rsocket-websocket-client';
|
|
19
|
+
import { WebsocketDuplexConnection } from 'rsocket-websocket-client/dist/WebsocketDuplexConnection.js';
|
|
20
|
+
|
|
21
|
+
export class WebsocketClientTransport implements ClientTransport {
|
|
22
|
+
private readonly url: string;
|
|
23
|
+
private readonly factory: (url: string) => WebSocket;
|
|
24
|
+
|
|
25
|
+
constructor(options: ClientOptions) {
|
|
26
|
+
this.url = options.url;
|
|
27
|
+
this.factory = options.wsCreator ?? ((url: string) => new WebSocket(url));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
connect(
|
|
31
|
+
multiplexerDemultiplexerFactory: (outbound: Outbound & Closeable) => Multiplexer & Demultiplexer & FrameHandler
|
|
32
|
+
): Promise<DuplexConnection> {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const websocket = this.factory(this.url);
|
|
35
|
+
|
|
36
|
+
websocket.binaryType = 'arraybuffer';
|
|
37
|
+
|
|
38
|
+
let removeListeners: () => void;
|
|
39
|
+
|
|
40
|
+
const openListener = () => {
|
|
41
|
+
removeListeners();
|
|
42
|
+
resolve(new WebsocketDuplexConnection(websocket, new Deserializer(), multiplexerDemultiplexerFactory));
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const errorListener = (ev: ErrorEvent) => {
|
|
46
|
+
removeListeners();
|
|
47
|
+
// We add a default error in that case.
|
|
48
|
+
if (ev.error != null) {
|
|
49
|
+
// undici typically provides an error object
|
|
50
|
+
reject(ev.error);
|
|
51
|
+
} else if (ev.message != null) {
|
|
52
|
+
// React Native typically does not provide an error object, but does provide a message
|
|
53
|
+
reject(new Error(`Failed to create websocket connection: ${ev.message}`));
|
|
54
|
+
} else {
|
|
55
|
+
// Browsers often provide no details at all
|
|
56
|
+
reject(new Error(`Failed to create websocket connection to ${this.url}`));
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* In some cases, such as React Native iOS, the WebSocket connection may close immediately after opening
|
|
62
|
+
* without and error. In such cases, we need to handle the close event to reject the promise.
|
|
63
|
+
*/
|
|
64
|
+
const closeListener = () => {
|
|
65
|
+
removeListeners();
|
|
66
|
+
reject(new Error('WebSocket connection closed while opening'));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
removeListeners = () => {
|
|
70
|
+
websocket.removeEventListener('open', openListener);
|
|
71
|
+
websocket.removeEventListener('error', errorListener);
|
|
72
|
+
websocket.removeEventListener('close', closeListener);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
websocket.addEventListener('open', openListener);
|
|
76
|
+
websocket.addEventListener('error', errorListener);
|
|
77
|
+
websocket.addEventListener('close', closeListener);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { StreamingSyncRequest } from './streaming-sync-types.js';
|
|
2
|
+
import * as sync_status from '../../../db/crud/SyncStatus.js';
|
|
3
|
+
import { FULL_SYNC_PRIORITY } from '../../../db/crud/SyncProgress.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An internal instruction emitted by the sync client in the core extension in response to the JS
|
|
7
|
+
* SDK passing sync data into the extension.
|
|
8
|
+
*/
|
|
9
|
+
export type Instruction =
|
|
10
|
+
| { LogLine: LogLine }
|
|
11
|
+
| { UpdateSyncStatus: UpdateSyncStatus }
|
|
12
|
+
| { EstablishSyncStream: EstablishSyncStream }
|
|
13
|
+
| { FetchCredentials: FetchCredentials }
|
|
14
|
+
| { CloseSyncStream: { hide_disconnect: boolean } }
|
|
15
|
+
| { FlushFileSystem: any }
|
|
16
|
+
| { DidCompleteSync: any };
|
|
17
|
+
|
|
18
|
+
export interface LogLine {
|
|
19
|
+
severity: 'DEBUG' | 'INFO' | 'WARNING';
|
|
20
|
+
line: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface EstablishSyncStream {
|
|
24
|
+
request: StreamingSyncRequest;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface UpdateSyncStatus {
|
|
28
|
+
status: CoreSyncStatus;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface CoreSyncStatus {
|
|
32
|
+
connected: boolean;
|
|
33
|
+
connecting: boolean;
|
|
34
|
+
priority_status: SyncPriorityStatus[];
|
|
35
|
+
downloading: DownloadProgress | null;
|
|
36
|
+
streams: CoreStreamSubscription[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// An `ActiveStreamSubscription` from the core extension + serialized progress information.
|
|
40
|
+
export interface CoreStreamSubscription {
|
|
41
|
+
progress: { total: number; downloaded: number };
|
|
42
|
+
name: string;
|
|
43
|
+
parameters: any;
|
|
44
|
+
priority: number | null;
|
|
45
|
+
active: boolean;
|
|
46
|
+
is_default: boolean;
|
|
47
|
+
has_explicit_subscription: boolean;
|
|
48
|
+
expires_at: number | null;
|
|
49
|
+
last_synced_at: number | null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface SyncPriorityStatus {
|
|
53
|
+
priority: number;
|
|
54
|
+
last_synced_at: number | number;
|
|
55
|
+
has_synced: boolean | null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface DownloadProgress {
|
|
59
|
+
buckets: Record<string, BucketProgress>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface BucketProgress {
|
|
63
|
+
priority: number;
|
|
64
|
+
at_last: number;
|
|
65
|
+
since_last: number;
|
|
66
|
+
target_count: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface FetchCredentials {
|
|
70
|
+
did_expire: boolean;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function priorityToJs(status: SyncPriorityStatus): sync_status.SyncPriorityStatus {
|
|
74
|
+
return {
|
|
75
|
+
priority: status.priority,
|
|
76
|
+
hasSynced: status.has_synced ?? undefined,
|
|
77
|
+
lastSyncedAt: status.last_synced_at != null ? new Date(status.last_synced_at * 1000) : undefined
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function coreStatusToJs(status: CoreSyncStatus): sync_status.SyncStatusOptions {
|
|
82
|
+
const coreCompleteSync = status.priority_status.find((s) => s.priority == FULL_SYNC_PRIORITY);
|
|
83
|
+
const completeSync = coreCompleteSync != null ? priorityToJs(coreCompleteSync) : null;
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
connected: status.connected,
|
|
87
|
+
connecting: status.connecting,
|
|
88
|
+
dataFlow: {
|
|
89
|
+
// We expose downloading as a boolean field, the core extension reports download information as a nullable
|
|
90
|
+
// download status. When that status is non-null, a download is in progress.
|
|
91
|
+
downloading: status.downloading != null,
|
|
92
|
+
downloadProgress: status.downloading?.buckets,
|
|
93
|
+
internalStreamSubscriptions: status.streams
|
|
94
|
+
},
|
|
95
|
+
lastSyncedAt: completeSync?.lastSyncedAt,
|
|
96
|
+
hasSynced: completeSync?.hasSynced,
|
|
97
|
+
priorityStatusEntries: status.priority_status.map(priorityToJs)
|
|
98
|
+
};
|
|
99
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { BucketChecksum, Checkpoint } from '../bucket/BucketStorageAdapter.js';
|
|
2
|
+
import { CrudEntry, OpId } from '../bucket/CrudEntry.js';
|
|
3
|
+
import { SyncDataBucketJSON } from '../bucket/SyncDataBucket.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* For sync2.json
|
|
7
|
+
*/
|
|
8
|
+
export interface ContinueCheckpointRequest {
|
|
9
|
+
/**
|
|
10
|
+
* Existing bucket states. Only these buckets are synchronized.
|
|
11
|
+
*/
|
|
12
|
+
buckets: BucketRequest[];
|
|
13
|
+
|
|
14
|
+
checkpoint_token: string;
|
|
15
|
+
|
|
16
|
+
limit?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SyncNewCheckpointRequest {
|
|
20
|
+
/**
|
|
21
|
+
* Existing bucket states. Used if include_data is specified.
|
|
22
|
+
*/
|
|
23
|
+
buckets?: BucketRequest[];
|
|
24
|
+
|
|
25
|
+
request_checkpoint: {
|
|
26
|
+
/**
|
|
27
|
+
* Whether or not to include an initial data request.
|
|
28
|
+
*/
|
|
29
|
+
include_data: boolean;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Whether or not to compute a checksum.
|
|
33
|
+
*/
|
|
34
|
+
include_checksum: boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
limit?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type SyncRequest = ContinueCheckpointRequest | SyncNewCheckpointRequest;
|
|
41
|
+
|
|
42
|
+
export interface SyncResponse {
|
|
43
|
+
/**
|
|
44
|
+
* Data for the buckets returned. May not have an an entry for each bucket in the request.
|
|
45
|
+
*/
|
|
46
|
+
data?: SyncDataBucketJSON[];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* True if the response limit has been reached, and another request must be made.
|
|
50
|
+
*/
|
|
51
|
+
has_more: boolean;
|
|
52
|
+
|
|
53
|
+
checkpoint_token?: string;
|
|
54
|
+
|
|
55
|
+
checkpoint?: Checkpoint;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type JSONValue = string | number | boolean | null | undefined | JSONObject | JSONArray;
|
|
59
|
+
|
|
60
|
+
interface JSONObject {
|
|
61
|
+
[key: string]: JSONValue;
|
|
62
|
+
}
|
|
63
|
+
type JSONArray = JSONValue[];
|
|
64
|
+
|
|
65
|
+
export type StreamingSyncRequestParameterType = JSONValue;
|
|
66
|
+
|
|
67
|
+
export interface StreamingSyncRequest {
|
|
68
|
+
/**
|
|
69
|
+
* Existing bucket states.
|
|
70
|
+
*/
|
|
71
|
+
buckets?: BucketRequest[];
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* If specified, limit the response to only include these buckets.
|
|
75
|
+
*/
|
|
76
|
+
only?: string[];
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Whether or not to compute a checksum for each checkpoint
|
|
80
|
+
*/
|
|
81
|
+
include_checksum: boolean;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Changes the response to stringified data in each OplogEntry
|
|
85
|
+
*/
|
|
86
|
+
raw_data: boolean;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Client parameters to be passed to the sync rules.
|
|
90
|
+
*/
|
|
91
|
+
parameters?: Record<string, StreamingSyncRequestParameterType>;
|
|
92
|
+
|
|
93
|
+
client_id?: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface StreamingSyncCheckpoint {
|
|
97
|
+
checkpoint: Checkpoint;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface StreamingSyncCheckpointDiff {
|
|
101
|
+
checkpoint_diff: {
|
|
102
|
+
last_op_id: OpId;
|
|
103
|
+
updated_buckets: BucketChecksum[];
|
|
104
|
+
removed_buckets: string[];
|
|
105
|
+
write_checkpoint?: string;
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface StreamingSyncDataJSON {
|
|
110
|
+
data: SyncDataBucketJSON;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface StreamingSyncCheckpointComplete {
|
|
114
|
+
checkpoint_complete: {
|
|
115
|
+
last_op_id: OpId;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface StreamingSyncCheckpointPartiallyComplete {
|
|
120
|
+
partial_checkpoint_complete: {
|
|
121
|
+
priority: number;
|
|
122
|
+
last_op_id: OpId;
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface StreamingSyncKeepalive {
|
|
127
|
+
/** If specified, token expires in this many seconds. */
|
|
128
|
+
token_expires_in: number;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export type StreamingSyncLine =
|
|
132
|
+
| StreamingSyncDataJSON
|
|
133
|
+
| StreamingSyncCheckpoint
|
|
134
|
+
| StreamingSyncCheckpointDiff
|
|
135
|
+
| StreamingSyncCheckpointComplete
|
|
136
|
+
| StreamingSyncCheckpointPartiallyComplete
|
|
137
|
+
| StreamingSyncKeepalive;
|
|
138
|
+
|
|
139
|
+
export type CrudUploadNotification = { crud_upload_completed: null };
|
|
140
|
+
|
|
141
|
+
export type StreamingSyncLineOrCrudUploadComplete = StreamingSyncLine | CrudUploadNotification;
|
|
142
|
+
|
|
143
|
+
export interface BucketRequest {
|
|
144
|
+
name: string;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Base-10 number. Sync all data from this bucket with op_id > after.
|
|
148
|
+
*/
|
|
149
|
+
after: OpId;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function isStreamingSyncData(line: StreamingSyncLine): line is StreamingSyncDataJSON {
|
|
153
|
+
return (line as StreamingSyncDataJSON).data != null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function isStreamingKeepalive(line: StreamingSyncLine): line is StreamingSyncKeepalive {
|
|
157
|
+
return (line as StreamingSyncKeepalive).token_expires_in != null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function isStreamingSyncCheckpoint(line: StreamingSyncLine): line is StreamingSyncCheckpoint {
|
|
161
|
+
return (line as StreamingSyncCheckpoint).checkpoint != null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function isStreamingSyncCheckpointComplete(line: StreamingSyncLine): line is StreamingSyncCheckpointComplete {
|
|
165
|
+
return (line as StreamingSyncCheckpointComplete).checkpoint_complete != null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function isStreamingSyncCheckpointPartiallyComplete(
|
|
169
|
+
line: StreamingSyncLine
|
|
170
|
+
): line is StreamingSyncCheckpointPartiallyComplete {
|
|
171
|
+
return (line as StreamingSyncCheckpointPartiallyComplete).partial_checkpoint_complete != null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function isStreamingSyncCheckpointDiff(line: StreamingSyncLine): line is StreamingSyncCheckpointDiff {
|
|
175
|
+
return (line as StreamingSyncCheckpointDiff).checkpoint_diff != null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function isContinueCheckpointRequest(request: SyncRequest): request is ContinueCheckpointRequest {
|
|
179
|
+
return (
|
|
180
|
+
Array.isArray((request as ContinueCheckpointRequest).buckets) &&
|
|
181
|
+
typeof (request as ContinueCheckpointRequest).checkpoint_token == 'string'
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function isSyncNewCheckpointRequest(request: SyncRequest): request is SyncNewCheckpointRequest {
|
|
186
|
+
return typeof (request as SyncNewCheckpointRequest).request_checkpoint == 'object';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* For crud.json
|
|
191
|
+
*/
|
|
192
|
+
export interface CrudRequest {
|
|
193
|
+
data: CrudEntry[];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface CrudResponse {
|
|
197
|
+
/**
|
|
198
|
+
* A sync response with a checkpoint >= this checkpoint would contain all the changes in this request.
|
|
199
|
+
*
|
|
200
|
+
* Any earlier checkpoint may or may not contain these changes.
|
|
201
|
+
*
|
|
202
|
+
* May be empty when the request contains no ops.
|
|
203
|
+
*/
|
|
204
|
+
checkpoint?: OpId;
|
|
205
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { AbstractPowerSyncDatabase } from '../AbstractPowerSyncDatabase.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A description of a sync stream, consisting of its {@link name} and the {@link parameters} used when subscribing.
|
|
5
|
+
*/
|
|
6
|
+
export interface SyncStreamDescription {
|
|
7
|
+
/**
|
|
8
|
+
* The name of the stream as it appears in the stream definition for the PowerSync service.
|
|
9
|
+
*/
|
|
10
|
+
name: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The parameters used to subscribe to the stream, if any.
|
|
14
|
+
*
|
|
15
|
+
* The same stream can be subscribed to multiple times with different parameters.
|
|
16
|
+
*/
|
|
17
|
+
parameters: Record<string, any> | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Information about a subscribed sync stream.
|
|
22
|
+
*
|
|
23
|
+
* This includes the {@link SyncStreamDescription}, along with information about the current sync status.
|
|
24
|
+
*/
|
|
25
|
+
export interface SyncSubscriptionDescription extends SyncStreamDescription {
|
|
26
|
+
active: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Whether this stream subscription is included by default, regardless of whether the stream has explicitly been
|
|
29
|
+
* subscribed to or not.
|
|
30
|
+
*
|
|
31
|
+
* It's possible for both {@link isDefault} and {@link hasExplicitSubscription} to be true at the same time - this
|
|
32
|
+
* happens when a default stream was subscribed explicitly.
|
|
33
|
+
*/
|
|
34
|
+
isDefault: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Whether this stream has been subscribed to explicitly.
|
|
37
|
+
*
|
|
38
|
+
* It's possible for both {@link isDefault} and {@link hasExplicitSubscription} to be true at the same time - this
|
|
39
|
+
* happens when a default stream was subscribed explicitly.
|
|
40
|
+
*/
|
|
41
|
+
hasExplicitSubscription: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* For sync streams that have a time-to-live, the current time at which the stream would expire if not subscribed to
|
|
44
|
+
* again.
|
|
45
|
+
*/
|
|
46
|
+
expiresAt: Date | null;
|
|
47
|
+
/**
|
|
48
|
+
* Whether this stream subscription has been synced at least once.
|
|
49
|
+
*/
|
|
50
|
+
hasSynced: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* If {@link hasSynced} is true, the last time data from this stream has been synced.
|
|
53
|
+
*/
|
|
54
|
+
lastSyncedAt: Date | null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface SyncStreamSubscribeOptions {
|
|
58
|
+
/**
|
|
59
|
+
* A "time to live" for this stream subscription, in seconds.
|
|
60
|
+
*
|
|
61
|
+
* The TTL control when a stream gets evicted after not having an active {@link SyncStreamSubscription} object
|
|
62
|
+
* attached to it.
|
|
63
|
+
*/
|
|
64
|
+
ttl?: number;
|
|
65
|
+
/**
|
|
66
|
+
* A priority to assign to this subscription. This overrides the default priority that may have been set on streams.
|
|
67
|
+
*
|
|
68
|
+
* For details on priorities, see [priotized sync](https://docs.powersync.com/usage/use-case-examples/prioritized-sync).
|
|
69
|
+
*/
|
|
70
|
+
priority?: 0 | 1 | 2 | 3;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A handle to a {@link SyncStreamDescription} that allows subscribing to the stream.
|
|
75
|
+
*
|
|
76
|
+
* To obtain an instance of {@link SyncStream}, call {@link AbstractPowerSyncDatabase.syncStream}.
|
|
77
|
+
*/
|
|
78
|
+
export interface SyncStream extends SyncStreamDescription {
|
|
79
|
+
/**
|
|
80
|
+
* Adds a subscription to this stream, requesting it to be included when connecting to the sync service.
|
|
81
|
+
*
|
|
82
|
+
* You should keep a reference to the returned {@link SyncStreamSubscription} object along as you need data for that
|
|
83
|
+
* stream. As soon as {@link SyncStreamSubscription.unsubscribe} is called for all subscriptions on this stream
|
|
84
|
+
* (including subscriptions created on other tabs), the {@link SyncStreamSubscribeOptions.ttl} starts ticking and will
|
|
85
|
+
* eventually evict the stream (unless {@link subscribe} is called again).
|
|
86
|
+
*/
|
|
87
|
+
subscribe(options?: SyncStreamSubscribeOptions): Promise<SyncStreamSubscription>;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Clears all subscriptions attached to this stream and resets the TTL for the stream.
|
|
91
|
+
*
|
|
92
|
+
* This is a potentially dangerous operations, as it interferes with other stream subscriptions.
|
|
93
|
+
*/
|
|
94
|
+
unsubscribeAll(): Promise<void>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface SyncStreamSubscription extends SyncStreamDescription {
|
|
98
|
+
/**
|
|
99
|
+
* A promise that resolves once data from in this sync stream has been synced and applied.
|
|
100
|
+
*/
|
|
101
|
+
waitForFirstSync(abort?: AbortSignal): Promise<void>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Removes this stream subscription.
|
|
105
|
+
*/
|
|
106
|
+
unsubscribe(): void;
|
|
107
|
+
}
|