@powersync/common 0.0.0-dev-20260504100448 → 0.0.0-dev-20260630144038
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/attachments/AttachmentContext.d.ts +9 -8
- package/lib/attachments/AttachmentContext.js +8 -3
- package/lib/attachments/AttachmentContext.js.map +1 -1
- package/lib/attachments/AttachmentErrorHandler.d.ts +6 -6
- package/lib/attachments/AttachmentQueue.d.ts +85 -36
- package/lib/attachments/AttachmentQueue.js +19 -21
- package/lib/attachments/AttachmentQueue.js.map +1 -1
- package/lib/attachments/AttachmentService.d.ts +3 -3
- package/lib/attachments/AttachmentService.js +4 -3
- package/lib/attachments/AttachmentService.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 +14 -6
- package/lib/attachments/Schema.js +8 -3
- package/lib/attachments/Schema.js.map +1 -1
- package/lib/attachments/SyncingService.d.ts +2 -2
- package/lib/attachments/SyncingService.js +8 -3
- package/lib/attachments/SyncingService.js.map +1 -1
- package/lib/attachments/WatchedAttachmentItem.d.ts +3 -1
- package/lib/client/{AbstractPowerSyncDatabase.d.ts → CommonPowerSyncDatabase.d.ts} +112 -238
- package/lib/client/CommonPowerSyncDatabase.js +2 -0
- package/lib/client/CommonPowerSyncDatabase.js.map +1 -0
- package/lib/client/Query.d.ts +9 -0
- package/lib/client/SQLOpenFactory.d.ts +44 -10
- package/lib/client/SQLOpenFactory.js +1 -19
- package/lib/client/SQLOpenFactory.js.map +1 -1
- package/lib/client/compilableQueryWatch.d.ts +8 -2
- package/lib/client/compilableQueryWatch.js +4 -2
- package/lib/client/compilableQueryWatch.js.map +1 -1
- package/lib/client/connection/PowerSyncBackendConnector.d.ts +6 -3
- package/lib/client/connection/PowerSyncCredentials.d.ts +3 -0
- package/lib/client/runOnSchemaChange.d.ts +5 -2
- package/lib/client/runOnSchemaChange.js +3 -0
- package/lib/client/runOnSchemaChange.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 -31
- package/lib/client/sync/bucket/CrudEntry.js +2 -97
- 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/options.d.ts +65 -0
- package/lib/client/sync/options.js +25 -0
- package/lib/client/sync/options.js.map +1 -0
- package/lib/client/sync/stream/JsonValue.d.ts +3 -0
- package/lib/client/sync/sync-streams.d.ts +23 -8
- package/lib/client/triggers/TriggerManager.d.ts +28 -56
- package/lib/client/triggers/TriggerManager.js +2 -1
- package/lib/client/triggers/TriggerManager.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 +7 -3
- package/lib/client/watched/GetAllQuery.js +3 -1
- package/lib/client/watched/GetAllQuery.js.map +1 -1
- package/lib/client/watched/WatchedQuery.d.ts +20 -6
- package/lib/client/watched/WatchedQuery.js +3 -5
- package/lib/client/watched/WatchedQuery.js.map +1 -1
- package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +15 -45
- package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -172
- package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +4 -22
- package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -82
- 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/DBAdapter.d.ts +90 -113
- package/lib/db/DBAdapter.js +70 -89
- package/lib/db/DBAdapter.js.map +1 -1
- package/lib/db/QueryResult.d.ts +104 -0
- package/lib/db/QueryResult.js +96 -0
- package/lib/db/QueryResult.js.map +1 -0
- package/lib/db/crud/SyncProgress.d.ts +7 -14
- package/lib/db/crud/SyncProgress.js +1 -60
- package/lib/db/crud/SyncProgress.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +54 -91
- package/lib/db/crud/SyncStatus.js +1 -245
- 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 +22 -1
- package/lib/db/schema/Column.js +10 -4
- package/lib/db/schema/Column.js.map +1 -1
- package/lib/db/schema/Index.d.ts +8 -3
- package/lib/db/schema/Index.js +4 -1
- package/lib/db/schema/Index.js.map +1 -1
- package/lib/db/schema/IndexedColumn.d.ts +8 -3
- package/lib/db/schema/IndexedColumn.js +4 -1
- 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 +10 -29
- package/lib/db/schema/Schema.js +3 -1
- package/lib/db/schema/Schema.js.map +1 -1
- package/lib/db/schema/Table.d.ts +69 -91
- package/lib/db/schema/Table.js +88 -87
- package/lib/db/schema/Table.js.map +1 -1
- package/lib/index.d.ts +6 -22
- package/lib/index.js +5 -22
- package/lib/index.js.map +1 -1
- package/lib/types/types.d.ts +6 -0
- 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/Logger.d.ts +60 -22
- package/lib/utils/Logger.js +38 -30
- package/lib/utils/Logger.js.map +1 -1
- package/lib/utils/MetaBaseObserver.d.ts +11 -11
- package/lib/utils/MetaBaseObserver.js +1 -50
- package/lib/utils/MetaBaseObserver.js.map +1 -1
- package/lib/utils/mutex.d.ts +3 -45
- package/lib/utils/mutex.js +1 -153
- package/lib/utils/mutex.js.map +1 -1
- package/package.json +9 -46
- package/src/attachments/AttachmentContext.ts +15 -13
- package/src/attachments/AttachmentErrorHandler.ts +6 -6
- package/src/attachments/AttachmentQueue.ts +99 -42
- package/src/attachments/AttachmentService.ts +7 -6
- 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 +14 -6
- package/src/attachments/SyncingService.ts +10 -6
- package/src/attachments/WatchedAttachmentItem.ts +3 -1
- package/src/client/CommonPowerSyncDatabase.ts +540 -0
- package/src/client/Query.ts +9 -0
- package/src/client/SQLOpenFactory.ts +47 -19
- package/src/client/compilableQueryWatch.ts +9 -4
- package/src/client/connection/PowerSyncBackendConnector.ts +6 -3
- package/src/client/connection/PowerSyncCredentials.ts +3 -0
- package/src/client/runOnSchemaChange.ts +5 -2
- package/src/client/sync/bucket/CrudBatch.ts +2 -0
- package/src/client/sync/bucket/CrudEntry.ts +10 -101
- package/src/client/sync/bucket/CrudTransaction.ts +3 -0
- package/src/client/sync/options.ts +77 -0
- package/src/client/sync/stream/JsonValue.ts +3 -0
- package/src/client/sync/sync-streams.ts +23 -10
- package/src/client/triggers/TriggerManager.ts +29 -59
- package/src/client/triggers/sanitizeSQL.ts +5 -0
- package/src/client/watched/GetAllQuery.ts +8 -4
- package/src/client/watched/WatchedQuery.ts +20 -11
- package/src/client/watched/processors/DifferentialQueryProcessor.ts +19 -219
- package/src/client/watched/processors/OnChangeQueryProcessor.ts +4 -109
- package/src/client/watched/processors/comparators.ts +8 -0
- package/src/db/DBAdapter.ts +160 -175
- package/src/db/QueryResult.ts +195 -0
- package/src/db/crud/SyncProgress.ts +8 -43
- package/src/db/crud/SyncStatus.ts +66 -216
- package/src/db/crud/UploadQueueStatus.ts +3 -0
- package/src/db/schema/Column.ts +22 -5
- package/src/db/schema/Index.ts +9 -3
- package/src/db/schema/IndexedColumn.ts +9 -3
- package/src/db/schema/RawTable.ts +7 -1
- package/src/db/schema/Schema.ts +12 -7
- package/src/db/schema/Table.ts +130 -180
- package/src/index.ts +6 -22
- package/src/types/types.ts +6 -0
- package/src/utils/BaseObserver.ts +12 -0
- package/src/utils/Logger.ts +86 -31
- package/src/utils/MetaBaseObserver.ts +14 -60
- package/src/utils/mutex.ts +4 -189
- package/dist/bundle.cjs +0 -14136
- package/dist/bundle.cjs.map +0 -1
- package/dist/bundle.mjs +0 -14056
- package/dist/bundle.mjs.map +0 -1
- package/dist/bundle.node.cjs +0 -11613
- package/dist/bundle.node.cjs.map +0 -1
- package/dist/bundle.node.mjs +0 -11533
- package/dist/bundle.node.mjs.map +0 -1
- package/dist/index.d.cts +0 -4052
- package/legacy/sync_protocol.d.ts +0 -103
- package/lib/client/AbstractPowerSyncDatabase.js +0 -987
- package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
- package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -22
- package/lib/client/AbstractPowerSyncOpenFactory.js +0 -25
- package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
- package/lib/client/ConnectionManager.d.ts +0 -112
- package/lib/client/ConnectionManager.js +0 -294
- package/lib/client/ConnectionManager.js.map +0 -1
- package/lib/client/CustomQuery.d.ts +0 -22
- package/lib/client/CustomQuery.js +0 -43
- package/lib/client/CustomQuery.js.map +0 -1
- package/lib/client/constants.d.ts +0 -1
- package/lib/client/constants.js +0 -2
- package/lib/client/constants.js.map +0 -1
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -40
- package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -19
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
- package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -34
- package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -162
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +0 -151
- package/lib/client/sync/stream/AbstractRemote.js +0 -473
- package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -218
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -663
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
- package/lib/client/sync/stream/WebsocketClientTransport.d.ts +0 -15
- package/lib/client/sync/stream/WebsocketClientTransport.js +0 -61
- package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
- package/lib/client/sync/stream/core-instruction.d.ts +0 -71
- package/lib/client/sync/stream/core-instruction.js +0 -27
- package/lib/client/sync/stream/core-instruction.js.map +0 -1
- package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +0 -6
- package/lib/client/triggers/MemoryTriggerClaimManager.js +0 -21
- package/lib/client/triggers/MemoryTriggerClaimManager.js.map +0 -1
- package/lib/client/triggers/TriggerManagerImpl.d.ts +0 -39
- package/lib/client/triggers/TriggerManagerImpl.js +0 -405
- package/lib/client/triggers/TriggerManagerImpl.js.map +0 -1
- package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -68
- package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -151
- package/lib/client/watched/processors/AbstractQueryProcessor.js.map +0 -1
- package/lib/db/ConnectionClosedError.d.ts +0 -10
- package/lib/db/ConnectionClosedError.js +0 -21
- package/lib/db/ConnectionClosedError.js.map +0 -1
- package/lib/db/schema/TableV2.d.ts +0 -9
- package/lib/db/schema/TableV2.js +0 -9
- package/lib/db/schema/TableV2.js.map +0 -1
- package/lib/utils/AbortOperation.d.ts +0 -9
- package/lib/utils/AbortOperation.js +0 -19
- package/lib/utils/AbortOperation.js.map +0 -1
- package/lib/utils/ControlledExecutor.d.ts +0 -25
- package/lib/utils/ControlledExecutor.js +0 -51
- package/lib/utils/ControlledExecutor.js.map +0 -1
- package/lib/utils/async.d.ts +0 -14
- package/lib/utils/async.js +0 -46
- package/lib/utils/async.js.map +0 -1
- package/lib/utils/parseQuery.d.ts +0 -6
- package/lib/utils/parseQuery.js +0 -17
- package/lib/utils/parseQuery.js.map +0 -1
- package/lib/utils/queue.d.ts +0 -16
- package/lib/utils/queue.js +0 -42
- package/lib/utils/queue.js.map +0 -1
- package/lib/utils/stream_transform.d.ts +0 -39
- package/lib/utils/stream_transform.js +0 -206
- package/lib/utils/stream_transform.js.map +0 -1
- package/src/client/AbstractPowerSyncDatabase.ts +0 -1362
- package/src/client/AbstractPowerSyncOpenFactory.ts +0 -39
- package/src/client/ConnectionManager.ts +0 -402
- package/src/client/CustomQuery.ts +0 -56
- package/src/client/constants.ts +0 -1
- package/src/client/sync/bucket/BucketStorageAdapter.ts +0 -49
- package/src/client/sync/bucket/SqliteBucketStorage.ts +0 -199
- package/src/client/sync/stream/AbstractRemote.ts +0 -602
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +0 -905
- package/src/client/sync/stream/WebsocketClientTransport.ts +0 -80
- package/src/client/sync/stream/core-instruction.ts +0 -98
- package/src/client/triggers/MemoryTriggerClaimManager.ts +0 -25
- package/src/client/triggers/TriggerManagerImpl.ts +0 -496
- package/src/client/watched/processors/AbstractQueryProcessor.ts +0 -226
- package/src/db/ConnectionClosedError.ts +0 -23
- package/src/db/schema/TableV2.ts +0 -9
- package/src/utils/AbortOperation.ts +0 -17
- package/src/utils/ControlledExecutor.ts +0 -72
- package/src/utils/async.ts +0 -50
- package/src/utils/parseQuery.ts +0 -25
- package/src/utils/queue.ts +0 -48
- package/src/utils/stream_transform.ts +0 -252
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { CompiledQuery } from '../../types/types.js';
|
|
2
|
-
import {
|
|
2
|
+
import { CommonPowerSyncDatabase } from '../CommonPowerSyncDatabase.js';
|
|
3
3
|
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;
|
|
@@ -22,7 +24,9 @@ export type GetAllQueryOptions<RowType = unknown> = {
|
|
|
22
24
|
};
|
|
23
25
|
|
|
24
26
|
/**
|
|
25
|
-
* Performs a {@link
|
|
27
|
+
* Performs a {@link DBGetUtils.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>) {}
|
|
@@ -34,10 +38,10 @@ export class GetAllQuery<RowType = unknown> implements WatchCompatibleQuery<RowT
|
|
|
34
38
|
};
|
|
35
39
|
}
|
|
36
40
|
|
|
37
|
-
async execute(options: { db:
|
|
41
|
+
async execute(options: { db: CommonPowerSyncDatabase }): 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
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { CompiledQuery } from '../../types/types.js';
|
|
2
2
|
import { BaseListener } from '../../utils/BaseObserver.js';
|
|
3
3
|
import { MetaBaseObserverInterface } from '../../utils/MetaBaseObserver.js';
|
|
4
|
-
import {
|
|
4
|
+
import { CommonPowerSyncDatabase } from '../CommonPowerSyncDatabase.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,22 +35,27 @@ 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;
|
|
39
43
|
parameters: any[];
|
|
40
|
-
db:
|
|
44
|
+
db: CommonPowerSyncDatabase;
|
|
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,9 @@ export interface WatchedQueryListener<Data> extends BaseListener {
|
|
|
83
96
|
[WatchedQueryListenerEvent.CLOSED]?: () => void | Promise<void>;
|
|
84
97
|
}
|
|
85
98
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
throttleMs: DEFAULT_WATCH_THROTTLE_MS,
|
|
90
|
-
reportFetching: true
|
|
91
|
-
};
|
|
92
|
-
|
|
99
|
+
/**
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
93
102
|
export interface WatchedQuery<
|
|
94
103
|
Data = unknown,
|
|
95
104
|
Settings extends WatchedQueryOptions = WatchedQueryOptions,
|
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
import { WatchCompatibleQuery, WatchedQuery, WatchedQueryListener, WatchedQueryOptions } from '../WatchedQuery.js';
|
|
2
|
-
import {
|
|
3
|
-
AbstractQueryProcessor,
|
|
4
|
-
AbstractQueryProcessorOptions,
|
|
5
|
-
LinkQueryOptions,
|
|
6
|
-
MutableWatchedQueryState
|
|
7
|
-
} from './AbstractQueryProcessor.js';
|
|
8
2
|
|
|
9
3
|
/**
|
|
10
4
|
* Represents an updated row in a differential watched query.
|
|
11
5
|
* It contains both the current and previous state of the row.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
12
8
|
*/
|
|
13
9
|
export interface WatchedQueryRowDifferential<RowType> {
|
|
14
10
|
readonly current: RowType;
|
|
@@ -18,6 +14,8 @@ export interface WatchedQueryRowDifferential<RowType> {
|
|
|
18
14
|
/**
|
|
19
15
|
* Represents the result of a watched query that has been diffed.
|
|
20
16
|
* {@link DifferentialWatchedQueryState#diff} is of the {@link WatchedQueryDifferential} form.
|
|
17
|
+
*
|
|
18
|
+
* @public
|
|
21
19
|
*/
|
|
22
20
|
export interface WatchedQueryDifferential<RowType> {
|
|
23
21
|
readonly added: ReadonlyArray<Readonly<RowType>>;
|
|
@@ -43,6 +41,8 @@ export interface WatchedQueryDifferential<RowType> {
|
|
|
43
41
|
|
|
44
42
|
/**
|
|
45
43
|
* Row comparator for differentially watched queries which keys and compares items in the result set.
|
|
44
|
+
*
|
|
45
|
+
* @public
|
|
46
46
|
*/
|
|
47
47
|
export interface DifferentialWatchedQueryComparator<RowType> {
|
|
48
48
|
/**
|
|
@@ -57,6 +57,8 @@ export interface DifferentialWatchedQueryComparator<RowType> {
|
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
59
|
* Options for building a differential watched query with the {@link Query} builder.
|
|
60
|
+
*
|
|
61
|
+
* @public
|
|
60
62
|
*/
|
|
61
63
|
export interface DifferentialWatchedQueryOptions<RowType> extends WatchedQueryOptions {
|
|
62
64
|
/**
|
|
@@ -68,13 +70,14 @@ export interface DifferentialWatchedQueryOptions<RowType> extends WatchedQueryOp
|
|
|
68
70
|
* Row comparator used to identify and compare rows in the result set.
|
|
69
71
|
* If not provided, the default comparator will be used which keys items by their `id` property if available,
|
|
70
72
|
* otherwise it uses JSON stringification of the entire item for keying and comparison.
|
|
71
|
-
* @defaultValue {@link DEFAULT_ROW_COMPARATOR}
|
|
72
73
|
*/
|
|
73
74
|
rowComparator?: DifferentialWatchedQueryComparator<RowType>;
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
/**
|
|
77
78
|
* Settings for differential incremental watched queries using.
|
|
79
|
+
*
|
|
80
|
+
* @public
|
|
78
81
|
*/
|
|
79
82
|
export interface DifferentialWatchedQuerySettings<RowType> extends DifferentialWatchedQueryOptions<RowType> {
|
|
80
83
|
/**
|
|
@@ -83,223 +86,20 @@ export interface DifferentialWatchedQuerySettings<RowType> extends DifferentialW
|
|
|
83
86
|
query: WatchCompatibleQuery<RowType[]>;
|
|
84
87
|
}
|
|
85
88
|
|
|
86
|
-
|
|
87
|
-
|
|
89
|
+
/**
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
export interface DifferentialWatchedQueryListener<RowType> extends WatchedQueryListener<
|
|
93
|
+
ReadonlyArray<Readonly<RowType>>
|
|
94
|
+
> {
|
|
88
95
|
onDiff?: (diff: WatchedQueryDifferential<RowType>) => void | Promise<void>;
|
|
89
96
|
}
|
|
90
97
|
|
|
98
|
+
/**
|
|
99
|
+
* @public
|
|
100
|
+
*/
|
|
91
101
|
export type DifferentialWatchedQuery<RowType> = WatchedQuery<
|
|
92
102
|
ReadonlyArray<Readonly<RowType>>,
|
|
93
103
|
DifferentialWatchedQuerySettings<RowType>,
|
|
94
104
|
DifferentialWatchedQueryListener<RowType>
|
|
95
105
|
>;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* @internal
|
|
99
|
-
*/
|
|
100
|
-
export interface DifferentialQueryProcessorOptions<RowType>
|
|
101
|
-
extends AbstractQueryProcessorOptions<RowType[], DifferentialWatchedQuerySettings<RowType>> {
|
|
102
|
-
rowComparator?: DifferentialWatchedQueryComparator<RowType>;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
type DataHashMap<RowType> = Map<string, { hash: string; item: RowType }>;
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* An empty differential result set.
|
|
109
|
-
* This is used as the initial state for differential incrementally watched queries.
|
|
110
|
-
*/
|
|
111
|
-
export const EMPTY_DIFFERENTIAL = {
|
|
112
|
-
added: [],
|
|
113
|
-
all: [],
|
|
114
|
-
removed: [],
|
|
115
|
-
updated: [],
|
|
116
|
-
unchanged: []
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Default implementation of the {@link DifferentialWatchedQueryComparator} for watched queries.
|
|
121
|
-
* It keys items by their `id` property if available, alternatively it uses JSON stringification
|
|
122
|
-
* of the entire item for the key and comparison.
|
|
123
|
-
*/
|
|
124
|
-
export const DEFAULT_ROW_COMPARATOR: DifferentialWatchedQueryComparator<any> = {
|
|
125
|
-
keyBy: (item) => {
|
|
126
|
-
if (item && typeof item == 'object' && typeof item['id'] == 'string') {
|
|
127
|
-
return item['id'];
|
|
128
|
-
}
|
|
129
|
-
return JSON.stringify(item);
|
|
130
|
-
},
|
|
131
|
-
compareBy: (item) => JSON.stringify(item)
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Uses the PowerSync onChange event to trigger watched queries.
|
|
136
|
-
* Results are emitted on every change of the relevant tables.
|
|
137
|
-
* @internal
|
|
138
|
-
*/
|
|
139
|
-
export class DifferentialQueryProcessor<RowType>
|
|
140
|
-
extends AbstractQueryProcessor<ReadonlyArray<Readonly<RowType>>, DifferentialWatchedQuerySettings<RowType>>
|
|
141
|
-
implements DifferentialWatchedQuery<RowType>
|
|
142
|
-
{
|
|
143
|
-
protected comparator: DifferentialWatchedQueryComparator<RowType>;
|
|
144
|
-
|
|
145
|
-
constructor(protected options: DifferentialQueryProcessorOptions<RowType>) {
|
|
146
|
-
super(options);
|
|
147
|
-
this.comparator = options.rowComparator ?? DEFAULT_ROW_COMPARATOR;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/*
|
|
151
|
-
* @returns If the sets are equal
|
|
152
|
-
*/
|
|
153
|
-
protected differentiate(
|
|
154
|
-
current: RowType[],
|
|
155
|
-
previousMap: DataHashMap<RowType>
|
|
156
|
-
): { diff: WatchedQueryDifferential<RowType>; map: DataHashMap<RowType>; hasChanged: boolean } {
|
|
157
|
-
const { keyBy, compareBy } = this.comparator;
|
|
158
|
-
|
|
159
|
-
let hasChanged = false;
|
|
160
|
-
const currentMap = new Map<string, { hash: string; item: RowType }>();
|
|
161
|
-
const removedTracker = new Set(previousMap.keys());
|
|
162
|
-
|
|
163
|
-
// Allow mutating to populate the data temporarily.
|
|
164
|
-
const diff = {
|
|
165
|
-
all: [] as RowType[],
|
|
166
|
-
added: [] as RowType[],
|
|
167
|
-
removed: [] as RowType[],
|
|
168
|
-
updated: [] as WatchedQueryRowDifferential<RowType>[],
|
|
169
|
-
unchanged: [] as RowType[]
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Looping over the current result set array is important to preserve
|
|
174
|
-
* the ordering of the result set.
|
|
175
|
-
* We can replace items in the current array with previous object references if they are equal.
|
|
176
|
-
*/
|
|
177
|
-
for (const item of current) {
|
|
178
|
-
const key = keyBy(item);
|
|
179
|
-
const hash = compareBy(item);
|
|
180
|
-
currentMap.set(key, { hash, item });
|
|
181
|
-
|
|
182
|
-
const previousItem = previousMap.get(key);
|
|
183
|
-
if (!previousItem) {
|
|
184
|
-
// New item
|
|
185
|
-
hasChanged = true;
|
|
186
|
-
diff.added.push(item);
|
|
187
|
-
diff.all.push(item);
|
|
188
|
-
} else {
|
|
189
|
-
// Existing item
|
|
190
|
-
if (hash == previousItem.hash) {
|
|
191
|
-
diff.unchanged.push(previousItem.item);
|
|
192
|
-
// Use the previous object reference
|
|
193
|
-
diff.all.push(previousItem.item);
|
|
194
|
-
// update the map to preserve the reference
|
|
195
|
-
currentMap.set(key, previousItem);
|
|
196
|
-
} else {
|
|
197
|
-
hasChanged = true;
|
|
198
|
-
diff.updated.push({ current: item, previous: previousItem.item });
|
|
199
|
-
// Use the new reference
|
|
200
|
-
diff.all.push(item);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
// The item is present, we don't consider it removed
|
|
204
|
-
removedTracker.delete(key);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
diff.removed = Array.from(removedTracker).map((key) => previousMap.get(key)!.item);
|
|
208
|
-
hasChanged = hasChanged || diff.removed.length > 0;
|
|
209
|
-
|
|
210
|
-
return {
|
|
211
|
-
diff,
|
|
212
|
-
hasChanged,
|
|
213
|
-
map: currentMap
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
protected async linkQuery(options: LinkQueryOptions<WatchedQueryDifferential<RowType>>): Promise<void> {
|
|
218
|
-
const { db, watchOptions } = this.options;
|
|
219
|
-
const { abortSignal } = options;
|
|
220
|
-
|
|
221
|
-
const compiledQuery = watchOptions.query.compile();
|
|
222
|
-
const tables = await db.resolveTables(compiledQuery.sql, compiledQuery.parameters as any[], {
|
|
223
|
-
tables: options.settings.triggerOnTables
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
let currentMap: DataHashMap<RowType> = new Map();
|
|
227
|
-
|
|
228
|
-
// populate the currentMap from the placeholder data
|
|
229
|
-
this.state.data.forEach((item) => {
|
|
230
|
-
currentMap.set(this.comparator.keyBy(item), {
|
|
231
|
-
hash: this.comparator.compareBy(item),
|
|
232
|
-
item
|
|
233
|
-
});
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
db.onChangeWithCallback(
|
|
237
|
-
{
|
|
238
|
-
onChange: async () => {
|
|
239
|
-
if (this.closed || abortSignal.aborted) {
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
// This fires for each change of the relevant tables
|
|
243
|
-
try {
|
|
244
|
-
if (this.reportFetching && !this.state.isFetching) {
|
|
245
|
-
await this.updateState({ isFetching: true });
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
const partialStateUpdate: Partial<MutableWatchedQueryState<RowType[]>> = {};
|
|
249
|
-
|
|
250
|
-
// Always run the query if an underlying table has changed
|
|
251
|
-
const result = await watchOptions.query.execute({
|
|
252
|
-
sql: compiledQuery.sql,
|
|
253
|
-
// Allows casting from ReadOnlyArray[unknown] to Array<unknown>
|
|
254
|
-
// This allows simpler compatibility with PowerSync queries
|
|
255
|
-
parameters: [...compiledQuery.parameters],
|
|
256
|
-
db: this.options.db
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
if (abortSignal.aborted) {
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (this.reportFetching) {
|
|
264
|
-
partialStateUpdate.isFetching = false;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
if (this.state.isLoading) {
|
|
268
|
-
partialStateUpdate.isLoading = false;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const { diff, hasChanged, map } = this.differentiate(result, currentMap);
|
|
272
|
-
// Update for future comparisons
|
|
273
|
-
currentMap = map;
|
|
274
|
-
|
|
275
|
-
if (hasChanged) {
|
|
276
|
-
await this.iterateAsyncListenersWithError((l) => l.onDiff?.(diff));
|
|
277
|
-
Object.assign(partialStateUpdate, {
|
|
278
|
-
data: diff.all
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
if (this.state.error) {
|
|
283
|
-
partialStateUpdate.error = null;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
if (Object.keys(partialStateUpdate).length > 0) {
|
|
287
|
-
await this.updateState(partialStateUpdate);
|
|
288
|
-
}
|
|
289
|
-
} catch (error) {
|
|
290
|
-
await this.updateState({ error });
|
|
291
|
-
}
|
|
292
|
-
},
|
|
293
|
-
onError: async (error) => {
|
|
294
|
-
await this.updateState({ error });
|
|
295
|
-
}
|
|
296
|
-
},
|
|
297
|
-
{
|
|
298
|
-
signal: abortSignal,
|
|
299
|
-
tables,
|
|
300
|
-
throttleMs: watchOptions.throttleMs,
|
|
301
|
-
triggerImmediate: true // used to emit the initial state
|
|
302
|
-
}
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { WatchCompatibleQuery, WatchedQuery, WatchedQueryOptions } from '../WatchedQuery.js';
|
|
2
|
-
import {
|
|
3
|
-
AbstractQueryProcessor,
|
|
4
|
-
AbstractQueryProcessorOptions,
|
|
5
|
-
LinkQueryOptions,
|
|
6
|
-
MutableWatchedQueryState
|
|
7
|
-
} from './AbstractQueryProcessor.js';
|
|
8
|
-
import { WatchedQueryComparator } from './comparators.js';
|
|
9
2
|
|
|
10
3
|
/**
|
|
11
4
|
* Settings for {@link WatchedQuery} instances created via {@link Query#watch}.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
12
7
|
*/
|
|
13
8
|
export interface WatchedQuerySettings<DataType> extends WatchedQueryOptions {
|
|
14
9
|
query: WatchCompatibleQuery<DataType>;
|
|
@@ -16,107 +11,7 @@ export interface WatchedQuerySettings<DataType> extends WatchedQueryOptions {
|
|
|
16
11
|
|
|
17
12
|
/**
|
|
18
13
|
* {@link WatchedQuery} returned from {@link Query#watch}.
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
19
16
|
*/
|
|
20
17
|
export type StandardWatchedQuery<DataType> = WatchedQuery<DataType, WatchedQuerySettings<DataType>>;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @internal
|
|
24
|
-
*/
|
|
25
|
-
export interface OnChangeQueryProcessorOptions<Data>
|
|
26
|
-
extends AbstractQueryProcessorOptions<Data, WatchedQuerySettings<Data>> {
|
|
27
|
-
comparator?: WatchedQueryComparator<Data>;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Uses the PowerSync onChange event to trigger watched queries.
|
|
32
|
-
* Results are emitted on every change of the relevant tables.
|
|
33
|
-
* @internal
|
|
34
|
-
*/
|
|
35
|
-
export class OnChangeQueryProcessor<Data> extends AbstractQueryProcessor<Data, WatchedQuerySettings<Data>> {
|
|
36
|
-
constructor(protected options: OnChangeQueryProcessorOptions<Data>) {
|
|
37
|
-
super(options);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @returns If the sets are equal
|
|
42
|
-
*/
|
|
43
|
-
protected checkEquality(current: Data, previous: Data): boolean {
|
|
44
|
-
// Use the provided comparator if available. Assume values are unique if not available.
|
|
45
|
-
return this.options.comparator?.checkEquality?.(current, previous) ?? false;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
protected async linkQuery(options: LinkQueryOptions<Data>): Promise<void> {
|
|
49
|
-
const { db, watchOptions } = this.options;
|
|
50
|
-
const { abortSignal } = options;
|
|
51
|
-
|
|
52
|
-
const compiledQuery = watchOptions.query.compile();
|
|
53
|
-
const tables = await db.resolveTables(compiledQuery.sql, compiledQuery.parameters as any[], {
|
|
54
|
-
tables: options.settings.triggerOnTables
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
db.onChangeWithCallback(
|
|
58
|
-
{
|
|
59
|
-
onChange: async () => {
|
|
60
|
-
if (this.closed || abortSignal.aborted) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
// This fires for each change of the relevant tables
|
|
64
|
-
try {
|
|
65
|
-
if (this.reportFetching && !this.state.isFetching) {
|
|
66
|
-
await this.updateState({ isFetching: true });
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const partialStateUpdate: Partial<MutableWatchedQueryState<Data>> & { data?: Data } = {};
|
|
70
|
-
|
|
71
|
-
// Always run the query if an underlying table has changed
|
|
72
|
-
const result = await watchOptions.query.execute({
|
|
73
|
-
sql: compiledQuery.sql,
|
|
74
|
-
// Allows casting from ReadOnlyArray[unknown] to Array<unknown>
|
|
75
|
-
// This allows simpler compatibility with PowerSync queries
|
|
76
|
-
parameters: [...compiledQuery.parameters],
|
|
77
|
-
db: this.options.db
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
if (abortSignal.aborted) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (this.reportFetching) {
|
|
85
|
-
partialStateUpdate.isFetching = false;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (this.state.isLoading) {
|
|
89
|
-
partialStateUpdate.isLoading = false;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Check if the result has changed
|
|
93
|
-
if (!this.checkEquality(result, this.state.data)) {
|
|
94
|
-
Object.assign(partialStateUpdate, {
|
|
95
|
-
data: result
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (this.state.error) {
|
|
100
|
-
partialStateUpdate.error = null;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (Object.keys(partialStateUpdate).length > 0) {
|
|
104
|
-
await this.updateState(partialStateUpdate);
|
|
105
|
-
}
|
|
106
|
-
} catch (error) {
|
|
107
|
-
await this.updateState({ error });
|
|
108
|
-
}
|
|
109
|
-
},
|
|
110
|
-
onError: async (error) => {
|
|
111
|
-
await this.updateState({ error });
|
|
112
|
-
}
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
signal: abortSignal,
|
|
116
|
-
tables,
|
|
117
|
-
throttleMs: watchOptions.throttleMs,
|
|
118
|
-
triggerImmediate: true // used to emit the initial state
|
|
119
|
-
}
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
@@ -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
|