@powersync/common 1.41.0 → 1.41.1
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 +10809 -22
- package/dist/bundle.cjs.map +1 -0
- package/dist/bundle.mjs +10730 -22
- package/dist/bundle.mjs.map +1 -0
- package/dist/bundle.node.cjs +10809 -0
- package/dist/bundle.node.cjs.map +1 -0
- package/dist/bundle.node.mjs +10730 -0
- package/dist/bundle.node.mjs.map +1 -0
- package/dist/index.d.cts +5 -0
- 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 +9 -2
- 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.js +1 -0
- package/lib/client/triggers/TriggerManager.js.map +1 -0
- package/lib/client/triggers/TriggerManagerImpl.js +1 -0
- 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 +626 -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 +384 -0
- package/src/client/triggers/TriggerManagerImpl.ts +314 -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,100 @@
|
|
|
1
|
+
import type { BucketProgress } from '../../client/sync/stream/core-instruction.js';
|
|
2
|
+
import type { SyncStatus } from './SyncStatus.js';
|
|
3
|
+
|
|
4
|
+
// (bucket, progress) pairs
|
|
5
|
+
/** @internal */
|
|
6
|
+
export type InternalProgressInformation = Record<string, BucketProgress>;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @internal The priority used by the core extension to indicate that a full sync was completed.
|
|
10
|
+
*/
|
|
11
|
+
export const FULL_SYNC_PRIORITY = 2147483647;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Information about a progressing download made by the PowerSync SDK.
|
|
15
|
+
*
|
|
16
|
+
* To obtain these values, use {@link SyncProgress}, available through
|
|
17
|
+
* {@link SyncStatus#downloadProgress}.
|
|
18
|
+
*/
|
|
19
|
+
export interface ProgressWithOperations {
|
|
20
|
+
/**
|
|
21
|
+
* The total amount of operations to download for the current sync iteration
|
|
22
|
+
* to complete.
|
|
23
|
+
*/
|
|
24
|
+
totalOperations: number;
|
|
25
|
+
/**
|
|
26
|
+
* The amount of operations that have already been downloaded.
|
|
27
|
+
*/
|
|
28
|
+
downloadedOperations: number;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Relative progress, as {@link downloadedOperations} of {@link totalOperations}.
|
|
32
|
+
*
|
|
33
|
+
* This will be a number between `0.0` and `1.0` (inclusive).
|
|
34
|
+
*
|
|
35
|
+
* When this number reaches `1.0`, all changes have been received from the sync service.
|
|
36
|
+
* Actually applying these changes happens before the `downloadProgress` field is cleared from
|
|
37
|
+
* {@link SyncStatus}, so progress can stay at `1.0` for a short while before completing.
|
|
38
|
+
*/
|
|
39
|
+
downloadedFraction: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Provides realtime progress on how PowerSync is downloading rows.
|
|
44
|
+
*
|
|
45
|
+
* The progress until the next complete sync is available through the fields on {@link ProgressWithOperations},
|
|
46
|
+
* which this class implements.
|
|
47
|
+
* Additionally, the {@link SyncProgress.untilPriority} method can be used to otbain progress towards
|
|
48
|
+
* a specific priority (instead of the progress for the entire download).
|
|
49
|
+
*
|
|
50
|
+
* The reported progress always reflects the status towards the end of a sync iteration (after
|
|
51
|
+
* which a consistent snapshot of all buckets is available locally).
|
|
52
|
+
*
|
|
53
|
+
* In rare cases (in particular, when a [compacting](https://docs.powersync.com/usage/lifecycle-maintenance/compacting-buckets)
|
|
54
|
+
* operation takes place between syncs), it's possible for the returned numbers to be slightly
|
|
55
|
+
* inaccurate. For this reason, {@link SyncProgress} should be seen as an approximation of progress.
|
|
56
|
+
* The information returned is good enough to build progress bars, but not exact enough to track
|
|
57
|
+
* individual download counts.
|
|
58
|
+
*
|
|
59
|
+
* Also note that data is downloaded in bulk, which means that individual counters are unlikely
|
|
60
|
+
* to be updated one-by-one.
|
|
61
|
+
*/
|
|
62
|
+
export class SyncProgress implements ProgressWithOperations {
|
|
63
|
+
totalOperations: number;
|
|
64
|
+
downloadedOperations: number;
|
|
65
|
+
downloadedFraction: number;
|
|
66
|
+
|
|
67
|
+
constructor(protected internal: InternalProgressInformation) {
|
|
68
|
+
const untilCompletion = this.untilPriority(FULL_SYNC_PRIORITY);
|
|
69
|
+
|
|
70
|
+
this.totalOperations = untilCompletion.totalOperations;
|
|
71
|
+
this.downloadedOperations = untilCompletion.downloadedOperations;
|
|
72
|
+
this.downloadedFraction = untilCompletion.downloadedFraction;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns download progress towards all data up until the specified priority being received.
|
|
77
|
+
*
|
|
78
|
+
* The returned {@link ProgressWithOperations} tracks the target amount of operations that need
|
|
79
|
+
* to be downloaded in total and how many of them have already been received.
|
|
80
|
+
*/
|
|
81
|
+
untilPriority(priority: number): ProgressWithOperations {
|
|
82
|
+
let total = 0;
|
|
83
|
+
let downloaded = 0;
|
|
84
|
+
|
|
85
|
+
for (const progress of Object.values(this.internal)) {
|
|
86
|
+
// Include higher-priority buckets, which are represented by lower numbers.
|
|
87
|
+
if (progress.priority <= priority) {
|
|
88
|
+
downloaded += progress.since_last;
|
|
89
|
+
total += progress.target_count - progress.at_last;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let progress = total == 0 ? 0.0 : downloaded / total;
|
|
94
|
+
return {
|
|
95
|
+
totalOperations: total,
|
|
96
|
+
downloadedOperations: downloaded,
|
|
97
|
+
downloadedFraction: progress
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { CoreStreamSubscription } from '../../client/sync/stream/core-instruction.js';
|
|
2
|
+
import { SyncClientImplementation } from '../../client/sync/stream/AbstractStreamingSyncImplementation.js';
|
|
3
|
+
import { InternalProgressInformation, ProgressWithOperations, SyncProgress } from './SyncProgress.js';
|
|
4
|
+
import { SyncStreamDescription, SyncSubscriptionDescription } from '../../client/sync/sync-streams.js';
|
|
5
|
+
|
|
6
|
+
export type SyncDataFlowStatus = Partial<{
|
|
7
|
+
downloading: boolean;
|
|
8
|
+
uploading: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Error during downloading (including connecting).
|
|
11
|
+
*
|
|
12
|
+
* Cleared on the next successful data download.
|
|
13
|
+
*/
|
|
14
|
+
downloadError?: Error;
|
|
15
|
+
/**
|
|
16
|
+
* Error during uploading.
|
|
17
|
+
* Cleared on the next successful upload.
|
|
18
|
+
*/
|
|
19
|
+
uploadError?: Error;
|
|
20
|
+
/**
|
|
21
|
+
* Internal information about how far we are downloading operations in buckets.
|
|
22
|
+
*
|
|
23
|
+
* Please use the {@link SyncStatus#downloadProgress} property to track sync progress.
|
|
24
|
+
*/
|
|
25
|
+
downloadProgress: InternalProgressInformation | null;
|
|
26
|
+
internalStreamSubscriptions: CoreStreamSubscription[] | null;
|
|
27
|
+
}>;
|
|
28
|
+
|
|
29
|
+
export interface SyncPriorityStatus {
|
|
30
|
+
priority: number;
|
|
31
|
+
lastSyncedAt?: Date;
|
|
32
|
+
hasSynced?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type SyncStatusOptions = {
|
|
36
|
+
connected?: boolean;
|
|
37
|
+
connecting?: boolean;
|
|
38
|
+
dataFlow?: SyncDataFlowStatus;
|
|
39
|
+
lastSyncedAt?: Date;
|
|
40
|
+
hasSynced?: boolean;
|
|
41
|
+
priorityStatusEntries?: SyncPriorityStatus[];
|
|
42
|
+
clientImplementation?: SyncClientImplementation;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export class SyncStatus {
|
|
46
|
+
constructor(protected options: SyncStatusOptions) {}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns the used sync client implementation (either the one implemented in JavaScript or the newer Rust-based
|
|
50
|
+
* implementation).
|
|
51
|
+
*
|
|
52
|
+
* This information is only available after a connection has been requested.
|
|
53
|
+
*/
|
|
54
|
+
get clientImplementation() {
|
|
55
|
+
return this.options.clientImplementation;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Indicates if the client is currently connected to the PowerSync service.
|
|
60
|
+
*
|
|
61
|
+
* @returns {boolean} True if connected, false otherwise. Defaults to false if not specified.
|
|
62
|
+
*/
|
|
63
|
+
get connected() {
|
|
64
|
+
return this.options.connected ?? false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Indicates if the client is in the process of establishing a connection to the PowerSync service.
|
|
69
|
+
*
|
|
70
|
+
* @returns {boolean} True if connecting, false otherwise. Defaults to false if not specified.
|
|
71
|
+
*/
|
|
72
|
+
get connecting() {
|
|
73
|
+
return this.options.connecting ?? false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Time that a last sync has fully completed, if any.
|
|
78
|
+
* This timestamp is reset to null after a restart of the PowerSync service.
|
|
79
|
+
*
|
|
80
|
+
* @returns {Date | undefined} The timestamp of the last successful sync, or undefined if no sync has completed.
|
|
81
|
+
*/
|
|
82
|
+
get lastSyncedAt() {
|
|
83
|
+
return this.options.lastSyncedAt;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Indicates whether there has been at least one full sync completed since initialization.
|
|
88
|
+
*
|
|
89
|
+
* @returns {boolean | undefined} True if at least one sync has completed, false if no sync has completed,
|
|
90
|
+
* or undefined when the state is still being loaded from the database.
|
|
91
|
+
*/
|
|
92
|
+
get hasSynced() {
|
|
93
|
+
return this.options.hasSynced;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Provides the current data flow status regarding uploads and downloads.
|
|
98
|
+
*
|
|
99
|
+
* @returns {SyncDataFlowStatus} An object containing:
|
|
100
|
+
* - downloading: True if actively downloading changes (only when connected is also true)
|
|
101
|
+
* - uploading: True if actively uploading changes
|
|
102
|
+
* Defaults to {downloading: false, uploading: false} if not specified.
|
|
103
|
+
*/
|
|
104
|
+
get dataFlowStatus() {
|
|
105
|
+
return (
|
|
106
|
+
this.options.dataFlow ?? {
|
|
107
|
+
/**
|
|
108
|
+
* true if actively downloading changes.
|
|
109
|
+
* This is only true when {@link connected} is also true.
|
|
110
|
+
*/
|
|
111
|
+
downloading: false,
|
|
112
|
+
/**
|
|
113
|
+
* true if uploading changes.
|
|
114
|
+
*/
|
|
115
|
+
uploading: false
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* All sync streams currently being tracked in teh database.
|
|
122
|
+
*
|
|
123
|
+
* This returns null when the database is currently being opened and we don't have reliable information about all
|
|
124
|
+
* included streams yet.
|
|
125
|
+
*
|
|
126
|
+
* @experimental Sync streams are currently in alpha.
|
|
127
|
+
*/
|
|
128
|
+
get syncStreams(): SyncStreamStatus[] | undefined {
|
|
129
|
+
return this.options.dataFlow?.internalStreamSubscriptions?.map((core) => new SyncStreamStatusView(this, core));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* If the `stream` appears in {@link syncStreams}, returns the current status for that stream.
|
|
134
|
+
*
|
|
135
|
+
* @experimental Sync streams are currently in alpha.
|
|
136
|
+
*/
|
|
137
|
+
forStream(stream: SyncStreamDescription): SyncStreamStatus | undefined {
|
|
138
|
+
const asJson = JSON.stringify(stream.parameters);
|
|
139
|
+
const raw = this.options.dataFlow?.internalStreamSubscriptions?.find(
|
|
140
|
+
(r) => r.name == stream.name && asJson == JSON.stringify(r.parameters)
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
return raw && new SyncStreamStatusView(this, raw);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Provides sync status information for all bucket priorities, sorted by priority (highest first).
|
|
148
|
+
*
|
|
149
|
+
* @returns {SyncPriorityStatus[]} An array of status entries for different sync priority levels,
|
|
150
|
+
* sorted with highest priorities (lower numbers) first.
|
|
151
|
+
*/
|
|
152
|
+
get priorityStatusEntries() {
|
|
153
|
+
return (this.options.priorityStatusEntries ?? []).slice().sort(SyncStatus.comparePriorities);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A realtime progress report on how many operations have been downloaded and
|
|
158
|
+
* how many are necessary in total to complete the next sync iteration.
|
|
159
|
+
*
|
|
160
|
+
* This field is only set when {@link SyncDataFlowStatus#downloading} is also true.
|
|
161
|
+
*/
|
|
162
|
+
get downloadProgress(): SyncProgress | null {
|
|
163
|
+
const internalProgress = this.options.dataFlow?.downloadProgress;
|
|
164
|
+
if (internalProgress == null) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return new SyncProgress(internalProgress);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Reports the sync status (a pair of {@link SyncStatus#hasSynced} and {@link SyncStatus#lastSyncedAt} fields)
|
|
173
|
+
* for a specific bucket priority level.
|
|
174
|
+
*
|
|
175
|
+
* When buckets with different priorities are declared, PowerSync may choose to synchronize higher-priority
|
|
176
|
+
* buckets first. When a consistent view over all buckets for all priorities up until the given priority is
|
|
177
|
+
* reached, PowerSync makes data from those buckets available before lower-priority buckets have finished
|
|
178
|
+
* syncing.
|
|
179
|
+
*
|
|
180
|
+
* This method returns the status for the requested priority or the next higher priority level that has
|
|
181
|
+
* status information available. This is because when PowerSync makes data for a given priority available,
|
|
182
|
+
* all buckets in higher-priorities are guaranteed to be consistent with that checkpoint.
|
|
183
|
+
*
|
|
184
|
+
* For example, if PowerSync just finished synchronizing buckets in priority level 3, calling this method
|
|
185
|
+
* with a priority of 1 may return information for priority level 3.
|
|
186
|
+
*
|
|
187
|
+
* @param {number} priority The bucket priority for which the status should be reported
|
|
188
|
+
* @returns {SyncPriorityStatus} Status information for the requested priority level or the next higher level with available status
|
|
189
|
+
*/
|
|
190
|
+
statusForPriority(priority: number): SyncPriorityStatus {
|
|
191
|
+
// priorityStatusEntries are sorted by ascending priorities (so higher numbers to lower numbers).
|
|
192
|
+
for (const known of this.priorityStatusEntries) {
|
|
193
|
+
// We look for the first entry that doesn't have a higher priority.
|
|
194
|
+
if (known.priority >= priority) {
|
|
195
|
+
return known;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// If we have a complete sync, that necessarily includes all priorities.
|
|
200
|
+
return {
|
|
201
|
+
priority,
|
|
202
|
+
lastSyncedAt: this.lastSyncedAt,
|
|
203
|
+
hasSynced: this.hasSynced
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Compares this SyncStatus instance with another to determine if they are equal.
|
|
209
|
+
* Equality is determined by comparing the serialized JSON representation of both instances.
|
|
210
|
+
*
|
|
211
|
+
* @param {SyncStatus} status The SyncStatus instance to compare against
|
|
212
|
+
* @returns {boolean} True if the instances are considered equal, false otherwise
|
|
213
|
+
*/
|
|
214
|
+
isEqual(status: SyncStatus) {
|
|
215
|
+
/**
|
|
216
|
+
* By default Error object are serialized to an empty object.
|
|
217
|
+
* This replaces Errors with more useful information before serialization.
|
|
218
|
+
*/
|
|
219
|
+
const replacer = (_: string, value: any) => {
|
|
220
|
+
if (value instanceof Error) {
|
|
221
|
+
return {
|
|
222
|
+
name: value.name,
|
|
223
|
+
message: value.message,
|
|
224
|
+
stack: value.stack
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
return value;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
return JSON.stringify(this.options, replacer) == JSON.stringify(status.options, replacer);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Creates a human-readable string representation of the current sync status.
|
|
235
|
+
* Includes information about connection state, sync completion, and data flow.
|
|
236
|
+
*
|
|
237
|
+
* @returns {string} A string representation of the sync status
|
|
238
|
+
*/
|
|
239
|
+
getMessage() {
|
|
240
|
+
const dataFlow = this.dataFlowStatus;
|
|
241
|
+
return `SyncStatus<connected: ${this.connected} connecting: ${this.connecting} lastSyncedAt: ${this.lastSyncedAt} hasSynced: ${this.hasSynced}. Downloading: ${dataFlow.downloading}. Uploading: ${dataFlow.uploading}. UploadError: ${dataFlow.uploadError}, DownloadError?: ${dataFlow.downloadError}>`;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Serializes the SyncStatus instance to a plain object.
|
|
246
|
+
*
|
|
247
|
+
* @returns {SyncStatusOptions} A plain object representation of the sync status
|
|
248
|
+
*/
|
|
249
|
+
toJSON(): SyncStatusOptions {
|
|
250
|
+
return {
|
|
251
|
+
connected: this.connected,
|
|
252
|
+
connecting: this.connecting,
|
|
253
|
+
dataFlow: this.dataFlowStatus,
|
|
254
|
+
lastSyncedAt: this.lastSyncedAt,
|
|
255
|
+
hasSynced: this.hasSynced,
|
|
256
|
+
priorityStatusEntries: this.priorityStatusEntries
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private static comparePriorities(a: SyncPriorityStatus, b: SyncPriorityStatus) {
|
|
261
|
+
return b.priority - a.priority; // Reverse because higher priorities have lower numbers
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Information about a sync stream subscription.
|
|
267
|
+
*/
|
|
268
|
+
export interface SyncStreamStatus {
|
|
269
|
+
progress: ProgressWithOperations | null;
|
|
270
|
+
subscription: SyncSubscriptionDescription;
|
|
271
|
+
priority: number | null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
class SyncStreamStatusView implements SyncStreamStatus {
|
|
275
|
+
subscription: SyncSubscriptionDescription;
|
|
276
|
+
|
|
277
|
+
constructor(
|
|
278
|
+
private status: SyncStatus,
|
|
279
|
+
private core: CoreStreamSubscription
|
|
280
|
+
) {
|
|
281
|
+
this.subscription = {
|
|
282
|
+
name: core.name,
|
|
283
|
+
parameters: core.parameters,
|
|
284
|
+
active: core.active,
|
|
285
|
+
isDefault: core.is_default,
|
|
286
|
+
hasExplicitSubscription: core.has_explicit_subscription,
|
|
287
|
+
expiresAt: core.expires_at != null ? new Date(core.expires_at * 1000) : null,
|
|
288
|
+
hasSynced: core.last_synced_at != null,
|
|
289
|
+
lastSyncedAt: core.last_synced_at != null ? new Date(core.last_synced_at * 1000) : null
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
get progress() {
|
|
294
|
+
if (this.status.dataFlowStatus.downloadProgress == null) {
|
|
295
|
+
// Don't make download progress public if we're not currently downloading.
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const { total, downloaded } = this.core.progress;
|
|
300
|
+
const progress = total == 0 ? 0.0 : downloaded / total;
|
|
301
|
+
|
|
302
|
+
return { totalOperations: total, downloadedOperations: downloaded, downloadedFraction: progress };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
get priority() {
|
|
306
|
+
return this.core.priority;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export class UploadQueueStats {
|
|
2
|
+
constructor(
|
|
3
|
+
/**
|
|
4
|
+
* Number of records in the upload queue.
|
|
5
|
+
*/
|
|
6
|
+
public count: number,
|
|
7
|
+
/**
|
|
8
|
+
* Size of the upload queue in bytes.
|
|
9
|
+
*/
|
|
10
|
+
public size: number | null = null
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
toString() {
|
|
14
|
+
if (this.size == null) {
|
|
15
|
+
return `UploadQueueStats<count:${this.count}>`;
|
|
16
|
+
} else {
|
|
17
|
+
return `UploadQueueStats<count: $count size: ${this.size / 1024}kB>`;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// https://www.sqlite.org/lang_expr.html#castexpr
|
|
2
|
+
export enum ColumnType {
|
|
3
|
+
TEXT = 'TEXT',
|
|
4
|
+
INTEGER = 'INTEGER',
|
|
5
|
+
REAL = 'REAL'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ColumnOptions {
|
|
9
|
+
name: string;
|
|
10
|
+
type?: ColumnType;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type BaseColumnType<T extends number | string | null> = {
|
|
14
|
+
type: ColumnType;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type ColumnsType = Record<string, BaseColumnType<any>>;
|
|
18
|
+
|
|
19
|
+
export type ExtractColumnValueType<T extends BaseColumnType<any>> = T extends BaseColumnType<infer R> ? R : unknown;
|
|
20
|
+
|
|
21
|
+
const text: BaseColumnType<string | null> = {
|
|
22
|
+
type: ColumnType.TEXT
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const integer: BaseColumnType<number | null> = {
|
|
26
|
+
type: ColumnType.INTEGER
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const real: BaseColumnType<number | null> = {
|
|
30
|
+
type: ColumnType.REAL
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// powersync-sqlite-core limits the number of column per table to 1999, due to internal SQLite limits.
|
|
34
|
+
// In earlier versions this was limited to 63.
|
|
35
|
+
export const MAX_AMOUNT_OF_COLUMNS = 1999;
|
|
36
|
+
|
|
37
|
+
export const column = {
|
|
38
|
+
text,
|
|
39
|
+
integer,
|
|
40
|
+
real
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export class Column {
|
|
44
|
+
constructor(protected options: ColumnOptions) {}
|
|
45
|
+
|
|
46
|
+
get name() {
|
|
47
|
+
return this.options.name;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get type() {
|
|
51
|
+
return this.options.type;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
toJSON() {
|
|
55
|
+
return {
|
|
56
|
+
name: this.name,
|
|
57
|
+
type: this.type
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { IndexedColumn } from './IndexedColumn.js';
|
|
2
|
+
import { Table } from './Table.js';
|
|
3
|
+
|
|
4
|
+
export interface IndexOptions {
|
|
5
|
+
name: string;
|
|
6
|
+
columns?: IndexedColumn[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_INDEX_OPTIONS: Partial<IndexOptions> = {
|
|
10
|
+
columns: []
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export class Index {
|
|
14
|
+
static createAscending(options: IndexOptions, columnNames: string[]) {
|
|
15
|
+
return new Index({
|
|
16
|
+
...options,
|
|
17
|
+
columns: columnNames.map((name) => IndexedColumn.createAscending(name))
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
constructor(protected options: IndexOptions) {
|
|
22
|
+
this.options = { ...DEFAULT_INDEX_OPTIONS, ...options };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get name() {
|
|
26
|
+
return this.options.name;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get columns() {
|
|
30
|
+
return this.options.columns ?? [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
toJSON(table: Table) {
|
|
34
|
+
return {
|
|
35
|
+
name: this.name,
|
|
36
|
+
columns: this.columns.map((c) => c.toJSON(table))
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ColumnType } from './Column.js';
|
|
2
|
+
import { Table } from './Table.js';
|
|
3
|
+
|
|
4
|
+
export interface IndexColumnOptions {
|
|
5
|
+
name: string;
|
|
6
|
+
ascending?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_INDEX_COLUMN_OPTIONS: Partial<IndexColumnOptions> = {
|
|
10
|
+
ascending: true
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export class IndexedColumn {
|
|
14
|
+
protected options: IndexColumnOptions;
|
|
15
|
+
|
|
16
|
+
static createAscending(column: string) {
|
|
17
|
+
return new IndexedColumn({
|
|
18
|
+
name: column,
|
|
19
|
+
ascending: true
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
constructor(options: IndexColumnOptions) {
|
|
24
|
+
this.options = { ...DEFAULT_INDEX_COLUMN_OPTIONS, ...options };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get name() {
|
|
28
|
+
return this.options.name;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get ascending() {
|
|
32
|
+
return this.options.ascending;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
toJSON(table: Table) {
|
|
36
|
+
return {
|
|
37
|
+
name: this.name,
|
|
38
|
+
ascending: this.ascending,
|
|
39
|
+
type: table.columns.find((column) => column.name === this.name)?.type ?? ColumnType.TEXT
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A pending variant of a {@link RawTable} that doesn't have a name (because it would be inferred when creating the
|
|
3
|
+
* schema).
|
|
4
|
+
*/
|
|
5
|
+
export type RawTableType = {
|
|
6
|
+
/**
|
|
7
|
+
* The statement to run when PowerSync detects that a row needs to be inserted or updated.
|
|
8
|
+
*/
|
|
9
|
+
put: PendingStatement;
|
|
10
|
+
/**
|
|
11
|
+
* The statement to run when PowerSync detects that a row needs to be deleted.
|
|
12
|
+
*/
|
|
13
|
+
delete: PendingStatement;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A parameter to use as part of {@link PendingStatement}.
|
|
18
|
+
*
|
|
19
|
+
* For delete statements, only the `"Id"` value is supported - the sync client will replace it with the id of the row to
|
|
20
|
+
* be synced.
|
|
21
|
+
*
|
|
22
|
+
* For insert and replace operations, the values of columns in the table are available as parameters through
|
|
23
|
+
* `{Column: 'name'}`.
|
|
24
|
+
*/
|
|
25
|
+
export type PendingStatementParameter = 'Id' | { Column: string };
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A statement that the PowerSync client should use to insert or delete data into a table managed by the user.
|
|
29
|
+
*/
|
|
30
|
+
export type PendingStatement = {
|
|
31
|
+
sql: string;
|
|
32
|
+
params: PendingStatementParameter[];
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Instructs PowerSync to sync data into a "raw" table.
|
|
37
|
+
*
|
|
38
|
+
* Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
|
|
39
|
+
* using client-side table and column constraints.
|
|
40
|
+
*
|
|
41
|
+
* To collect local writes to raw tables with PowerSync, custom triggers are required. See
|
|
42
|
+
* {@link https://docs.powersync.com/usage/use-case-examples/raw-tables the documentation} for details and an example on
|
|
43
|
+
* using raw tables.
|
|
44
|
+
*
|
|
45
|
+
* Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
|
|
46
|
+
*
|
|
47
|
+
* @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or
|
|
48
|
+
* stability guarantees.
|
|
49
|
+
*/
|
|
50
|
+
export class RawTable implements RawTableType {
|
|
51
|
+
/**
|
|
52
|
+
* The name of the table.
|
|
53
|
+
*
|
|
54
|
+
* This does not have to match the actual table name in the schema - {@link put} and {@link delete} are free to use
|
|
55
|
+
* another table. Instead, this name is used by the sync client to recognize that operations on this table (as it
|
|
56
|
+
* appears in the source / backend database) are to be handled specially.
|
|
57
|
+
*/
|
|
58
|
+
name: string;
|
|
59
|
+
put: PendingStatement;
|
|
60
|
+
delete: PendingStatement;
|
|
61
|
+
|
|
62
|
+
constructor(name: string, type: RawTableType) {
|
|
63
|
+
this.name = name;
|
|
64
|
+
this.put = type.put;
|
|
65
|
+
this.delete = type.delete;
|
|
66
|
+
}
|
|
67
|
+
}
|