@powersync/common 1.53.1 → 1.54.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 +560 -353
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +560 -353
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +560 -353
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +560 -353
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +733 -198
- package/lib/attachments/AttachmentContext.d.ts +7 -6
- package/lib/attachments/AttachmentContext.js +2 -1
- package/lib/attachments/AttachmentContext.js.map +1 -1
- package/lib/attachments/AttachmentErrorHandler.d.ts +6 -6
- package/lib/attachments/AttachmentQueue.d.ts +61 -20
- package/lib/attachments/AttachmentQueue.js +16 -18
- package/lib/attachments/AttachmentQueue.js.map +1 -1
- package/lib/attachments/LocalStorageAdapter.d.ts +14 -8
- package/lib/attachments/LocalStorageAdapter.js +3 -0
- package/lib/attachments/LocalStorageAdapter.js.map +1 -1
- package/lib/attachments/RemoteStorageAdapter.d.ts +4 -4
- package/lib/attachments/Schema.d.ts +12 -4
- package/lib/attachments/Schema.js +8 -3
- package/lib/attachments/Schema.js.map +1 -1
- package/lib/attachments/WatchedAttachmentItem.d.ts +3 -1
- package/lib/client/AbstractPowerSyncDatabase.d.ts +110 -58
- package/lib/client/AbstractPowerSyncDatabase.js +59 -48
- package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
- package/lib/client/AbstractPowerSyncOpenFactory.d.ts +6 -0
- package/lib/client/AbstractPowerSyncOpenFactory.js +3 -0
- package/lib/client/AbstractPowerSyncOpenFactory.js.map +1 -1
- package/lib/client/ConnectionManager.d.ts +4 -1
- package/lib/client/ConnectionManager.js +1 -1
- package/lib/client/ConnectionManager.js.map +1 -1
- package/lib/client/Query.d.ts +9 -0
- package/lib/client/SQLOpenFactory.d.ts +12 -0
- package/lib/client/SQLOpenFactory.js +6 -0
- package/lib/client/SQLOpenFactory.js.map +1 -1
- package/lib/client/compilableQueryWatch.d.ts +6 -0
- package/lib/client/compilableQueryWatch.js +3 -0
- package/lib/client/compilableQueryWatch.js.map +1 -1
- package/lib/client/connection/PowerSyncBackendConnector.d.ts +3 -0
- package/lib/client/connection/PowerSyncCredentials.d.ts +3 -0
- package/lib/client/constants.d.ts +3 -0
- package/lib/client/constants.js +3 -0
- package/lib/client/constants.js.map +1 -1
- package/lib/client/runOnSchemaChange.d.ts +3 -0
- package/lib/client/runOnSchemaChange.js +3 -0
- package/lib/client/runOnSchemaChange.js.map +1 -1
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +12 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js +6 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +1 -1
- package/lib/client/sync/bucket/CrudBatch.d.ts +2 -0
- package/lib/client/sync/bucket/CrudBatch.js +2 -0
- package/lib/client/sync/bucket/CrudBatch.js.map +1 -1
- package/lib/client/sync/bucket/CrudEntry.d.ts +9 -0
- package/lib/client/sync/bucket/CrudEntry.js +4 -0
- package/lib/client/sync/bucket/CrudEntry.js.map +1 -1
- package/lib/client/sync/bucket/CrudTransaction.d.ts +3 -0
- package/lib/client/sync/bucket/CrudTransaction.js +3 -0
- package/lib/client/sync/bucket/CrudTransaction.js.map +1 -1
- package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +3 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js +3 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +30 -1
- package/lib/client/sync/stream/AbstractRemote.js +15 -1
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +61 -14
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +60 -47
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/client/sync/stream/JsonValue.d.ts +3 -0
- package/lib/client/sync/stream/WebsocketClientTransport.js +2 -1
- package/lib/client/sync/stream/WebsocketClientTransport.js.map +1 -1
- package/lib/client/sync/sync-streams.d.ts +22 -7
- package/lib/client/triggers/TriggerManager.d.ts +19 -18
- package/lib/client/triggers/TriggerManager.js +2 -1
- package/lib/client/triggers/TriggerManager.js.map +1 -1
- package/lib/client/triggers/TriggerManagerImpl.d.ts +1 -1
- package/lib/client/triggers/TriggerManagerImpl.js +3 -3
- package/lib/client/triggers/TriggerManagerImpl.js.map +1 -1
- package/lib/client/triggers/sanitizeSQL.d.ts +4 -0
- package/lib/client/triggers/sanitizeSQL.js +4 -0
- package/lib/client/triggers/sanitizeSQL.js.map +1 -1
- package/lib/client/watched/GetAllQuery.d.ts +4 -0
- package/lib/client/watched/GetAllQuery.js +2 -0
- package/lib/client/watched/GetAllQuery.js.map +1 -1
- package/lib/client/watched/WatchedQuery.d.ts +24 -2
- package/lib/client/watched/WatchedQuery.js +9 -0
- package/lib/client/watched/WatchedQuery.js.map +1 -1
- package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +1 -1
- package/lib/client/watched/processors/AbstractQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +20 -0
- package/lib/client/watched/processors/DifferentialQueryProcessor.js +4 -0
- package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +4 -0
- package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/comparators.d.ts +8 -0
- package/lib/client/watched/processors/comparators.js +4 -0
- package/lib/client/watched/processors/comparators.js.map +1 -1
- package/lib/db/ConnectionClosedError.d.ts +2 -0
- package/lib/db/ConnectionClosedError.js +2 -0
- package/lib/db/ConnectionClosedError.js.map +1 -1
- package/lib/db/DBAdapter.d.ts +56 -6
- package/lib/db/DBAdapter.js +15 -3
- package/lib/db/DBAdapter.js.map +1 -1
- package/lib/db/crud/SyncProgress.d.ts +6 -1
- package/lib/db/crud/SyncProgress.js +2 -0
- package/lib/db/crud/SyncProgress.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +36 -38
- package/lib/db/crud/SyncStatus.js +19 -14
- package/lib/db/crud/SyncStatus.js.map +1 -1
- package/lib/db/crud/UploadQueueStatus.d.ts +3 -0
- package/lib/db/crud/UploadQueueStatus.js +3 -0
- package/lib/db/crud/UploadQueueStatus.js.map +1 -1
- package/lib/db/schema/Column.d.ts +28 -0
- package/lib/db/schema/Column.js +16 -3
- package/lib/db/schema/Column.js.map +1 -1
- package/lib/db/schema/Index.d.ts +9 -0
- package/lib/db/schema/Index.js +6 -0
- package/lib/db/schema/Index.js.map +1 -1
- package/lib/db/schema/IndexedColumn.d.ts +9 -0
- package/lib/db/schema/IndexedColumn.js +6 -0
- package/lib/db/schema/IndexedColumn.js.map +1 -1
- package/lib/db/schema/RawTable.d.ts +7 -1
- package/lib/db/schema/Schema.d.ts +6 -1
- package/lib/db/schema/Schema.js +3 -1
- package/lib/db/schema/Schema.js.map +1 -1
- package/lib/db/schema/Table.d.ts +27 -3
- package/lib/db/schema/Table.js +9 -0
- package/lib/db/schema/Table.js.map +1 -1
- package/lib/db/schema/TableV2.d.ts +2 -0
- package/lib/db/schema/TableV2.js +2 -0
- package/lib/db/schema/TableV2.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/types/types.d.ts +6 -0
- package/lib/utils/AbortOperation.d.ts +2 -0
- package/lib/utils/AbortOperation.js +2 -0
- package/lib/utils/AbortOperation.js.map +1 -1
- package/lib/utils/BaseObserver.d.ts +12 -0
- package/lib/utils/BaseObserver.js +3 -0
- package/lib/utils/BaseObserver.js.map +1 -1
- package/lib/utils/ControlledExecutor.d.ts +6 -0
- package/lib/utils/ControlledExecutor.js +3 -0
- package/lib/utils/ControlledExecutor.js.map +1 -1
- package/lib/utils/Logger.d.ts +9 -0
- package/lib/utils/Logger.js +6 -0
- package/lib/utils/Logger.js.map +1 -1
- package/lib/utils/async.d.ts +13 -7
- package/lib/utils/async.js +38 -24
- package/lib/utils/async.js.map +1 -1
- package/lib/utils/mutex.d.ts +8 -0
- package/lib/utils/mutex.js +3 -0
- package/lib/utils/mutex.js.map +1 -1
- package/lib/utils/parseQuery.d.ts +6 -0
- package/lib/utils/parseQuery.js +3 -0
- package/lib/utils/parseQuery.js.map +1 -1
- package/lib/utils/stream_transform.d.ts +3 -1
- package/lib/utils/stream_transform.js.map +1 -1
- package/package.json +3 -2
- package/src/attachments/AttachmentContext.ts +7 -6
- package/src/attachments/AttachmentErrorHandler.ts +6 -6
- package/src/attachments/AttachmentQueue.ts +71 -23
- package/src/attachments/LocalStorageAdapter.ts +14 -8
- package/src/attachments/README.md +2 -0
- package/src/attachments/RemoteStorageAdapter.ts +4 -4
- package/src/attachments/Schema.ts +12 -4
- package/src/attachments/WatchedAttachmentItem.ts +3 -1
- package/src/client/AbstractPowerSyncDatabase.ts +117 -62
- package/src/client/AbstractPowerSyncOpenFactory.ts +6 -0
- package/src/client/ConnectionManager.ts +4 -1
- package/src/client/Query.ts +9 -0
- package/src/client/SQLOpenFactory.ts +12 -0
- package/src/client/compilableQueryWatch.ts +6 -0
- package/src/client/connection/PowerSyncBackendConnector.ts +3 -0
- package/src/client/connection/PowerSyncCredentials.ts +3 -0
- package/src/client/constants.ts +3 -0
- package/src/client/runOnSchemaChange.ts +3 -0
- package/src/client/sync/bucket/BucketStorageAdapter.ts +12 -0
- package/src/client/sync/bucket/CrudBatch.ts +2 -0
- package/src/client/sync/bucket/CrudEntry.ts +9 -0
- package/src/client/sync/bucket/CrudTransaction.ts +3 -0
- package/src/client/sync/bucket/SqliteBucketStorage.ts +3 -0
- package/src/client/sync/stream/AbstractRemote.ts +30 -1
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +86 -59
- package/src/client/sync/stream/JsonValue.ts +3 -0
- package/src/client/sync/stream/WebsocketClientTransport.ts +3 -1
- package/src/client/sync/sync-streams.ts +22 -9
- package/src/client/triggers/TriggerManager.ts +19 -18
- package/src/client/triggers/TriggerManagerImpl.ts +5 -5
- package/src/client/triggers/sanitizeSQL.ts +5 -0
- package/src/client/watched/GetAllQuery.ts +5 -1
- package/src/client/watched/WatchedQuery.ts +24 -2
- package/src/client/watched/processors/AbstractQueryProcessor.ts +6 -6
- package/src/client/watched/processors/DifferentialQueryProcessor.ts +28 -5
- package/src/client/watched/processors/OnChangeQueryProcessor.ts +9 -3
- package/src/client/watched/processors/comparators.ts +8 -0
- package/src/db/ConnectionClosedError.ts +2 -0
- package/src/db/DBAdapter.ts +58 -6
- package/src/db/crud/SyncProgress.ts +6 -1
- package/src/db/crud/SyncStatus.ts +40 -21
- package/src/db/crud/UploadQueueStatus.ts +3 -0
- package/src/db/schema/Column.ts +28 -3
- package/src/db/schema/Index.ts +9 -0
- package/src/db/schema/IndexedColumn.ts +9 -0
- package/src/db/schema/RawTable.ts +7 -1
- package/src/db/schema/Schema.ts +8 -3
- package/src/db/schema/Table.ts +30 -5
- package/src/db/schema/TableV2.ts +2 -0
- package/src/index.ts +1 -1
- package/src/types/types.ts +6 -0
- package/src/utils/AbortOperation.ts +2 -0
- package/src/utils/BaseObserver.ts +12 -0
- package/src/utils/ControlledExecutor.ts +6 -0
- package/src/utils/Logger.ts +9 -0
- package/src/utils/async.ts +51 -24
- package/src/utils/mutex.ts +12 -0
- package/src/utils/parseQuery.ts +6 -0
- package/src/utils/stream_transform.ts +3 -1
|
@@ -2,7 +2,8 @@ import { LockContext } from '../../db/DBAdapter.js';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* SQLite operations to track changes for with {@link TriggerManager}
|
|
5
|
-
*
|
|
5
|
+
*
|
|
6
|
+
* @experimental @alpha
|
|
6
7
|
*/
|
|
7
8
|
export enum DiffTriggerOperation {
|
|
8
9
|
INSERT = 'INSERT',
|
|
@@ -11,7 +12,7 @@ export enum DiffTriggerOperation {
|
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
|
-
* @experimental
|
|
15
|
+
* @experimental @alpha
|
|
15
16
|
* Diffs created by {@link TriggerManager#createDiffTrigger} are stored in a temporary table.
|
|
16
17
|
* This is the base record structure for all diff records.
|
|
17
18
|
*
|
|
@@ -42,7 +43,7 @@ export interface BaseTriggerDiffRecord<TOperationId extends string | number = nu
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
/**
|
|
45
|
-
* @experimental
|
|
46
|
+
* @experimental @alpha
|
|
46
47
|
* Represents a diff record for a SQLite UPDATE operation.
|
|
47
48
|
* This record contains the new value and optionally the previous value.
|
|
48
49
|
* Values are stored as JSON strings.
|
|
@@ -62,7 +63,7 @@ export interface TriggerDiffUpdateRecord<
|
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
/**
|
|
65
|
-
* @experimental
|
|
66
|
+
* @experimental @alpha
|
|
66
67
|
* Represents a diff record for a SQLite INSERT operation.
|
|
67
68
|
* This record contains the new value represented as a JSON string.
|
|
68
69
|
*/
|
|
@@ -77,7 +78,7 @@ export interface TriggerDiffInsertRecord<
|
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
/**
|
|
80
|
-
* @experimental
|
|
81
|
+
* @experimental @alpha
|
|
81
82
|
* Represents a diff record for a SQLite DELETE operation.
|
|
82
83
|
* This record contains the new value represented as a JSON string.
|
|
83
84
|
*/
|
|
@@ -92,7 +93,7 @@ export interface TriggerDiffDeleteRecord<
|
|
|
92
93
|
}
|
|
93
94
|
|
|
94
95
|
/**
|
|
95
|
-
* @experimental
|
|
96
|
+
* @experimental @alpha
|
|
96
97
|
* Diffs created by {@link TriggerManager#createDiffTrigger} are stored in a temporary table.
|
|
97
98
|
* This is the record structure for all diff records.
|
|
98
99
|
*
|
|
@@ -121,7 +122,7 @@ export type TriggerDiffRecord<TOperationId extends string | number = number> =
|
|
|
121
122
|
| TriggerDiffDeleteRecord<TOperationId>;
|
|
122
123
|
|
|
123
124
|
/**
|
|
124
|
-
* @experimental
|
|
125
|
+
* @experimental @alpha
|
|
125
126
|
* Querying the DIFF table directly with {@link TriggerDiffHandlerContext#withExtractedDiff} will return records
|
|
126
127
|
* with the tracked columns extracted from the JSON value.
|
|
127
128
|
* This type represents the structure of such records.
|
|
@@ -150,7 +151,7 @@ export type ExtractedTriggerDiffRecord<T, TOperationId extends string | number =
|
|
|
150
151
|
};
|
|
151
152
|
|
|
152
153
|
/**
|
|
153
|
-
* @experimental
|
|
154
|
+
* @experimental @alpha
|
|
154
155
|
* Hooks used in the creation of a table diff trigger.
|
|
155
156
|
*/
|
|
156
157
|
export interface TriggerCreationHooks {
|
|
@@ -213,7 +214,7 @@ interface BaseCreateDiffTriggerOptions {
|
|
|
213
214
|
}
|
|
214
215
|
|
|
215
216
|
/**
|
|
216
|
-
* @experimental
|
|
217
|
+
* @experimental @alpha
|
|
217
218
|
* Options for {@link TriggerManager#createDiffTrigger}.
|
|
218
219
|
*/
|
|
219
220
|
export interface CreateDiffTriggerOptions extends BaseCreateDiffTriggerOptions {
|
|
@@ -232,7 +233,7 @@ export interface CreateDiffTriggerOptions extends BaseCreateDiffTriggerOptions {
|
|
|
232
233
|
}
|
|
233
234
|
|
|
234
235
|
/**
|
|
235
|
-
* @experimental
|
|
236
|
+
* @experimental @alpha
|
|
236
237
|
* Options for {@link TriggerRemoveCallback}.
|
|
237
238
|
*/
|
|
238
239
|
export interface TriggerRemoveCallbackOptions {
|
|
@@ -240,12 +241,12 @@ export interface TriggerRemoveCallbackOptions {
|
|
|
240
241
|
}
|
|
241
242
|
|
|
242
243
|
/**
|
|
243
|
-
* @experimental
|
|
244
|
+
* @experimental @alpha
|
|
244
245
|
* Callback to drop a trigger after it has been created.
|
|
245
246
|
*/
|
|
246
247
|
export type TriggerRemoveCallback = (options?: TriggerRemoveCallbackOptions) => Promise<void>;
|
|
247
248
|
/**
|
|
248
|
-
* @experimental
|
|
249
|
+
* @experimental @alpha
|
|
249
250
|
* Options for {@link TriggerDiffHandlerContext#withDiff}.
|
|
250
251
|
*/
|
|
251
252
|
export interface WithDiffOptions {
|
|
@@ -260,7 +261,7 @@ export interface WithDiffOptions {
|
|
|
260
261
|
}
|
|
261
262
|
|
|
262
263
|
/**
|
|
263
|
-
* @experimental
|
|
264
|
+
* @experimental @alpha
|
|
264
265
|
* Context for the `onChange` handler provided to {@link TriggerManager#trackTableDiff}.
|
|
265
266
|
*/
|
|
266
267
|
export interface TriggerDiffHandlerContext extends LockContext {
|
|
@@ -317,7 +318,7 @@ export interface TriggerDiffHandlerContext extends LockContext {
|
|
|
317
318
|
* Allows querying the database with access to the table containing diff records.
|
|
318
319
|
* The diff table is accessible via the `DIFF` accessor.
|
|
319
320
|
*
|
|
320
|
-
* This is similar to {@link withDiff} but extracts the row columns from the tracked JSON value. The diff operation
|
|
321
|
+
* This is similar to {@link TriggerDiffHandlerContext#withDiff} but extracts the row columns from the tracked JSON value. The diff operation
|
|
321
322
|
* data is aliased as `__` columns to avoid column conflicts.
|
|
322
323
|
*
|
|
323
324
|
* For {@link DiffTriggerOperation#DELETE} operations the previous_value columns are extracted for convenience.
|
|
@@ -349,7 +350,7 @@ export interface TriggerDiffHandlerContext extends LockContext {
|
|
|
349
350
|
}
|
|
350
351
|
|
|
351
352
|
/**
|
|
352
|
-
* @experimental
|
|
353
|
+
* @experimental @alpha
|
|
353
354
|
* Options for tracking changes to a table with {@link TriggerManager#trackTableDiff}.
|
|
354
355
|
*/
|
|
355
356
|
export interface TrackDiffOptions extends BaseCreateDiffTriggerOptions {
|
|
@@ -361,14 +362,14 @@ export interface TrackDiffOptions extends BaseCreateDiffTriggerOptions {
|
|
|
361
362
|
onChange: (context: TriggerDiffHandlerContext) => Promise<void>;
|
|
362
363
|
|
|
363
364
|
/**
|
|
364
|
-
* The minimum interval, in milliseconds, between {@link onChange} invocations.
|
|
365
|
+
* The minimum interval, in milliseconds, between {@link TrackDiffOptions.onChange} invocations.
|
|
365
366
|
* @default {@link DEFAULT_WATCH_THROTTLE_MS}
|
|
366
367
|
*/
|
|
367
368
|
throttleMs?: number;
|
|
368
369
|
}
|
|
369
370
|
|
|
370
371
|
/**
|
|
371
|
-
* @experimental
|
|
372
|
+
* @experimental @alpha
|
|
372
373
|
*/
|
|
373
374
|
export interface TriggerManager {
|
|
374
375
|
/**
|
|
@@ -417,7 +418,7 @@ export interface TriggerManager {
|
|
|
417
418
|
/**
|
|
418
419
|
* @experimental
|
|
419
420
|
* Tracks changes for a table. Triggering a provided handler on changes.
|
|
420
|
-
* Uses {@link createDiffTrigger} internally to create a temporary destination table.
|
|
421
|
+
* Uses {@link TriggerManager.createDiffTrigger} internally to create a temporary destination table.
|
|
421
422
|
*
|
|
422
423
|
* @returns A callback to cleanup the trigger and stop tracking changes.
|
|
423
424
|
*
|
|
@@ -97,8 +97,8 @@ export class TriggerManagerImpl implements TriggerManager {
|
|
|
97
97
|
return this.options.db;
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
protected async getUUID() {
|
|
101
|
-
const { id: uuid } = await this.db.get<{ id: string }>(/* sql */ `
|
|
100
|
+
protected async getUUID(ctx?: LockContext) {
|
|
101
|
+
const { id: uuid } = await (ctx ?? this.db).get<{ id: string }>(/* sql */ `
|
|
102
102
|
SELECT
|
|
103
103
|
uuid () as id
|
|
104
104
|
`);
|
|
@@ -237,7 +237,7 @@ export class TriggerManagerImpl implements TriggerManager {
|
|
|
237
237
|
const internalSource = sourceDefinition.internalName;
|
|
238
238
|
const triggerIds: string[] = [];
|
|
239
239
|
|
|
240
|
-
const id = await this.getUUID();
|
|
240
|
+
const id = await this.getUUID(setupContext);
|
|
241
241
|
|
|
242
242
|
const releaseStorageClaim = useStorage ? await this.options.claimManager.obtainClaim(id) : null;
|
|
243
243
|
|
|
@@ -421,7 +421,7 @@ export class TriggerManagerImpl implements TriggerManager {
|
|
|
421
421
|
const callbackResult = await options.onChange({
|
|
422
422
|
...tx,
|
|
423
423
|
destinationTable: destination,
|
|
424
|
-
withDiff: async <T>(query, params, options?: WithDiffOptions) => {
|
|
424
|
+
withDiff: async <T>(query: string, params: any, options?: WithDiffOptions) => {
|
|
425
425
|
// Wrap the query to expose the destination table
|
|
426
426
|
const operationIdSelect = options?.castOperationIdAsText
|
|
427
427
|
? 'id, operation, CAST(operation_id AS TEXT) as operation_id, timestamp, value, previous_value'
|
|
@@ -439,7 +439,7 @@ export class TriggerManagerImpl implements TriggerManager {
|
|
|
439
439
|
`;
|
|
440
440
|
return tx.getAll<T>(wrappedQuery, params);
|
|
441
441
|
},
|
|
442
|
-
withExtractedDiff: async <T>(query, params) => {
|
|
442
|
+
withExtractedDiff: async <T>(query: string, params: any) => {
|
|
443
443
|
// Wrap the query to expose the destination table
|
|
444
444
|
const wrappedQuery = /* sql */ `
|
|
445
445
|
WITH
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
function sanitizeString(input: string): string {
|
|
2
2
|
return `'${input.replace(/'/g, "''")}'`;
|
|
3
3
|
}
|
|
4
|
+
|
|
4
5
|
/**
|
|
5
6
|
* Helper function for sanitizing UUID input strings.
|
|
6
7
|
* Typically used with {@link sanitizeSQL}.
|
|
8
|
+
*
|
|
9
|
+
* @alpha
|
|
7
10
|
*/
|
|
8
11
|
export function sanitizeUUID(uuid: string): string {
|
|
9
12
|
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
@@ -41,6 +44,8 @@ export function sanitizeUUID(uuid: string): string {
|
|
|
41
44
|
* // Incorrect:
|
|
42
45
|
* sanitizeSQL`New.id = '${myID}'` // Produces double quotes: New.id = ''O''Reilly''
|
|
43
46
|
* ```
|
|
47
|
+
*
|
|
48
|
+
* @alpha
|
|
44
49
|
*/
|
|
45
50
|
export function sanitizeSQL(strings: TemplateStringsArray, ...values: any[]): string {
|
|
46
51
|
let result = '';
|
|
@@ -4,6 +4,8 @@ import { WatchCompatibleQuery } from './WatchedQuery.js';
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Options for {@link GetAllQuery}.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
7
9
|
*/
|
|
8
10
|
export type GetAllQueryOptions<RowType = unknown> = {
|
|
9
11
|
sql: string;
|
|
@@ -23,6 +25,8 @@ export type GetAllQueryOptions<RowType = unknown> = {
|
|
|
23
25
|
|
|
24
26
|
/**
|
|
25
27
|
* Performs a {@link AbstractPowerSyncDatabase.getAll} operation for a watched query.
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
26
30
|
*/
|
|
27
31
|
export class GetAllQuery<RowType = unknown> implements WatchCompatibleQuery<RowType[]> {
|
|
28
32
|
constructor(protected options: GetAllQueryOptions<RowType>) {}
|
|
@@ -37,7 +41,7 @@ export class GetAllQuery<RowType = unknown> implements WatchCompatibleQuery<RowT
|
|
|
37
41
|
async execute(options: { db: AbstractPowerSyncDatabase }): Promise<RowType[]> {
|
|
38
42
|
const { db } = options;
|
|
39
43
|
const { sql, parameters = [] } = this.compile();
|
|
40
|
-
const rawResult = await db.getAll<unknown
|
|
44
|
+
const rawResult = await db.getAll<Record<string, unknown>>(sql, [...parameters]);
|
|
41
45
|
if (this.options.mapper) {
|
|
42
46
|
return rawResult.map(this.options.mapper);
|
|
43
47
|
}
|
|
@@ -5,6 +5,8 @@ import { AbstractPowerSyncDatabase } from '../AbstractPowerSyncDatabase.js';
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* State for {@link WatchedQuery} instances.
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
8
10
|
*/
|
|
9
11
|
export interface WatchedQueryState<Data> {
|
|
10
12
|
/**
|
|
@@ -33,6 +35,8 @@ export interface WatchedQueryState<Data> {
|
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* Options provided to the `execute` method of a {@link WatchCompatibleQuery}.
|
|
38
|
+
*
|
|
39
|
+
* @public
|
|
36
40
|
*/
|
|
37
41
|
export interface WatchExecuteOptions {
|
|
38
42
|
sql: string;
|
|
@@ -41,14 +45,17 @@ export interface WatchExecuteOptions {
|
|
|
41
45
|
}
|
|
42
46
|
|
|
43
47
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
48
|
+
*
|
|
49
|
+
* @public
|
|
46
50
|
*/
|
|
47
51
|
export interface WatchCompatibleQuery<ResultType> {
|
|
48
52
|
execute(options: WatchExecuteOptions): Promise<ResultType>;
|
|
49
53
|
compile(): CompiledQuery;
|
|
50
54
|
}
|
|
51
55
|
|
|
56
|
+
/**
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
52
59
|
export interface WatchedQueryOptions {
|
|
53
60
|
/** The minimum interval between queries. */
|
|
54
61
|
throttleMs?: number;
|
|
@@ -67,6 +74,9 @@ export interface WatchedQueryOptions {
|
|
|
67
74
|
triggerOnTables?: string[];
|
|
68
75
|
}
|
|
69
76
|
|
|
77
|
+
/**
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
70
80
|
export enum WatchedQueryListenerEvent {
|
|
71
81
|
ON_DATA = 'onData',
|
|
72
82
|
ON_ERROR = 'onError',
|
|
@@ -75,6 +85,9 @@ export enum WatchedQueryListenerEvent {
|
|
|
75
85
|
CLOSED = 'closed'
|
|
76
86
|
}
|
|
77
87
|
|
|
88
|
+
/**
|
|
89
|
+
* @public
|
|
90
|
+
*/
|
|
78
91
|
export interface WatchedQueryListener<Data> extends BaseListener {
|
|
79
92
|
[WatchedQueryListenerEvent.ON_DATA]?: (data: Data) => void | Promise<void>;
|
|
80
93
|
[WatchedQueryListenerEvent.ON_ERROR]?: (error: Error) => void | Promise<void>;
|
|
@@ -83,13 +96,22 @@ export interface WatchedQueryListener<Data> extends BaseListener {
|
|
|
83
96
|
[WatchedQueryListenerEvent.CLOSED]?: () => void | Promise<void>;
|
|
84
97
|
}
|
|
85
98
|
|
|
99
|
+
/**
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
86
102
|
export const DEFAULT_WATCH_THROTTLE_MS = 30;
|
|
87
103
|
|
|
104
|
+
/**
|
|
105
|
+
* @internal
|
|
106
|
+
*/
|
|
88
107
|
export const DEFAULT_WATCH_QUERY_OPTIONS: WatchedQueryOptions = {
|
|
89
108
|
throttleMs: DEFAULT_WATCH_THROTTLE_MS,
|
|
90
109
|
reportFetching: true
|
|
91
110
|
};
|
|
92
111
|
|
|
112
|
+
/**
|
|
113
|
+
* @public
|
|
114
|
+
*/
|
|
93
115
|
export interface WatchedQuery<
|
|
94
116
|
Data = unknown,
|
|
95
117
|
Settings extends WatchedQueryOptions = WatchedQueryOptions,
|
|
@@ -45,9 +45,9 @@ type WatchedQueryProcessorListener<Data> = WatchedQueryListener<Data>;
|
|
|
45
45
|
* @internal
|
|
46
46
|
*/
|
|
47
47
|
export abstract class AbstractQueryProcessor<
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
Data = unknown[],
|
|
49
|
+
Settings extends WatchedQueryOptions = WatchedQueryOptions
|
|
50
|
+
>
|
|
51
51
|
extends MetaBaseObserver<WatchedQueryProcessorListener<Data>>
|
|
52
52
|
implements WatchedQuery<Data, Settings>
|
|
53
53
|
{
|
|
@@ -127,7 +127,7 @@ export abstract class AbstractQueryProcessor<
|
|
|
127
127
|
|
|
128
128
|
/**
|
|
129
129
|
* This method is used to link a query to the subscribers of this listener class.
|
|
130
|
-
* This method should perform actual query watching and report results via {@link updateState} method.
|
|
130
|
+
* This method should perform actual query watching and report results via {@link AbstractQueryProcessor.updateState} method.
|
|
131
131
|
*/
|
|
132
132
|
protected abstract linkQuery(options: LinkQueryOptions<Data>): Promise<void>;
|
|
133
133
|
|
|
@@ -199,7 +199,7 @@ export abstract class AbstractQueryProcessor<
|
|
|
199
199
|
protected async runWithReporting<T>(callback: () => Promise<T>): Promise<void> {
|
|
200
200
|
try {
|
|
201
201
|
await callback();
|
|
202
|
-
} catch (error) {
|
|
202
|
+
} catch (error: any) {
|
|
203
203
|
// This will update the error on the state and iterate error listeners
|
|
204
204
|
await this.updateState({ error });
|
|
205
205
|
}
|
|
@@ -213,7 +213,7 @@ export abstract class AbstractQueryProcessor<
|
|
|
213
213
|
) {
|
|
214
214
|
try {
|
|
215
215
|
await this.iterateAsyncListeners(async (l) => callback(l));
|
|
216
|
-
} catch (error) {
|
|
216
|
+
} catch (error: any) {
|
|
217
217
|
try {
|
|
218
218
|
await this.iterateAsyncListeners(async (l) => l.onError?.(error));
|
|
219
219
|
} catch (error) {
|
|
@@ -9,6 +9,8 @@ import {
|
|
|
9
9
|
/**
|
|
10
10
|
* Represents an updated row in a differential watched query.
|
|
11
11
|
* It contains both the current and previous state of the row.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
12
14
|
*/
|
|
13
15
|
export interface WatchedQueryRowDifferential<RowType> {
|
|
14
16
|
readonly current: RowType;
|
|
@@ -18,6 +20,8 @@ export interface WatchedQueryRowDifferential<RowType> {
|
|
|
18
20
|
/**
|
|
19
21
|
* Represents the result of a watched query that has been diffed.
|
|
20
22
|
* {@link DifferentialWatchedQueryState#diff} is of the {@link WatchedQueryDifferential} form.
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
21
25
|
*/
|
|
22
26
|
export interface WatchedQueryDifferential<RowType> {
|
|
23
27
|
readonly added: ReadonlyArray<Readonly<RowType>>;
|
|
@@ -43,6 +47,8 @@ export interface WatchedQueryDifferential<RowType> {
|
|
|
43
47
|
|
|
44
48
|
/**
|
|
45
49
|
* Row comparator for differentially watched queries which keys and compares items in the result set.
|
|
50
|
+
*
|
|
51
|
+
* @public
|
|
46
52
|
*/
|
|
47
53
|
export interface DifferentialWatchedQueryComparator<RowType> {
|
|
48
54
|
/**
|
|
@@ -57,6 +63,8 @@ export interface DifferentialWatchedQueryComparator<RowType> {
|
|
|
57
63
|
|
|
58
64
|
/**
|
|
59
65
|
* Options for building a differential watched query with the {@link Query} builder.
|
|
66
|
+
*
|
|
67
|
+
* @public
|
|
60
68
|
*/
|
|
61
69
|
export interface DifferentialWatchedQueryOptions<RowType> extends WatchedQueryOptions {
|
|
62
70
|
/**
|
|
@@ -75,6 +83,8 @@ export interface DifferentialWatchedQueryOptions<RowType> extends WatchedQueryOp
|
|
|
75
83
|
|
|
76
84
|
/**
|
|
77
85
|
* Settings for differential incremental watched queries using.
|
|
86
|
+
*
|
|
87
|
+
* @public
|
|
78
88
|
*/
|
|
79
89
|
export interface DifferentialWatchedQuerySettings<RowType> extends DifferentialWatchedQueryOptions<RowType> {
|
|
80
90
|
/**
|
|
@@ -83,11 +93,18 @@ export interface DifferentialWatchedQuerySettings<RowType> extends DifferentialW
|
|
|
83
93
|
query: WatchCompatibleQuery<RowType[]>;
|
|
84
94
|
}
|
|
85
95
|
|
|
86
|
-
|
|
87
|
-
|
|
96
|
+
/**
|
|
97
|
+
* @public
|
|
98
|
+
*/
|
|
99
|
+
export interface DifferentialWatchedQueryListener<RowType> extends WatchedQueryListener<
|
|
100
|
+
ReadonlyArray<Readonly<RowType>>
|
|
101
|
+
> {
|
|
88
102
|
onDiff?: (diff: WatchedQueryDifferential<RowType>) => void | Promise<void>;
|
|
89
103
|
}
|
|
90
104
|
|
|
105
|
+
/**
|
|
106
|
+
* @public
|
|
107
|
+
*/
|
|
91
108
|
export type DifferentialWatchedQuery<RowType> = WatchedQuery<
|
|
92
109
|
ReadonlyArray<Readonly<RowType>>,
|
|
93
110
|
DifferentialWatchedQuerySettings<RowType>,
|
|
@@ -97,8 +114,10 @@ export type DifferentialWatchedQuery<RowType> = WatchedQuery<
|
|
|
97
114
|
/**
|
|
98
115
|
* @internal
|
|
99
116
|
*/
|
|
100
|
-
export interface DifferentialQueryProcessorOptions<RowType>
|
|
101
|
-
|
|
117
|
+
export interface DifferentialQueryProcessorOptions<RowType> extends AbstractQueryProcessorOptions<
|
|
118
|
+
RowType[],
|
|
119
|
+
DifferentialWatchedQuerySettings<RowType>
|
|
120
|
+
> {
|
|
102
121
|
rowComparator?: DifferentialWatchedQueryComparator<RowType>;
|
|
103
122
|
}
|
|
104
123
|
|
|
@@ -107,6 +126,8 @@ type DataHashMap<RowType> = Map<string, { hash: string; item: RowType }>;
|
|
|
107
126
|
/**
|
|
108
127
|
* An empty differential result set.
|
|
109
128
|
* This is used as the initial state for differential incrementally watched queries.
|
|
129
|
+
*
|
|
130
|
+
* @internal
|
|
110
131
|
*/
|
|
111
132
|
export const EMPTY_DIFFERENTIAL = {
|
|
112
133
|
added: [],
|
|
@@ -120,6 +141,8 @@ export const EMPTY_DIFFERENTIAL = {
|
|
|
120
141
|
* Default implementation of the {@link DifferentialWatchedQueryComparator} for watched queries.
|
|
121
142
|
* It keys items by their `id` property if available, alternatively it uses JSON stringification
|
|
122
143
|
* of the entire item for the key and comparison.
|
|
144
|
+
*
|
|
145
|
+
* @internal
|
|
123
146
|
*/
|
|
124
147
|
export const DEFAULT_ROW_COMPARATOR: DifferentialWatchedQueryComparator<any> = {
|
|
125
148
|
keyBy: (item) => {
|
|
@@ -286,7 +309,7 @@ export class DifferentialQueryProcessor<RowType>
|
|
|
286
309
|
if (Object.keys(partialStateUpdate).length > 0) {
|
|
287
310
|
await this.updateState(partialStateUpdate);
|
|
288
311
|
}
|
|
289
|
-
} catch (error) {
|
|
312
|
+
} catch (error: any) {
|
|
290
313
|
await this.updateState({ error });
|
|
291
314
|
}
|
|
292
315
|
},
|
|
@@ -9,6 +9,8 @@ import { WatchedQueryComparator } from './comparators.js';
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Settings for {@link WatchedQuery} instances created via {@link Query#watch}.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
12
14
|
*/
|
|
13
15
|
export interface WatchedQuerySettings<DataType> extends WatchedQueryOptions {
|
|
14
16
|
query: WatchCompatibleQuery<DataType>;
|
|
@@ -16,14 +18,18 @@ export interface WatchedQuerySettings<DataType> extends WatchedQueryOptions {
|
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* {@link WatchedQuery} returned from {@link Query#watch}.
|
|
21
|
+
*
|
|
22
|
+
* @public
|
|
19
23
|
*/
|
|
20
24
|
export type StandardWatchedQuery<DataType> = WatchedQuery<DataType, WatchedQuerySettings<DataType>>;
|
|
21
25
|
|
|
22
26
|
/**
|
|
23
27
|
* @internal
|
|
24
28
|
*/
|
|
25
|
-
export interface OnChangeQueryProcessorOptions<Data>
|
|
26
|
-
|
|
29
|
+
export interface OnChangeQueryProcessorOptions<Data> extends AbstractQueryProcessorOptions<
|
|
30
|
+
Data,
|
|
31
|
+
WatchedQuerySettings<Data>
|
|
32
|
+
> {
|
|
27
33
|
comparator?: WatchedQueryComparator<Data>;
|
|
28
34
|
}
|
|
29
35
|
|
|
@@ -103,7 +109,7 @@ export class OnChangeQueryProcessor<Data> extends AbstractQueryProcessor<Data, W
|
|
|
103
109
|
if (Object.keys(partialStateUpdate).length > 0) {
|
|
104
110
|
await this.updateState(partialStateUpdate);
|
|
105
111
|
}
|
|
106
|
-
} catch (error) {
|
|
112
|
+
} catch (error: any) {
|
|
107
113
|
await this.updateState({ error });
|
|
108
114
|
}
|
|
109
115
|
},
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
* A basic comparator for incrementally watched queries. This performs a single comparison which
|
|
3
3
|
* determines if the result set has changed. The {@link WatchedQuery} will only emit the new result
|
|
4
4
|
* if a change has been detected.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
5
7
|
*/
|
|
6
8
|
export interface WatchedQueryComparator<Data> {
|
|
7
9
|
checkEquality: (current: Data, previous: Data) => boolean;
|
|
@@ -9,6 +11,8 @@ export interface WatchedQueryComparator<Data> {
|
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Options for {@link ArrayComparator}
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
12
16
|
*/
|
|
13
17
|
export type ArrayComparatorOptions<ItemType> = {
|
|
14
18
|
/**
|
|
@@ -20,6 +24,8 @@ export type ArrayComparatorOptions<ItemType> = {
|
|
|
20
24
|
/**
|
|
21
25
|
* An efficient comparator for {@link WatchedQuery} created with {@link Query#watch}. This has the ability to determine if a query
|
|
22
26
|
* result has changes without necessarily processing all items in the result.
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
23
29
|
*/
|
|
24
30
|
export class ArrayComparator<ItemType> implements WatchedQueryComparator<ItemType[]> {
|
|
25
31
|
constructor(protected options: ArrayComparatorOptions<ItemType>) {}
|
|
@@ -51,6 +57,8 @@ export class ArrayComparator<ItemType> implements WatchedQueryComparator<ItemTyp
|
|
|
51
57
|
|
|
52
58
|
/**
|
|
53
59
|
* Watched query comparator that always reports changed result sets.
|
|
60
|
+
*
|
|
61
|
+
* @public
|
|
54
62
|
*/
|
|
55
63
|
export const FalsyComparator: WatchedQueryComparator<unknown> = {
|
|
56
64
|
checkEquality: () => false // Default comparator that always returns false
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
* Thrown when an underlying database connection is closed.
|
|
3
3
|
* This is particularly relevant when worker connections are marked as closed while
|
|
4
4
|
* operations are still in progress.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
5
7
|
*/
|
|
6
8
|
export class ConnectionClosedError extends Error {
|
|
7
9
|
static NAME = 'ConnectionClosedError';
|