@powersync/common 1.36.0 → 1.37.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 +3 -3
- package/dist/bundle.mjs +3 -3
- package/dist/index.d.cts +436 -2
- package/lib/client/AbstractPowerSyncDatabase.d.ts +38 -0
- package/lib/client/AbstractPowerSyncDatabase.js +76 -17
- package/lib/client/sync/stream/AbstractRemote.js +3 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +3 -0
- package/lib/client/triggers/TriggerManager.d.ts +363 -0
- package/lib/client/triggers/TriggerManager.js +10 -0
- package/lib/client/triggers/TriggerManagerImpl.d.ts +18 -0
- package/lib/client/triggers/TriggerManagerImpl.js +265 -0
- package/lib/client/triggers/sanitizeSQL.d.ts +34 -0
- package/lib/client/triggers/sanitizeSQL.js +68 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/utils/async.d.ts +9 -0
- package/lib/utils/async.js +9 -0
- package/package.json +1 -1
|
@@ -693,6 +693,9 @@ The next upload iteration will be delayed.`);
|
|
|
693
693
|
const remote = this.options.remote;
|
|
694
694
|
let receivingLines = null;
|
|
695
695
|
let hadSyncLine = false;
|
|
696
|
+
if (signal.aborted) {
|
|
697
|
+
throw new AbortOperation('Connection request has been aborted');
|
|
698
|
+
}
|
|
696
699
|
const abortController = new AbortController();
|
|
697
700
|
signal.addEventListener('abort', () => abortController.abort());
|
|
698
701
|
// Pending sync lines received from the service, as well as local events that trigger a powersync_control
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { LockContext } from '../../db/DBAdapter.js';
|
|
2
|
+
/**
|
|
3
|
+
* SQLite operations to track changes for with {@link TriggerManager}
|
|
4
|
+
* @experimental
|
|
5
|
+
*/
|
|
6
|
+
export declare enum DiffTriggerOperation {
|
|
7
|
+
INSERT = "INSERT",
|
|
8
|
+
UPDATE = "UPDATE",
|
|
9
|
+
DELETE = "DELETE"
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* @experimental
|
|
13
|
+
* Diffs created by {@link TriggerManager#createDiffTrigger} are stored in a temporary table.
|
|
14
|
+
* This is the base record structure for all diff records.
|
|
15
|
+
*/
|
|
16
|
+
export interface BaseTriggerDiffRecord {
|
|
17
|
+
/**
|
|
18
|
+
* The modified row's `id` column value.
|
|
19
|
+
*/
|
|
20
|
+
id: string;
|
|
21
|
+
/**
|
|
22
|
+
* The operation performed which created this record.
|
|
23
|
+
*/
|
|
24
|
+
operation: DiffTriggerOperation;
|
|
25
|
+
/**
|
|
26
|
+
* Time the change operation was recorded.
|
|
27
|
+
* This is in ISO 8601 format, e.g. `2023-10-01T12:00:00.000Z`.
|
|
28
|
+
*/
|
|
29
|
+
timestamp: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @experimental
|
|
33
|
+
* Represents a diff record for a SQLite UPDATE operation.
|
|
34
|
+
* This record contains the new value and optionally the previous value.
|
|
35
|
+
* Values are stored as JSON strings.
|
|
36
|
+
*/
|
|
37
|
+
export interface TriggerDiffUpdateRecord extends BaseTriggerDiffRecord {
|
|
38
|
+
operation: DiffTriggerOperation.UPDATE;
|
|
39
|
+
/**
|
|
40
|
+
* The updated state of the row in JSON string format.
|
|
41
|
+
*/
|
|
42
|
+
value: string;
|
|
43
|
+
/**
|
|
44
|
+
* The previous value of the row in JSON string format.
|
|
45
|
+
*/
|
|
46
|
+
previous_value: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @experimental
|
|
50
|
+
* Represents a diff record for a SQLite INSERT operation.
|
|
51
|
+
* This record contains the new value represented as a JSON string.
|
|
52
|
+
*/
|
|
53
|
+
export interface TriggerDiffInsertRecord extends BaseTriggerDiffRecord {
|
|
54
|
+
operation: DiffTriggerOperation.INSERT;
|
|
55
|
+
/**
|
|
56
|
+
* The value of the row, at the time of INSERT, in JSON string format.
|
|
57
|
+
*/
|
|
58
|
+
value: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @experimental
|
|
62
|
+
* Represents a diff record for a SQLite DELETE operation.
|
|
63
|
+
* This record contains the new value represented as a JSON string.
|
|
64
|
+
*/
|
|
65
|
+
export interface TriggerDiffDeleteRecord extends BaseTriggerDiffRecord {
|
|
66
|
+
operation: DiffTriggerOperation.DELETE;
|
|
67
|
+
/**
|
|
68
|
+
* The value of the row, before the DELETE operation, in JSON string format.
|
|
69
|
+
*/
|
|
70
|
+
value: string;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @experimental
|
|
74
|
+
* Diffs created by {@link TriggerManager#createDiffTrigger} are stored in a temporary table.
|
|
75
|
+
* This is the record structure for all diff records.
|
|
76
|
+
*
|
|
77
|
+
* Querying the DIFF table directly with {@link TriggerDiffHandlerContext#withDiff} will return records
|
|
78
|
+
* with the structure of this type.
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const diffs = await context.withDiff<TriggerDiffRecord>('SELECT * FROM DIFF');
|
|
82
|
+
* diff.forEach(diff => console.log(diff.operation, diff.timestamp, JSON.parse(diff.value)))
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export type TriggerDiffRecord = TriggerDiffUpdateRecord | TriggerDiffInsertRecord | TriggerDiffDeleteRecord;
|
|
86
|
+
/**
|
|
87
|
+
* @experimental
|
|
88
|
+
* Querying the DIFF table directly with {@link TriggerDiffHandlerContext#withExtractedDiff} will return records
|
|
89
|
+
* with the tracked columns extracted from the JSON value.
|
|
90
|
+
* This type represents the structure of such records.
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const diffs = await context.withExtractedDiff<ExtractedTriggerDiffRecord<{id: string, name: string}>>('SELECT * FROM DIFF');
|
|
94
|
+
* diff.forEach(diff => console.log(diff.__operation, diff.__timestamp, diff.columnName))
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export type ExtractedTriggerDiffRecord<T> = T & {
|
|
98
|
+
[K in keyof Omit<BaseTriggerDiffRecord, 'id'> as `__${string & K}`]: TriggerDiffRecord[K];
|
|
99
|
+
} & {
|
|
100
|
+
__previous_value?: string;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* @experimental
|
|
104
|
+
* Hooks used in the creation of a table diff trigger.
|
|
105
|
+
*/
|
|
106
|
+
export interface TriggerCreationHooks {
|
|
107
|
+
/**
|
|
108
|
+
* Executed inside a write lock before the trigger is created.
|
|
109
|
+
*/
|
|
110
|
+
beforeCreate?: (context: LockContext) => Promise<void>;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Common interface for options used in creating a diff trigger.
|
|
114
|
+
*/
|
|
115
|
+
interface BaseCreateDiffTriggerOptions {
|
|
116
|
+
/**
|
|
117
|
+
* PowerSync source table/view to trigger and track changes from.
|
|
118
|
+
* This should be present in the PowerSync database's schema.
|
|
119
|
+
*/
|
|
120
|
+
source: string;
|
|
121
|
+
/**
|
|
122
|
+
* Columns to track and report changes for.
|
|
123
|
+
* Defaults to all columns in the source table.
|
|
124
|
+
* Use an empty array to track only the ID and operation.
|
|
125
|
+
*/
|
|
126
|
+
columns?: string[];
|
|
127
|
+
/**
|
|
128
|
+
* Condition to filter when the triggers should fire.
|
|
129
|
+
* This corresponds to a SQLite [WHEN](https://sqlite.org/lang_createtrigger.html) clause in the trigger body.
|
|
130
|
+
* This is useful for only triggering on specific conditions.
|
|
131
|
+
* For example, you can use it to only trigger on certain values in the NEW row.
|
|
132
|
+
* Note that for PowerSync the row data is stored in a JSON column named `data`.
|
|
133
|
+
* The row id is available in the `id` column.
|
|
134
|
+
*
|
|
135
|
+
* NB! The WHEN clauses here are added directly to the SQLite trigger creation SQL.
|
|
136
|
+
* Any user input strings here should be sanitized externally. The {@link when} string template function performs
|
|
137
|
+
* some basic sanitization, extra external sanitization is recommended.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* {
|
|
141
|
+
* 'INSERT': sanitizeSQL`json_extract(NEW.data, '$.list_id') = ${sanitizeUUID(list.id)}`,
|
|
142
|
+
* 'INSERT': `TRUE`,
|
|
143
|
+
* 'UPDATE': sanitizeSQL`NEW.id = 'abcd' AND json_extract(NEW.data, '$.status') = 'active'`,
|
|
144
|
+
* 'DELETE': sanitizeSQL`json_extract(OLD.data, '$.list_id') = 'abcd'`
|
|
145
|
+
* }
|
|
146
|
+
*/
|
|
147
|
+
when: Partial<Record<DiffTriggerOperation, string>>;
|
|
148
|
+
/**
|
|
149
|
+
* Hooks which allow execution during the trigger creation process.
|
|
150
|
+
*/
|
|
151
|
+
hooks?: TriggerCreationHooks;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* @experimental
|
|
155
|
+
* Options for {@link TriggerManager#createDiffTrigger}.
|
|
156
|
+
*/
|
|
157
|
+
export interface CreateDiffTriggerOptions extends BaseCreateDiffTriggerOptions {
|
|
158
|
+
/**
|
|
159
|
+
* Destination table to send changes to.
|
|
160
|
+
* This table is created internally as a SQLite temporary table.
|
|
161
|
+
* This table will be dropped once the trigger is removed.
|
|
162
|
+
*/
|
|
163
|
+
destination: string;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* @experimental
|
|
167
|
+
* Callback to drop a trigger after it has been created.
|
|
168
|
+
*/
|
|
169
|
+
export type TriggerRemoveCallback = () => Promise<void>;
|
|
170
|
+
/**
|
|
171
|
+
* @experimental
|
|
172
|
+
* Context for the `onChange` handler provided to {@link TriggerManager#trackTableDiff}.
|
|
173
|
+
*/
|
|
174
|
+
export interface TriggerDiffHandlerContext extends LockContext {
|
|
175
|
+
/**
|
|
176
|
+
* The name of the temporary destination table created by the trigger.
|
|
177
|
+
*/
|
|
178
|
+
destinationTable: string;
|
|
179
|
+
/**
|
|
180
|
+
* Allows querying the database with access to the table containing DIFF records.
|
|
181
|
+
* The diff table is accessible via the `DIFF` accessor.
|
|
182
|
+
*
|
|
183
|
+
* The `DIFF` table is of the form described in {@link TriggerManager#createDiffTrigger}
|
|
184
|
+
* ```sql
|
|
185
|
+
* CREATE TEMP DIFF (
|
|
186
|
+
* id TEXT,
|
|
187
|
+
* operation TEXT,
|
|
188
|
+
* timestamp TEXT
|
|
189
|
+
* value TEXT,
|
|
190
|
+
* previous_value TEXT
|
|
191
|
+
* );
|
|
192
|
+
* ```
|
|
193
|
+
*
|
|
194
|
+
* Note that the `value` and `previous_value` columns store the row state in JSON string format.
|
|
195
|
+
* To access the row state in an extracted form see {@link TriggerDiffHandlerContext#withExtractedDiff}.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```sql
|
|
199
|
+
* --- This fetches the current state of `todo` rows which have a diff operation present.
|
|
200
|
+
* --- The state of the row at the time of the operation is accessible in the DIFF records.
|
|
201
|
+
* SELECT
|
|
202
|
+
* todos.*
|
|
203
|
+
* FROM
|
|
204
|
+
* DIFF
|
|
205
|
+
* JOIN todos ON DIFF.id = todos.id
|
|
206
|
+
* WHERE json_extract(DIFF.value, '$.status') = 'active'
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
withDiff: <T = any>(query: string, params?: ReadonlyArray<Readonly<any>>) => Promise<T[]>;
|
|
210
|
+
/**
|
|
211
|
+
* Allows querying the database with access to the table containing diff records.
|
|
212
|
+
* The diff table is accessible via the `DIFF` accessor.
|
|
213
|
+
*
|
|
214
|
+
* This is similar to {@link withDiff} but extracts the row columns from the tracked JSON value. The diff operation
|
|
215
|
+
* data is aliased as `__` columns to avoid column conflicts.
|
|
216
|
+
*
|
|
217
|
+
* For {@link DiffTriggerOperation#DELETE} operations the previous_value columns are extracted for convenience.
|
|
218
|
+
*
|
|
219
|
+
*
|
|
220
|
+
* ```sql
|
|
221
|
+
* CREATE TEMP TABLE DIFF (
|
|
222
|
+
* id TEXT,
|
|
223
|
+
* replicated_column_1 COLUMN_TYPE,
|
|
224
|
+
* replicated_column_2 COLUMN_TYPE,
|
|
225
|
+
* __operation TEXT,
|
|
226
|
+
* __timestamp TEXT,
|
|
227
|
+
* __previous_value TEXT
|
|
228
|
+
* );
|
|
229
|
+
* ```
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```sql
|
|
233
|
+
* SELECT
|
|
234
|
+
* todos.*
|
|
235
|
+
* FROM
|
|
236
|
+
* DIFF
|
|
237
|
+
* JOIN todos ON DIFF.id = todos.id
|
|
238
|
+
* --- The todo column names are extracted from json and are available as DIFF.name
|
|
239
|
+
* WHERE DIFF.name = 'example'
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
withExtractedDiff: <T = any>(query: string, params?: ReadonlyArray<Readonly<any>>) => Promise<T[]>;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* @experimental
|
|
246
|
+
* Options for tracking changes to a table with {@link TriggerManager#trackTableDiff}.
|
|
247
|
+
*/
|
|
248
|
+
export interface TrackDiffOptions extends BaseCreateDiffTriggerOptions {
|
|
249
|
+
/**
|
|
250
|
+
* Handler for processing diff operations.
|
|
251
|
+
* Automatically invoked once diff items are present.
|
|
252
|
+
* Diff items are automatically cleared after the handler is invoked.
|
|
253
|
+
*/
|
|
254
|
+
onChange: (context: TriggerDiffHandlerContext) => Promise<void>;
|
|
255
|
+
/**
|
|
256
|
+
* The minimum interval, in milliseconds, between {@link onChange} invocations.
|
|
257
|
+
* @default {@link DEFAULT_WATCH_THROTTLE_MS}
|
|
258
|
+
*/
|
|
259
|
+
throttleMs?: number;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* @experimental
|
|
263
|
+
*/
|
|
264
|
+
export interface TriggerManager {
|
|
265
|
+
/**
|
|
266
|
+
* @experimental
|
|
267
|
+
* Creates a temporary trigger which tracks changes to a source table
|
|
268
|
+
* and writes changes to a destination table.
|
|
269
|
+
* The temporary destination table is created internally and will be dropped when the trigger is removed.
|
|
270
|
+
* The temporary destination table is created with the structure:
|
|
271
|
+
*
|
|
272
|
+
* ```sql
|
|
273
|
+
* CREATE TEMP TABLE ${destination} (
|
|
274
|
+
* id TEXT,
|
|
275
|
+
* operation TEXT,
|
|
276
|
+
* timestamp TEXT
|
|
277
|
+
* value TEXT,
|
|
278
|
+
* previous_value TEXT
|
|
279
|
+
* );
|
|
280
|
+
* ```
|
|
281
|
+
* The `value` column contains the JSON representation of the row's value at the change.
|
|
282
|
+
*
|
|
283
|
+
* For {@link DiffTriggerOperation#UPDATE} operations the `previous_value` column contains the previous value of the changed row
|
|
284
|
+
* in a JSON format.
|
|
285
|
+
*
|
|
286
|
+
* NB: The triggers created by this method might be invalidated by {@link AbstractPowerSyncDatabase#updateSchema} calls.
|
|
287
|
+
* These triggers should manually be dropped and recreated when updating the schema.
|
|
288
|
+
*
|
|
289
|
+
* @returns A callback to remove the trigger and drop the destination table.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```javascript
|
|
293
|
+
* const dispose = await database.triggers.createDiffTrigger({
|
|
294
|
+
* source: 'lists',
|
|
295
|
+
* destination: 'ps_temp_lists_diff',
|
|
296
|
+
* columns: ['name'],
|
|
297
|
+
* when: {
|
|
298
|
+
* [DiffTriggerOperation.INSERT]: 'TRUE',
|
|
299
|
+
* [DiffTriggerOperation.UPDATE]: 'TRUE',
|
|
300
|
+
* [DiffTriggerOperation.DELETE]: 'TRUE'
|
|
301
|
+
* }
|
|
302
|
+
* });
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
createDiffTrigger(options: CreateDiffTriggerOptions): Promise<TriggerRemoveCallback>;
|
|
306
|
+
/**
|
|
307
|
+
* @experimental
|
|
308
|
+
* Tracks changes for a table. Triggering a provided handler on changes.
|
|
309
|
+
* Uses {@link createDiffTrigger} internally to create a temporary destination table.
|
|
310
|
+
*
|
|
311
|
+
* @returns A callback to cleanup the trigger and stop tracking changes.
|
|
312
|
+
*
|
|
313
|
+
* NB: The triggers created by this method might be invalidated by {@link AbstractPowerSyncDatabase#updateSchema} calls.
|
|
314
|
+
* These triggers should manually be dropped and recreated when updating the schema.
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```javascript
|
|
318
|
+
* const dispose = database.triggers.trackTableDiff({
|
|
319
|
+
* source: 'todos',
|
|
320
|
+
* columns: ['list_id'],
|
|
321
|
+
* when: {
|
|
322
|
+
* [DiffTriggerOperation.INSERT]: sanitizeSQL`json_extract(NEW.data, '$.list_id') = ${sanitizeUUID(someIdVariable)}`
|
|
323
|
+
* },
|
|
324
|
+
* onChange: async (context) => {
|
|
325
|
+
* // Fetches the todo records that were inserted during this diff
|
|
326
|
+
* const newTodos = await context.getAll<Database['todos']>(`
|
|
327
|
+
* SELECT
|
|
328
|
+
* todos.*
|
|
329
|
+
* FROM
|
|
330
|
+
* DIFF
|
|
331
|
+
* JOIN todos ON DIFF.id = todos.id
|
|
332
|
+
* `);
|
|
333
|
+
*
|
|
334
|
+
* // Process newly created todos
|
|
335
|
+
* },
|
|
336
|
+
* hooks: {
|
|
337
|
+
* beforeCreate: async (lockContext) => {
|
|
338
|
+
* // This hook is executed inside the write lock before the trigger is created.
|
|
339
|
+
* // It can be used to synchronize the current state of the table with processor logic.
|
|
340
|
+
* // Any changes after this callback are guaranteed to trigger the `onChange` handler.
|
|
341
|
+
*
|
|
342
|
+
* // Read the current state of the todos table
|
|
343
|
+
* const currentTodos = await lockContext.getAll<Database['todos']>(
|
|
344
|
+
* `
|
|
345
|
+
* SELECT
|
|
346
|
+
* *
|
|
347
|
+
* FROM
|
|
348
|
+
* todos
|
|
349
|
+
* WHERE
|
|
350
|
+
* list_id = ?
|
|
351
|
+
* `,
|
|
352
|
+
* ['123']
|
|
353
|
+
* );
|
|
354
|
+
*
|
|
355
|
+
* // Process existing todos
|
|
356
|
+
* }
|
|
357
|
+
* }
|
|
358
|
+
* });
|
|
359
|
+
* ```
|
|
360
|
+
*/
|
|
361
|
+
trackTableDiff(options: TrackDiffOptions): Promise<TriggerRemoveCallback>;
|
|
362
|
+
}
|
|
363
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLite operations to track changes for with {@link TriggerManager}
|
|
3
|
+
* @experimental
|
|
4
|
+
*/
|
|
5
|
+
export var DiffTriggerOperation;
|
|
6
|
+
(function (DiffTriggerOperation) {
|
|
7
|
+
DiffTriggerOperation["INSERT"] = "INSERT";
|
|
8
|
+
DiffTriggerOperation["UPDATE"] = "UPDATE";
|
|
9
|
+
DiffTriggerOperation["DELETE"] = "DELETE";
|
|
10
|
+
})(DiffTriggerOperation || (DiffTriggerOperation = {}));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { LockContext } from '../../db/DBAdapter.js';
|
|
2
|
+
import { Schema } from '../../db/schema/Schema.js';
|
|
3
|
+
import { type AbstractPowerSyncDatabase } from '../AbstractPowerSyncDatabase.js';
|
|
4
|
+
import { CreateDiffTriggerOptions, TrackDiffOptions, TriggerManager, TriggerRemoveCallback } from './TriggerManager.js';
|
|
5
|
+
export type TriggerManagerImplOptions = {
|
|
6
|
+
db: AbstractPowerSyncDatabase;
|
|
7
|
+
schema: Schema;
|
|
8
|
+
};
|
|
9
|
+
export declare class TriggerManagerImpl implements TriggerManager {
|
|
10
|
+
protected options: TriggerManagerImplOptions;
|
|
11
|
+
protected schema: Schema;
|
|
12
|
+
constructor(options: TriggerManagerImplOptions);
|
|
13
|
+
protected get db(): AbstractPowerSyncDatabase;
|
|
14
|
+
protected getUUID(): Promise<string>;
|
|
15
|
+
protected removeTriggers(tx: LockContext, triggerIds: string[]): Promise<void>;
|
|
16
|
+
createDiffTrigger(options: CreateDiffTriggerOptions): Promise<() => Promise<void>>;
|
|
17
|
+
trackTableDiff(options: TrackDiffOptions): Promise<TriggerRemoveCallback>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { DEFAULT_WATCH_THROTTLE_MS } from '../watched/WatchedQuery.js';
|
|
2
|
+
import { DiffTriggerOperation } from './TriggerManager.js';
|
|
3
|
+
export class TriggerManagerImpl {
|
|
4
|
+
options;
|
|
5
|
+
schema;
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.options = options;
|
|
8
|
+
this.schema = options.schema;
|
|
9
|
+
options.db.registerListener({
|
|
10
|
+
schemaChanged: (schema) => {
|
|
11
|
+
this.schema = schema;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
get db() {
|
|
16
|
+
return this.options.db;
|
|
17
|
+
}
|
|
18
|
+
async getUUID() {
|
|
19
|
+
const { id: uuid } = await this.db.get(/* sql */ `
|
|
20
|
+
SELECT
|
|
21
|
+
uuid () as id
|
|
22
|
+
`);
|
|
23
|
+
// Replace dashes with underscores for SQLite table/trigger name compatibility
|
|
24
|
+
return uuid.replace(/-/g, '_');
|
|
25
|
+
}
|
|
26
|
+
async removeTriggers(tx, triggerIds) {
|
|
27
|
+
for (const triggerId of triggerIds) {
|
|
28
|
+
await tx.execute(/* sql */ `DROP TRIGGER IF EXISTS ${triggerId}; `);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async createDiffTrigger(options) {
|
|
32
|
+
await this.db.waitForReady();
|
|
33
|
+
const { source, destination, columns, when, hooks } = options;
|
|
34
|
+
const operations = Object.keys(when);
|
|
35
|
+
if (operations.length == 0) {
|
|
36
|
+
throw new Error('At least one WHEN operation must be specified for the trigger.');
|
|
37
|
+
}
|
|
38
|
+
const whenClauses = Object.fromEntries(Object.entries(when).map(([operation, filter]) => [operation, `WHEN ${filter}`]));
|
|
39
|
+
/**
|
|
40
|
+
* Allow specifying the View name as the source.
|
|
41
|
+
* We can lookup the internal table name from the schema.
|
|
42
|
+
*/
|
|
43
|
+
const sourceDefinition = this.schema.tables.find((table) => table.viewName == source);
|
|
44
|
+
if (!sourceDefinition) {
|
|
45
|
+
throw new Error(`Source table or view "${source}" not found in the schema.`);
|
|
46
|
+
}
|
|
47
|
+
const replicatedColumns = columns ?? sourceDefinition.columns.map((col) => col.name);
|
|
48
|
+
const internalSource = sourceDefinition.internalName;
|
|
49
|
+
const triggerIds = [];
|
|
50
|
+
const id = await this.getUUID();
|
|
51
|
+
/**
|
|
52
|
+
* We default to replicating all columns if no columns array is provided.
|
|
53
|
+
*/
|
|
54
|
+
const jsonFragment = (source = 'NEW') => {
|
|
55
|
+
if (columns == null) {
|
|
56
|
+
// Track all columns
|
|
57
|
+
return `${source}.data`;
|
|
58
|
+
}
|
|
59
|
+
else if (columns.length == 0) {
|
|
60
|
+
// Don't track any columns except for the id
|
|
61
|
+
return `'{}'`;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// Filter the data by the replicated columns
|
|
65
|
+
return `json_object(${replicatedColumns.map((col) => `'${col}', json_extract(${source}.data, '$.${col}')`).join(', ')})`;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const disposeWarningListener = this.db.registerListener({
|
|
69
|
+
schemaChanged: () => {
|
|
70
|
+
this.db.logger.warn(`The PowerSync schema has changed while previously configured triggers are still operational. This might cause unexpected results.`);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
/**
|
|
74
|
+
* Declare the cleanup function early since if any of the init steps fail,
|
|
75
|
+
* we need to ensure we can cleanup the created resources.
|
|
76
|
+
* We unfortunately cannot rely on transaction rollback.
|
|
77
|
+
*/
|
|
78
|
+
const cleanup = async () => {
|
|
79
|
+
disposeWarningListener();
|
|
80
|
+
return this.db.writeLock(async (tx) => {
|
|
81
|
+
await this.removeTriggers(tx, triggerIds);
|
|
82
|
+
await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
const setup = async (tx) => {
|
|
86
|
+
// Allow user code to execute in this lock context before the trigger is created.
|
|
87
|
+
await hooks?.beforeCreate?.(tx);
|
|
88
|
+
await tx.execute(/* sql */ `
|
|
89
|
+
CREATE TEMP TABLE ${destination} (
|
|
90
|
+
id TEXT,
|
|
91
|
+
operation TEXT,
|
|
92
|
+
timestamp TEXT,
|
|
93
|
+
value TEXT,
|
|
94
|
+
previous_value TEXT
|
|
95
|
+
);
|
|
96
|
+
`);
|
|
97
|
+
if (operations.includes(DiffTriggerOperation.INSERT)) {
|
|
98
|
+
const insertTriggerId = `ps_temp_trigger_insert_${id}`;
|
|
99
|
+
triggerIds.push(insertTriggerId);
|
|
100
|
+
await tx.execute(/* sql */ `
|
|
101
|
+
CREATE TEMP TRIGGER ${insertTriggerId} AFTER INSERT ON ${internalSource} ${whenClauses[DiffTriggerOperation.INSERT]} BEGIN
|
|
102
|
+
INSERT INTO
|
|
103
|
+
${destination} (id, operation, timestamp, value)
|
|
104
|
+
VALUES
|
|
105
|
+
(
|
|
106
|
+
NEW.id,
|
|
107
|
+
'INSERT',
|
|
108
|
+
strftime ('%Y-%m-%dT%H:%M:%fZ', 'now'),
|
|
109
|
+
${jsonFragment('NEW')}
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
END;
|
|
113
|
+
`);
|
|
114
|
+
}
|
|
115
|
+
if (operations.includes(DiffTriggerOperation.UPDATE)) {
|
|
116
|
+
const updateTriggerId = `ps_temp_trigger_update_${id}`;
|
|
117
|
+
triggerIds.push(updateTriggerId);
|
|
118
|
+
await tx.execute(/* sql */ `
|
|
119
|
+
CREATE TEMP TRIGGER ${updateTriggerId} AFTER
|
|
120
|
+
UPDATE ON ${internalSource} ${whenClauses[DiffTriggerOperation.UPDATE]} BEGIN
|
|
121
|
+
INSERT INTO
|
|
122
|
+
${destination} (id, operation, timestamp, value, previous_value)
|
|
123
|
+
VALUES
|
|
124
|
+
(
|
|
125
|
+
NEW.id,
|
|
126
|
+
'UPDATE',
|
|
127
|
+
strftime ('%Y-%m-%dT%H:%M:%fZ', 'now'),
|
|
128
|
+
${jsonFragment('NEW')},
|
|
129
|
+
${jsonFragment('OLD')}
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
END;
|
|
133
|
+
`);
|
|
134
|
+
}
|
|
135
|
+
if (operations.includes(DiffTriggerOperation.DELETE)) {
|
|
136
|
+
const deleteTriggerId = `ps_temp_trigger_delete_${id}`;
|
|
137
|
+
triggerIds.push(deleteTriggerId);
|
|
138
|
+
// Create delete trigger for basic JSON
|
|
139
|
+
await tx.execute(/* sql */ `
|
|
140
|
+
CREATE TEMP TRIGGER ${deleteTriggerId} AFTER DELETE ON ${internalSource} ${whenClauses[DiffTriggerOperation.DELETE]} BEGIN
|
|
141
|
+
INSERT INTO
|
|
142
|
+
${destination} (id, operation, timestamp, value)
|
|
143
|
+
VALUES
|
|
144
|
+
(
|
|
145
|
+
OLD.id,
|
|
146
|
+
'DELETE',
|
|
147
|
+
strftime ('%Y-%m-%dT%H:%M:%fZ', 'now'),
|
|
148
|
+
${jsonFragment('OLD')}
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
END;
|
|
152
|
+
`);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
try {
|
|
156
|
+
await this.db.writeLock(setup);
|
|
157
|
+
return cleanup;
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
try {
|
|
161
|
+
await cleanup();
|
|
162
|
+
}
|
|
163
|
+
catch (cleanupError) {
|
|
164
|
+
throw new AggregateError([error, cleanupError], 'Error during operation and cleanup');
|
|
165
|
+
}
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async trackTableDiff(options) {
|
|
170
|
+
const { source, when, columns, hooks, throttleMs = DEFAULT_WATCH_THROTTLE_MS } = options;
|
|
171
|
+
await this.db.waitForReady();
|
|
172
|
+
/**
|
|
173
|
+
* Allow specifying the View name as the source.
|
|
174
|
+
* We can lookup the internal table name from the schema.
|
|
175
|
+
*/
|
|
176
|
+
const sourceDefinition = this.schema.tables.find((table) => table.viewName == source);
|
|
177
|
+
if (!sourceDefinition) {
|
|
178
|
+
throw new Error(`Source table or view "${source}" not found in the schema.`);
|
|
179
|
+
}
|
|
180
|
+
// The columns to present in the onChange context methods.
|
|
181
|
+
// If no array is provided, we use all columns from the source table.
|
|
182
|
+
const contextColumns = columns ?? sourceDefinition.columns.map((col) => col.name);
|
|
183
|
+
const id = await this.getUUID();
|
|
184
|
+
const destination = `ps_temp_track_${source}_${id}`;
|
|
185
|
+
// register an onChange before the trigger is created
|
|
186
|
+
const abortController = new AbortController();
|
|
187
|
+
const abortOnChange = () => abortController.abort();
|
|
188
|
+
this.db.onChange({
|
|
189
|
+
// Note that the onChange events here have their execution scheduled.
|
|
190
|
+
// Callbacks are throttled and are sequential.
|
|
191
|
+
onChange: async () => {
|
|
192
|
+
if (abortController.signal.aborted)
|
|
193
|
+
return;
|
|
194
|
+
// Run the handler in a write lock to keep the state of the
|
|
195
|
+
// destination table consistent.
|
|
196
|
+
await this.db.writeTransaction(async (tx) => {
|
|
197
|
+
const callbackResult = await options.onChange({
|
|
198
|
+
...tx,
|
|
199
|
+
destinationTable: destination,
|
|
200
|
+
withDiff: async (query, params) => {
|
|
201
|
+
// Wrap the query to expose the destination table
|
|
202
|
+
const wrappedQuery = /* sql */ `
|
|
203
|
+
WITH
|
|
204
|
+
DIFF AS (
|
|
205
|
+
SELECT
|
|
206
|
+
*
|
|
207
|
+
FROM
|
|
208
|
+
${destination}
|
|
209
|
+
ORDER BY
|
|
210
|
+
timestamp ASC
|
|
211
|
+
) ${query}
|
|
212
|
+
`;
|
|
213
|
+
return tx.getAll(wrappedQuery, params);
|
|
214
|
+
},
|
|
215
|
+
withExtractedDiff: async (query, params) => {
|
|
216
|
+
// Wrap the query to expose the destination table
|
|
217
|
+
const wrappedQuery = /* sql */ `
|
|
218
|
+
WITH
|
|
219
|
+
DIFF AS (
|
|
220
|
+
SELECT
|
|
221
|
+
id,
|
|
222
|
+
${contextColumns.length > 0
|
|
223
|
+
? `${contextColumns.map((col) => `json_extract(value, '$.${col}') as ${col}`).join(', ')},`
|
|
224
|
+
: ''} operation as __operation,
|
|
225
|
+
timestamp as __timestamp,
|
|
226
|
+
previous_value as __previous_value
|
|
227
|
+
FROM
|
|
228
|
+
${destination}
|
|
229
|
+
ORDER BY
|
|
230
|
+
__timestamp ASC
|
|
231
|
+
) ${query}
|
|
232
|
+
`;
|
|
233
|
+
return tx.getAll(wrappedQuery, params);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
// Clear the destination table after processing
|
|
237
|
+
await tx.execute(/* sql */ `DELETE FROM ${destination};`);
|
|
238
|
+
return callbackResult;
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}, { tables: [destination], signal: abortController.signal, throttleMs });
|
|
242
|
+
try {
|
|
243
|
+
const removeTrigger = await this.createDiffTrigger({
|
|
244
|
+
source,
|
|
245
|
+
destination,
|
|
246
|
+
columns: contextColumns,
|
|
247
|
+
when,
|
|
248
|
+
hooks
|
|
249
|
+
});
|
|
250
|
+
return async () => {
|
|
251
|
+
abortOnChange();
|
|
252
|
+
await removeTrigger();
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
try {
|
|
257
|
+
abortOnChange();
|
|
258
|
+
}
|
|
259
|
+
catch (cleanupError) {
|
|
260
|
+
throw new AggregateError([error, cleanupError], 'Error during operation and cleanup');
|
|
261
|
+
}
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|