@powersync/service-core 0.0.0-dev-20241007145127 → 0.0.0-dev-20241015210820

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 (112) hide show
  1. package/CHANGELOG.md +9 -5
  2. package/dist/api/RouteAPI.d.ts +6 -4
  3. package/dist/api/diagnostics.js +169 -105
  4. package/dist/api/diagnostics.js.map +1 -1
  5. package/dist/api/schema.js +2 -2
  6. package/dist/api/schema.js.map +1 -1
  7. package/dist/entry/commands/compact-action.js +73 -9
  8. package/dist/entry/commands/compact-action.js.map +1 -1
  9. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +3 -0
  10. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +31 -0
  11. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +1 -0
  12. package/dist/replication/AbstractReplicationJob.d.ts +1 -1
  13. package/dist/replication/AbstractReplicationJob.js +2 -2
  14. package/dist/replication/AbstractReplicationJob.js.map +1 -1
  15. package/dist/replication/AbstractReplicator.d.ts +2 -2
  16. package/dist/replication/AbstractReplicator.js +66 -3
  17. package/dist/replication/AbstractReplicator.js.map +1 -1
  18. package/dist/replication/ReplicationEngine.js.map +1 -1
  19. package/dist/replication/ReplicationModule.js +3 -0
  20. package/dist/replication/ReplicationModule.js.map +1 -1
  21. package/dist/replication/replication-index.d.ts +1 -1
  22. package/dist/replication/replication-index.js +1 -1
  23. package/dist/replication/replication-index.js.map +1 -1
  24. package/dist/routes/configure-fastify.js +12 -12
  25. package/dist/routes/configure-fastify.js.map +1 -1
  26. package/dist/routes/configure-rsocket.js +4 -1
  27. package/dist/routes/configure-rsocket.js.map +1 -1
  28. package/dist/routes/endpoints/admin.js.map +1 -1
  29. package/dist/routes/endpoints/checkpointing.js +5 -2
  30. package/dist/routes/endpoints/checkpointing.js.map +1 -1
  31. package/dist/routes/endpoints/sync-rules.js.map +1 -1
  32. package/dist/routes/router.d.ts +8 -1
  33. package/dist/routes/router.js.map +1 -1
  34. package/dist/runner/teardown.js +66 -4
  35. package/dist/runner/teardown.js.map +1 -1
  36. package/dist/storage/BucketStorage.d.ts +41 -18
  37. package/dist/storage/BucketStorage.js +6 -0
  38. package/dist/storage/BucketStorage.js.map +1 -1
  39. package/dist/storage/MongoBucketStorage.d.ts +12 -5
  40. package/dist/storage/MongoBucketStorage.js +44 -23
  41. package/dist/storage/MongoBucketStorage.js.map +1 -1
  42. package/dist/storage/ReplicationEventPayload.d.ts +14 -0
  43. package/dist/storage/ReplicationEventPayload.js +2 -0
  44. package/dist/storage/ReplicationEventPayload.js.map +1 -0
  45. package/dist/storage/SourceTable.d.ts +8 -0
  46. package/dist/storage/SourceTable.js +9 -1
  47. package/dist/storage/SourceTable.js.map +1 -1
  48. package/dist/storage/StorageEngine.d.ts +10 -2
  49. package/dist/storage/StorageEngine.js +23 -3
  50. package/dist/storage/StorageEngine.js.map +1 -1
  51. package/dist/storage/StorageProvider.d.ts +9 -2
  52. package/dist/storage/mongo/MongoBucketBatch.d.ts +12 -4
  53. package/dist/storage/mongo/MongoBucketBatch.js +60 -21
  54. package/dist/storage/mongo/MongoBucketBatch.js.map +1 -1
  55. package/dist/storage/mongo/MongoStorageProvider.d.ts +1 -1
  56. package/dist/storage/mongo/MongoStorageProvider.js +3 -2
  57. package/dist/storage/mongo/MongoStorageProvider.js.map +1 -1
  58. package/dist/storage/mongo/MongoSyncBucketStorage.d.ts +4 -5
  59. package/dist/storage/mongo/MongoSyncBucketStorage.js +74 -12
  60. package/dist/storage/mongo/MongoSyncBucketStorage.js.map +1 -1
  61. package/dist/storage/mongo/MongoWriteCheckpointAPI.d.ts +18 -0
  62. package/dist/storage/mongo/MongoWriteCheckpointAPI.js +90 -0
  63. package/dist/storage/mongo/MongoWriteCheckpointAPI.js.map +1 -0
  64. package/dist/storage/mongo/db.d.ts +3 -2
  65. package/dist/storage/mongo/db.js +1 -0
  66. package/dist/storage/mongo/db.js.map +1 -1
  67. package/dist/storage/mongo/models.d.ts +7 -1
  68. package/dist/storage/storage-index.d.ts +2 -0
  69. package/dist/storage/storage-index.js +2 -0
  70. package/dist/storage/storage-index.js.map +1 -1
  71. package/dist/storage/write-checkpoint.d.ts +55 -0
  72. package/dist/storage/write-checkpoint.js +16 -0
  73. package/dist/storage/write-checkpoint.js.map +1 -0
  74. package/dist/util/protocol-types.d.ts +2 -1
  75. package/package.json +5 -5
  76. package/src/api/RouteAPI.ts +7 -4
  77. package/src/api/diagnostics.ts +4 -2
  78. package/src/api/schema.ts +3 -3
  79. package/src/entry/commands/compact-action.ts +4 -2
  80. package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +37 -0
  81. package/src/replication/AbstractReplicationJob.ts +4 -4
  82. package/src/replication/AbstractReplicator.ts +5 -4
  83. package/src/replication/ReplicationEngine.ts +1 -1
  84. package/src/replication/ReplicationModule.ts +4 -0
  85. package/src/replication/replication-index.ts +1 -1
  86. package/src/routes/configure-fastify.ts +16 -17
  87. package/src/routes/configure-rsocket.ts +7 -2
  88. package/src/routes/endpoints/admin.ts +2 -2
  89. package/src/routes/endpoints/checkpointing.ts +5 -2
  90. package/src/routes/endpoints/sync-rules.ts +1 -0
  91. package/src/routes/router.ts +7 -1
  92. package/src/runner/teardown.ts +3 -3
  93. package/src/storage/BucketStorage.ts +50 -19
  94. package/src/storage/MongoBucketStorage.ts +70 -29
  95. package/src/storage/ReplicationEventPayload.ts +16 -0
  96. package/src/storage/SourceTable.ts +10 -1
  97. package/src/storage/StorageEngine.ts +34 -5
  98. package/src/storage/StorageProvider.ts +10 -2
  99. package/src/storage/mongo/MongoBucketBatch.ts +83 -27
  100. package/src/storage/mongo/MongoStorageProvider.ts +4 -3
  101. package/src/storage/mongo/MongoSyncBucketStorage.ts +22 -18
  102. package/src/storage/mongo/MongoWriteCheckpointAPI.ts +136 -0
  103. package/src/storage/mongo/db.ts +4 -1
  104. package/src/storage/mongo/models.ts +8 -1
  105. package/src/storage/storage-index.ts +2 -0
  106. package/src/storage/write-checkpoint.ts +67 -0
  107. package/src/util/protocol-types.ts +1 -1
  108. package/test/src/compacting.test.ts +13 -15
  109. package/test/src/data_storage.test.ts +95 -63
  110. package/test/src/sync.test.ts +10 -9
  111. package/test/src/util.ts +1 -2
  112. package/tsconfig.tsbuildinfo +1 -1
@@ -1,10 +1,5 @@
1
- import {
2
- BucketDataBatchOptions,
3
- ParseSyncRulesOptions,
4
- PersistedSyncRulesContent,
5
- StartBatchOptions
6
- } from '@/storage/BucketStorage.js';
7
- import { RequestParameters, SqlSyncRules } from '@powersync/service-sync-rules';
1
+ import { BucketDataBatchOptions, SaveOperationTag } from '@/storage/BucketStorage.js';
2
+ import { RequestParameters } from '@powersync/service-sync-rules';
8
3
  import { describe, expect, test } from 'vitest';
9
4
  import { fromAsync, oneFromAsync } from './stream_utils.js';
10
5
  import {
@@ -16,8 +11,7 @@ import {
16
11
  PARSE_OPTIONS,
17
12
  rid,
18
13
  StorageFactory,
19
- testRules,
20
- ZERO_LSN
14
+ testRules
21
15
  } from './util.js';
22
16
  import { getUuidReplicaIdentityBson } from '@/util/util-index.js';
23
17
 
@@ -42,7 +36,7 @@ bucket_definitions:
42
36
  const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
43
37
  await batch.save({
44
38
  sourceTable: TEST_TABLE,
45
- tag: 'insert',
39
+ tag: SaveOperationTag.INSERT,
46
40
  after: {
47
41
  id: 't2',
48
42
  id1: 'user3',
@@ -54,7 +48,7 @@ bucket_definitions:
54
48
 
55
49
  await batch.save({
56
50
  sourceTable: TEST_TABLE,
57
- tag: 'insert',
51
+ tag: SaveOperationTag.INSERT,
58
52
  after: {
59
53
  id: 't1',
60
54
  id1: 'user1',
@@ -89,7 +83,7 @@ bucket_definitions:
89
83
  const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
90
84
  await batch.save({
91
85
  sourceTable: TEST_TABLE,
92
- tag: 'insert',
86
+ tag: SaveOperationTag.INSERT,
93
87
  after: {
94
88
  id: 'user1',
95
89
  group_id: 'group1'
@@ -100,7 +94,7 @@ bucket_definitions:
100
94
  const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
101
95
  await batch.save({
102
96
  sourceTable: TEST_TABLE,
103
- tag: 'insert',
97
+ tag: SaveOperationTag.INSERT,
104
98
  after: {
105
99
  id: 'user1',
106
100
  group_id: 'group2'
@@ -141,7 +135,7 @@ bucket_definitions:
141
135
  const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
142
136
  await batch.save({
143
137
  sourceTable: TEST_TABLE,
144
- tag: 'insert',
138
+ tag: SaveOperationTag.INSERT,
145
139
  after: {
146
140
  id: 't1',
147
141
  group_id: 'group1',
@@ -185,7 +179,7 @@ bucket_definitions:
185
179
  const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
186
180
  await batch.save({
187
181
  sourceTable: TEST_TABLE,
188
- tag: 'insert',
182
+ tag: SaveOperationTag.INSERT,
189
183
  after: {
190
184
  id: 't1',
191
185
  group_id: 'group1',
@@ -196,7 +190,7 @@ bucket_definitions:
196
190
 
197
191
  await batch.save({
198
192
  sourceTable: TEST_TABLE,
199
- tag: 'update',
193
+ tag: SaveOperationTag.UPDATE,
200
194
  after: {
201
195
  id: 't1',
202
196
  group_id: 'group1',
@@ -232,7 +226,7 @@ bucket_definitions:
232
226
 
233
227
  await batch.save({
234
228
  sourceTable,
235
- tag: 'insert',
229
+ tag: SaveOperationTag.INSERT,
236
230
  after: {
237
231
  id: 'test1',
238
232
  description: 'test1'
@@ -241,7 +235,7 @@ bucket_definitions:
241
235
  });
242
236
  await batch.save({
243
237
  sourceTable,
244
- tag: 'delete',
238
+ tag: SaveOperationTag.DELETE,
245
239
  beforeReplicaId: rid('test1')
246
240
  });
247
241
  });
@@ -295,7 +289,7 @@ bucket_definitions:
295
289
  const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
296
290
  await batch.save({
297
291
  sourceTable: WORKSPACE_TABLE,
298
- tag: 'insert',
292
+ tag: SaveOperationTag.INSERT,
299
293
  after: {
300
294
  id: 'workspace1',
301
295
  userId: 'u1'
@@ -345,7 +339,7 @@ bucket_definitions:
345
339
  const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
346
340
  await batch.save({
347
341
  sourceTable: WORKSPACE_TABLE,
348
- tag: 'insert',
342
+ tag: SaveOperationTag.INSERT,
349
343
  after: {
350
344
  id: 'workspace1',
351
345
  visibility: 'public'
@@ -355,7 +349,7 @@ bucket_definitions:
355
349
 
356
350
  await batch.save({
357
351
  sourceTable: WORKSPACE_TABLE,
358
- tag: 'insert',
352
+ tag: SaveOperationTag.INSERT,
359
353
  after: {
360
354
  id: 'workspace2',
361
355
  visibility: 'private'
@@ -365,7 +359,7 @@ bucket_definitions:
365
359
 
366
360
  await batch.save({
367
361
  sourceTable: WORKSPACE_TABLE,
368
- tag: 'insert',
362
+ tag: SaveOperationTag.INSERT,
369
363
  after: {
370
364
  id: 'workspace3',
371
365
  visibility: 'public'
@@ -419,7 +413,7 @@ bucket_definitions:
419
413
  const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
420
414
  await batch.save({
421
415
  sourceTable: WORKSPACE_TABLE,
422
- tag: 'insert',
416
+ tag: SaveOperationTag.INSERT,
423
417
  after: {
424
418
  id: 'workspace1',
425
419
  visibility: 'public'
@@ -429,7 +423,7 @@ bucket_definitions:
429
423
 
430
424
  await batch.save({
431
425
  sourceTable: WORKSPACE_TABLE,
432
- tag: 'insert',
426
+ tag: SaveOperationTag.INSERT,
433
427
  after: {
434
428
  id: 'workspace2',
435
429
  visibility: 'private'
@@ -439,7 +433,7 @@ bucket_definitions:
439
433
 
440
434
  await batch.save({
441
435
  sourceTable: WORKSPACE_TABLE,
442
- tag: 'insert',
436
+ tag: SaveOperationTag.INSERT,
443
437
  after: {
444
438
  id: 'workspace3',
445
439
  user_id: 'u1',
@@ -450,7 +444,7 @@ bucket_definitions:
450
444
 
451
445
  await batch.save({
452
446
  sourceTable: WORKSPACE_TABLE,
453
- tag: 'insert',
447
+ tag: SaveOperationTag.INSERT,
454
448
  after: {
455
449
  id: 'workspace4',
456
450
  user_id: 'u2',
@@ -507,7 +501,7 @@ bucket_definitions:
507
501
  const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
508
502
  await batch.save({
509
503
  sourceTable,
510
- tag: 'insert',
504
+ tag: SaveOperationTag.INSERT,
511
505
  after: {
512
506
  id: 'test1',
513
507
  client_id: 'client1a',
@@ -517,7 +511,7 @@ bucket_definitions:
517
511
  });
518
512
  await batch.save({
519
513
  sourceTable,
520
- tag: 'update',
514
+ tag: SaveOperationTag.UPDATE,
521
515
  after: {
522
516
  id: 'test1',
523
517
  client_id: 'client1b',
@@ -528,7 +522,7 @@ bucket_definitions:
528
522
 
529
523
  await batch.save({
530
524
  sourceTable,
531
- tag: 'insert',
525
+ tag: SaveOperationTag.INSERT,
532
526
  after: {
533
527
  id: 'test2',
534
528
  client_id: 'client2',
@@ -570,7 +564,7 @@ bucket_definitions:
570
564
 
571
565
  await batch.save({
572
566
  sourceTable,
573
- tag: 'insert',
567
+ tag: SaveOperationTag.INSERT,
574
568
  after: {
575
569
  id: 'test1',
576
570
  description: 'test1'
@@ -584,7 +578,7 @@ bucket_definitions:
584
578
 
585
579
  await batch.save({
586
580
  sourceTable,
587
- tag: 'delete',
581
+ tag: SaveOperationTag.DELETE,
588
582
  beforeReplicaId: rid('test1')
589
583
  });
590
584
  });
@@ -594,7 +588,7 @@ bucket_definitions:
594
588
 
595
589
  await batch.save({
596
590
  sourceTable,
597
- tag: 'delete',
591
+ tag: SaveOperationTag.DELETE,
598
592
  beforeReplicaId: rid('test1')
599
593
  });
600
594
  });
@@ -644,7 +638,7 @@ bucket_definitions:
644
638
 
645
639
  await batch.save({
646
640
  sourceTable,
647
- tag: 'insert',
641
+ tag: SaveOperationTag.INSERT,
648
642
  after: {
649
643
  id: 'test1',
650
644
  description: 'test1'
@@ -658,7 +652,7 @@ bucket_definitions:
658
652
 
659
653
  await batch.save({
660
654
  sourceTable,
661
- tag: 'update',
655
+ tag: SaveOperationTag.UPDATE,
662
656
  after: {
663
657
  id: 'test1',
664
658
  description: undefined
@@ -668,7 +662,7 @@ bucket_definitions:
668
662
 
669
663
  await batch.save({
670
664
  sourceTable,
671
- tag: 'update',
665
+ tag: SaveOperationTag.UPDATE,
672
666
  after: {
673
667
  id: 'test1',
674
668
  description: undefined
@@ -678,7 +672,7 @@ bucket_definitions:
678
672
 
679
673
  await batch.save({
680
674
  sourceTable,
681
- tag: 'delete',
675
+ tag: SaveOperationTag.DELETE,
682
676
  beforeReplicaId: rid('test1')
683
677
  });
684
678
  });
@@ -688,7 +682,7 @@ bucket_definitions:
688
682
 
689
683
  await batch.save({
690
684
  sourceTable,
691
- tag: 'update',
685
+ tag: SaveOperationTag.UPDATE,
692
686
  after: {
693
687
  id: 'test1',
694
688
  description: undefined
@@ -698,7 +692,7 @@ bucket_definitions:
698
692
 
699
693
  await batch.save({
700
694
  sourceTable,
701
- tag: 'update',
695
+ tag: SaveOperationTag.UPDATE,
702
696
  after: {
703
697
  id: 'test1',
704
698
  description: undefined
@@ -708,7 +702,7 @@ bucket_definitions:
708
702
 
709
703
  await batch.save({
710
704
  sourceTable,
711
- tag: 'delete',
705
+ tag: SaveOperationTag.DELETE,
712
706
  beforeReplicaId: rid('test1')
713
707
  });
714
708
  });
@@ -761,7 +755,7 @@ bucket_definitions:
761
755
  await storage.startBatch(BATCH_OPTIONS, async (batch) => {
762
756
  await batch.save({
763
757
  sourceTable: TEST_TABLE,
764
- tag: 'insert',
758
+ tag: SaveOperationTag.INSERT,
765
759
  after: {
766
760
  id: 't2',
767
761
  id1: 'user3',
@@ -805,7 +799,7 @@ bucket_definitions:
805
799
 
806
800
  await batch.save({
807
801
  sourceTable,
808
- tag: 'insert',
802
+ tag: SaveOperationTag.INSERT,
809
803
  after: {
810
804
  id: 'test1',
811
805
  description: 'test1a'
@@ -815,7 +809,7 @@ bucket_definitions:
815
809
 
816
810
  await batch.save({
817
811
  sourceTable,
818
- tag: 'insert',
812
+ tag: SaveOperationTag.INSERT,
819
813
  after: {
820
814
  id: 'test2',
821
815
  description: 'test2a'
@@ -832,7 +826,7 @@ bucket_definitions:
832
826
  // b
833
827
  await batch.save({
834
828
  sourceTable,
835
- tag: 'insert',
829
+ tag: SaveOperationTag.INSERT,
836
830
  after: {
837
831
  id: 'test1',
838
832
  description: 'test1b'
@@ -842,7 +836,7 @@ bucket_definitions:
842
836
 
843
837
  await batch.save({
844
838
  sourceTable,
845
- tag: 'update',
839
+ tag: SaveOperationTag.UPDATE,
846
840
  before: {
847
841
  id: 'test1'
848
842
  },
@@ -856,7 +850,7 @@ bucket_definitions:
856
850
 
857
851
  await batch.save({
858
852
  sourceTable,
859
- tag: 'update',
853
+ tag: SaveOperationTag.UPDATE,
860
854
  before: {
861
855
  id: 'test2'
862
856
  },
@@ -872,7 +866,7 @@ bucket_definitions:
872
866
  // c
873
867
  await batch.save({
874
868
  sourceTable,
875
- tag: 'update',
869
+ tag: SaveOperationTag.UPDATE,
876
870
  after: {
877
871
  id: 'test2',
878
872
  description: 'test2c'
@@ -883,7 +877,7 @@ bucket_definitions:
883
877
  // d
884
878
  await batch.save({
885
879
  sourceTable,
886
- tag: 'insert',
880
+ tag: SaveOperationTag.INSERT,
887
881
  after: {
888
882
  id: 'test4',
889
883
  description: 'test4d'
@@ -893,7 +887,7 @@ bucket_definitions:
893
887
 
894
888
  await batch.save({
895
889
  sourceTable,
896
- tag: 'update',
890
+ tag: SaveOperationTag.UPDATE,
897
891
  before: {
898
892
  id: 'test4'
899
893
  },
@@ -959,7 +953,7 @@ bucket_definitions:
959
953
  const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
960
954
  await batch.save({
961
955
  sourceTable,
962
- tag: 'insert',
956
+ tag: SaveOperationTag.INSERT,
963
957
  after: {
964
958
  id: 'test1',
965
959
  description: 'test1a'
@@ -974,7 +968,7 @@ bucket_definitions:
974
968
  // Unchanged, but has a before id
975
969
  await batch.save({
976
970
  sourceTable,
977
- tag: 'update',
971
+ tag: SaveOperationTag.UPDATE,
978
972
  before: {
979
973
  id: 'test1',
980
974
  description: 'test1a'
@@ -992,7 +986,7 @@ bucket_definitions:
992
986
  // Delete
993
987
  await batch.save({
994
988
  sourceTable,
995
- tag: 'delete',
989
+ tag: SaveOperationTag.DELETE,
996
990
  before: {
997
991
  id: 'test1',
998
992
  description: 'test1b'
@@ -1064,7 +1058,7 @@ bucket_definitions:
1064
1058
  const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1065
1059
  await batch.save({
1066
1060
  sourceTable,
1067
- tag: 'insert',
1061
+ tag: SaveOperationTag.INSERT,
1068
1062
  after: {
1069
1063
  id: 'test1',
1070
1064
  description: 'test1a'
@@ -1079,7 +1073,7 @@ bucket_definitions:
1079
1073
  // Unchanged, but has a before id
1080
1074
  await batch.save({
1081
1075
  sourceTable,
1082
- tag: 'update',
1076
+ tag: SaveOperationTag.UPDATE,
1083
1077
  before: {
1084
1078
  id: 'test1',
1085
1079
  description: 'test1a'
@@ -1097,7 +1091,7 @@ bucket_definitions:
1097
1091
  // Delete
1098
1092
  await batch.save({
1099
1093
  sourceTable,
1100
- tag: 'delete',
1094
+ tag: SaveOperationTag.DELETE,
1101
1095
  before: {
1102
1096
  id: 'test1',
1103
1097
  description: 'test1a'
@@ -1160,7 +1154,7 @@ bucket_definitions:
1160
1154
 
1161
1155
  await batch.save({
1162
1156
  sourceTable,
1163
- tag: 'insert',
1157
+ tag: SaveOperationTag.INSERT,
1164
1158
  after: {
1165
1159
  id: 'test1',
1166
1160
  description: 'test1'
@@ -1170,7 +1164,7 @@ bucket_definitions:
1170
1164
 
1171
1165
  await batch.save({
1172
1166
  sourceTable,
1173
- tag: 'insert',
1167
+ tag: SaveOperationTag.INSERT,
1174
1168
  after: {
1175
1169
  id: 'large1',
1176
1170
  description: largeDescription
@@ -1181,7 +1175,7 @@ bucket_definitions:
1181
1175
  // Large enough to split the returned batch
1182
1176
  await batch.save({
1183
1177
  sourceTable,
1184
- tag: 'insert',
1178
+ tag: SaveOperationTag.INSERT,
1185
1179
  after: {
1186
1180
  id: 'large2',
1187
1181
  description: largeDescription
@@ -1191,7 +1185,7 @@ bucket_definitions:
1191
1185
 
1192
1186
  await batch.save({
1193
1187
  sourceTable,
1194
- tag: 'insert',
1188
+ tag: SaveOperationTag.INSERT,
1195
1189
  after: {
1196
1190
  id: 'test3',
1197
1191
  description: 'test3'
@@ -1258,7 +1252,7 @@ bucket_definitions:
1258
1252
 
1259
1253
  await batch.save({
1260
1254
  sourceTable,
1261
- tag: 'insert',
1255
+ tag: SaveOperationTag.INSERT,
1262
1256
  after: {
1263
1257
  id: 'test1',
1264
1258
  description: 'test1'
@@ -1268,7 +1262,7 @@ bucket_definitions:
1268
1262
 
1269
1263
  await batch.save({
1270
1264
  sourceTable,
1271
- tag: 'insert',
1265
+ tag: SaveOperationTag.INSERT,
1272
1266
  after: {
1273
1267
  id: 'large1',
1274
1268
  description: largeDescription
@@ -1279,7 +1273,7 @@ bucket_definitions:
1279
1273
  // Large enough to split the returned batch
1280
1274
  await batch.save({
1281
1275
  sourceTable,
1282
- tag: 'insert',
1276
+ tag: SaveOperationTag.INSERT,
1283
1277
  after: {
1284
1278
  id: 'large2',
1285
1279
  description: largeDescription
@@ -1289,7 +1283,7 @@ bucket_definitions:
1289
1283
 
1290
1284
  await batch.save({
1291
1285
  sourceTable,
1292
- tag: 'insert',
1286
+ tag: SaveOperationTag.INSERT,
1293
1287
  after: {
1294
1288
  id: 'test3',
1295
1289
  description: 'test3'
@@ -1352,7 +1346,7 @@ bucket_definitions:
1352
1346
  for (let i = 1; i <= 6; i++) {
1353
1347
  await batch.save({
1354
1348
  sourceTable,
1355
- tag: 'insert',
1349
+ tag: SaveOperationTag.INSERT,
1356
1350
  after: {
1357
1351
  id: `test${i}`,
1358
1352
  description: `test${i}`
@@ -1406,4 +1400,42 @@ bucket_definitions:
1406
1400
 
1407
1401
  expect(getBatchMeta(batch3)).toEqual(null);
1408
1402
  });
1403
+
1404
+ test('batch should be disposed automatically', async () => {
1405
+ const sync_rules = testRules(`
1406
+ bucket_definitions:
1407
+ global:
1408
+ data: []
1409
+ `);
1410
+
1411
+ const storage = (await factory()).getInstance(sync_rules);
1412
+
1413
+ let isDisposed = false;
1414
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1415
+ batch.registerListener({
1416
+ disposed: () => {
1417
+ isDisposed = true;
1418
+ }
1419
+ });
1420
+ });
1421
+ expect(isDisposed).true;
1422
+
1423
+ isDisposed = false;
1424
+ let errorCaught = false;
1425
+ try {
1426
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1427
+ batch.registerListener({
1428
+ disposed: () => {
1429
+ isDisposed = true;
1430
+ }
1431
+ });
1432
+ throw new Error(`Testing exceptions`);
1433
+ });
1434
+ } catch (ex) {
1435
+ errorCaught = true;
1436
+ expect(ex.message.includes('Testing')).true;
1437
+ }
1438
+ expect(errorCaught).true;
1439
+ expect(isDisposed).true;
1440
+ });
1409
1441
  }
@@ -1,3 +1,4 @@
1
+ import { SaveOperationTag } from '@/storage/storage-index.js';
1
2
  import { RequestTracker } from '@/sync/RequestTracker.js';
2
3
  import { streamResponse } from '@/sync/sync.js';
3
4
  import { StreamingSyncLine } from '@/util/protocol-types.js';
@@ -45,7 +46,7 @@ function defineTests(factory: StorageFactory) {
45
46
  const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
46
47
  await batch.save({
47
48
  sourceTable: TEST_TABLE,
48
- tag: 'insert',
49
+ tag: SaveOperationTag.INSERT,
49
50
  after: {
50
51
  id: 't1',
51
52
  description: 'Test 1'
@@ -55,7 +56,7 @@ function defineTests(factory: StorageFactory) {
55
56
 
56
57
  await batch.save({
57
58
  sourceTable: TEST_TABLE,
58
- tag: 'insert',
59
+ tag: SaveOperationTag.INSERT,
59
60
  after: {
60
61
  id: 't2',
61
62
  description: 'Test 2'
@@ -97,7 +98,7 @@ function defineTests(factory: StorageFactory) {
97
98
  const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
98
99
  await batch.save({
99
100
  sourceTable: TEST_TABLE,
100
- tag: 'insert',
101
+ tag: SaveOperationTag.INSERT,
101
102
  after: {
102
103
  id: 't1',
103
104
  description: 'Test\n"string"',
@@ -186,7 +187,7 @@ function defineTests(factory: StorageFactory) {
186
187
  await storage.startBatch(BATCH_OPTIONS, async (batch) => {
187
188
  await batch.save({
188
189
  sourceTable: TEST_TABLE,
189
- tag: 'insert',
190
+ tag: SaveOperationTag.INSERT,
190
191
  after: {
191
192
  id: 't1',
192
193
  description: 'Test 1'
@@ -202,7 +203,7 @@ function defineTests(factory: StorageFactory) {
202
203
  await storage.startBatch(BATCH_OPTIONS, async (batch) => {
203
204
  await batch.save({
204
205
  sourceTable: TEST_TABLE,
205
- tag: 'insert',
206
+ tag: SaveOperationTag.INSERT,
206
207
  after: {
207
208
  id: 't2',
208
209
  description: 'Test 2'
@@ -271,7 +272,7 @@ function defineTests(factory: StorageFactory) {
271
272
  await storage.startBatch(BATCH_OPTIONS, async (batch) => {
272
273
  await batch.save({
273
274
  sourceTable: TEST_TABLE,
274
- tag: 'insert',
275
+ tag: SaveOperationTag.INSERT,
275
276
  after: {
276
277
  id: 't1',
277
278
  description: 'Test 1'
@@ -281,7 +282,7 @@ function defineTests(factory: StorageFactory) {
281
282
 
282
283
  await batch.save({
283
284
  sourceTable: TEST_TABLE,
284
- tag: 'insert',
285
+ tag: SaveOperationTag.INSERT,
285
286
  after: {
286
287
  id: 't2',
287
288
  description: 'Test 2'
@@ -322,7 +323,7 @@ function defineTests(factory: StorageFactory) {
322
323
  await storage.startBatch(BATCH_OPTIONS, async (batch) => {
323
324
  await batch.save({
324
325
  sourceTable: TEST_TABLE,
325
- tag: 'update',
326
+ tag: SaveOperationTag.UPDATE,
326
327
  after: {
327
328
  id: 't1',
328
329
  description: 'Test 1b'
@@ -332,7 +333,7 @@ function defineTests(factory: StorageFactory) {
332
333
 
333
334
  await batch.save({
334
335
  sourceTable: TEST_TABLE,
335
- tag: 'update',
336
+ tag: SaveOperationTag.UPDATE,
336
337
  after: {
337
338
  id: 't2',
338
339
  description: 'Test 2b'
package/test/src/util.ts CHANGED
@@ -11,11 +11,10 @@ import { SourceTable } from '@/storage/SourceTable.js';
11
11
  import { PowerSyncMongo } from '@/storage/mongo/db.js';
12
12
  import { SyncBucketData } from '@/util/protocol-types.js';
13
13
  import { getUuidReplicaIdentityBson, hashData } from '@/util/utils.js';
14
+ import { SqlSyncRules } from '@powersync/service-sync-rules';
14
15
  import * as bson from 'bson';
15
16
  import * as mongo from 'mongodb';
16
17
  import { env } from './env.js';
17
- import { SqlSyncRules } from '@powersync/service-sync-rules';
18
- import { ReplicaId } from '@/storage/storage-index.js';
19
18
 
20
19
  // The metrics need to be initialised before they can be used
21
20
  await Metrics.initialise({