@powersync/common 1.41.0 → 1.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.cjs +10820 -22
- package/dist/bundle.cjs.map +1 -0
- package/dist/bundle.mjs +10741 -22
- package/dist/bundle.mjs.map +1 -0
- package/dist/bundle.node.cjs +10820 -0
- package/dist/bundle.node.cjs.map +1 -0
- package/dist/bundle.node.mjs +10741 -0
- package/dist/bundle.node.mjs.map +1 -0
- package/dist/index.d.cts +77 -13
- package/lib/client/AbstractPowerSyncDatabase.js +1 -0
- package/lib/client/AbstractPowerSyncDatabase.js.map +1 -0
- package/lib/client/AbstractPowerSyncOpenFactory.js +1 -0
- package/lib/client/AbstractPowerSyncOpenFactory.js.map +1 -0
- package/lib/client/ConnectionManager.js +1 -0
- package/lib/client/ConnectionManager.js.map +1 -0
- package/lib/client/CustomQuery.js +1 -0
- package/lib/client/CustomQuery.js.map +1 -0
- package/lib/client/Query.js +1 -0
- package/lib/client/Query.js.map +1 -0
- package/lib/client/SQLOpenFactory.js +1 -0
- package/lib/client/SQLOpenFactory.js.map +1 -0
- package/lib/client/compilableQueryWatch.js +1 -0
- package/lib/client/compilableQueryWatch.js.map +1 -0
- package/lib/client/connection/PowerSyncBackendConnector.js +1 -0
- package/lib/client/connection/PowerSyncBackendConnector.js.map +1 -0
- package/lib/client/connection/PowerSyncCredentials.js +1 -0
- package/lib/client/connection/PowerSyncCredentials.js.map +1 -0
- package/lib/client/constants.js +1 -0
- package/lib/client/constants.js.map +1 -0
- package/lib/client/runOnSchemaChange.js +1 -0
- package/lib/client/runOnSchemaChange.js.map +1 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js +1 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +1 -0
- package/lib/client/sync/bucket/CrudBatch.js +1 -0
- package/lib/client/sync/bucket/CrudBatch.js.map +1 -0
- package/lib/client/sync/bucket/CrudEntry.js +1 -0
- package/lib/client/sync/bucket/CrudEntry.js.map +1 -0
- package/lib/client/sync/bucket/CrudTransaction.js +1 -0
- package/lib/client/sync/bucket/CrudTransaction.js.map +1 -0
- package/lib/client/sync/bucket/OpType.js +1 -0
- package/lib/client/sync/bucket/OpType.js.map +1 -0
- package/lib/client/sync/bucket/OplogEntry.js +1 -0
- package/lib/client/sync/bucket/OplogEntry.js.map +1 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js +1 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -0
- package/lib/client/sync/bucket/SyncDataBatch.js +1 -0
- package/lib/client/sync/bucket/SyncDataBatch.js.map +1 -0
- package/lib/client/sync/bucket/SyncDataBucket.js +1 -0
- package/lib/client/sync/bucket/SyncDataBucket.js.map +1 -0
- package/lib/client/sync/stream/AbstractRemote.d.ts +5 -0
- package/lib/client/sync/stream/AbstractRemote.js +19 -6
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +1 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -0
- package/lib/client/sync/stream/WebsocketClientTransport.js +1 -0
- package/lib/client/sync/stream/WebsocketClientTransport.js.map +1 -0
- package/lib/client/sync/stream/core-instruction.js +1 -0
- package/lib/client/sync/stream/core-instruction.js.map +1 -0
- package/lib/client/sync/stream/streaming-sync-types.js +1 -0
- package/lib/client/sync/stream/streaming-sync-types.js.map +1 -0
- package/lib/client/sync/sync-streams.js +1 -0
- package/lib/client/sync/sync-streams.js.map +1 -0
- package/lib/client/triggers/TriggerManager.d.ts +71 -12
- package/lib/client/triggers/TriggerManager.js +1 -0
- package/lib/client/triggers/TriggerManager.js.map +1 -0
- package/lib/client/triggers/TriggerManagerImpl.js +11 -5
- package/lib/client/triggers/TriggerManagerImpl.js.map +1 -0
- package/lib/client/triggers/sanitizeSQL.js +1 -0
- package/lib/client/triggers/sanitizeSQL.js.map +1 -0
- package/lib/client/watched/GetAllQuery.js +1 -0
- package/lib/client/watched/GetAllQuery.js.map +1 -0
- package/lib/client/watched/WatchedQuery.js +1 -0
- package/lib/client/watched/WatchedQuery.js.map +1 -0
- package/lib/client/watched/processors/AbstractQueryProcessor.js +1 -0
- package/lib/client/watched/processors/AbstractQueryProcessor.js.map +1 -0
- package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -0
- package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -0
- package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -0
- package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -0
- package/lib/client/watched/processors/comparators.js +1 -0
- package/lib/client/watched/processors/comparators.js.map +1 -0
- package/lib/db/DBAdapter.js +1 -0
- package/lib/db/DBAdapter.js.map +1 -0
- package/lib/db/crud/SyncProgress.js +1 -0
- package/lib/db/crud/SyncProgress.js.map +1 -0
- package/lib/db/crud/SyncStatus.js +1 -0
- package/lib/db/crud/SyncStatus.js.map +1 -0
- package/lib/db/crud/UploadQueueStatus.js +1 -0
- package/lib/db/crud/UploadQueueStatus.js.map +1 -0
- package/lib/db/schema/Column.js +1 -0
- package/lib/db/schema/Column.js.map +1 -0
- package/lib/db/schema/Index.js +1 -0
- package/lib/db/schema/Index.js.map +1 -0
- package/lib/db/schema/IndexedColumn.js +1 -0
- package/lib/db/schema/IndexedColumn.js.map +1 -0
- package/lib/db/schema/RawTable.js +1 -0
- package/lib/db/schema/RawTable.js.map +1 -0
- package/lib/db/schema/Schema.js +1 -0
- package/lib/db/schema/Schema.js.map +1 -0
- package/lib/db/schema/Table.js +1 -0
- package/lib/db/schema/Table.js.map +1 -0
- package/lib/db/schema/TableV2.js +1 -0
- package/lib/db/schema/TableV2.js.map +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -0
- package/lib/types/types.js +1 -0
- package/lib/types/types.js.map +1 -0
- package/lib/utils/AbortOperation.js +1 -0
- package/lib/utils/AbortOperation.js.map +1 -0
- package/lib/utils/BaseObserver.js +1 -0
- package/lib/utils/BaseObserver.js.map +1 -0
- package/lib/utils/ControlledExecutor.js +1 -0
- package/lib/utils/ControlledExecutor.js.map +1 -0
- package/lib/utils/DataStream.js +1 -0
- package/lib/utils/DataStream.js.map +1 -0
- package/lib/utils/Logger.js +1 -0
- package/lib/utils/Logger.js.map +1 -0
- package/lib/utils/MetaBaseObserver.js +1 -0
- package/lib/utils/MetaBaseObserver.js.map +1 -0
- package/lib/utils/async.js +1 -0
- package/lib/utils/async.js.map +1 -0
- package/lib/utils/mutex.js +1 -0
- package/lib/utils/mutex.js.map +1 -0
- package/lib/utils/parseQuery.js +1 -0
- package/lib/utils/parseQuery.js.map +1 -0
- package/package.json +23 -15
- package/src/client/AbstractPowerSyncDatabase.ts +1343 -0
- package/src/client/AbstractPowerSyncOpenFactory.ts +39 -0
- package/src/client/ConnectionManager.ts +402 -0
- package/src/client/CustomQuery.ts +56 -0
- package/src/client/Query.ts +106 -0
- package/src/client/SQLOpenFactory.ts +55 -0
- package/src/client/compilableQueryWatch.ts +55 -0
- package/src/client/connection/PowerSyncBackendConnector.ts +25 -0
- package/src/client/connection/PowerSyncCredentials.ts +5 -0
- package/src/client/constants.ts +1 -0
- package/src/client/runOnSchemaChange.ts +31 -0
- package/src/client/sync/bucket/BucketStorageAdapter.ts +118 -0
- package/src/client/sync/bucket/CrudBatch.ts +21 -0
- package/src/client/sync/bucket/CrudEntry.ts +172 -0
- package/src/client/sync/bucket/CrudTransaction.ts +21 -0
- package/src/client/sync/bucket/OpType.ts +23 -0
- package/src/client/sync/bucket/OplogEntry.ts +50 -0
- package/src/client/sync/bucket/SqliteBucketStorage.ts +395 -0
- package/src/client/sync/bucket/SyncDataBatch.ts +11 -0
- package/src/client/sync/bucket/SyncDataBucket.ts +49 -0
- package/src/client/sync/stream/AbstractRemote.ts +636 -0
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +1258 -0
- package/src/client/sync/stream/WebsocketClientTransport.ts +80 -0
- package/src/client/sync/stream/core-instruction.ts +99 -0
- package/src/client/sync/stream/streaming-sync-types.ts +205 -0
- package/src/client/sync/sync-streams.ts +107 -0
- package/src/client/triggers/TriggerManager.ts +451 -0
- package/src/client/triggers/TriggerManagerImpl.ts +320 -0
- package/src/client/triggers/sanitizeSQL.ts +66 -0
- package/src/client/watched/GetAllQuery.ts +46 -0
- package/src/client/watched/WatchedQuery.ts +121 -0
- package/src/client/watched/processors/AbstractQueryProcessor.ts +226 -0
- package/src/client/watched/processors/DifferentialQueryProcessor.ts +305 -0
- package/src/client/watched/processors/OnChangeQueryProcessor.ts +122 -0
- package/src/client/watched/processors/comparators.ts +57 -0
- package/src/db/DBAdapter.ts +134 -0
- package/src/db/crud/SyncProgress.ts +100 -0
- package/src/db/crud/SyncStatus.ts +308 -0
- package/src/db/crud/UploadQueueStatus.ts +20 -0
- package/src/db/schema/Column.ts +60 -0
- package/src/db/schema/Index.ts +39 -0
- package/src/db/schema/IndexedColumn.ts +42 -0
- package/src/db/schema/RawTable.ts +67 -0
- package/src/db/schema/Schema.ts +76 -0
- package/src/db/schema/Table.ts +359 -0
- package/src/db/schema/TableV2.ts +9 -0
- package/src/index.ts +52 -0
- package/src/types/types.ts +9 -0
- package/src/utils/AbortOperation.ts +17 -0
- package/src/utils/BaseObserver.ts +41 -0
- package/src/utils/ControlledExecutor.ts +72 -0
- package/src/utils/DataStream.ts +211 -0
- package/src/utils/Logger.ts +47 -0
- package/src/utils/MetaBaseObserver.ts +81 -0
- package/src/utils/async.ts +61 -0
- package/src/utils/mutex.ts +34 -0
- package/src/utils/parseQuery.ts +25 -0
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import { LockContext } from '../../db/DBAdapter.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SQLite operations to track changes for with {@link TriggerManager}
|
|
5
|
+
* @experimental
|
|
6
|
+
*/
|
|
7
|
+
export enum DiffTriggerOperation {
|
|
8
|
+
INSERT = 'INSERT',
|
|
9
|
+
UPDATE = 'UPDATE',
|
|
10
|
+
DELETE = 'DELETE'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @experimental
|
|
15
|
+
* Diffs created by {@link TriggerManager#createDiffTrigger} are stored in a temporary table.
|
|
16
|
+
* This is the base record structure for all diff records.
|
|
17
|
+
*
|
|
18
|
+
* @template TOperationId - The type for `operation_id`. Defaults to `number` as returned by default SQLite database queries.
|
|
19
|
+
* Use `string` for full 64-bit precision when using `{ castOperationIdAsText: true }` option.
|
|
20
|
+
*/
|
|
21
|
+
export interface BaseTriggerDiffRecord<TOperationId extends string | number = number> {
|
|
22
|
+
/**
|
|
23
|
+
* The modified row's `id` column value.
|
|
24
|
+
*/
|
|
25
|
+
id: string;
|
|
26
|
+
/**
|
|
27
|
+
* The operation performed which created this record.
|
|
28
|
+
*/
|
|
29
|
+
operation: DiffTriggerOperation;
|
|
30
|
+
/**
|
|
31
|
+
* Auto-incrementing primary key for the operation.
|
|
32
|
+
* Defaults to number as returned by database queries (wa-sqlite returns lower 32 bits).
|
|
33
|
+
* Can be string for full 64-bit precision when using `{ castOperationIdAsText: true }` option.
|
|
34
|
+
*/
|
|
35
|
+
operation_id: TOperationId;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Time the change operation was recorded.
|
|
39
|
+
* This is in ISO 8601 format, e.g. `2023-10-01T12:00:00.000Z`.
|
|
40
|
+
*/
|
|
41
|
+
timestamp: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @experimental
|
|
46
|
+
* Represents a diff record for a SQLite UPDATE operation.
|
|
47
|
+
* This record contains the new value and optionally the previous value.
|
|
48
|
+
* Values are stored as JSON strings.
|
|
49
|
+
*/
|
|
50
|
+
export interface TriggerDiffUpdateRecord<TOperationId extends string | number = number>
|
|
51
|
+
extends BaseTriggerDiffRecord<TOperationId> {
|
|
52
|
+
operation: DiffTriggerOperation.UPDATE;
|
|
53
|
+
/**
|
|
54
|
+
* The updated state of the row in JSON string format.
|
|
55
|
+
*/
|
|
56
|
+
value: string;
|
|
57
|
+
/**
|
|
58
|
+
* The previous value of the row in JSON string format.
|
|
59
|
+
*/
|
|
60
|
+
previous_value: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @experimental
|
|
65
|
+
* Represents a diff record for a SQLite INSERT operation.
|
|
66
|
+
* This record contains the new value represented as a JSON string.
|
|
67
|
+
*/
|
|
68
|
+
export interface TriggerDiffInsertRecord<TOperationId extends string | number = number>
|
|
69
|
+
extends BaseTriggerDiffRecord<TOperationId> {
|
|
70
|
+
operation: DiffTriggerOperation.INSERT;
|
|
71
|
+
/**
|
|
72
|
+
* The value of the row, at the time of INSERT, in JSON string format.
|
|
73
|
+
*/
|
|
74
|
+
value: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @experimental
|
|
79
|
+
* Represents a diff record for a SQLite DELETE operation.
|
|
80
|
+
* This record contains the new value represented as a JSON string.
|
|
81
|
+
*/
|
|
82
|
+
export interface TriggerDiffDeleteRecord<TOperationId extends string | number = number>
|
|
83
|
+
extends BaseTriggerDiffRecord<TOperationId> {
|
|
84
|
+
operation: DiffTriggerOperation.DELETE;
|
|
85
|
+
/**
|
|
86
|
+
* The value of the row, before the DELETE operation, in JSON string format.
|
|
87
|
+
*/
|
|
88
|
+
value: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @experimental
|
|
93
|
+
* Diffs created by {@link TriggerManager#createDiffTrigger} are stored in a temporary table.
|
|
94
|
+
* This is the record structure for all diff records.
|
|
95
|
+
*
|
|
96
|
+
* Querying the DIFF table directly with {@link TriggerDiffHandlerContext#withDiff} will return records
|
|
97
|
+
* with the structure of this type.
|
|
98
|
+
*
|
|
99
|
+
* @template TOperationId - The type for `operation_id`. Defaults to `number` as returned by database queries.
|
|
100
|
+
* Use `string` for full 64-bit precision when using `{ castOperationIdAsText: true }` option.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* // Default: operation_id is number
|
|
105
|
+
* const diffs = await context.withDiff<TriggerDiffRecord>('SELECT * FROM DIFF');
|
|
106
|
+
*
|
|
107
|
+
* // With string operation_id for full precision
|
|
108
|
+
* const diffsWithString = await context.withDiff<TriggerDiffRecord<string>>(
|
|
109
|
+
* 'SELECT * FROM DIFF',
|
|
110
|
+
* undefined,
|
|
111
|
+
* { castOperationIdAsText: true }
|
|
112
|
+
* );
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export type TriggerDiffRecord<TOperationId extends string | number = number> =
|
|
116
|
+
| TriggerDiffUpdateRecord<TOperationId>
|
|
117
|
+
| TriggerDiffInsertRecord<TOperationId>
|
|
118
|
+
| TriggerDiffDeleteRecord<TOperationId>;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @experimental
|
|
122
|
+
* Querying the DIFF table directly with {@link TriggerDiffHandlerContext#withExtractedDiff} will return records
|
|
123
|
+
* with the tracked columns extracted from the JSON value.
|
|
124
|
+
* This type represents the structure of such records.
|
|
125
|
+
*
|
|
126
|
+
* @template T - The type for the extracted columns from the tracked JSON value.
|
|
127
|
+
* @template TOperationId - The type for `operation_id`. Defaults to `number` as returned by database queries.
|
|
128
|
+
* Use `string` for full 64-bit precision when using `{ castOperationIdAsText: true }` option.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* // Default: operation_id is number
|
|
133
|
+
* const diffs = await context.withExtractedDiff<ExtractedTriggerDiffRecord<{id: string, name: string}>>('SELECT * FROM DIFF');
|
|
134
|
+
*
|
|
135
|
+
* // With string operation_id for full precision
|
|
136
|
+
* const diffsWithString = await context.withExtractedDiff<ExtractedTriggerDiffRecord<{id: string, name: string}, string>>(
|
|
137
|
+
* 'SELECT * FROM DIFF',
|
|
138
|
+
* undefined,
|
|
139
|
+
* { castOperationIdAsText: true }
|
|
140
|
+
* );
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export type ExtractedTriggerDiffRecord<T, TOperationId extends string | number = number> = T & {
|
|
144
|
+
[K in keyof Omit<BaseTriggerDiffRecord<TOperationId>, 'id'> as `__${string & K}`]: TriggerDiffRecord<TOperationId>[K];
|
|
145
|
+
} & {
|
|
146
|
+
__previous_value?: string;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @experimental
|
|
151
|
+
* Hooks used in the creation of a table diff trigger.
|
|
152
|
+
*/
|
|
153
|
+
export interface TriggerCreationHooks {
|
|
154
|
+
/**
|
|
155
|
+
* Executed inside a write lock before the trigger is created.
|
|
156
|
+
*/
|
|
157
|
+
beforeCreate?: (context: LockContext) => Promise<void>;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Common interface for options used in creating a diff trigger.
|
|
162
|
+
*/
|
|
163
|
+
|
|
164
|
+
interface BaseCreateDiffTriggerOptions {
|
|
165
|
+
/**
|
|
166
|
+
* PowerSync source table/view to trigger and track changes from.
|
|
167
|
+
* This should be present in the PowerSync database's schema.
|
|
168
|
+
*/
|
|
169
|
+
source: string;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Columns to track and report changes for.
|
|
173
|
+
* Defaults to all columns in the source table.
|
|
174
|
+
* Use an empty array to track only the ID and operation.
|
|
175
|
+
*/
|
|
176
|
+
columns?: string[];
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Condition to filter when the triggers should fire.
|
|
180
|
+
* This corresponds to a SQLite [WHEN](https://sqlite.org/lang_createtrigger.html) clause in the trigger body.
|
|
181
|
+
* This is useful for only triggering on specific conditions.
|
|
182
|
+
* For example, you can use it to only trigger on certain values in the NEW row.
|
|
183
|
+
* Note that for PowerSync the row data is stored in a JSON column named `data`.
|
|
184
|
+
* The row id is available in the `id` column.
|
|
185
|
+
*
|
|
186
|
+
* NB! The WHEN clauses here are added directly to the SQLite trigger creation SQL.
|
|
187
|
+
* Any user input strings here should be sanitized externally. The {@link when} string template function performs
|
|
188
|
+
* some basic sanitization, extra external sanitization is recommended.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* {
|
|
192
|
+
* 'INSERT': sanitizeSQL`json_extract(NEW.data, '$.list_id') = ${sanitizeUUID(list.id)}`,
|
|
193
|
+
* 'INSERT': `TRUE`,
|
|
194
|
+
* 'UPDATE': sanitizeSQL`NEW.id = 'abcd' AND json_extract(NEW.data, '$.status') = 'active'`,
|
|
195
|
+
* 'DELETE': sanitizeSQL`json_extract(OLD.data, '$.list_id') = 'abcd'`
|
|
196
|
+
* }
|
|
197
|
+
*/
|
|
198
|
+
when: Partial<Record<DiffTriggerOperation, string>>;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Hooks which allow execution during the trigger creation process.
|
|
202
|
+
*/
|
|
203
|
+
hooks?: TriggerCreationHooks;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @experimental
|
|
208
|
+
* Options for {@link TriggerManager#createDiffTrigger}.
|
|
209
|
+
*/
|
|
210
|
+
export interface CreateDiffTriggerOptions extends BaseCreateDiffTriggerOptions {
|
|
211
|
+
/**
|
|
212
|
+
* Destination table to send changes to.
|
|
213
|
+
* This table is created internally as a SQLite temporary table.
|
|
214
|
+
* This table will be dropped once the trigger is removed.
|
|
215
|
+
*/
|
|
216
|
+
destination: string;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @experimental
|
|
221
|
+
* Callback to drop a trigger after it has been created.
|
|
222
|
+
*/
|
|
223
|
+
export type TriggerRemoveCallback = () => Promise<void>;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @experimental
|
|
227
|
+
* Options for {@link TriggerDiffHandlerContext#withDiff}.
|
|
228
|
+
*/
|
|
229
|
+
export interface WithDiffOptions {
|
|
230
|
+
/**
|
|
231
|
+
* If true, casts `operation_id` as TEXT in the internal CTE to preserve full 64-bit precision.
|
|
232
|
+
* Use this when you need to ensure `operation_id` is treated as a string to avoid precision loss
|
|
233
|
+
* for values exceeding JavaScript's Number.MAX_SAFE_INTEGER.
|
|
234
|
+
*
|
|
235
|
+
* When enabled, use {@link TriggerDiffRecord}<string> to type the result correctly.
|
|
236
|
+
*/
|
|
237
|
+
castOperationIdAsText?: boolean;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @experimental
|
|
242
|
+
* Context for the `onChange` handler provided to {@link TriggerManager#trackTableDiff}.
|
|
243
|
+
*/
|
|
244
|
+
export interface TriggerDiffHandlerContext extends LockContext {
|
|
245
|
+
/**
|
|
246
|
+
* The name of the temporary destination table created by the trigger.
|
|
247
|
+
*/
|
|
248
|
+
destinationTable: string;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Allows querying the database with access to the table containing DIFF records.
|
|
252
|
+
* The diff table is accessible via the `DIFF` accessor.
|
|
253
|
+
*
|
|
254
|
+
* The `DIFF` table is of the form described in {@link TriggerManager#createDiffTrigger}
|
|
255
|
+
* ```sql
|
|
256
|
+
* CREATE TEMP DIFF (
|
|
257
|
+
* operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
258
|
+
* id TEXT,
|
|
259
|
+
* operation TEXT,
|
|
260
|
+
* timestamp TEXT,
|
|
261
|
+
* value TEXT,
|
|
262
|
+
* previous_value TEXT
|
|
263
|
+
* );
|
|
264
|
+
* ```
|
|
265
|
+
*
|
|
266
|
+
* Note that the `value` and `previous_value` columns store the row state in JSON string format.
|
|
267
|
+
* To access the row state in an extracted form see {@link TriggerDiffHandlerContext#withExtractedDiff}.
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* ```sql
|
|
271
|
+
* --- This fetches the current state of `todo` rows which have a diff operation present.
|
|
272
|
+
* --- The state of the row at the time of the operation is accessible in the DIFF records.
|
|
273
|
+
* SELECT
|
|
274
|
+
* todos.*
|
|
275
|
+
* FROM
|
|
276
|
+
* DIFF
|
|
277
|
+
* JOIN todos ON DIFF.id = todos.id
|
|
278
|
+
* WHERE json_extract(DIFF.value, '$.status') = 'active'
|
|
279
|
+
* ```
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* // With operation_id cast as TEXT for full precision
|
|
284
|
+
* const diffs = await context.withDiff<TriggerDiffRecord<string>>(
|
|
285
|
+
* 'SELECT * FROM DIFF',
|
|
286
|
+
* undefined,
|
|
287
|
+
* { castOperationIdAsText: true }
|
|
288
|
+
* );
|
|
289
|
+
* // diffs[0].operation_id is now typed as string
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
withDiff: <T = any>(query: string, params?: ReadonlyArray<Readonly<any>>, options?: WithDiffOptions) => Promise<T[]>;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Allows querying the database with access to the table containing diff records.
|
|
296
|
+
* The diff table is accessible via the `DIFF` accessor.
|
|
297
|
+
*
|
|
298
|
+
* This is similar to {@link withDiff} but extracts the row columns from the tracked JSON value. The diff operation
|
|
299
|
+
* data is aliased as `__` columns to avoid column conflicts.
|
|
300
|
+
*
|
|
301
|
+
* For {@link DiffTriggerOperation#DELETE} operations the previous_value columns are extracted for convenience.
|
|
302
|
+
*
|
|
303
|
+
*
|
|
304
|
+
* ```sql
|
|
305
|
+
* CREATE TEMP TABLE DIFF (
|
|
306
|
+
* id TEXT,
|
|
307
|
+
* replicated_column_1 COLUMN_TYPE,
|
|
308
|
+
* replicated_column_2 COLUMN_TYPE,
|
|
309
|
+
* __operation TEXT,
|
|
310
|
+
* __timestamp TEXT,
|
|
311
|
+
* __previous_value TEXT
|
|
312
|
+
* );
|
|
313
|
+
* ```
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```sql
|
|
317
|
+
* SELECT
|
|
318
|
+
* todos.*
|
|
319
|
+
* FROM
|
|
320
|
+
* DIFF
|
|
321
|
+
* JOIN todos ON DIFF.id = todos.id
|
|
322
|
+
* --- The todo column names are extracted from json and are available as DIFF.name
|
|
323
|
+
* WHERE DIFF.name = 'example'
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
withExtractedDiff: <T = any>(query: string, params?: ReadonlyArray<Readonly<any>>) => Promise<T[]>;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* @experimental
|
|
331
|
+
* Options for tracking changes to a table with {@link TriggerManager#trackTableDiff}.
|
|
332
|
+
*/
|
|
333
|
+
export interface TrackDiffOptions extends BaseCreateDiffTriggerOptions {
|
|
334
|
+
/**
|
|
335
|
+
* Handler for processing diff operations.
|
|
336
|
+
* Automatically invoked once diff items are present.
|
|
337
|
+
* Diff items are automatically cleared after the handler is invoked.
|
|
338
|
+
*/
|
|
339
|
+
onChange: (context: TriggerDiffHandlerContext) => Promise<void>;
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* The minimum interval, in milliseconds, between {@link onChange} invocations.
|
|
343
|
+
* @default {@link DEFAULT_WATCH_THROTTLE_MS}
|
|
344
|
+
*/
|
|
345
|
+
throttleMs?: number;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* @experimental
|
|
350
|
+
*/
|
|
351
|
+
export interface TriggerManager {
|
|
352
|
+
/**
|
|
353
|
+
* @experimental
|
|
354
|
+
* Creates a temporary trigger which tracks changes to a source table
|
|
355
|
+
* and writes changes to a destination table.
|
|
356
|
+
* The temporary destination table is created internally and will be dropped when the trigger is removed.
|
|
357
|
+
* The temporary destination table is created with the structure:
|
|
358
|
+
*
|
|
359
|
+
* ```sql
|
|
360
|
+
* CREATE TEMP TABLE ${destination} (
|
|
361
|
+
* operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
362
|
+
* id TEXT,
|
|
363
|
+
* operation TEXT,
|
|
364
|
+
* timestamp TEXT,
|
|
365
|
+
* value TEXT,
|
|
366
|
+
* previous_value TEXT
|
|
367
|
+
* );
|
|
368
|
+
* ```
|
|
369
|
+
* The `value` column contains the JSON representation of the row's value at the change.
|
|
370
|
+
*
|
|
371
|
+
* For {@link DiffTriggerOperation#UPDATE} operations the `previous_value` column contains the previous value of the changed row
|
|
372
|
+
* in a JSON format.
|
|
373
|
+
*
|
|
374
|
+
* NB: The triggers created by this method might be invalidated by {@link AbstractPowerSyncDatabase#updateSchema} calls.
|
|
375
|
+
* These triggers should manually be dropped and recreated when updating the schema.
|
|
376
|
+
*
|
|
377
|
+
* @returns A callback to remove the trigger and drop the destination table.
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* ```javascript
|
|
381
|
+
* const dispose = await database.triggers.createDiffTrigger({
|
|
382
|
+
* source: 'lists',
|
|
383
|
+
* destination: 'ps_temp_lists_diff',
|
|
384
|
+
* columns: ['name'],
|
|
385
|
+
* when: {
|
|
386
|
+
* [DiffTriggerOperation.INSERT]: 'TRUE',
|
|
387
|
+
* [DiffTriggerOperation.UPDATE]: 'TRUE',
|
|
388
|
+
* [DiffTriggerOperation.DELETE]: 'TRUE'
|
|
389
|
+
* }
|
|
390
|
+
* });
|
|
391
|
+
* ```
|
|
392
|
+
*/
|
|
393
|
+
createDiffTrigger(options: CreateDiffTriggerOptions): Promise<TriggerRemoveCallback>;
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* @experimental
|
|
397
|
+
* Tracks changes for a table. Triggering a provided handler on changes.
|
|
398
|
+
* Uses {@link createDiffTrigger} internally to create a temporary destination table.
|
|
399
|
+
*
|
|
400
|
+
* @returns A callback to cleanup the trigger and stop tracking changes.
|
|
401
|
+
*
|
|
402
|
+
* NB: The triggers created by this method might be invalidated by {@link AbstractPowerSyncDatabase#updateSchema} calls.
|
|
403
|
+
* These triggers should manually be dropped and recreated when updating the schema.
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* ```javascript
|
|
407
|
+
* const dispose = database.triggers.trackTableDiff({
|
|
408
|
+
* source: 'todos',
|
|
409
|
+
* columns: ['list_id'],
|
|
410
|
+
* when: {
|
|
411
|
+
* [DiffTriggerOperation.INSERT]: sanitizeSQL`json_extract(NEW.data, '$.list_id') = ${sanitizeUUID(someIdVariable)}`
|
|
412
|
+
* },
|
|
413
|
+
* onChange: async (context) => {
|
|
414
|
+
* // Fetches the todo records that were inserted during this diff
|
|
415
|
+
* const newTodos = await context.withDiff<Database['todos']>(`
|
|
416
|
+
* SELECT
|
|
417
|
+
* todos.*
|
|
418
|
+
* FROM
|
|
419
|
+
* DIFF
|
|
420
|
+
* JOIN todos ON DIFF.id = todos.id
|
|
421
|
+
* `);
|
|
422
|
+
*
|
|
423
|
+
* // Process newly created todos
|
|
424
|
+
* },
|
|
425
|
+
* hooks: {
|
|
426
|
+
* beforeCreate: async (lockContext) => {
|
|
427
|
+
* // This hook is executed inside the write lock before the trigger is created.
|
|
428
|
+
* // It can be used to synchronize the current state of the table with processor logic.
|
|
429
|
+
* // Any changes after this callback are guaranteed to trigger the `onChange` handler.
|
|
430
|
+
*
|
|
431
|
+
* // Read the current state of the todos table
|
|
432
|
+
* const currentTodos = await lockContext.getAll<Database['todos']>(
|
|
433
|
+
* `
|
|
434
|
+
* SELECT
|
|
435
|
+
* *
|
|
436
|
+
* FROM
|
|
437
|
+
* todos
|
|
438
|
+
* WHERE
|
|
439
|
+
* list_id = ?
|
|
440
|
+
* `,
|
|
441
|
+
* ['123']
|
|
442
|
+
* );
|
|
443
|
+
*
|
|
444
|
+
* // Process existing todos
|
|
445
|
+
* }
|
|
446
|
+
* }
|
|
447
|
+
* });
|
|
448
|
+
* ```
|
|
449
|
+
*/
|
|
450
|
+
trackTableDiff(options: TrackDiffOptions): Promise<TriggerRemoveCallback>;
|
|
451
|
+
}
|