@powersync/service-core-tests 0.0.0-dev-20260225160713 → 0.0.0-dev-20260511080634

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.
Files changed (55) hide show
  1. package/CHANGELOG.md +95 -3
  2. package/dist/test-utils/MetricsHelper.js +1 -1
  3. package/dist/test-utils/MetricsHelper.js.map +1 -1
  4. package/dist/test-utils/StorageDataHelpers.d.ts +8 -0
  5. package/dist/test-utils/StorageDataHelpers.js +33 -0
  6. package/dist/test-utils/StorageDataHelpers.js.map +1 -0
  7. package/dist/test-utils/general-utils.d.ts +22 -3
  8. package/dist/test-utils/general-utils.js +56 -3
  9. package/dist/test-utils/general-utils.js.map +1 -1
  10. package/dist/test-utils/stream_utils.js +2 -2
  11. package/dist/test-utils/stream_utils.js.map +1 -1
  12. package/dist/test-utils/test-utils-index.d.ts +1 -0
  13. package/dist/test-utils/test-utils-index.js +1 -0
  14. package/dist/test-utils/test-utils-index.js.map +1 -1
  15. package/dist/tests/register-compacting-tests.d.ts +1 -1
  16. package/dist/tests/register-compacting-tests.js +514 -587
  17. package/dist/tests/register-compacting-tests.js.map +1 -1
  18. package/dist/tests/register-data-storage-checkpoint-tests.d.ts +1 -1
  19. package/dist/tests/register-data-storage-checkpoint-tests.js +200 -301
  20. package/dist/tests/register-data-storage-checkpoint-tests.js.map +1 -1
  21. package/dist/tests/register-data-storage-data-tests.d.ts +2 -2
  22. package/dist/tests/register-data-storage-data-tests.js +1220 -1041
  23. package/dist/tests/register-data-storage-data-tests.js.map +1 -1
  24. package/dist/tests/register-data-storage-parameter-tests.d.ts +1 -1
  25. package/dist/tests/register-data-storage-parameter-tests.js +681 -632
  26. package/dist/tests/register-data-storage-parameter-tests.js.map +1 -1
  27. package/dist/tests/register-migration-tests.js +63 -139
  28. package/dist/tests/register-migration-tests.js.map +1 -1
  29. package/dist/tests/register-parameter-compacting-tests.d.ts +1 -1
  30. package/dist/tests/register-parameter-compacting-tests.js +121 -201
  31. package/dist/tests/register-parameter-compacting-tests.js.map +1 -1
  32. package/dist/tests/register-sync-tests.d.ts +2 -1
  33. package/dist/tests/register-sync-tests.js +974 -1136
  34. package/dist/tests/register-sync-tests.js.map +1 -1
  35. package/dist/tests/tests-index.d.ts +4 -4
  36. package/dist/tests/tests-index.js +4 -4
  37. package/dist/tests/tests-index.js.map +1 -1
  38. package/dist/tests/util.d.ts +5 -4
  39. package/dist/tests/util.js +27 -12
  40. package/dist/tests/util.js.map +1 -1
  41. package/package.json +6 -6
  42. package/src/test-utils/MetricsHelper.ts +1 -1
  43. package/src/test-utils/StorageDataHelpers.ts +44 -0
  44. package/src/test-utils/general-utils.ts +81 -4
  45. package/src/test-utils/stream_utils.ts +2 -2
  46. package/src/test-utils/test-utils-index.ts +1 -0
  47. package/src/tests/register-compacting-tests.ts +376 -322
  48. package/src/tests/register-data-storage-checkpoint-tests.ts +85 -53
  49. package/src/tests/register-data-storage-data-tests.ts +1076 -563
  50. package/src/tests/register-data-storage-parameter-tests.ts +631 -327
  51. package/src/tests/register-parameter-compacting-tests.ts +92 -96
  52. package/src/tests/register-sync-tests.ts +463 -379
  53. package/src/tests/tests-index.ts +4 -4
  54. package/src/tests/util.ts +46 -17
  55. package/tsconfig.tsbuildinfo +1 -1
@@ -14,14 +14,11 @@ import * as timers from 'timers/promises';
14
14
  import { fileURLToPath } from 'url';
15
15
  import { expect, test } from 'vitest';
16
16
  import * as test_utils from '../test-utils/test-utils-index.js';
17
- import { METRICS_HELPER } from '../test-utils/test-utils-index.js';
18
- import { bucketRequest } from './util.js';
17
+ import { bucketRequest, METRICS_HELPER } from '../test-utils/test-utils-index.js';
19
18
 
20
19
  const __filename = fileURLToPath(import.meta.url);
21
20
  const __dirname = path.dirname(__filename);
22
21
 
23
- const TEST_TABLE = test_utils.makeTestTable('test', ['id']);
24
-
25
22
  const BASIC_SYNC_RULES = `
26
23
  bucket_definitions:
27
24
  mybucket:
@@ -39,7 +36,15 @@ export const SYNC_SNAPSHOT_PATH = path.resolve(__dirname, '../__snapshots/sync.t
39
36
  * });
40
37
  * ```
41
38
  */
42
- export function registerSyncTests(factory: storage.TestStorageFactory, options: { storageVersion?: number } = {}) {
39
+ export function registerSyncTests(
40
+ configOrFactory: storage.TestStorageConfig | storage.TestStorageFactory,
41
+ options: { storageVersion?: number; tableIdStrings?: boolean } = {}
42
+ ) {
43
+ const config: storage.TestStorageConfig =
44
+ typeof configOrFactory == 'function'
45
+ ? { factory: configOrFactory, tableIdStrings: options.tableIdStrings ?? true }
46
+ : configOrFactory;
47
+ const factory = config.factory;
43
48
  createCoreAPIMetrics(METRICS_HELPER.metricsEngine);
44
49
  const tracker = new sync.RequestTracker(METRICS_HELPER.metricsEngine);
45
50
  const syncContext = new sync.SyncContext({
@@ -65,31 +70,33 @@ export function registerSyncTests(factory: storage.TestStorageFactory, options:
65
70
  });
66
71
 
67
72
  const bucketStorage = f.getInstance(syncRules);
73
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
74
+ const sourceTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config);
68
75
 
69
- const result = await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
70
- await batch.save({
71
- sourceTable: TEST_TABLE,
72
- tag: storage.SaveOperationTag.INSERT,
73
- after: {
74
- id: 't1',
75
- description: 'Test 1'
76
- },
77
- afterReplicaId: 't1'
78
- });
76
+ await writer.markAllSnapshotDone('0/1');
79
77
 
80
- await batch.save({
81
- sourceTable: TEST_TABLE,
82
- tag: storage.SaveOperationTag.INSERT,
83
- after: {
84
- id: 't2',
85
- description: 'Test 2'
86
- },
87
- afterReplicaId: 't2'
88
- });
78
+ await writer.save({
79
+ sourceTable,
80
+ tag: storage.SaveOperationTag.INSERT,
81
+ after: {
82
+ id: 't1',
83
+ description: 'Test 1'
84
+ },
85
+ afterReplicaId: 't1'
86
+ });
89
87
 
90
- await batch.commit('0/1');
88
+ await writer.save({
89
+ sourceTable,
90
+ tag: storage.SaveOperationTag.INSERT,
91
+ after: {
92
+ id: 't2',
93
+ description: 'Test 2'
94
+ },
95
+ afterReplicaId: 't2'
91
96
  });
92
97
 
98
+ await writer.commit('0/1');
99
+
93
100
  const stream = sync.streamResponse({
94
101
  syncContext,
95
102
  bucketStorage: bucketStorage,
@@ -126,31 +133,91 @@ bucket_definitions:
126
133
  });
127
134
 
128
135
  const bucketStorage = f.getInstance(syncRules);
136
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
137
+ const testTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config);
138
+
139
+ await writer.markAllSnapshotDone('0/1');
140
+ await writer.save({
141
+ sourceTable: testTable,
142
+ tag: storage.SaveOperationTag.INSERT,
143
+ after: {
144
+ id: 't1',
145
+ description: 'Test 1'
146
+ },
147
+ afterReplicaId: 't1'
148
+ });
129
149
 
130
- const result = await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
131
- await batch.save({
132
- sourceTable: TEST_TABLE,
133
- tag: storage.SaveOperationTag.INSERT,
134
- after: {
135
- id: 't1',
136
- description: 'Test 1'
137
- },
138
- afterReplicaId: 't1'
139
- });
150
+ await writer.save({
151
+ sourceTable: testTable,
152
+ tag: storage.SaveOperationTag.INSERT,
153
+ after: {
154
+ id: 'earlier',
155
+ description: 'Test 2'
156
+ },
157
+ afterReplicaId: 'earlier'
158
+ });
140
159
 
141
- await batch.save({
142
- sourceTable: TEST_TABLE,
143
- tag: storage.SaveOperationTag.INSERT,
144
- after: {
145
- id: 'earlier',
146
- description: 'Test 2'
147
- },
148
- afterReplicaId: 'earlier'
149
- });
160
+ await writer.commit('0/1');
161
+
162
+ const stream = sync.streamResponse({
163
+ syncContext,
164
+ bucketStorage,
165
+ syncRules: bucketStorage.getParsedSyncRules(test_utils.PARSE_OPTIONS),
166
+ params: {
167
+ buckets: [],
168
+ include_checksum: true,
169
+ raw_data: true
170
+ },
171
+ tracker,
172
+ token: new JwtPayload({ sub: '', exp: Date.now() / 1000 + 10 }),
173
+ isEncodingAsBson: false
174
+ });
175
+
176
+ const lines = await consumeCheckpointLines(stream);
177
+ expect(lines).toMatchSnapshot();
178
+ });
179
+
180
+ test('can override priority when subscribing to stream', async () => {
181
+ await using f = await factory();
182
+
183
+ const syncRules = await updateSyncRules(f, {
184
+ content: `
185
+ config:
186
+ edition: 3
187
+
188
+ streams:
189
+ todos:
190
+ query: SELECT * FROM test WHERE id IN subscription.parameter('test_ids')
191
+ `
192
+ });
193
+
194
+ const bucketStorage = f.getInstance(syncRules);
195
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
196
+ const testTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config);
197
+
198
+ await writer.markAllSnapshotDone('0/1');
199
+ await writer.save({
200
+ sourceTable: testTable,
201
+ tag: storage.SaveOperationTag.INSERT,
202
+ after: {
203
+ id: 'a',
204
+ description: 'Test 1'
205
+ },
206
+ afterReplicaId: 't1'
207
+ });
150
208
 
151
- await batch.commit('0/1');
209
+ await writer.save({
210
+ sourceTable: testTable,
211
+ tag: storage.SaveOperationTag.INSERT,
212
+ after: {
213
+ id: 'b',
214
+ description: 'Test 2'
215
+ },
216
+ afterReplicaId: 'earlier'
152
217
  });
153
218
 
219
+ await writer.commit('0/1');
220
+
154
221
  const stream = sync.streamResponse({
155
222
  syncContext,
156
223
  bucketStorage,
@@ -158,7 +225,22 @@ bucket_definitions:
158
225
  params: {
159
226
  buckets: [],
160
227
  include_checksum: true,
161
- raw_data: true
228
+ raw_data: true,
229
+ streams: {
230
+ include_defaults: true,
231
+ subscriptions: [
232
+ {
233
+ stream: 'todos',
234
+ parameters: { test_ids: ['a'] },
235
+ override_priority: 0
236
+ },
237
+ {
238
+ stream: 'todos',
239
+ parameters: { test_ids: ['a', 'b'] },
240
+ override_priority: null
241
+ }
242
+ ]
243
+ }
162
244
  },
163
245
  tracker,
164
246
  token: new JwtPayload({ sub: '', exp: Date.now() / 1000 + 10 }),
@@ -187,32 +269,33 @@ bucket_definitions:
187
269
  });
188
270
 
189
271
  const bucketStorage = f.getInstance(syncRules);
190
-
191
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
192
- // Initial data: Add one priority row and 10k low-priority rows.
193
- await batch.save({
194
- sourceTable: TEST_TABLE,
272
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
273
+ const testTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config);
274
+
275
+ await writer.markAllSnapshotDone('0/1');
276
+ // Initial data: Add one priority row and 10k low-priority rows.
277
+ await writer.save({
278
+ sourceTable: testTable,
279
+ tag: storage.SaveOperationTag.INSERT,
280
+ after: {
281
+ id: 'highprio',
282
+ description: 'High priority row'
283
+ },
284
+ afterReplicaId: 'highprio'
285
+ });
286
+ for (let i = 0; i < 10_000; i++) {
287
+ await writer.save({
288
+ sourceTable: testTable,
195
289
  tag: storage.SaveOperationTag.INSERT,
196
290
  after: {
197
- id: 'highprio',
198
- description: 'High priority row'
291
+ id: `${i}`,
292
+ description: 'low prio'
199
293
  },
200
- afterReplicaId: 'highprio'
294
+ afterReplicaId: `${i}`
201
295
  });
202
- for (let i = 0; i < 10_000; i++) {
203
- await batch.save({
204
- sourceTable: TEST_TABLE,
205
- tag: storage.SaveOperationTag.INSERT,
206
- after: {
207
- id: `${i}`,
208
- description: 'low prio'
209
- },
210
- afterReplicaId: `${i}`
211
- });
212
- }
296
+ }
213
297
 
214
- await batch.commit('0/1');
215
- });
298
+ await writer.commit('0/1');
216
299
 
217
300
  const stream = sync.streamResponse({
218
301
  syncContext,
@@ -240,20 +323,18 @@ bucket_definitions:
240
323
  if (sentCheckpoints == 1) {
241
324
  // Save new data to interrupt the low-priority sync.
242
325
 
243
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
244
- // Add another high-priority row. This should interrupt the long-running low-priority sync.
245
- await batch.save({
246
- sourceTable: TEST_TABLE,
247
- tag: storage.SaveOperationTag.INSERT,
248
- after: {
249
- id: 'highprio2',
250
- description: 'Another high-priority row'
251
- },
252
- afterReplicaId: 'highprio2'
253
- });
254
-
255
- await batch.commit('0/2');
326
+ // Add another high-priority row. This should interrupt the long-running low-priority sync.
327
+ await writer.save({
328
+ sourceTable: testTable,
329
+ tag: storage.SaveOperationTag.INSERT,
330
+ after: {
331
+ id: 'highprio2',
332
+ description: 'Another high-priority row'
333
+ },
334
+ afterReplicaId: 'highprio2'
256
335
  });
336
+
337
+ await writer.commit('0/2');
257
338
  } else {
258
339
  // Low-priority sync from the first checkpoint was interrupted. This should not happen before
259
340
  // 1000 low-priority items were synchronized.
@@ -297,32 +378,33 @@ bucket_definitions:
297
378
  });
298
379
 
299
380
  const bucketStorage = f.getInstance(syncRules);
300
-
301
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
302
- // Initial data: Add one priority row and 10k low-priority rows.
303
- await batch.save({
304
- sourceTable: TEST_TABLE,
381
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
382
+ const testTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config);
383
+
384
+ await writer.markAllSnapshotDone('0/1');
385
+ // Initial data: Add one priority row and 10k low-priority rows.
386
+ await writer.save({
387
+ sourceTable: testTable,
388
+ tag: storage.SaveOperationTag.INSERT,
389
+ after: {
390
+ id: 'highprio',
391
+ description: 'user_one'
392
+ },
393
+ afterReplicaId: 'highprio'
394
+ });
395
+ for (let i = 0; i < 10_000; i++) {
396
+ await writer.save({
397
+ sourceTable: testTable,
305
398
  tag: storage.SaveOperationTag.INSERT,
306
399
  after: {
307
- id: 'highprio',
308
- description: 'user_one'
400
+ id: `${i}`,
401
+ description: 'low prio'
309
402
  },
310
- afterReplicaId: 'highprio'
403
+ afterReplicaId: `${i}`
311
404
  });
312
- for (let i = 0; i < 10_000; i++) {
313
- await batch.save({
314
- sourceTable: TEST_TABLE,
315
- tag: storage.SaveOperationTag.INSERT,
316
- after: {
317
- id: `${i}`,
318
- description: 'low prio'
319
- },
320
- afterReplicaId: `${i}`
321
- });
322
- }
405
+ }
323
406
 
324
- await batch.commit('0/1');
325
- });
407
+ await writer.commit('0/1');
326
408
 
327
409
  const stream = sync.streamResponse({
328
410
  syncContext,
@@ -355,20 +437,18 @@ bucket_definitions:
355
437
  if (typeof next === 'object' && next !== null) {
356
438
  if ('partial_checkpoint_complete' in next) {
357
439
  if (sentCheckpoints == 1) {
358
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
359
- // Add a high-priority row that doesn't affect this sync stream.
360
- await batch.save({
361
- sourceTable: TEST_TABLE,
362
- tag: storage.SaveOperationTag.INSERT,
363
- after: {
364
- id: 'highprio2',
365
- description: 'user_two'
366
- },
367
- afterReplicaId: 'highprio2'
368
- });
369
-
370
- await batch.commit('0/2');
440
+ // Add a high-priority row that doesn't affect this sync stream.
441
+ await writer.save({
442
+ sourceTable: testTable,
443
+ tag: storage.SaveOperationTag.INSERT,
444
+ after: {
445
+ id: 'highprio2',
446
+ description: 'user_two'
447
+ },
448
+ afterReplicaId: 'highprio2'
371
449
  });
450
+
451
+ await writer.commit('0/2');
372
452
  } else {
373
453
  expect(sentCheckpoints).toBe(2);
374
454
  expect(sentRows).toBe(10002);
@@ -389,20 +469,18 @@ bucket_definitions:
389
469
  if (completedCheckpoints == 1) {
390
470
  expect(sentRows).toBe(10001);
391
471
 
392
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
393
- // Add a high-priority row that affects this sync stream.
394
- await batch.save({
395
- sourceTable: TEST_TABLE,
396
- tag: storage.SaveOperationTag.INSERT,
397
- after: {
398
- id: 'highprio3',
399
- description: 'user_one'
400
- },
401
- afterReplicaId: 'highprio3'
402
- });
403
-
404
- await batch.commit('0/3');
472
+ // Add a high-priority row that affects this sync stream.
473
+ await writer.save({
474
+ sourceTable: testTable,
475
+ tag: storage.SaveOperationTag.INSERT,
476
+ after: {
477
+ id: 'highprio3',
478
+ description: 'user_one'
479
+ },
480
+ afterReplicaId: 'highprio3'
405
481
  });
482
+
483
+ await writer.commit('0/3');
406
484
  }
407
485
  }
408
486
  }
@@ -438,32 +516,33 @@ bucket_definitions:
438
516
  });
439
517
 
440
518
  const bucketStorage = f.getInstance(syncRules);
441
-
442
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
443
- // Initial data: Add one priority row and 10k low-priority rows.
444
- await batch.save({
445
- sourceTable: TEST_TABLE,
519
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
520
+ const testTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config);
521
+
522
+ await writer.markAllSnapshotDone('0/1');
523
+ // Initial data: Add one priority row and 10k low-priority rows.
524
+ await writer.save({
525
+ sourceTable: testTable,
526
+ tag: storage.SaveOperationTag.INSERT,
527
+ after: {
528
+ id: 'highprio',
529
+ description: 'High priority row'
530
+ },
531
+ afterReplicaId: 'highprio'
532
+ });
533
+ for (let i = 0; i < 2_000; i++) {
534
+ await writer.save({
535
+ sourceTable: testTable,
446
536
  tag: storage.SaveOperationTag.INSERT,
447
537
  after: {
448
- id: 'highprio',
449
- description: 'High priority row'
538
+ id: `${i}`,
539
+ description: 'low prio'
450
540
  },
451
- afterReplicaId: 'highprio'
541
+ afterReplicaId: `${i}`
452
542
  });
453
- for (let i = 0; i < 2_000; i++) {
454
- await batch.save({
455
- sourceTable: TEST_TABLE,
456
- tag: storage.SaveOperationTag.INSERT,
457
- after: {
458
- id: `${i}`,
459
- description: 'low prio'
460
- },
461
- afterReplicaId: `${i}`
462
- });
463
- }
543
+ }
464
544
 
465
- await batch.commit('0/1');
466
- });
545
+ await writer.commit('0/1');
467
546
 
468
547
  const stream = sync.streamResponse({
469
548
  syncContext,
@@ -500,31 +579,29 @@ bucket_definitions:
500
579
 
501
580
  if (sentRows == 1001) {
502
581
  // Save new data to interrupt the low-priority sync.
503
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
504
- // Add another high-priority row. This should interrupt the long-running low-priority sync.
505
- await batch.save({
506
- sourceTable: TEST_TABLE,
507
- tag: storage.SaveOperationTag.INSERT,
508
- after: {
509
- id: 'highprio2',
510
- description: 'Another high-priority row'
511
- },
512
- afterReplicaId: 'highprio2'
513
- });
514
-
515
- // Also add a low-priority row
516
- await batch.save({
517
- sourceTable: TEST_TABLE,
518
- tag: storage.SaveOperationTag.INSERT,
519
- after: {
520
- id: '2001',
521
- description: 'Another low-priority row'
522
- },
523
- afterReplicaId: '2001'
524
- });
525
-
526
- await batch.commit('0/2');
582
+ // Add another high-priority row. This should interrupt the long-running low-priority sync.
583
+ await writer.save({
584
+ sourceTable: testTable,
585
+ tag: storage.SaveOperationTag.INSERT,
586
+ after: {
587
+ id: 'highprio2',
588
+ description: 'Another high-priority row'
589
+ },
590
+ afterReplicaId: 'highprio2'
591
+ });
592
+
593
+ // Also add a low-priority row
594
+ await writer.save({
595
+ sourceTable: testTable,
596
+ tag: storage.SaveOperationTag.INSERT,
597
+ after: {
598
+ id: '2001',
599
+ description: 'Another low-priority row'
600
+ },
601
+ afterReplicaId: '2001'
527
602
  });
603
+
604
+ await writer.commit('0/2');
528
605
  }
529
606
 
530
607
  if (sentRows >= 1000 && sentRows <= 2001) {
@@ -567,19 +644,20 @@ bucket_definitions:
567
644
  content: BASIC_SYNC_RULES
568
645
  });
569
646
  const bucketStorage = f.getInstance(syncRules);
570
-
571
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
572
- await batch.save({
573
- sourceTable: TEST_TABLE,
574
- tag: storage.SaveOperationTag.INSERT,
575
- after: {
576
- id: 't1',
577
- description: 'sync'
578
- },
579
- afterReplicaId: 't1'
580
- });
581
- await batch.commit('0/1');
647
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
648
+ const testTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config);
649
+
650
+ await writer.markAllSnapshotDone('0/1');
651
+ await writer.save({
652
+ sourceTable: testTable,
653
+ tag: storage.SaveOperationTag.INSERT,
654
+ after: {
655
+ id: 't1',
656
+ description: 'sync'
657
+ },
658
+ afterReplicaId: 't1'
582
659
  });
660
+ await writer.commit('0/1');
583
661
 
584
662
  const stream = sync.streamResponse({
585
663
  syncContext,
@@ -610,9 +688,7 @@ bucket_definitions:
610
688
  if (receivedCompletions == 1) {
611
689
  // Trigger an empty bucket update.
612
690
  await bucketStorage.createManagedWriteCheckpoint({ user_id: '', heads: { '1': '1/0' } });
613
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
614
- await batch.commit('1/0');
615
- });
691
+ await writer.commit('1/0');
616
692
  } else {
617
693
  break;
618
694
  }
@@ -624,29 +700,30 @@ bucket_definitions:
624
700
  });
625
701
 
626
702
  test('sync legacy non-raw data', async () => {
627
- const f = await factory();
703
+ await using f = await factory();
628
704
 
629
705
  const syncRules = await updateSyncRules(f, {
630
706
  content: BASIC_SYNC_RULES
631
707
  });
632
708
 
633
709
  const bucketStorage = await f.getInstance(syncRules);
634
-
635
- const result = await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
636
- await batch.save({
637
- sourceTable: TEST_TABLE,
638
- tag: storage.SaveOperationTag.INSERT,
639
- after: {
640
- id: 't1',
641
- description: 'Test\n"string"',
642
- large_num: 12345678901234567890n
643
- },
644
- afterReplicaId: 't1'
645
- });
646
-
647
- await batch.commit('0/1');
710
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
711
+ const testTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config);
712
+
713
+ await writer.markAllSnapshotDone('0/1');
714
+ await writer.save({
715
+ sourceTable: testTable,
716
+ tag: storage.SaveOperationTag.INSERT,
717
+ after: {
718
+ id: 't1',
719
+ description: 'Test\n"string"',
720
+ large_num: 12345678901234567890n
721
+ },
722
+ afterReplicaId: 't1'
648
723
  });
649
724
 
725
+ await writer.commit('0/1');
726
+
650
727
  const stream = sync.streamResponse({
651
728
  syncContext,
652
729
  bucketStorage,
@@ -702,10 +779,11 @@ bucket_definitions:
702
779
  });
703
780
 
704
781
  const bucketStorage = await f.getInstance(syncRules);
782
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
783
+ const testTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config);
705
784
  // Activate
706
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
707
- await batch.keepalive('0/0');
708
- });
785
+ await writer.markAllSnapshotDone('0/0');
786
+ await writer.keepalive('0/0');
709
787
 
710
788
  const stream = sync.streamResponse({
711
789
  syncContext,
@@ -727,36 +805,32 @@ bucket_definitions:
727
805
 
728
806
  expect(await getCheckpointLines(iter)).toMatchSnapshot();
729
807
 
730
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
731
- await batch.save({
732
- sourceTable: TEST_TABLE,
733
- tag: storage.SaveOperationTag.INSERT,
734
- after: {
735
- id: 't1',
736
- description: 'Test 1'
737
- },
738
- afterReplicaId: 't1'
739
- });
740
-
741
- await batch.commit('0/1');
808
+ await writer.save({
809
+ sourceTable: testTable,
810
+ tag: storage.SaveOperationTag.INSERT,
811
+ after: {
812
+ id: 't1',
813
+ description: 'Test 1'
814
+ },
815
+ afterReplicaId: 't1'
742
816
  });
743
817
 
744
- expect(await getCheckpointLines(iter)).toMatchSnapshot();
818
+ await writer.commit('0/1');
745
819
 
746
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
747
- await batch.save({
748
- sourceTable: TEST_TABLE,
749
- tag: storage.SaveOperationTag.INSERT,
750
- after: {
751
- id: 't2',
752
- description: 'Test 2'
753
- },
754
- afterReplicaId: 't2'
755
- });
820
+ expect(await getCheckpointLines(iter)).toMatchSnapshot();
756
821
 
757
- await batch.commit('0/2');
822
+ await writer.save({
823
+ sourceTable: testTable,
824
+ tag: storage.SaveOperationTag.INSERT,
825
+ after: {
826
+ id: 't2',
827
+ description: 'Test 2'
828
+ },
829
+ afterReplicaId: 't2'
758
830
  });
759
831
 
832
+ await writer.commit('0/2');
833
+
760
834
  expect(await getCheckpointLines(iter)).toMatchSnapshot();
761
835
  });
762
836
 
@@ -772,14 +846,13 @@ bucket_definitions:
772
846
  `
773
847
  });
774
848
 
775
- const usersTable = test_utils.makeTestTable('users', ['id']);
776
- const listsTable = test_utils.makeTestTable('lists', ['id']);
777
-
778
849
  const bucketStorage = await f.getInstance(syncRules);
850
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
851
+ const usersTable = await test_utils.resolveTestTable(writer, 'users', ['id'], config, 1);
852
+
779
853
  // Activate
780
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
781
- await batch.keepalive('0/0');
782
- });
854
+ await writer.markAllSnapshotDone('0/0');
855
+ await writer.keepalive('0/0');
783
856
 
784
857
  const stream = sync.streamResponse({
785
858
  syncContext,
@@ -805,24 +878,24 @@ bucket_definitions:
805
878
  expect(checkpoint1).toMatchSnapshot();
806
879
 
807
880
  // Add user
808
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
809
- await batch.save({
810
- sourceTable: usersTable,
811
- tag: storage.SaveOperationTag.INSERT,
812
- after: {
813
- id: 'user1',
814
- name: 'User 1'
815
- },
816
- afterReplicaId: 'user1'
817
- });
818
-
819
- await batch.commit('0/1');
881
+ await writer.save({
882
+ sourceTable: usersTable,
883
+ tag: storage.SaveOperationTag.INSERT,
884
+ after: {
885
+ id: 'user1',
886
+ name: 'User 1'
887
+ },
888
+ afterReplicaId: 'user1'
820
889
  });
821
890
 
891
+ await writer.commit('0/1');
892
+
822
893
  const checkpoint2 = await getCheckpointLines(iter);
894
+
895
+ const { bucket } = test_utils.bucketRequest(syncRules, 'by_user["user1"]');
823
896
  expect(
824
897
  (checkpoint2[0] as StreamingSyncCheckpointDiff).checkpoint_diff?.updated_buckets?.map((b) => b.bucket)
825
- ).toEqual([bucketRequest(syncRules, 'by_user["user1"]')]);
898
+ ).toEqual([bucket]);
826
899
  expect(checkpoint2).toMatchSnapshot();
827
900
  });
828
901
 
@@ -838,25 +911,24 @@ bucket_definitions:
838
911
  `
839
912
  });
840
913
 
841
- const usersTable = test_utils.makeTestTable('users', ['id']);
842
- const listsTable = test_utils.makeTestTable('lists', ['id']);
843
-
844
914
  const bucketStorage = await f.getInstance(syncRules);
845
-
846
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
847
- await batch.save({
848
- sourceTable: usersTable,
849
- tag: storage.SaveOperationTag.INSERT,
850
- after: {
851
- id: 'user1',
852
- name: 'User 1'
853
- },
854
- afterReplicaId: 'user1'
855
- });
856
-
857
- await batch.commit('0/1');
915
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
916
+ const usersTable = await test_utils.resolveTestTable(writer, 'users', ['id'], config, 1);
917
+ const listsTable = await test_utils.resolveTestTable(writer, 'lists', ['id'], config, 2);
918
+
919
+ await writer.markAllSnapshotDone('0/1');
920
+ await writer.save({
921
+ sourceTable: usersTable,
922
+ tag: storage.SaveOperationTag.INSERT,
923
+ after: {
924
+ id: 'user1',
925
+ name: 'User 1'
926
+ },
927
+ afterReplicaId: 'user1'
858
928
  });
859
929
 
930
+ await writer.commit('0/1');
931
+
860
932
  const stream = sync.streamResponse({
861
933
  syncContext,
862
934
  bucketStorage,
@@ -875,31 +947,29 @@ bucket_definitions:
875
947
  iter.return?.();
876
948
  });
877
949
 
950
+ const { bucket } = bucketRequest(syncRules, 'by_user["user1"]');
878
951
  const checkpoint1 = await getCheckpointLines(iter);
879
- expect((checkpoint1[0] as StreamingSyncCheckpoint).checkpoint?.buckets?.map((b) => b.bucket)).toEqual([
880
- bucketRequest(syncRules, 'by_user["user1"]')
881
- ]);
882
- expect(checkpoint1).toMatchSnapshot();
883
952
 
884
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
885
- await batch.save({
886
- sourceTable: listsTable,
887
- tag: storage.SaveOperationTag.INSERT,
888
- after: {
889
- id: 'list1',
890
- user_id: 'user1',
891
- name: 'User 1'
892
- },
893
- afterReplicaId: 'list1'
894
- });
953
+ expect((checkpoint1[0] as StreamingSyncCheckpoint).checkpoint?.buckets?.map((b) => b.bucket)).toEqual([bucket]);
954
+ expect(checkpoint1).toMatchSnapshot();
895
955
 
896
- await batch.commit('0/1');
956
+ await writer.save({
957
+ sourceTable: listsTable,
958
+ tag: storage.SaveOperationTag.INSERT,
959
+ after: {
960
+ id: 'list1',
961
+ user_id: 'user1',
962
+ name: 'User 1'
963
+ },
964
+ afterReplicaId: 'list1'
897
965
  });
898
966
 
967
+ await writer.commit('0/1');
968
+
899
969
  const checkpoint2 = await getCheckpointLines(iter);
900
970
  expect(
901
971
  (checkpoint2[0] as StreamingSyncCheckpointDiff).checkpoint_diff?.updated_buckets?.map((b) => b.bucket)
902
- ).toEqual([bucketRequest(syncRules, 'by_user["user1"]')]);
972
+ ).toEqual([bucket]);
903
973
  expect(checkpoint2).toMatchSnapshot();
904
974
  });
905
975
 
@@ -915,14 +985,13 @@ bucket_definitions:
915
985
  `
916
986
  });
917
987
 
918
- const usersTable = test_utils.makeTestTable('users', ['id']);
919
- const listsTable = test_utils.makeTestTable('lists', ['id']);
920
-
921
988
  const bucketStorage = await f.getInstance(syncRules);
989
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
990
+ const usersTable = await test_utils.resolveTestTable(writer, 'users', ['id'], config, 1);
991
+ const listsTable = await test_utils.resolveTestTable(writer, 'lists', ['id'], config, 2);
922
992
  // Activate
923
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
924
- await batch.keepalive('0/0');
925
- });
993
+ await writer.markAllSnapshotDone('0/0');
994
+ await writer.keepalive('0/0');
926
995
 
927
996
  const stream = sync.streamResponse({
928
997
  syncContext,
@@ -945,35 +1014,36 @@ bucket_definitions:
945
1014
  // Initial empty checkpoint
946
1015
  expect(await getCheckpointLines(iter)).toMatchSnapshot();
947
1016
 
948
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
949
- await batch.save({
950
- sourceTable: listsTable,
951
- tag: storage.SaveOperationTag.INSERT,
952
- after: {
953
- id: 'list1',
954
- user_id: 'user1',
955
- name: 'User 1'
956
- },
957
- afterReplicaId: 'list1'
958
- });
959
-
960
- await batch.save({
961
- sourceTable: usersTable,
962
- tag: storage.SaveOperationTag.INSERT,
963
- after: {
964
- id: 'user1',
965
- name: 'User 1'
966
- },
967
- afterReplicaId: 'user1'
968
- });
1017
+ await writer.markAllSnapshotDone('0/1');
1018
+ await writer.save({
1019
+ sourceTable: listsTable,
1020
+ tag: storage.SaveOperationTag.INSERT,
1021
+ after: {
1022
+ id: 'list1',
1023
+ user_id: 'user1',
1024
+ name: 'User 1'
1025
+ },
1026
+ afterReplicaId: 'list1'
1027
+ });
969
1028
 
970
- await batch.commit('0/1');
1029
+ await writer.save({
1030
+ sourceTable: usersTable,
1031
+ tag: storage.SaveOperationTag.INSERT,
1032
+ after: {
1033
+ id: 'user1',
1034
+ name: 'User 1'
1035
+ },
1036
+ afterReplicaId: 'user1'
971
1037
  });
972
1038
 
1039
+ await writer.commit('0/1');
1040
+
1041
+ const { bucket } = test_utils.bucketRequest(syncRules, 'by_user["user1"]');
1042
+
973
1043
  const checkpoint2 = await getCheckpointLines(iter);
974
1044
  expect(
975
1045
  (checkpoint2[0] as StreamingSyncCheckpointDiff).checkpoint_diff?.updated_buckets?.map((b) => b.bucket)
976
- ).toEqual([bucketRequest(syncRules, 'by_user["user1"]')]);
1046
+ ).toEqual([bucket]);
977
1047
  expect(checkpoint2).toMatchSnapshot();
978
1048
  });
979
1049
 
@@ -985,10 +1055,10 @@ bucket_definitions:
985
1055
  });
986
1056
 
987
1057
  const bucketStorage = await f.getInstance(syncRules);
1058
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
988
1059
  // Activate
989
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
990
- await batch.keepalive('0/0');
991
- });
1060
+ await writer.markAllSnapshotDone('0/0');
1061
+ await writer.keepalive('0/0');
992
1062
 
993
1063
  const exp = Date.now() / 1000 + 0.1;
994
1064
 
@@ -1030,31 +1100,32 @@ bucket_definitions:
1030
1100
  });
1031
1101
 
1032
1102
  const bucketStorage = await f.getInstance(syncRules);
1103
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
1104
+ const testTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config);
1105
+
1106
+ await writer.markAllSnapshotDone('0/1');
1107
+ await writer.save({
1108
+ sourceTable: testTable,
1109
+ tag: storage.SaveOperationTag.INSERT,
1110
+ after: {
1111
+ id: 't1',
1112
+ description: 'Test 1'
1113
+ },
1114
+ afterReplicaId: 't1'
1115
+ });
1033
1116
 
1034
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
1035
- await batch.save({
1036
- sourceTable: TEST_TABLE,
1037
- tag: storage.SaveOperationTag.INSERT,
1038
- after: {
1039
- id: 't1',
1040
- description: 'Test 1'
1041
- },
1042
- afterReplicaId: 't1'
1043
- });
1044
-
1045
- await batch.save({
1046
- sourceTable: TEST_TABLE,
1047
- tag: storage.SaveOperationTag.INSERT,
1048
- after: {
1049
- id: 't2',
1050
- description: 'Test 2'
1051
- },
1052
- afterReplicaId: 't2'
1053
- });
1054
-
1055
- await batch.commit('0/1');
1117
+ await writer.save({
1118
+ sourceTable: testTable,
1119
+ tag: storage.SaveOperationTag.INSERT,
1120
+ after: {
1121
+ id: 't2',
1122
+ description: 'Test 2'
1123
+ },
1124
+ afterReplicaId: 't2'
1056
1125
  });
1057
1126
 
1127
+ await writer.commit('0/1');
1128
+
1058
1129
  const stream = sync.streamResponse({
1059
1130
  syncContext,
1060
1131
  bucketStorage,
@@ -1086,30 +1157,29 @@ bucket_definitions:
1086
1157
  // Now we save additional data AND compact before continuing.
1087
1158
  // This invalidates the checkpoint we've received above.
1088
1159
 
1089
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
1090
- await batch.save({
1091
- sourceTable: TEST_TABLE,
1092
- tag: storage.SaveOperationTag.UPDATE,
1093
- after: {
1094
- id: 't1',
1095
- description: 'Test 1b'
1096
- },
1097
- afterReplicaId: 't1'
1098
- });
1099
-
1100
- await batch.save({
1101
- sourceTable: TEST_TABLE,
1102
- tag: storage.SaveOperationTag.UPDATE,
1103
- after: {
1104
- id: 't2',
1105
- description: 'Test 2b'
1106
- },
1107
- afterReplicaId: 't2'
1108
- });
1160
+ await writer.markAllSnapshotDone('0/1');
1161
+ await writer.save({
1162
+ sourceTable: testTable,
1163
+ tag: storage.SaveOperationTag.UPDATE,
1164
+ after: {
1165
+ id: 't1',
1166
+ description: 'Test 1b'
1167
+ },
1168
+ afterReplicaId: 't1'
1169
+ });
1109
1170
 
1110
- await batch.commit('0/2');
1171
+ await writer.save({
1172
+ sourceTable: testTable,
1173
+ tag: storage.SaveOperationTag.UPDATE,
1174
+ after: {
1175
+ id: 't2',
1176
+ description: 'Test 2b'
1177
+ },
1178
+ afterReplicaId: 't2'
1111
1179
  });
1112
1180
 
1181
+ await writer.commit('0/2');
1182
+
1113
1183
  await bucketStorage.compact({
1114
1184
  minBucketChanges: 1,
1115
1185
  minChangeRatio: 0
@@ -1173,11 +1243,11 @@ bucket_definitions:
1173
1243
  });
1174
1244
 
1175
1245
  const bucketStorage = f.getInstance(syncRules);
1246
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
1176
1247
 
1177
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
1178
- // <= the managed write checkpoint LSN below
1179
- await batch.commit('0/1');
1180
- });
1248
+ await writer.markAllSnapshotDone('0/1');
1249
+ // <= the managed write checkpoint LSN below
1250
+ await writer.commit('0/1');
1181
1251
 
1182
1252
  const checkpoint = await bucketStorage.createManagedWriteCheckpoint({
1183
1253
  user_id: 'test',
@@ -1209,10 +1279,9 @@ bucket_definitions:
1209
1279
  })
1210
1280
  });
1211
1281
 
1212
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
1213
- // must be >= the managed write checkpoint LSN
1214
- await batch.commit('1/0');
1215
- });
1282
+ await writer.markAllSnapshotDone('0/1');
1283
+ // must be >= the managed write checkpoint LSN
1284
+ await writer.commit('1/0');
1216
1285
 
1217
1286
  // At this point the LSN has advanced, so the write checkpoint should be
1218
1287
  // included in the next checkpoint message.
@@ -1226,9 +1295,12 @@ bucket_definitions:
1226
1295
  });
1227
1296
  });
1228
1297
 
1229
- test('encodes sync rules id in buckes for streams', async () => {
1298
+ test('encodes sync rules id in buckets for streams', async () => {
1230
1299
  await using f = await factory();
1231
- const rules = `
1300
+ // This test relies making an actual update to sync rules to test the different bucket names.
1301
+ // The actual naming scheme may change, as long as the two buckets have different names.
1302
+ const rules = [
1303
+ `
1232
1304
  streams:
1233
1305
  test:
1234
1306
  auto_subscribe: true
@@ -1236,26 +1308,38 @@ streams:
1236
1308
 
1237
1309
  config:
1238
1310
  edition: 2
1239
- `;
1311
+ `,
1312
+ `
1313
+ streams:
1314
+ test2:
1315
+ auto_subscribe: true
1316
+ query: SELECT * FROM test WHERE 1;
1317
+
1318
+ config:
1319
+ edition: 2
1320
+ `
1321
+ ];
1240
1322
 
1241
1323
  for (let i = 0; i < 2; i++) {
1242
1324
  const syncRules = await updateSyncRules(f, {
1243
- content: rules
1325
+ content: rules[i]
1244
1326
  });
1245
1327
  const bucketStorage = f.getInstance(syncRules);
1328
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
1329
+
1330
+ const testTable = await test_utils.resolveTestTable(writer, 'test', ['id'], config, i + 1);
1246
1331
 
1247
- await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
1248
- await batch.save({
1249
- sourceTable: TEST_TABLE,
1250
- tag: storage.SaveOperationTag.INSERT,
1251
- after: {
1252
- id: 't1',
1253
- description: 'Test 1'
1254
- },
1255
- afterReplicaId: 't1'
1256
- });
1257
- await batch.commit('0/1');
1332
+ await writer.markAllSnapshotDone('0/1');
1333
+ await writer.save({
1334
+ sourceTable: testTable,
1335
+ tag: storage.SaveOperationTag.INSERT,
1336
+ after: {
1337
+ id: 't1',
1338
+ description: 'Test 1'
1339
+ },
1340
+ afterReplicaId: 't1'
1258
1341
  });
1342
+ await writer.commit('0/1');
1259
1343
 
1260
1344
  const stream = sync.streamResponse({
1261
1345
  syncContext,