@powersync/service-core 1.10.1 → 1.10.3

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 (68) hide show
  1. package/.probes/poll +1 -0
  2. package/CHANGELOG.md +12 -0
  3. package/dist/sync/sync.js +8 -1
  4. package/dist/sync/sync.js.map +1 -1
  5. package/package.json +3 -3
  6. package/src/sync/sync.ts +8 -1
  7. package/test/dist/auth.test.d.ts +1 -0
  8. package/test/dist/auth.test.js +332 -0
  9. package/test/dist/auth.test.js.map +1 -0
  10. package/test/dist/broadcast_iterable.test.d.ts +1 -0
  11. package/test/dist/broadcast_iterable.test.js +131 -0
  12. package/test/dist/broadcast_iterable.test.js.map +1 -0
  13. package/test/dist/bucket_validation.d.ts +26 -0
  14. package/test/dist/bucket_validation.js +56 -0
  15. package/test/dist/bucket_validation.js.map +1 -0
  16. package/test/dist/bucket_validation.test.d.ts +1 -0
  17. package/test/dist/bucket_validation.test.js +134 -0
  18. package/test/dist/bucket_validation.test.js.map +1 -0
  19. package/test/dist/checksum_cache.test.d.ts +1 -0
  20. package/test/dist/checksum_cache.test.js +375 -0
  21. package/test/dist/checksum_cache.test.js.map +1 -0
  22. package/test/dist/compacting.test.d.ts +1 -0
  23. package/test/dist/compacting.test.js +254 -0
  24. package/test/dist/compacting.test.js.map +1 -0
  25. package/test/dist/data_storage.test.d.ts +1 -0
  26. package/test/dist/data_storage.test.js +1306 -0
  27. package/test/dist/data_storage.test.js.map +1 -0
  28. package/test/dist/demultiplexer.test.d.ts +1 -0
  29. package/test/dist/demultiplexer.test.js +166 -0
  30. package/test/dist/demultiplexer.test.js.map +1 -0
  31. package/test/dist/env.d.ts +3 -0
  32. package/test/dist/env.js +5 -0
  33. package/test/dist/env.js.map +1 -0
  34. package/test/dist/merge_iterable.test.d.ts +1 -0
  35. package/test/dist/merge_iterable.test.js +321 -0
  36. package/test/dist/merge_iterable.test.js.map +1 -0
  37. package/test/dist/routes/probes.integration.test.d.ts +1 -0
  38. package/test/dist/routes/probes.integration.test.js +192 -0
  39. package/test/dist/routes/probes.integration.test.js.map +1 -0
  40. package/test/dist/routes/probes.test.d.ts +1 -0
  41. package/test/dist/routes/probes.test.js +119 -0
  42. package/test/dist/routes/probes.test.js.map +1 -0
  43. package/test/dist/setup.d.ts +1 -0
  44. package/test/dist/setup.js +7 -0
  45. package/test/dist/setup.js.map +1 -0
  46. package/test/dist/stream_utils.d.ts +6 -0
  47. package/test/dist/stream_utils.js +37 -0
  48. package/test/dist/stream_utils.js.map +1 -0
  49. package/test/dist/sync/BucketChecksumState.test.d.ts +1 -0
  50. package/test/dist/sync/BucketChecksumState.test.js +499 -0
  51. package/test/dist/sync/BucketChecksumState.test.js.map +1 -0
  52. package/test/dist/sync/checksum_state.test.d.ts +1 -0
  53. package/test/dist/sync/checksum_state.test.js +21 -0
  54. package/test/dist/sync/checksum_state.test.js.map +1 -0
  55. package/test/dist/sync/util.test.d.ts +1 -0
  56. package/test/dist/sync/util.test.js +28 -0
  57. package/test/dist/sync/util.test.js.map +1 -0
  58. package/test/dist/sync.test.d.ts +1 -0
  59. package/test/dist/sync.test.js +428 -0
  60. package/test/dist/sync.test.js.map +1 -0
  61. package/test/dist/util.d.ts +36 -0
  62. package/test/dist/util.js +114 -0
  63. package/test/dist/util.js.map +1 -0
  64. package/test/dist/util.test.d.ts +1 -0
  65. package/test/dist/util.test.js +45 -0
  66. package/test/dist/util.test.js.map +1 -0
  67. package/test/tsconfig.tsbuildinfo +1 -0
  68. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,1306 @@
1
+ import { SaveOperationTag } from '@/storage/BucketStorage.js';
2
+ import { getUuidReplicaIdentityBson } from '@/util/util-index.js';
3
+ import { RequestParameters } from '@powersync/service-sync-rules';
4
+ import { describe, expect, test } from 'vitest';
5
+ import { fromAsync, oneFromAsync } from './stream_utils.js';
6
+ import { BATCH_OPTIONS, getBatchData, getBatchMeta, makeTestTable, MONGO_STORAGE_FACTORY, PARSE_OPTIONS, rid, testRules } from './util.js';
7
+ const TEST_TABLE = makeTestTable('test', ['id']);
8
+ describe('store - mongodb', function () {
9
+ defineDataStorageTests(MONGO_STORAGE_FACTORY);
10
+ });
11
+ function defineDataStorageTests(factory) {
12
+ test('save and load parameters', async () => {
13
+ const sync_rules = testRules(`
14
+ bucket_definitions:
15
+ mybucket:
16
+ parameters:
17
+ - SELECT group_id FROM test WHERE id1 = token_parameters.user_id OR id2 = token_parameters.user_id
18
+ data: []
19
+ `);
20
+ const storage = (await factory()).getInstance(sync_rules);
21
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
22
+ await batch.save({
23
+ sourceTable: TEST_TABLE,
24
+ tag: SaveOperationTag.INSERT,
25
+ after: {
26
+ id: 't2',
27
+ id1: 'user3',
28
+ id2: 'user4',
29
+ group_id: 'group2a'
30
+ },
31
+ afterReplicaId: rid('t2')
32
+ });
33
+ await batch.save({
34
+ sourceTable: TEST_TABLE,
35
+ tag: SaveOperationTag.INSERT,
36
+ after: {
37
+ id: 't1',
38
+ id1: 'user1',
39
+ id2: 'user2',
40
+ group_id: 'group1a'
41
+ },
42
+ afterReplicaId: rid('t1')
43
+ });
44
+ });
45
+ const parameters = await storage.getParameterSets(result.flushed_op, [['mybucket', '1', 'user1']]);
46
+ expect(parameters).toEqual([
47
+ {
48
+ group_id: 'group1a'
49
+ }
50
+ ]);
51
+ });
52
+ test('it should use the latest version', async () => {
53
+ const sync_rules = testRules(`
54
+ bucket_definitions:
55
+ mybucket:
56
+ parameters:
57
+ - SELECT group_id FROM test WHERE id = token_parameters.user_id
58
+ data: []
59
+ `);
60
+ const storage = (await factory()).getInstance(sync_rules);
61
+ const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
62
+ await batch.save({
63
+ sourceTable: TEST_TABLE,
64
+ tag: SaveOperationTag.INSERT,
65
+ after: {
66
+ id: 'user1',
67
+ group_id: 'group1'
68
+ },
69
+ afterReplicaId: rid('user1')
70
+ });
71
+ });
72
+ const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
73
+ await batch.save({
74
+ sourceTable: TEST_TABLE,
75
+ tag: SaveOperationTag.INSERT,
76
+ after: {
77
+ id: 'user1',
78
+ group_id: 'group2'
79
+ },
80
+ afterReplicaId: rid('user1')
81
+ });
82
+ });
83
+ const parameters = await storage.getParameterSets(result2.flushed_op, [['mybucket', '1', 'user1']]);
84
+ expect(parameters).toEqual([
85
+ {
86
+ group_id: 'group2'
87
+ }
88
+ ]);
89
+ // Use the checkpoint to get older data if relevant
90
+ const parameters2 = await storage.getParameterSets(result1.flushed_op, [['mybucket', '1', 'user1']]);
91
+ expect(parameters2).toEqual([
92
+ {
93
+ group_id: 'group1'
94
+ }
95
+ ]);
96
+ });
97
+ test('it should use the latest version after updates', async () => {
98
+ const sync_rules = testRules(`
99
+ bucket_definitions:
100
+ mybucket:
101
+ parameters:
102
+ - SELECT id AS todo_id
103
+ FROM todos
104
+ WHERE list_id IN token_parameters.list_id
105
+ data: []
106
+ `);
107
+ const storage = (await factory()).getInstance(sync_rules);
108
+ const table = makeTestTable('todos', ['id', 'list_id']);
109
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
110
+ // Create two todos which initially belong to different lists
111
+ await batch.save({
112
+ sourceTable: table,
113
+ tag: SaveOperationTag.INSERT,
114
+ after: {
115
+ id: 'todo1',
116
+ list_id: 'list1'
117
+ },
118
+ afterReplicaId: rid('todo1')
119
+ });
120
+ await batch.save({
121
+ sourceTable: table,
122
+ tag: SaveOperationTag.INSERT,
123
+ after: {
124
+ id: 'todo2',
125
+ list_id: 'list2'
126
+ },
127
+ afterReplicaId: rid('todo2')
128
+ });
129
+ });
130
+ const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
131
+ // Update the second todo item to now belong to list 1
132
+ await batch.save({
133
+ sourceTable: table,
134
+ tag: SaveOperationTag.UPDATE,
135
+ after: {
136
+ id: 'todo2',
137
+ list_id: 'list1'
138
+ },
139
+ afterReplicaId: rid('todo2')
140
+ });
141
+ });
142
+ // We specifically request the todo_ids for both lists.
143
+ // There removal operation for the association of `list2`::`todo2` should not interfere with the new
144
+ // association of `list1`::`todo2`
145
+ const parameters = await storage.getParameterSets(BigInt(result2.flushed_op).toString(), [
146
+ ['mybucket', '1', 'list1'],
147
+ ['mybucket', '1', 'list2']
148
+ ]);
149
+ expect(parameters.sort((a, b) => a.todo_id.localeCompare(b.todo_id))).toEqual([
150
+ {
151
+ todo_id: 'todo1'
152
+ },
153
+ {
154
+ todo_id: 'todo2'
155
+ }
156
+ ]);
157
+ });
158
+ test('save and load parameters with different number types', async () => {
159
+ const sync_rules = testRules(`
160
+ bucket_definitions:
161
+ mybucket:
162
+ parameters:
163
+ - SELECT group_id FROM test WHERE n1 = token_parameters.n1 and f2 = token_parameters.f2 and f3 = token_parameters.f3
164
+ data: []
165
+ `);
166
+ const storage = (await factory()).getInstance(sync_rules);
167
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
168
+ await batch.save({
169
+ sourceTable: TEST_TABLE,
170
+ tag: SaveOperationTag.INSERT,
171
+ after: {
172
+ id: 't1',
173
+ group_id: 'group1',
174
+ n1: 314n,
175
+ f2: 314,
176
+ f3: 3.14
177
+ },
178
+ afterReplicaId: rid('t1')
179
+ });
180
+ });
181
+ const TEST_PARAMS = { group_id: 'group1' };
182
+ const checkpoint = result.flushed_op;
183
+ const parameters1 = await storage.getParameterSets(checkpoint, [['mybucket', '1', 314n, 314, 3.14]]);
184
+ expect(parameters1).toEqual([TEST_PARAMS]);
185
+ const parameters2 = await storage.getParameterSets(checkpoint, [['mybucket', '1', 314, 314n, 3.14]]);
186
+ expect(parameters2).toEqual([TEST_PARAMS]);
187
+ const parameters3 = await storage.getParameterSets(checkpoint, [['mybucket', '1', 314n, 314, 3]]);
188
+ expect(parameters3).toEqual([]);
189
+ });
190
+ test('save and load parameters with large numbers', async () => {
191
+ // This ensures serialization / deserialization of "current_data" is done correctly.
192
+ // This specific case tested here cannot happen with postgres in practice, but we still
193
+ // test this to ensure correct deserialization.
194
+ const sync_rules = testRules(`
195
+ bucket_definitions:
196
+ mybucket:
197
+ parameters:
198
+ - SELECT group_id FROM test WHERE n1 = token_parameters.n1
199
+ data: []
200
+ `);
201
+ const storage = (await factory()).getInstance(sync_rules);
202
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
203
+ await batch.save({
204
+ sourceTable: TEST_TABLE,
205
+ tag: SaveOperationTag.INSERT,
206
+ after: {
207
+ id: 't1',
208
+ group_id: 'group1',
209
+ n1: 1152921504606846976n // 2^60
210
+ },
211
+ afterReplicaId: rid('t1')
212
+ });
213
+ await batch.save({
214
+ sourceTable: TEST_TABLE,
215
+ tag: SaveOperationTag.UPDATE,
216
+ after: {
217
+ id: 't1',
218
+ group_id: 'group1',
219
+ // Simulate a TOAST value, even though it can't happen for values like this
220
+ // in practice.
221
+ n1: undefined
222
+ },
223
+ afterReplicaId: rid('t1')
224
+ });
225
+ });
226
+ const TEST_PARAMS = { group_id: 'group1' };
227
+ const checkpoint = result.flushed_op;
228
+ const parameters1 = await storage.getParameterSets(checkpoint, [['mybucket', '1', 1152921504606846976n]]);
229
+ expect(parameters1).toEqual([TEST_PARAMS]);
230
+ });
231
+ test('removing row', async () => {
232
+ const sync_rules = testRules(`
233
+ bucket_definitions:
234
+ global:
235
+ data:
236
+ - SELECT id, description FROM "%"
237
+ `);
238
+ const storage = (await factory()).getInstance(sync_rules);
239
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
240
+ const sourceTable = TEST_TABLE;
241
+ await batch.save({
242
+ sourceTable,
243
+ tag: SaveOperationTag.INSERT,
244
+ after: {
245
+ id: 'test1',
246
+ description: 'test1'
247
+ },
248
+ afterReplicaId: rid('test1')
249
+ });
250
+ await batch.save({
251
+ sourceTable,
252
+ tag: SaveOperationTag.DELETE,
253
+ beforeReplicaId: rid('test1')
254
+ });
255
+ });
256
+ const checkpoint = result.flushed_op;
257
+ const batch = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']])));
258
+ const data = batch[0].batch.data.map((d) => {
259
+ return {
260
+ op: d.op,
261
+ object_id: d.object_id,
262
+ checksum: d.checksum
263
+ };
264
+ });
265
+ const c1 = 2871785649;
266
+ const c2 = 2872534815;
267
+ expect(data).toEqual([
268
+ { op: 'PUT', object_id: 'test1', checksum: c1 },
269
+ { op: 'REMOVE', object_id: 'test1', checksum: c2 }
270
+ ]);
271
+ const checksums = [...(await storage.getChecksums(checkpoint, ['global[]'])).values()];
272
+ expect(checksums).toEqual([
273
+ {
274
+ bucket: 'global[]',
275
+ checksum: (c1 + c2) & 0xffffffff,
276
+ count: 2
277
+ }
278
+ ]);
279
+ });
280
+ test('save and load parameters with workspaceId', async () => {
281
+ const WORKSPACE_TABLE = makeTestTable('workspace', ['id']);
282
+ const sync_rules_content = testRules(`
283
+ bucket_definitions:
284
+ by_workspace:
285
+ parameters:
286
+ - SELECT id as workspace_id FROM workspace WHERE
287
+ workspace."userId" = token_parameters.user_id
288
+ data: []
289
+ `);
290
+ const sync_rules = sync_rules_content.parsed(PARSE_OPTIONS).sync_rules;
291
+ const storage = (await factory()).getInstance(sync_rules_content);
292
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
293
+ await batch.save({
294
+ sourceTable: WORKSPACE_TABLE,
295
+ tag: SaveOperationTag.INSERT,
296
+ after: {
297
+ id: 'workspace1',
298
+ userId: 'u1'
299
+ },
300
+ afterReplicaId: rid('workspace1')
301
+ });
302
+ });
303
+ const checkpoint = result.flushed_op;
304
+ const parameters = new RequestParameters({ sub: 'u1' }, {});
305
+ const q1 = sync_rules.bucket_descriptors[0].parameter_queries[0];
306
+ const lookups = q1.getLookups(parameters);
307
+ expect(lookups).toEqual([['by_workspace', '1', 'u1']]);
308
+ const parameter_sets = await storage.getParameterSets(checkpoint, lookups);
309
+ expect(parameter_sets).toEqual([{ workspace_id: 'workspace1' }]);
310
+ const buckets = await sync_rules.queryBucketIds({
311
+ getParameterSets(lookups) {
312
+ return storage.getParameterSets(checkpoint, lookups);
313
+ },
314
+ parameters
315
+ });
316
+ expect(buckets).toEqual(['by_workspace["workspace1"]']);
317
+ });
318
+ test('save and load parameters with dynamic global buckets', async () => {
319
+ const WORKSPACE_TABLE = makeTestTable('workspace');
320
+ const sync_rules_content = testRules(`
321
+ bucket_definitions:
322
+ by_public_workspace:
323
+ parameters:
324
+ - SELECT id as workspace_id FROM workspace WHERE
325
+ workspace.visibility = 'public'
326
+ data: []
327
+ `);
328
+ const sync_rules = sync_rules_content.parsed(PARSE_OPTIONS).sync_rules;
329
+ const storage = (await factory()).getInstance(sync_rules_content);
330
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
331
+ await batch.save({
332
+ sourceTable: WORKSPACE_TABLE,
333
+ tag: SaveOperationTag.INSERT,
334
+ after: {
335
+ id: 'workspace1',
336
+ visibility: 'public'
337
+ },
338
+ afterReplicaId: rid('workspace1')
339
+ });
340
+ await batch.save({
341
+ sourceTable: WORKSPACE_TABLE,
342
+ tag: SaveOperationTag.INSERT,
343
+ after: {
344
+ id: 'workspace2',
345
+ visibility: 'private'
346
+ },
347
+ afterReplicaId: rid('workspace2')
348
+ });
349
+ await batch.save({
350
+ sourceTable: WORKSPACE_TABLE,
351
+ tag: SaveOperationTag.INSERT,
352
+ after: {
353
+ id: 'workspace3',
354
+ visibility: 'public'
355
+ },
356
+ afterReplicaId: rid('workspace3')
357
+ });
358
+ });
359
+ const checkpoint = result.flushed_op;
360
+ const parameters = new RequestParameters({ sub: 'unknown' }, {});
361
+ const q1 = sync_rules.bucket_descriptors[0].parameter_queries[0];
362
+ const lookups = q1.getLookups(parameters);
363
+ expect(lookups).toEqual([['by_public_workspace', '1']]);
364
+ const parameter_sets = await storage.getParameterSets(checkpoint, lookups);
365
+ parameter_sets.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
366
+ expect(parameter_sets).toEqual([{ workspace_id: 'workspace1' }, { workspace_id: 'workspace3' }]);
367
+ const buckets = await sync_rules.queryBucketIds({
368
+ getParameterSets(lookups) {
369
+ return storage.getParameterSets(checkpoint, lookups);
370
+ },
371
+ parameters
372
+ });
373
+ buckets.sort();
374
+ expect(buckets).toEqual(['by_public_workspace["workspace1"]', 'by_public_workspace["workspace3"]']);
375
+ });
376
+ test('multiple parameter queries', async () => {
377
+ const WORKSPACE_TABLE = makeTestTable('workspace');
378
+ const sync_rules_content = testRules(`
379
+ bucket_definitions:
380
+ by_workspace:
381
+ parameters:
382
+ - SELECT id as workspace_id FROM workspace WHERE
383
+ workspace.visibility = 'public'
384
+ - SELECT id as workspace_id FROM workspace WHERE
385
+ workspace.user_id = token_parameters.user_id
386
+ data: []
387
+ `);
388
+ const sync_rules = sync_rules_content.parsed(PARSE_OPTIONS).sync_rules;
389
+ const storage = (await factory()).getInstance(sync_rules_content);
390
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
391
+ await batch.save({
392
+ sourceTable: WORKSPACE_TABLE,
393
+ tag: SaveOperationTag.INSERT,
394
+ after: {
395
+ id: 'workspace1',
396
+ visibility: 'public'
397
+ },
398
+ afterReplicaId: rid('workspace1')
399
+ });
400
+ await batch.save({
401
+ sourceTable: WORKSPACE_TABLE,
402
+ tag: SaveOperationTag.INSERT,
403
+ after: {
404
+ id: 'workspace2',
405
+ visibility: 'private'
406
+ },
407
+ afterReplicaId: rid('workspace2')
408
+ });
409
+ await batch.save({
410
+ sourceTable: WORKSPACE_TABLE,
411
+ tag: SaveOperationTag.INSERT,
412
+ after: {
413
+ id: 'workspace3',
414
+ user_id: 'u1',
415
+ visibility: 'private'
416
+ },
417
+ afterReplicaId: rid('workspace3')
418
+ });
419
+ await batch.save({
420
+ sourceTable: WORKSPACE_TABLE,
421
+ tag: SaveOperationTag.INSERT,
422
+ after: {
423
+ id: 'workspace4',
424
+ user_id: 'u2',
425
+ visibility: 'private'
426
+ },
427
+ afterReplicaId: rid('workspace4')
428
+ });
429
+ });
430
+ const checkpoint = result.flushed_op;
431
+ const parameters = new RequestParameters({ sub: 'u1' }, {});
432
+ // Test intermediate values - could be moved to sync_rules.test.ts
433
+ const q1 = sync_rules.bucket_descriptors[0].parameter_queries[0];
434
+ const lookups1 = q1.getLookups(parameters);
435
+ expect(lookups1).toEqual([['by_workspace', '1']]);
436
+ const parameter_sets1 = await storage.getParameterSets(checkpoint, lookups1);
437
+ parameter_sets1.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
438
+ expect(parameter_sets1).toEqual([{ workspace_id: 'workspace1' }]);
439
+ const q2 = sync_rules.bucket_descriptors[0].parameter_queries[1];
440
+ const lookups2 = q2.getLookups(parameters);
441
+ expect(lookups2).toEqual([['by_workspace', '2', 'u1']]);
442
+ const parameter_sets2 = await storage.getParameterSets(checkpoint, lookups2);
443
+ parameter_sets2.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
444
+ expect(parameter_sets2).toEqual([{ workspace_id: 'workspace3' }]);
445
+ // Test final values - the important part
446
+ const buckets = await sync_rules.queryBucketIds({
447
+ getParameterSets(lookups) {
448
+ return storage.getParameterSets(checkpoint, lookups);
449
+ },
450
+ parameters
451
+ });
452
+ buckets.sort();
453
+ expect(buckets).toEqual(['by_workspace["workspace1"]', 'by_workspace["workspace3"]']);
454
+ });
455
+ test('changing client ids', async () => {
456
+ const sync_rules = testRules(`
457
+ bucket_definitions:
458
+ global:
459
+ data:
460
+ - SELECT client_id as id, description FROM "%"
461
+ `);
462
+ const storage = (await factory()).getInstance(sync_rules);
463
+ const sourceTable = TEST_TABLE;
464
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
465
+ await batch.save({
466
+ sourceTable,
467
+ tag: SaveOperationTag.INSERT,
468
+ after: {
469
+ id: 'test1',
470
+ client_id: 'client1a',
471
+ description: 'test1a'
472
+ },
473
+ afterReplicaId: rid('test1')
474
+ });
475
+ await batch.save({
476
+ sourceTable,
477
+ tag: SaveOperationTag.UPDATE,
478
+ after: {
479
+ id: 'test1',
480
+ client_id: 'client1b',
481
+ description: 'test1b'
482
+ },
483
+ afterReplicaId: rid('test1')
484
+ });
485
+ await batch.save({
486
+ sourceTable,
487
+ tag: SaveOperationTag.INSERT,
488
+ after: {
489
+ id: 'test2',
490
+ client_id: 'client2',
491
+ description: 'test2'
492
+ },
493
+ afterReplicaId: rid('test2')
494
+ });
495
+ });
496
+ const checkpoint = result.flushed_op;
497
+ const batch = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']])));
498
+ const data = batch[0].batch.data.map((d) => {
499
+ return {
500
+ op: d.op,
501
+ object_id: d.object_id
502
+ };
503
+ });
504
+ expect(data).toEqual([
505
+ { op: 'PUT', object_id: 'client1a' },
506
+ { op: 'PUT', object_id: 'client1b' },
507
+ { op: 'REMOVE', object_id: 'client1a' },
508
+ { op: 'PUT', object_id: 'client2' }
509
+ ]);
510
+ });
511
+ test('re-apply delete', async () => {
512
+ const sync_rules = testRules(`
513
+ bucket_definitions:
514
+ global:
515
+ data:
516
+ - SELECT id, description FROM "%"
517
+ `);
518
+ const storage = (await factory()).getInstance(sync_rules);
519
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
520
+ const sourceTable = TEST_TABLE;
521
+ await batch.save({
522
+ sourceTable,
523
+ tag: SaveOperationTag.INSERT,
524
+ after: {
525
+ id: 'test1',
526
+ description: 'test1'
527
+ },
528
+ afterReplicaId: rid('test1')
529
+ });
530
+ });
531
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
532
+ const sourceTable = TEST_TABLE;
533
+ await batch.save({
534
+ sourceTable,
535
+ tag: SaveOperationTag.DELETE,
536
+ beforeReplicaId: rid('test1')
537
+ });
538
+ });
539
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
540
+ const sourceTable = TEST_TABLE;
541
+ await batch.save({
542
+ sourceTable,
543
+ tag: SaveOperationTag.DELETE,
544
+ beforeReplicaId: rid('test1')
545
+ });
546
+ });
547
+ const checkpoint = result.flushed_op;
548
+ const batch = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']])));
549
+ const data = batch[0].batch.data.map((d) => {
550
+ return {
551
+ op: d.op,
552
+ object_id: d.object_id,
553
+ checksum: d.checksum
554
+ };
555
+ });
556
+ const c1 = 2871785649;
557
+ const c2 = 2872534815;
558
+ expect(data).toEqual([
559
+ { op: 'PUT', object_id: 'test1', checksum: c1 },
560
+ { op: 'REMOVE', object_id: 'test1', checksum: c2 }
561
+ ]);
562
+ const checksums = [...(await storage.getChecksums(checkpoint, ['global[]'])).values()];
563
+ expect(checksums).toEqual([
564
+ {
565
+ bucket: 'global[]',
566
+ checksum: (c1 + c2) & 0xffffffff,
567
+ count: 2
568
+ }
569
+ ]);
570
+ });
571
+ test('re-apply update + delete', async () => {
572
+ const sync_rules = testRules(`
573
+ bucket_definitions:
574
+ global:
575
+ data:
576
+ - SELECT id, description FROM "%"
577
+ `);
578
+ const storage = (await factory()).getInstance(sync_rules);
579
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
580
+ const sourceTable = TEST_TABLE;
581
+ await batch.save({
582
+ sourceTable,
583
+ tag: SaveOperationTag.INSERT,
584
+ after: {
585
+ id: 'test1',
586
+ description: 'test1'
587
+ },
588
+ afterReplicaId: rid('test1')
589
+ });
590
+ });
591
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
592
+ const sourceTable = TEST_TABLE;
593
+ await batch.save({
594
+ sourceTable,
595
+ tag: SaveOperationTag.UPDATE,
596
+ after: {
597
+ id: 'test1',
598
+ description: undefined
599
+ },
600
+ afterReplicaId: rid('test1')
601
+ });
602
+ await batch.save({
603
+ sourceTable,
604
+ tag: SaveOperationTag.UPDATE,
605
+ after: {
606
+ id: 'test1',
607
+ description: undefined
608
+ },
609
+ afterReplicaId: rid('test1')
610
+ });
611
+ await batch.save({
612
+ sourceTable,
613
+ tag: SaveOperationTag.DELETE,
614
+ beforeReplicaId: rid('test1')
615
+ });
616
+ });
617
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
618
+ const sourceTable = TEST_TABLE;
619
+ await batch.save({
620
+ sourceTable,
621
+ tag: SaveOperationTag.UPDATE,
622
+ after: {
623
+ id: 'test1',
624
+ description: undefined
625
+ },
626
+ afterReplicaId: rid('test1')
627
+ });
628
+ await batch.save({
629
+ sourceTable,
630
+ tag: SaveOperationTag.UPDATE,
631
+ after: {
632
+ id: 'test1',
633
+ description: undefined
634
+ },
635
+ afterReplicaId: rid('test1')
636
+ });
637
+ await batch.save({
638
+ sourceTable,
639
+ tag: SaveOperationTag.DELETE,
640
+ beforeReplicaId: rid('test1')
641
+ });
642
+ });
643
+ const checkpoint = result.flushed_op;
644
+ const batch = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']])));
645
+ const data = batch[0].batch.data.map((d) => {
646
+ return {
647
+ op: d.op,
648
+ object_id: d.object_id,
649
+ checksum: d.checksum
650
+ };
651
+ });
652
+ const c1 = 2871785649;
653
+ const c2 = 2872534815;
654
+ expect(data).toEqual([
655
+ { op: 'PUT', object_id: 'test1', checksum: c1 },
656
+ { op: 'PUT', object_id: 'test1', checksum: c1 },
657
+ { op: 'PUT', object_id: 'test1', checksum: c1 },
658
+ { op: 'REMOVE', object_id: 'test1', checksum: c2 }
659
+ ]);
660
+ const checksums = [...(await storage.getChecksums(checkpoint, ['global[]'])).values()];
661
+ expect(checksums).toEqual([
662
+ {
663
+ bucket: 'global[]',
664
+ checksum: (c1 + c1 + c1 + c2) & 0xffffffff,
665
+ count: 4
666
+ }
667
+ ]);
668
+ });
669
+ test('truncate parameters', async () => {
670
+ const sync_rules = testRules(`
671
+ bucket_definitions:
672
+ mybucket:
673
+ parameters:
674
+ - SELECT group_id FROM test WHERE id1 = token_parameters.user_id OR id2 = token_parameters.user_id
675
+ data: []
676
+ `);
677
+ const storage = (await factory()).getInstance(sync_rules);
678
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
679
+ await batch.save({
680
+ sourceTable: TEST_TABLE,
681
+ tag: SaveOperationTag.INSERT,
682
+ after: {
683
+ id: 't2',
684
+ id1: 'user3',
685
+ id2: 'user4',
686
+ group_id: 'group2a'
687
+ },
688
+ afterReplicaId: rid('t2')
689
+ });
690
+ await batch.truncate([TEST_TABLE]);
691
+ });
692
+ const { checkpoint } = await storage.getCheckpoint();
693
+ const parameters = await storage.getParameterSets(checkpoint, [['mybucket', '1', 'user1']]);
694
+ expect(parameters).toEqual([]);
695
+ });
696
+ test('batch with overlapping replica ids', async () => {
697
+ // This test checks that we get the correct output when processing rows with:
698
+ // 1. changing replica ids
699
+ // 2. overlapping with replica ids of other rows in the same transaction (at different times)
700
+ // If operations are not processing in input order, this breaks easily.
701
+ // It can break at two places:
702
+ // 1. Not getting the correct "current_data" state for each operation.
703
+ // 2. Output order not being correct.
704
+ const sync_rules = testRules(`
705
+ bucket_definitions:
706
+ global:
707
+ data:
708
+ - SELECT id, description FROM "test"
709
+ `);
710
+ const storage = (await factory()).getInstance(sync_rules);
711
+ // Pre-setup
712
+ const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
713
+ const sourceTable = TEST_TABLE;
714
+ await batch.save({
715
+ sourceTable,
716
+ tag: SaveOperationTag.INSERT,
717
+ after: {
718
+ id: 'test1',
719
+ description: 'test1a'
720
+ },
721
+ afterReplicaId: rid('test1')
722
+ });
723
+ await batch.save({
724
+ sourceTable,
725
+ tag: SaveOperationTag.INSERT,
726
+ after: {
727
+ id: 'test2',
728
+ description: 'test2a'
729
+ },
730
+ afterReplicaId: rid('test2')
731
+ });
732
+ });
733
+ const checkpoint1 = result1?.flushed_op ?? '0';
734
+ // Test batch
735
+ const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
736
+ const sourceTable = TEST_TABLE;
737
+ // b
738
+ await batch.save({
739
+ sourceTable,
740
+ tag: SaveOperationTag.INSERT,
741
+ after: {
742
+ id: 'test1',
743
+ description: 'test1b'
744
+ },
745
+ afterReplicaId: rid('test1')
746
+ });
747
+ await batch.save({
748
+ sourceTable,
749
+ tag: SaveOperationTag.UPDATE,
750
+ before: {
751
+ id: 'test1'
752
+ },
753
+ beforeReplicaId: rid('test1'),
754
+ after: {
755
+ id: 'test2',
756
+ description: 'test2b'
757
+ },
758
+ afterReplicaId: rid('test2')
759
+ });
760
+ await batch.save({
761
+ sourceTable,
762
+ tag: SaveOperationTag.UPDATE,
763
+ before: {
764
+ id: 'test2'
765
+ },
766
+ beforeReplicaId: rid('test2'),
767
+ after: {
768
+ id: 'test3',
769
+ description: 'test3b'
770
+ },
771
+ afterReplicaId: rid('test3')
772
+ });
773
+ // c
774
+ await batch.save({
775
+ sourceTable,
776
+ tag: SaveOperationTag.UPDATE,
777
+ after: {
778
+ id: 'test2',
779
+ description: 'test2c'
780
+ },
781
+ afterReplicaId: rid('test2')
782
+ });
783
+ // d
784
+ await batch.save({
785
+ sourceTable,
786
+ tag: SaveOperationTag.INSERT,
787
+ after: {
788
+ id: 'test4',
789
+ description: 'test4d'
790
+ },
791
+ afterReplicaId: rid('test4')
792
+ });
793
+ await batch.save({
794
+ sourceTable,
795
+ tag: SaveOperationTag.UPDATE,
796
+ before: {
797
+ id: 'test4'
798
+ },
799
+ beforeReplicaId: rid('test4'),
800
+ after: {
801
+ id: 'test5',
802
+ description: 'test5d'
803
+ },
804
+ afterReplicaId: rid('test5')
805
+ });
806
+ });
807
+ const checkpoint2 = result2.flushed_op;
808
+ const batch = await fromAsync(storage.getBucketDataBatch(checkpoint2, new Map([['global[]', checkpoint1]])));
809
+ const data = batch[0].batch.data.map((d) => {
810
+ return {
811
+ op: d.op,
812
+ object_id: d.object_id,
813
+ data: d.data
814
+ };
815
+ });
816
+ // Operations must be in this order
817
+ expect(data).toEqual([
818
+ // b
819
+ { op: 'PUT', object_id: 'test1', data: JSON.stringify({ id: 'test1', description: 'test1b' }) },
820
+ { op: 'REMOVE', object_id: 'test1', data: null },
821
+ { op: 'PUT', object_id: 'test2', data: JSON.stringify({ id: 'test2', description: 'test2b' }) },
822
+ { op: 'REMOVE', object_id: 'test2', data: null },
823
+ { op: 'PUT', object_id: 'test3', data: JSON.stringify({ id: 'test3', description: 'test3b' }) },
824
+ // c
825
+ { op: 'PUT', object_id: 'test2', data: JSON.stringify({ id: 'test2', description: 'test2c' }) },
826
+ // d
827
+ { op: 'PUT', object_id: 'test4', data: JSON.stringify({ id: 'test4', description: 'test4d' }) },
828
+ { op: 'REMOVE', object_id: 'test4', data: null },
829
+ { op: 'PUT', object_id: 'test5', data: JSON.stringify({ id: 'test5', description: 'test5d' }) }
830
+ ]);
831
+ });
832
+ test('changed data with replica identity full', async () => {
833
+ const sync_rules = testRules(`
834
+ bucket_definitions:
835
+ global:
836
+ data:
837
+ - SELECT id, description FROM "test"
838
+ `);
839
+ function rid2(id, description) {
840
+ return getUuidReplicaIdentityBson({ id, description }, [
841
+ { name: 'id', type: 'VARCHAR', typeId: 25 },
842
+ { name: 'description', type: 'VARCHAR', typeId: 25 }
843
+ ]);
844
+ }
845
+ const storage = (await factory()).getInstance(sync_rules);
846
+ const sourceTable = makeTestTable('test', ['id', 'description']);
847
+ // Pre-setup
848
+ const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
849
+ await batch.save({
850
+ sourceTable,
851
+ tag: SaveOperationTag.INSERT,
852
+ after: {
853
+ id: 'test1',
854
+ description: 'test1a'
855
+ },
856
+ afterReplicaId: rid2('test1', 'test1a')
857
+ });
858
+ });
859
+ const checkpoint1 = result1?.flushed_op ?? '0';
860
+ const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
861
+ // Unchanged, but has a before id
862
+ await batch.save({
863
+ sourceTable,
864
+ tag: SaveOperationTag.UPDATE,
865
+ before: {
866
+ id: 'test1',
867
+ description: 'test1a'
868
+ },
869
+ beforeReplicaId: rid2('test1', 'test1a'),
870
+ after: {
871
+ id: 'test1',
872
+ description: 'test1b'
873
+ },
874
+ afterReplicaId: rid2('test1', 'test1b')
875
+ });
876
+ });
877
+ const result3 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
878
+ // Delete
879
+ await batch.save({
880
+ sourceTable,
881
+ tag: SaveOperationTag.DELETE,
882
+ before: {
883
+ id: 'test1',
884
+ description: 'test1b'
885
+ },
886
+ beforeReplicaId: rid2('test1', 'test1b'),
887
+ after: undefined
888
+ });
889
+ });
890
+ const checkpoint3 = result3.flushed_op;
891
+ const batch = await fromAsync(storage.getBucketDataBatch(checkpoint3, new Map([['global[]', checkpoint1]])));
892
+ const data = batch[0].batch.data.map((d) => {
893
+ return {
894
+ op: d.op,
895
+ object_id: d.object_id,
896
+ data: d.data,
897
+ subkey: d.subkey
898
+ };
899
+ });
900
+ // Operations must be in this order
901
+ expect(data).toEqual([
902
+ // 2
903
+ // The REMOVE is expected because the subkey changes
904
+ {
905
+ op: 'REMOVE',
906
+ object_id: 'test1',
907
+ data: null,
908
+ subkey: '6544e3899293153fa7b38331/740ba9f2-8b0f-53e3-bb17-5f38a9616f0e'
909
+ },
910
+ {
911
+ op: 'PUT',
912
+ object_id: 'test1',
913
+ data: JSON.stringify({ id: 'test1', description: 'test1b' }),
914
+ subkey: '6544e3899293153fa7b38331/500e9b68-a2fd-51ff-9c00-313e2fb9f562'
915
+ },
916
+ // 3
917
+ {
918
+ op: 'REMOVE',
919
+ object_id: 'test1',
920
+ data: null,
921
+ subkey: '6544e3899293153fa7b38331/500e9b68-a2fd-51ff-9c00-313e2fb9f562'
922
+ }
923
+ ]);
924
+ });
925
+ test('unchanged data with replica identity full', async () => {
926
+ const sync_rules = testRules(`
927
+ bucket_definitions:
928
+ global:
929
+ data:
930
+ - SELECT id, description FROM "test"
931
+ `);
932
+ function rid2(id, description) {
933
+ return getUuidReplicaIdentityBson({ id, description }, [
934
+ { name: 'id', type: 'VARCHAR', typeId: 25 },
935
+ { name: 'description', type: 'VARCHAR', typeId: 25 }
936
+ ]);
937
+ }
938
+ const storage = (await factory()).getInstance(sync_rules);
939
+ const sourceTable = makeTestTable('test', ['id', 'description']);
940
+ // Pre-setup
941
+ const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
942
+ await batch.save({
943
+ sourceTable,
944
+ tag: SaveOperationTag.INSERT,
945
+ after: {
946
+ id: 'test1',
947
+ description: 'test1a'
948
+ },
949
+ afterReplicaId: rid2('test1', 'test1a')
950
+ });
951
+ });
952
+ const checkpoint1 = result1?.flushed_op ?? '0';
953
+ const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
954
+ // Unchanged, but has a before id
955
+ await batch.save({
956
+ sourceTable,
957
+ tag: SaveOperationTag.UPDATE,
958
+ before: {
959
+ id: 'test1',
960
+ description: 'test1a'
961
+ },
962
+ beforeReplicaId: rid2('test1', 'test1a'),
963
+ after: {
964
+ id: 'test1',
965
+ description: 'test1a'
966
+ },
967
+ afterReplicaId: rid2('test1', 'test1a')
968
+ });
969
+ });
970
+ const result3 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
971
+ // Delete
972
+ await batch.save({
973
+ sourceTable,
974
+ tag: SaveOperationTag.DELETE,
975
+ before: {
976
+ id: 'test1',
977
+ description: 'test1a'
978
+ },
979
+ beforeReplicaId: rid2('test1', 'test1a'),
980
+ after: undefined
981
+ });
982
+ });
983
+ const checkpoint3 = result3.flushed_op;
984
+ const batch = await fromAsync(storage.getBucketDataBatch(checkpoint3, new Map([['global[]', checkpoint1]])));
985
+ const data = batch[0].batch.data.map((d) => {
986
+ return {
987
+ op: d.op,
988
+ object_id: d.object_id,
989
+ data: d.data,
990
+ subkey: d.subkey
991
+ };
992
+ });
993
+ // Operations must be in this order
994
+ expect(data).toEqual([
995
+ // 2
996
+ {
997
+ op: 'PUT',
998
+ object_id: 'test1',
999
+ data: JSON.stringify({ id: 'test1', description: 'test1a' }),
1000
+ subkey: '6544e3899293153fa7b38331/740ba9f2-8b0f-53e3-bb17-5f38a9616f0e'
1001
+ },
1002
+ // 3
1003
+ {
1004
+ op: 'REMOVE',
1005
+ object_id: 'test1',
1006
+ data: null,
1007
+ subkey: '6544e3899293153fa7b38331/740ba9f2-8b0f-53e3-bb17-5f38a9616f0e'
1008
+ }
1009
+ ]);
1010
+ });
1011
+ test('large batch', async () => {
1012
+ // Test syncing a batch of data that is small in count,
1013
+ // but large enough in size to be split over multiple returned batches.
1014
+ // The specific batch splits is an implementation detail of the storage driver,
1015
+ // and the test will have to updated when other implementations are added.
1016
+ const sync_rules = testRules(`
1017
+ bucket_definitions:
1018
+ global:
1019
+ data:
1020
+ - SELECT id, description FROM "%"
1021
+ `);
1022
+ const storage = (await factory()).getInstance(sync_rules);
1023
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1024
+ const sourceTable = TEST_TABLE;
1025
+ const largeDescription = '0123456789'.repeat(1200000);
1026
+ await batch.save({
1027
+ sourceTable,
1028
+ tag: SaveOperationTag.INSERT,
1029
+ after: {
1030
+ id: 'test1',
1031
+ description: 'test1'
1032
+ },
1033
+ afterReplicaId: rid('test1')
1034
+ });
1035
+ await batch.save({
1036
+ sourceTable,
1037
+ tag: SaveOperationTag.INSERT,
1038
+ after: {
1039
+ id: 'large1',
1040
+ description: largeDescription
1041
+ },
1042
+ afterReplicaId: rid('large1')
1043
+ });
1044
+ // Large enough to split the returned batch
1045
+ await batch.save({
1046
+ sourceTable,
1047
+ tag: SaveOperationTag.INSERT,
1048
+ after: {
1049
+ id: 'large2',
1050
+ description: largeDescription
1051
+ },
1052
+ afterReplicaId: rid('large2')
1053
+ });
1054
+ await batch.save({
1055
+ sourceTable,
1056
+ tag: SaveOperationTag.INSERT,
1057
+ after: {
1058
+ id: 'test3',
1059
+ description: 'test3'
1060
+ },
1061
+ afterReplicaId: rid('test3')
1062
+ });
1063
+ });
1064
+ const checkpoint = result.flushed_op;
1065
+ const options = {
1066
+ chunkLimitBytes: 16 * 1024 * 1024
1067
+ };
1068
+ const batch1 = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']]), options));
1069
+ expect(getBatchData(batch1)).toEqual([
1070
+ { op_id: '1', op: 'PUT', object_id: 'test1', checksum: 2871785649 },
1071
+ { op_id: '2', op: 'PUT', object_id: 'large1', checksum: 454746904 }
1072
+ ]);
1073
+ expect(getBatchMeta(batch1)).toEqual({
1074
+ after: '0',
1075
+ has_more: true,
1076
+ next_after: '2'
1077
+ });
1078
+ const batch2 = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch1[0].batch.next_after]]), options));
1079
+ expect(getBatchData(batch2)).toEqual([
1080
+ { op_id: '3', op: 'PUT', object_id: 'large2', checksum: 1795508474 },
1081
+ { op_id: '4', op: 'PUT', object_id: 'test3', checksum: 1359888332 }
1082
+ ]);
1083
+ expect(getBatchMeta(batch2)).toEqual({
1084
+ after: '2',
1085
+ has_more: false,
1086
+ next_after: '4'
1087
+ });
1088
+ const batch3 = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch2[0].batch.next_after]]), options));
1089
+ expect(getBatchData(batch3)).toEqual([]);
1090
+ expect(getBatchMeta(batch3)).toEqual(null);
1091
+ });
1092
+ test('large batch (2)', async () => {
1093
+ // Test syncing a batch of data that is small in count,
1094
+ // but large enough in size to be split over multiple returned chunks.
1095
+ // Similar to the above test, but splits over 1MB chunks.
1096
+ const sync_rules = testRules(`
1097
+ bucket_definitions:
1098
+ global:
1099
+ data:
1100
+ - SELECT id, description FROM "%"
1101
+ `);
1102
+ const storage = (await factory()).getInstance(sync_rules);
1103
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1104
+ const sourceTable = TEST_TABLE;
1105
+ const largeDescription = '0123456789'.repeat(200000);
1106
+ await batch.save({
1107
+ sourceTable,
1108
+ tag: SaveOperationTag.INSERT,
1109
+ after: {
1110
+ id: 'test1',
1111
+ description: 'test1'
1112
+ },
1113
+ afterReplicaId: rid('test1')
1114
+ });
1115
+ await batch.save({
1116
+ sourceTable,
1117
+ tag: SaveOperationTag.INSERT,
1118
+ after: {
1119
+ id: 'large1',
1120
+ description: largeDescription
1121
+ },
1122
+ afterReplicaId: rid('large1')
1123
+ });
1124
+ // Large enough to split the returned batch
1125
+ await batch.save({
1126
+ sourceTable,
1127
+ tag: SaveOperationTag.INSERT,
1128
+ after: {
1129
+ id: 'large2',
1130
+ description: largeDescription
1131
+ },
1132
+ afterReplicaId: rid('large2')
1133
+ });
1134
+ await batch.save({
1135
+ sourceTable,
1136
+ tag: SaveOperationTag.INSERT,
1137
+ after: {
1138
+ id: 'test3',
1139
+ description: 'test3'
1140
+ },
1141
+ afterReplicaId: rid('test3')
1142
+ });
1143
+ });
1144
+ const checkpoint = result.flushed_op;
1145
+ const options = {};
1146
+ const batch1 = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']]), options));
1147
+ expect(getBatchData(batch1)).toEqual([
1148
+ { op_id: '1', op: 'PUT', object_id: 'test1', checksum: 2871785649 },
1149
+ { op_id: '2', op: 'PUT', object_id: 'large1', checksum: 1178768505 }
1150
+ ]);
1151
+ expect(getBatchMeta(batch1)).toEqual({
1152
+ after: '0',
1153
+ has_more: true,
1154
+ next_after: '2'
1155
+ });
1156
+ const batch2 = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch1[0].batch.next_after]]), options));
1157
+ expect(getBatchData(batch2)).toEqual([{ op_id: '3', op: 'PUT', object_id: 'large2', checksum: 1607205872 }]);
1158
+ expect(getBatchMeta(batch2)).toEqual({
1159
+ after: '2',
1160
+ has_more: true,
1161
+ next_after: '3'
1162
+ });
1163
+ const batch3 = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch2[0].batch.next_after]]), options));
1164
+ expect(getBatchData(batch3)).toEqual([{ op_id: '4', op: 'PUT', object_id: 'test3', checksum: 1359888332 }]);
1165
+ expect(getBatchMeta(batch3)).toEqual({
1166
+ after: '3',
1167
+ has_more: false,
1168
+ next_after: '4'
1169
+ });
1170
+ });
1171
+ test('long batch', async () => {
1172
+ // Test syncing a batch of data that is limited by count.
1173
+ const sync_rules = testRules(`
1174
+ bucket_definitions:
1175
+ global:
1176
+ data:
1177
+ - SELECT id, description FROM "%"
1178
+ `);
1179
+ const storage = (await factory()).getInstance(sync_rules);
1180
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1181
+ const sourceTable = TEST_TABLE;
1182
+ for (let i = 1; i <= 6; i++) {
1183
+ await batch.save({
1184
+ sourceTable,
1185
+ tag: SaveOperationTag.INSERT,
1186
+ after: {
1187
+ id: `test${i}`,
1188
+ description: `test${i}`
1189
+ },
1190
+ afterReplicaId: `test${i}`
1191
+ });
1192
+ }
1193
+ });
1194
+ const checkpoint = result.flushed_op;
1195
+ const batch1 = await oneFromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']]), { limit: 4 }));
1196
+ expect(getBatchData(batch1)).toEqual([
1197
+ { op_id: '1', op: 'PUT', object_id: 'test1', checksum: 2871785649 },
1198
+ { op_id: '2', op: 'PUT', object_id: 'test2', checksum: 730027011 },
1199
+ { op_id: '3', op: 'PUT', object_id: 'test3', checksum: 1359888332 },
1200
+ { op_id: '4', op: 'PUT', object_id: 'test4', checksum: 2049153252 }
1201
+ ]);
1202
+ expect(getBatchMeta(batch1)).toEqual({
1203
+ after: '0',
1204
+ has_more: true,
1205
+ next_after: '4'
1206
+ });
1207
+ const batch2 = await oneFromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch1.batch.next_after]]), {
1208
+ limit: 4
1209
+ }));
1210
+ expect(getBatchData(batch2)).toEqual([
1211
+ { op_id: '5', op: 'PUT', object_id: 'test5', checksum: 3686902721 },
1212
+ { op_id: '6', op: 'PUT', object_id: 'test6', checksum: 1974820016 }
1213
+ ]);
1214
+ expect(getBatchMeta(batch2)).toEqual({
1215
+ after: '4',
1216
+ has_more: false,
1217
+ next_after: '6'
1218
+ });
1219
+ const batch3 = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch2.batch.next_after]]), {
1220
+ limit: 4
1221
+ }));
1222
+ expect(getBatchData(batch3)).toEqual([]);
1223
+ expect(getBatchMeta(batch3)).toEqual(null);
1224
+ });
1225
+ test('batch should be disposed automatically', async () => {
1226
+ const sync_rules = testRules(`
1227
+ bucket_definitions:
1228
+ global:
1229
+ data: []
1230
+ `);
1231
+ const storage = (await factory()).getInstance(sync_rules);
1232
+ let isDisposed = false;
1233
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1234
+ batch.registerListener({
1235
+ disposed: () => {
1236
+ isDisposed = true;
1237
+ }
1238
+ });
1239
+ });
1240
+ expect(isDisposed).true;
1241
+ isDisposed = false;
1242
+ let errorCaught = false;
1243
+ try {
1244
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1245
+ batch.registerListener({
1246
+ disposed: () => {
1247
+ isDisposed = true;
1248
+ }
1249
+ });
1250
+ throw new Error(`Testing exceptions`);
1251
+ });
1252
+ }
1253
+ catch (ex) {
1254
+ errorCaught = true;
1255
+ expect(ex.message.includes('Testing')).true;
1256
+ }
1257
+ expect(errorCaught).true;
1258
+ expect(isDisposed).true;
1259
+ });
1260
+ test('empty storage metrics', async () => {
1261
+ const f = await factory({ dropAll: true });
1262
+ const metrics = await f.getStorageMetrics();
1263
+ expect(metrics).toEqual({
1264
+ operations_size_bytes: 0,
1265
+ parameters_size_bytes: 0,
1266
+ replication_size_bytes: 0
1267
+ });
1268
+ const r = await f.configureSyncRules('bucket_definitions: {}');
1269
+ const storage = f.getInstance(r.persisted_sync_rules);
1270
+ await storage.autoActivate();
1271
+ const metrics2 = await f.getStorageMetrics();
1272
+ expect(metrics2).toEqual({
1273
+ operations_size_bytes: 0,
1274
+ parameters_size_bytes: 0,
1275
+ replication_size_bytes: 0
1276
+ });
1277
+ });
1278
+ test('invalidate cached parsed sync rules', async () => {
1279
+ const sync_rules_content = testRules(`
1280
+ bucket_definitions:
1281
+ by_workspace:
1282
+ parameters:
1283
+ - SELECT id as workspace_id FROM workspace WHERE
1284
+ workspace."userId" = token_parameters.user_id
1285
+ data: []
1286
+ `);
1287
+ const bucketStorageFactory = await factory();
1288
+ const syncBucketStorage = bucketStorageFactory.getInstance(sync_rules_content);
1289
+ const parsedSchema1 = syncBucketStorage.getParsedSyncRules({
1290
+ defaultSchema: 'public'
1291
+ });
1292
+ const parsedSchema2 = syncBucketStorage.getParsedSyncRules({
1293
+ defaultSchema: 'public'
1294
+ });
1295
+ // These should be cached, this will be the same instance
1296
+ expect(parsedSchema2).equals(parsedSchema1);
1297
+ expect(parsedSchema1.getSourceTables()[0].schema).equals('public');
1298
+ const parsedSchema3 = syncBucketStorage.getParsedSyncRules({
1299
+ defaultSchema: 'databasename'
1300
+ });
1301
+ // The cache should not be used
1302
+ expect(parsedSchema3).not.equals(parsedSchema2);
1303
+ expect(parsedSchema3.getSourceTables()[0].schema).equals('databasename');
1304
+ });
1305
+ }
1306
+ //# sourceMappingURL=data_storage.test.js.map