@powersync/common 1.57.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/attachments/AttachmentContext.d.ts +5 -5
- package/lib/attachments/AttachmentContext.js +6 -2
- package/lib/attachments/AttachmentContext.js.map +1 -1
- package/lib/attachments/AttachmentQueue.d.ts +6 -6
- package/lib/attachments/AttachmentQueue.js +9 -8
- 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/Schema.d.ts +2 -2
- package/lib/attachments/SyncingService.d.ts +2 -2
- package/lib/attachments/SyncingService.js +21 -6
- package/lib/attachments/SyncingService.js.map +1 -1
- package/lib/client/{AbstractPowerSyncDatabase.d.ts → CommonPowerSyncDatabase.d.ts} +56 -232
- package/lib/client/CommonPowerSyncDatabase.js +2 -0
- package/lib/client/CommonPowerSyncDatabase.js.map +1 -0
- package/lib/client/SQLOpenFactory.d.ts +35 -13
- package/lib/client/SQLOpenFactory.js +1 -25
- package/lib/client/SQLOpenFactory.js.map +1 -1
- package/lib/client/compilableQueryWatch.d.ts +2 -2
- package/lib/client/compilableQueryWatch.js +1 -2
- package/lib/client/compilableQueryWatch.js.map +1 -1
- package/lib/client/connection/PowerSyncBackendConnector.d.ts +3 -3
- package/lib/client/runOnSchemaChange.d.ts +2 -2
- package/lib/client/runOnSchemaChange.js.map +1 -1
- package/lib/client/sync/bucket/CrudEntry.d.ts +3 -34
- package/lib/client/sync/bucket/CrudEntry.js +0 -99
- package/lib/client/sync/bucket/CrudEntry.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/sync-streams.d.ts +1 -1
- package/lib/client/triggers/TriggerManager.d.ts +9 -38
- package/lib/client/watched/GetAllQuery.d.ts +3 -3
- package/lib/client/watched/GetAllQuery.js +1 -1
- package/lib/client/watched/GetAllQuery.js.map +1 -1
- package/lib/client/watched/WatchedQuery.d.ts +2 -10
- package/lib/client/watched/WatchedQuery.js +0 -11
- package/lib/client/watched/WatchedQuery.js.map +1 -1
- package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +0 -50
- package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -176
- package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +0 -22
- package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -82
- package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -1
- package/lib/db/DBAdapter.d.ts +72 -154
- package/lib/db/DBAdapter.js +70 -108
- 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 +1 -13
- package/lib/db/crud/SyncProgress.js +1 -62
- package/lib/db/crud/SyncProgress.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +32 -66
- package/lib/db/crud/SyncStatus.js +1 -253
- package/lib/db/crud/SyncStatus.js.map +1 -1
- package/lib/db/schema/Column.d.ts +0 -7
- package/lib/db/schema/Column.js +0 -7
- package/lib/db/schema/Column.js.map +1 -1
- package/lib/db/schema/Index.d.ts +2 -6
- package/lib/db/schema/Index.js +1 -4
- package/lib/db/schema/Index.js.map +1 -1
- package/lib/db/schema/IndexedColumn.d.ts +2 -6
- package/lib/db/schema/IndexedColumn.js +1 -4
- package/lib/db/schema/IndexedColumn.js.map +1 -1
- package/lib/db/schema/Schema.d.ts +4 -28
- package/lib/db/schema/Schema.js.map +1 -1
- package/lib/db/schema/Table.d.ts +50 -96
- package/lib/db/schema/Table.js +83 -91
- package/lib/db/schema/Table.js.map +1 -1
- package/lib/index.d.ts +5 -21
- package/lib/index.js +5 -22
- package/lib/index.js.map +1 -1
- package/lib/utils/BaseObserver.d.ts +1 -1
- package/lib/utils/BaseObserver.js +1 -1
- package/lib/utils/Logger.d.ts +52 -23
- package/lib/utils/Logger.js +35 -33
- 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 -53
- package/lib/utils/mutex.js +1 -156
- package/lib/utils/mutex.js.map +1 -1
- package/package.json +8 -45
- package/src/attachments/AttachmentContext.ts +11 -10
- package/src/attachments/AttachmentQueue.ts +15 -14
- package/src/attachments/AttachmentService.ts +7 -6
- package/src/attachments/Schema.ts +2 -2
- package/src/attachments/SyncingService.ts +23 -9
- package/src/client/CommonPowerSyncDatabase.ts +540 -0
- package/src/client/SQLOpenFactory.ts +38 -22
- package/src/client/compilableQueryWatch.ts +3 -4
- package/src/client/connection/PowerSyncBackendConnector.ts +3 -3
- package/src/client/runOnSchemaChange.ts +2 -2
- package/src/client/sync/bucket/CrudEntry.ts +4 -104
- package/src/client/sync/options.ts +77 -0
- package/src/client/sync/sync-streams.ts +1 -1
- package/src/client/triggers/TriggerManager.ts +10 -41
- package/src/client/watched/GetAllQuery.ts +3 -3
- package/src/client/watched/WatchedQuery.ts +2 -15
- package/src/client/watched/processors/DifferentialQueryProcessor.ts +0 -223
- package/src/client/watched/processors/OnChangeQueryProcessor.ts +0 -111
- package/src/db/DBAdapter.ts +141 -226
- package/src/db/QueryResult.ts +195 -0
- package/src/db/crud/SyncProgress.ts +2 -42
- package/src/db/crud/SyncStatus.ts +45 -220
- package/src/db/schema/Column.ts +0 -8
- package/src/db/schema/Index.ts +3 -6
- package/src/db/schema/IndexedColumn.ts +3 -6
- package/src/db/schema/Schema.ts +4 -4
- package/src/db/schema/Table.ts +111 -186
- package/src/index.ts +5 -21
- package/src/utils/BaseObserver.ts +1 -1
- package/src/utils/Logger.ts +80 -42
- package/src/utils/MetaBaseObserver.ts +14 -60
- package/src/utils/mutex.ts +4 -201
- package/dist/bundle.cjs +0 -14393
- package/dist/bundle.cjs.map +0 -1
- package/dist/bundle.mjs +0 -14312
- package/dist/bundle.mjs.map +0 -1
- package/dist/bundle.node.cjs +0 -12024
- package/dist/bundle.node.cjs.map +0 -1
- package/dist/bundle.node.mjs +0 -11943
- package/dist/bundle.node.mjs.map +0 -1
- package/dist/index.d.cts +0 -4651
- package/legacy/sync_protocol.d.ts +0 -103
- package/lib/client/AbstractPowerSyncDatabase.js +0 -990
- package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
- package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -28
- package/lib/client/AbstractPowerSyncOpenFactory.js +0 -28
- package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
- package/lib/client/ConnectionManager.d.ts +0 -115
- 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 -4
- package/lib/client/constants.js +0 -5
- package/lib/client/constants.js.map +0 -1
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -56
- package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -29
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
- package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -37
- package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -165
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +0 -180
- package/lib/client/sync/stream/AbstractRemote.js +0 -508
- package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -265
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -705
- 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 -62
- package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
- package/lib/client/sync/stream/core-instruction.d.ts +0 -77
- package/lib/client/sync/stream/core-instruction.js +0 -30
- 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 -410
- 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 -12
- package/lib/db/ConnectionClosedError.js +0 -23
- package/lib/db/ConnectionClosedError.js.map +0 -1
- package/lib/db/schema/TableV2.d.ts +0 -11
- package/lib/db/schema/TableV2.js +0 -11
- package/lib/db/schema/TableV2.js.map +0 -1
- package/lib/utils/AbortOperation.d.ts +0 -11
- package/lib/utils/AbortOperation.js +0 -21
- package/lib/utils/AbortOperation.js.map +0 -1
- package/lib/utils/ControlledExecutor.d.ts +0 -31
- package/lib/utils/ControlledExecutor.js +0 -54
- package/lib/utils/ControlledExecutor.js.map +0 -1
- package/lib/utils/async.d.ts +0 -46
- package/lib/utils/async.js +0 -147
- package/lib/utils/async.js.map +0 -1
- package/lib/utils/compatibility.d.ts +0 -8
- package/lib/utils/compatibility.js +0 -9
- package/lib/utils/compatibility.js.map +0 -1
- package/lib/utils/parseQuery.d.ts +0 -12
- package/lib/utils/parseQuery.js +0 -20
- 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 -41
- package/lib/utils/stream_transform.js +0 -211
- package/lib/utils/stream_transform.js.map +0 -1
- package/src/client/AbstractPowerSyncDatabase.ts +0 -1408
- package/src/client/AbstractPowerSyncOpenFactory.ts +0 -45
- package/src/client/ConnectionManager.ts +0 -405
- package/src/client/CustomQuery.ts +0 -56
- package/src/client/constants.ts +0 -4
- package/src/client/sync/bucket/BucketStorageAdapter.ts +0 -65
- package/src/client/sync/bucket/SqliteBucketStorage.ts +0 -202
- package/src/client/sync/stream/AbstractRemote.ts +0 -648
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +0 -970
- package/src/client/sync/stream/WebsocketClientTransport.ts +0 -82
- package/src/client/sync/stream/core-instruction.ts +0 -109
- package/src/client/triggers/MemoryTriggerClaimManager.ts +0 -25
- package/src/client/triggers/TriggerManagerImpl.ts +0 -501
- package/src/client/watched/processors/AbstractQueryProcessor.ts +0 -226
- package/src/db/ConnectionClosedError.ts +0 -25
- package/src/db/schema/TableV2.ts +0 -11
- package/src/utils/AbortOperation.ts +0 -19
- package/src/utils/ControlledExecutor.ts +0 -78
- package/src/utils/async.ts +0 -185
- package/src/utils/compatibility.ts +0 -9
- package/src/utils/parseQuery.ts +0 -31
- package/src/utils/queue.ts +0 -48
- package/src/utils/stream_transform.ts +0 -260
|
@@ -1,177 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* An empty differential result set.
|
|
4
|
-
* This is used as the initial state for differential incrementally watched queries.
|
|
5
|
-
*
|
|
6
|
-
* @internal
|
|
7
|
-
*/
|
|
8
|
-
export const EMPTY_DIFFERENTIAL = {
|
|
9
|
-
added: [],
|
|
10
|
-
all: [],
|
|
11
|
-
removed: [],
|
|
12
|
-
updated: [],
|
|
13
|
-
unchanged: []
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Default implementation of the {@link DifferentialWatchedQueryComparator} for watched queries.
|
|
17
|
-
* It keys items by their `id` property if available, alternatively it uses JSON stringification
|
|
18
|
-
* of the entire item for the key and comparison.
|
|
19
|
-
*
|
|
20
|
-
* @internal
|
|
21
|
-
*/
|
|
22
|
-
export const DEFAULT_ROW_COMPARATOR = {
|
|
23
|
-
keyBy: (item) => {
|
|
24
|
-
if (item && typeof item == 'object' && typeof item['id'] == 'string') {
|
|
25
|
-
return item['id'];
|
|
26
|
-
}
|
|
27
|
-
return JSON.stringify(item);
|
|
28
|
-
},
|
|
29
|
-
compareBy: (item) => JSON.stringify(item)
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* Uses the PowerSync onChange event to trigger watched queries.
|
|
33
|
-
* Results are emitted on every change of the relevant tables.
|
|
34
|
-
* @internal
|
|
35
|
-
*/
|
|
36
|
-
export class DifferentialQueryProcessor extends AbstractQueryProcessor {
|
|
37
|
-
options;
|
|
38
|
-
comparator;
|
|
39
|
-
constructor(options) {
|
|
40
|
-
super(options);
|
|
41
|
-
this.options = options;
|
|
42
|
-
this.comparator = options.rowComparator ?? DEFAULT_ROW_COMPARATOR;
|
|
43
|
-
}
|
|
44
|
-
/*
|
|
45
|
-
* @returns If the sets are equal
|
|
46
|
-
*/
|
|
47
|
-
differentiate(current, previousMap) {
|
|
48
|
-
const { keyBy, compareBy } = this.comparator;
|
|
49
|
-
let hasChanged = false;
|
|
50
|
-
const currentMap = new Map();
|
|
51
|
-
const removedTracker = new Set(previousMap.keys());
|
|
52
|
-
// Allow mutating to populate the data temporarily.
|
|
53
|
-
const diff = {
|
|
54
|
-
all: [],
|
|
55
|
-
added: [],
|
|
56
|
-
removed: [],
|
|
57
|
-
updated: [],
|
|
58
|
-
unchanged: []
|
|
59
|
-
};
|
|
60
|
-
/**
|
|
61
|
-
* Looping over the current result set array is important to preserve
|
|
62
|
-
* the ordering of the result set.
|
|
63
|
-
* We can replace items in the current array with previous object references if they are equal.
|
|
64
|
-
*/
|
|
65
|
-
for (const item of current) {
|
|
66
|
-
const key = keyBy(item);
|
|
67
|
-
const hash = compareBy(item);
|
|
68
|
-
currentMap.set(key, { hash, item });
|
|
69
|
-
const previousItem = previousMap.get(key);
|
|
70
|
-
if (!previousItem) {
|
|
71
|
-
// New item
|
|
72
|
-
hasChanged = true;
|
|
73
|
-
diff.added.push(item);
|
|
74
|
-
diff.all.push(item);
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
// Existing item
|
|
78
|
-
if (hash == previousItem.hash) {
|
|
79
|
-
diff.unchanged.push(previousItem.item);
|
|
80
|
-
// Use the previous object reference
|
|
81
|
-
diff.all.push(previousItem.item);
|
|
82
|
-
// update the map to preserve the reference
|
|
83
|
-
currentMap.set(key, previousItem);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
hasChanged = true;
|
|
87
|
-
diff.updated.push({ current: item, previous: previousItem.item });
|
|
88
|
-
// Use the new reference
|
|
89
|
-
diff.all.push(item);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
// The item is present, we don't consider it removed
|
|
93
|
-
removedTracker.delete(key);
|
|
94
|
-
}
|
|
95
|
-
diff.removed = Array.from(removedTracker).map((key) => previousMap.get(key).item);
|
|
96
|
-
hasChanged = hasChanged || diff.removed.length > 0;
|
|
97
|
-
return {
|
|
98
|
-
diff,
|
|
99
|
-
hasChanged,
|
|
100
|
-
map: currentMap
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
async linkQuery(options) {
|
|
104
|
-
const { db, watchOptions } = this.options;
|
|
105
|
-
const { abortSignal } = options;
|
|
106
|
-
const compiledQuery = watchOptions.query.compile();
|
|
107
|
-
const tables = await db.resolveTables(compiledQuery.sql, compiledQuery.parameters, {
|
|
108
|
-
tables: options.settings.triggerOnTables
|
|
109
|
-
});
|
|
110
|
-
let currentMap = new Map();
|
|
111
|
-
// populate the currentMap from the placeholder data
|
|
112
|
-
this.state.data.forEach((item) => {
|
|
113
|
-
currentMap.set(this.comparator.keyBy(item), {
|
|
114
|
-
hash: this.comparator.compareBy(item),
|
|
115
|
-
item
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
db.onChangeWithCallback({
|
|
119
|
-
onChange: async () => {
|
|
120
|
-
if (this.closed || abortSignal.aborted) {
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
// This fires for each change of the relevant tables
|
|
124
|
-
try {
|
|
125
|
-
if (this.reportFetching && !this.state.isFetching) {
|
|
126
|
-
await this.updateState({ isFetching: true });
|
|
127
|
-
}
|
|
128
|
-
const partialStateUpdate = {};
|
|
129
|
-
// Always run the query if an underlying table has changed
|
|
130
|
-
const result = await watchOptions.query.execute({
|
|
131
|
-
sql: compiledQuery.sql,
|
|
132
|
-
// Allows casting from ReadOnlyArray[unknown] to Array<unknown>
|
|
133
|
-
// This allows simpler compatibility with PowerSync queries
|
|
134
|
-
parameters: [...compiledQuery.parameters],
|
|
135
|
-
db: this.options.db
|
|
136
|
-
});
|
|
137
|
-
if (abortSignal.aborted) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
if (this.reportFetching) {
|
|
141
|
-
partialStateUpdate.isFetching = false;
|
|
142
|
-
}
|
|
143
|
-
if (this.state.isLoading) {
|
|
144
|
-
partialStateUpdate.isLoading = false;
|
|
145
|
-
}
|
|
146
|
-
const { diff, hasChanged, map } = this.differentiate(result, currentMap);
|
|
147
|
-
// Update for future comparisons
|
|
148
|
-
currentMap = map;
|
|
149
|
-
if (hasChanged) {
|
|
150
|
-
await this.iterateAsyncListenersWithError((l) => l.onDiff?.(diff));
|
|
151
|
-
Object.assign(partialStateUpdate, {
|
|
152
|
-
data: diff.all
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
if (this.state.error) {
|
|
156
|
-
partialStateUpdate.error = null;
|
|
157
|
-
}
|
|
158
|
-
if (Object.keys(partialStateUpdate).length > 0) {
|
|
159
|
-
await this.updateState(partialStateUpdate);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
catch (error) {
|
|
163
|
-
await this.updateState({ error });
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
onError: async (error) => {
|
|
167
|
-
await this.updateState({ error });
|
|
168
|
-
}
|
|
169
|
-
}, {
|
|
170
|
-
signal: abortSignal,
|
|
171
|
-
tables,
|
|
172
|
-
throttleMs: watchOptions.throttleMs,
|
|
173
|
-
triggerImmediate: true // used to emit the initial state
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
}
|
|
1
|
+
export {};
|
|
177
2
|
//# sourceMappingURL=DifferentialQueryProcessor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DifferentialQueryProcessor.js","sourceRoot":"","sources":["../../../../src/client/watched/processors/DifferentialQueryProcessor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DifferentialQueryProcessor.js","sourceRoot":"","sources":["../../../../src/client/watched/processors/DifferentialQueryProcessor.ts"],"names":[],"mappings":""}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { WatchCompatibleQuery, WatchedQuery, WatchedQueryOptions } from '../WatchedQuery.js';
|
|
2
|
-
import { AbstractQueryProcessor, AbstractQueryProcessorOptions, LinkQueryOptions } from './AbstractQueryProcessor.js';
|
|
3
|
-
import { WatchedQueryComparator } from './comparators.js';
|
|
4
2
|
/**
|
|
5
3
|
* Settings for {@link WatchedQuery} instances created via {@link Query#watch}.
|
|
6
4
|
*
|
|
@@ -15,23 +13,3 @@ export interface WatchedQuerySettings<DataType> extends WatchedQueryOptions {
|
|
|
15
13
|
* @public
|
|
16
14
|
*/
|
|
17
15
|
export type StandardWatchedQuery<DataType> = WatchedQuery<DataType, WatchedQuerySettings<DataType>>;
|
|
18
|
-
/**
|
|
19
|
-
* @internal
|
|
20
|
-
*/
|
|
21
|
-
export interface OnChangeQueryProcessorOptions<Data> extends AbstractQueryProcessorOptions<Data, WatchedQuerySettings<Data>> {
|
|
22
|
-
comparator?: WatchedQueryComparator<Data>;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Uses the PowerSync onChange event to trigger watched queries.
|
|
26
|
-
* Results are emitted on every change of the relevant tables.
|
|
27
|
-
* @internal
|
|
28
|
-
*/
|
|
29
|
-
export declare class OnChangeQueryProcessor<Data> extends AbstractQueryProcessor<Data, WatchedQuerySettings<Data>> {
|
|
30
|
-
protected options: OnChangeQueryProcessorOptions<Data>;
|
|
31
|
-
constructor(options: OnChangeQueryProcessorOptions<Data>);
|
|
32
|
-
/**
|
|
33
|
-
* @returns If the sets are equal
|
|
34
|
-
*/
|
|
35
|
-
protected checkEquality(current: Data, previous: Data): boolean;
|
|
36
|
-
protected linkQuery(options: LinkQueryOptions<Data>): Promise<void>;
|
|
37
|
-
}
|
|
@@ -1,83 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Uses the PowerSync onChange event to trigger watched queries.
|
|
4
|
-
* Results are emitted on every change of the relevant tables.
|
|
5
|
-
* @internal
|
|
6
|
-
*/
|
|
7
|
-
export class OnChangeQueryProcessor extends AbstractQueryProcessor {
|
|
8
|
-
options;
|
|
9
|
-
constructor(options) {
|
|
10
|
-
super(options);
|
|
11
|
-
this.options = options;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* @returns If the sets are equal
|
|
15
|
-
*/
|
|
16
|
-
checkEquality(current, previous) {
|
|
17
|
-
// Use the provided comparator if available. Assume values are unique if not available.
|
|
18
|
-
return this.options.comparator?.checkEquality?.(current, previous) ?? false;
|
|
19
|
-
}
|
|
20
|
-
async linkQuery(options) {
|
|
21
|
-
const { db, watchOptions } = this.options;
|
|
22
|
-
const { abortSignal } = options;
|
|
23
|
-
const compiledQuery = watchOptions.query.compile();
|
|
24
|
-
const tables = await db.resolveTables(compiledQuery.sql, compiledQuery.parameters, {
|
|
25
|
-
tables: options.settings.triggerOnTables
|
|
26
|
-
});
|
|
27
|
-
db.onChangeWithCallback({
|
|
28
|
-
onChange: async () => {
|
|
29
|
-
if (this.closed || abortSignal.aborted) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
// This fires for each change of the relevant tables
|
|
33
|
-
try {
|
|
34
|
-
if (this.reportFetching && !this.state.isFetching) {
|
|
35
|
-
await this.updateState({ isFetching: true });
|
|
36
|
-
}
|
|
37
|
-
const partialStateUpdate = {};
|
|
38
|
-
// Always run the query if an underlying table has changed
|
|
39
|
-
const result = await watchOptions.query.execute({
|
|
40
|
-
sql: compiledQuery.sql,
|
|
41
|
-
// Allows casting from ReadOnlyArray[unknown] to Array<unknown>
|
|
42
|
-
// This allows simpler compatibility with PowerSync queries
|
|
43
|
-
parameters: [...compiledQuery.parameters],
|
|
44
|
-
db: this.options.db
|
|
45
|
-
});
|
|
46
|
-
if (abortSignal.aborted) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
if (this.reportFetching) {
|
|
50
|
-
partialStateUpdate.isFetching = false;
|
|
51
|
-
}
|
|
52
|
-
if (this.state.isLoading) {
|
|
53
|
-
partialStateUpdate.isLoading = false;
|
|
54
|
-
}
|
|
55
|
-
// Check if the result has changed
|
|
56
|
-
if (!this.checkEquality(result, this.state.data)) {
|
|
57
|
-
Object.assign(partialStateUpdate, {
|
|
58
|
-
data: result
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
if (this.state.error) {
|
|
62
|
-
partialStateUpdate.error = null;
|
|
63
|
-
}
|
|
64
|
-
if (Object.keys(partialStateUpdate).length > 0) {
|
|
65
|
-
await this.updateState(partialStateUpdate);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
await this.updateState({ error });
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
onError: async (error) => {
|
|
73
|
-
await this.updateState({ error });
|
|
74
|
-
}
|
|
75
|
-
}, {
|
|
76
|
-
signal: abortSignal,
|
|
77
|
-
tables,
|
|
78
|
-
throttleMs: watchOptions.throttleMs,
|
|
79
|
-
triggerImmediate: true // used to emit the initial state
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
1
|
+
export {};
|
|
83
2
|
//# sourceMappingURL=OnChangeQueryProcessor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OnChangeQueryProcessor.js","sourceRoot":"","sources":["../../../../src/client/watched/processors/OnChangeQueryProcessor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"OnChangeQueryProcessor.js","sourceRoot":"","sources":["../../../../src/client/watched/processors/OnChangeQueryProcessor.ts"],"names":[],"mappings":""}
|
package/lib/db/DBAdapter.d.ts
CHANGED
|
@@ -2,148 +2,100 @@
|
|
|
2
2
|
* Set of generic interfaces to allow PowerSync compatibility with
|
|
3
3
|
* different SQLite DB implementations.
|
|
4
4
|
*/
|
|
5
|
-
import { BaseListener,
|
|
5
|
+
import { BaseListener, BaseObserver } from '../utils/BaseObserver.js';
|
|
6
|
+
import { QueryResult, RawQueryResult, SqliteRecord } from './QueryResult.js';
|
|
6
7
|
/**
|
|
7
|
-
* TODO most of these types could be exported to a common `types` package
|
|
8
|
-
* which is used by the DB adapter libraries as well.
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* Object returned by SQL Query executions.
|
|
12
|
-
*
|
|
13
8
|
* @public
|
|
14
9
|
*/
|
|
15
|
-
export
|
|
16
|
-
/** Represents the auto-generated row id if applicable. */
|
|
17
|
-
insertId?: number;
|
|
10
|
+
export interface DBGetUtils {
|
|
18
11
|
/**
|
|
19
|
-
*
|
|
12
|
+
* Execute a read-only query and return results.
|
|
20
13
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
14
|
+
* @param sql - The SQL query to execute
|
|
15
|
+
* @param parameters - Optional array of parameters to bind to the query
|
|
16
|
+
* @returns An array of results
|
|
24
17
|
*/
|
|
25
|
-
rowsAffected: number;
|
|
26
|
-
/** if status is undefined or 0 this object will contain the query results */
|
|
27
|
-
rows?: {
|
|
28
|
-
/** Raw array with all dataset */
|
|
29
|
-
_array: any[];
|
|
30
|
-
/** The length of the dataset */
|
|
31
|
-
length: number;
|
|
32
|
-
/** A convenience function to acess the index based the row object
|
|
33
|
-
* @param idx - the row index
|
|
34
|
-
* @returns the row structure identified by column names
|
|
35
|
-
*/
|
|
36
|
-
item: (idx: number) => any;
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* @public
|
|
41
|
-
*/
|
|
42
|
-
export interface DBGetUtils {
|
|
43
|
-
/** Execute a read-only query and return results. */
|
|
44
18
|
getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
|
|
45
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Execute a read-only query and return the first result, or null if the ResultSet is empty.
|
|
21
|
+
*
|
|
22
|
+
* @param sql - The SQL query to execute
|
|
23
|
+
* @param parameters - Optional array of parameters to bind to the query
|
|
24
|
+
* @returns The first result if found, or null if no results are returned
|
|
25
|
+
*/
|
|
46
26
|
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
|
|
47
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Execute a read-only query and return the first result, error if the ResultSet is empty.
|
|
29
|
+
*
|
|
30
|
+
* @param sql - The SQL query to execute
|
|
31
|
+
* @param parameters - Optional array of parameters to bind to the query
|
|
32
|
+
* @returns The first result matching the query
|
|
33
|
+
* @throws Error if no rows are returned
|
|
34
|
+
*/
|
|
48
35
|
get<T>(sql: string, parameters?: any[]): Promise<T>;
|
|
49
36
|
}
|
|
50
37
|
/**
|
|
51
38
|
* @public
|
|
52
39
|
*/
|
|
53
40
|
export interface SqlExecutor {
|
|
54
|
-
/** Execute a single write statement. */
|
|
55
|
-
execute: (query: string, params?: any[] | undefined) => Promise<QueryResult>;
|
|
56
41
|
/**
|
|
57
|
-
* Execute a
|
|
58
|
-
*
|
|
59
|
-
* `executeRaw` returns a nested array of raw values, where each row is
|
|
60
|
-
* represented as an array of column values without field names.
|
|
61
|
-
*
|
|
62
|
-
* Example result:
|
|
42
|
+
* Execute a SQL write (INSERT/UPDATE/DELETE) query
|
|
43
|
+
* and optionally return results.
|
|
63
44
|
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
45
|
+
* When using the default client-side [JSON-based view system](https://docs.powersync.com/architecture/client-architecture#client-side-schema-and-sqlite-database-structure),
|
|
46
|
+
* the returned result's `rowsAffected` may be `0` for successful `UPDATE` and `DELETE` statements.
|
|
47
|
+
* Use a `RETURNING` clause and inspect `result.rows` when you need to confirm which rows changed.
|
|
67
48
|
*
|
|
68
|
-
*
|
|
49
|
+
* @param sql - The SQL query to execute
|
|
50
|
+
* @param parameters - Optional array of parameters to bind to the query
|
|
51
|
+
* @returns The query result as an object with structured key-value pairs
|
|
52
|
+
*/
|
|
53
|
+
execute: <T = SqliteRecord>(query: string, params?: any[] | undefined) => Promise<QueryResult<T>>;
|
|
54
|
+
/**
|
|
55
|
+
* Execute a SQL write (INSERT/UPDATE/DELETE) query directly on the database without any PowerSync processing.
|
|
56
|
+
* This bypasses certain PowerSync abstractions and is useful for accessing the raw database results.
|
|
69
57
|
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
58
|
+
* @param sql - The SQL query to execute
|
|
59
|
+
* @param parameters - Optional array of parameters to bind to the query
|
|
60
|
+
* @returns The {@link RawQueryResult} representing each row as an array.
|
|
73
61
|
*/
|
|
74
|
-
executeRaw: (query: string, params?: any[] | undefined) => Promise<
|
|
75
|
-
executeBatch: (query: string, params?: any[][]) => Promise<QueryResult>;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* @public
|
|
79
|
-
*/
|
|
80
|
-
export interface LockContext extends SqlExecutor, DBGetUtils {
|
|
62
|
+
executeRaw: (query: string, params?: any[] | undefined) => Promise<RawQueryResult>;
|
|
81
63
|
/**
|
|
82
|
-
*
|
|
64
|
+
* Execute a write query (INSERT/UPDATE/DELETE) multiple times with each parameter set
|
|
65
|
+
* and optionally return results.
|
|
66
|
+
* This is faster than executing separately with each parameter set.
|
|
83
67
|
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
* `readOnly` indicates that the lock context has been opened by passing `SQLITE_OPEN_READONLY` to `sqlite3_open_v2`.
|
|
68
|
+
* @param sql - The SQL query to execute
|
|
69
|
+
* @param parameters - Optional 2D array of parameter sets, where each inner array is a set of parameters for one execution
|
|
70
|
+
* @returns The query result
|
|
88
71
|
*/
|
|
89
|
-
|
|
72
|
+
executeBatch: (query: string, params?: any[][]) => Promise<QueryResult<never>>;
|
|
90
73
|
}
|
|
91
74
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @internal
|
|
75
|
+
* @public
|
|
95
76
|
*/
|
|
96
|
-
export declare
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
};
|
|
105
|
-
} & TBase;
|
|
77
|
+
export declare abstract class LockContext implements SqlExecutor, DBGetUtils {
|
|
78
|
+
abstract executeRaw<T>(query: string, params?: any[] | undefined): Promise<RawQueryResult>;
|
|
79
|
+
getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
|
|
80
|
+
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
|
|
81
|
+
get<T>(sql: string, parameters?: any[]): Promise<T>;
|
|
82
|
+
execute<T = SqliteRecord>(query: string, params?: any[] | undefined): Promise<QueryResult<T>>;
|
|
83
|
+
executeBatch(query: string, params?: any[][]): Promise<QueryResult<never>>;
|
|
84
|
+
}
|
|
106
85
|
/**
|
|
107
86
|
* @public
|
|
108
87
|
*/
|
|
109
88
|
export interface Transaction extends LockContext {
|
|
110
89
|
/** Commit multiple changes to the local DB using the Transaction context. */
|
|
111
|
-
commit: () => Promise<
|
|
90
|
+
commit: () => Promise<void>;
|
|
112
91
|
/** Roll back multiple attempted changes using the Transaction context. */
|
|
113
|
-
rollback: () => Promise<
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Update table operation numbers from SQLite
|
|
117
|
-
*
|
|
118
|
-
* @public
|
|
119
|
-
*/
|
|
120
|
-
export declare enum RowUpdateType {
|
|
121
|
-
SQLITE_INSERT = 18,
|
|
122
|
-
SQLITE_DELETE = 9,
|
|
123
|
-
SQLITE_UPDATE = 23
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* @public
|
|
127
|
-
*/
|
|
128
|
-
export interface TableUpdateOperation {
|
|
129
|
-
opType: RowUpdateType;
|
|
130
|
-
rowId: number;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Notification of an update to one or more tables, for the purpose of realtime change notifications.
|
|
134
|
-
*
|
|
135
|
-
* @public
|
|
136
|
-
*/
|
|
137
|
-
export interface UpdateNotification extends TableUpdateOperation {
|
|
138
|
-
table: string;
|
|
92
|
+
rollback: () => Promise<void>;
|
|
139
93
|
}
|
|
140
94
|
/**
|
|
141
95
|
* @public
|
|
142
96
|
*/
|
|
143
97
|
export interface BatchedUpdateNotification {
|
|
144
|
-
rawUpdates: UpdateNotification[];
|
|
145
98
|
tables: string[];
|
|
146
|
-
groupedUpdates: Record<string, TableUpdateOperation[]>;
|
|
147
99
|
}
|
|
148
100
|
/**
|
|
149
101
|
* @public
|
|
@@ -155,7 +107,7 @@ export interface DBAdapterListener extends BaseListener {
|
|
|
155
107
|
* without the need for a major version bump
|
|
156
108
|
* The DB adapter can also batch update notifications if supported.
|
|
157
109
|
*/
|
|
158
|
-
tablesUpdated: (updateNotification: BatchedUpdateNotification
|
|
110
|
+
tablesUpdated: (updateNotification: BatchedUpdateNotification) => void;
|
|
159
111
|
}
|
|
160
112
|
/**
|
|
161
113
|
* @public
|
|
@@ -166,55 +118,21 @@ export interface DBLockOptions {
|
|
|
166
118
|
/**
|
|
167
119
|
* @public
|
|
168
120
|
*/
|
|
169
|
-
export
|
|
170
|
-
name: string;
|
|
171
|
-
close
|
|
172
|
-
readLock
|
|
173
|
-
writeLock
|
|
121
|
+
export declare abstract class DBAdapter extends BaseObserver<DBAdapterListener> implements SqlExecutor, DBGetUtils {
|
|
122
|
+
abstract get name(): string;
|
|
123
|
+
abstract close(): void | Promise<void>;
|
|
124
|
+
abstract readLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
125
|
+
abstract writeLock<T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
174
126
|
/**
|
|
175
127
|
* This method refreshes the schema information across all connections. This is for advanced use cases, and should generally not be needed.
|
|
176
128
|
*/
|
|
177
|
-
refreshSchema
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
129
|
+
abstract refreshSchema(): Promise<void>;
|
|
130
|
+
readTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
131
|
+
writeTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
132
|
+
getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
|
|
133
|
+
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
|
|
134
|
+
get<T>(sql: string, parameters?: any[]): Promise<T>;
|
|
135
|
+
execute<T>(query: string, params?: any[]): Promise<QueryResult<T>>;
|
|
136
|
+
executeRaw(query: string, params?: any[]): Promise<RawQueryResult>;
|
|
137
|
+
executeBatch(query: string, params?: any[][]): Promise<QueryResult<never>>;
|
|
185
138
|
}
|
|
186
|
-
/**
|
|
187
|
-
* A mixin to implement {@link DBAdapter} by delegating to {@link ConnectionPool#readLock} and
|
|
188
|
-
* {@link ConnectionPool#writeLock}.
|
|
189
|
-
*
|
|
190
|
-
* @internal
|
|
191
|
-
*/
|
|
192
|
-
export declare function DBAdapterDefaultMixin<TBase extends new (...args: any[]) => ConnectionPool>(Base: TBase): {
|
|
193
|
-
new (...args: any[]): {
|
|
194
|
-
readTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
195
|
-
writeTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions): Promise<T>;
|
|
196
|
-
getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
|
|
197
|
-
getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
|
|
198
|
-
get<T>(sql: string, parameters?: any[]): Promise<T>;
|
|
199
|
-
execute(query: string, params?: any[]): Promise<QueryResult>;
|
|
200
|
-
executeRaw(query: string, params?: any[]): Promise<any[][]>;
|
|
201
|
-
executeBatch(query: string, params?: any[][]): Promise<QueryResult>;
|
|
202
|
-
name: string;
|
|
203
|
-
close: () => void | Promise<void>;
|
|
204
|
-
readLock: <T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions) => Promise<T>;
|
|
205
|
-
writeLock: <T>(fn: (tx: LockContext) => Promise<T>, options?: DBLockOptions) => Promise<T>;
|
|
206
|
-
/**
|
|
207
|
-
* This method refreshes the schema information across all connections. This is for advanced use cases, and should generally not be needed.
|
|
208
|
-
*/
|
|
209
|
-
refreshSchema: () => Promise<void>;
|
|
210
|
-
registerListener(listener: Partial<DBAdapterListener>): () => void;
|
|
211
|
-
};
|
|
212
|
-
} & TBase;
|
|
213
|
-
/**
|
|
214
|
-
* @internal
|
|
215
|
-
*/
|
|
216
|
-
export declare function isBatchedUpdateNotification(update: BatchedUpdateNotification | UpdateNotification): update is BatchedUpdateNotification;
|
|
217
|
-
/**
|
|
218
|
-
* @internal
|
|
219
|
-
*/
|
|
220
|
-
export declare function extractTableUpdates(update: BatchedUpdateNotification | UpdateNotification): string[];
|