@powersync/service-module-mongodb-storage 0.18.2 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/dist/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.d.ts +3 -0
  3. package/dist/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.js +41 -0
  4. package/dist/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.js.map +1 -0
  5. package/dist/storage/MongoBucketStorage.d.ts +5 -1
  6. package/dist/storage/MongoBucketStorage.js +5 -1
  7. package/dist/storage/MongoBucketStorage.js.map +1 -1
  8. package/dist/storage/implementation/MongoBucketBatch.d.ts +3 -0
  9. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
  10. package/dist/storage/implementation/MongoChecksums.d.ts +2 -0
  11. package/dist/storage/implementation/MongoChecksums.js +3 -2
  12. package/dist/storage/implementation/MongoChecksums.js.map +1 -1
  13. package/dist/storage/implementation/MongoCompactor.d.ts +15 -19
  14. package/dist/storage/implementation/MongoCompactor.js +68 -260
  15. package/dist/storage/implementation/MongoCompactor.js.map +1 -1
  16. package/dist/storage/implementation/MongoStorageProvider.js +26 -3
  17. package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
  18. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +12 -5
  19. package/dist/storage/implementation/MongoSyncBucketStorage.js +25 -14
  20. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  21. package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +2 -1
  22. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +146 -34
  23. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
  24. package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.d.ts +2 -0
  25. package/dist/storage/implementation/common/PersistedBatch.d.ts +6 -0
  26. package/dist/storage/implementation/common/PersistedBatch.js +15 -8
  27. package/dist/storage/implementation/common/PersistedBatch.js.map +1 -1
  28. package/dist/storage/implementation/models.d.ts +16 -0
  29. package/dist/storage/implementation/models.js.map +1 -1
  30. package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +11 -2
  31. package/dist/storage/implementation/v1/MongoCompactorV1.js +250 -1
  32. package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -1
  33. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +20 -11
  34. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -1
  35. package/dist/storage/implementation/v1/SingleBucketStoreV1.d.ts +9 -10
  36. package/dist/storage/implementation/v1/SingleBucketStoreV1.js +2 -8
  37. package/dist/storage/implementation/v1/SingleBucketStoreV1.js.map +1 -1
  38. package/dist/storage/implementation/v3/BucketDataContextV3.d.ts +13 -0
  39. package/dist/storage/implementation/v3/BucketDataContextV3.js +23 -0
  40. package/dist/storage/implementation/v3/BucketDataContextV3.js.map +1 -0
  41. package/dist/storage/implementation/v3/MongoBucketBatchV3.js +3 -1
  42. package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
  43. package/dist/storage/implementation/v3/MongoChecksumsV3.d.ts +1 -0
  44. package/dist/storage/implementation/v3/MongoChecksumsV3.js +59 -103
  45. package/dist/storage/implementation/v3/MongoChecksumsV3.js.map +1 -1
  46. package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +23 -2
  47. package/dist/storage/implementation/v3/MongoCompactorV3.js +354 -219
  48. package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -1
  49. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.d.ts +5 -0
  50. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js +12 -11
  51. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js.map +1 -1
  52. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +5 -3
  53. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +109 -106
  54. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -1
  55. package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +5 -2
  56. package/dist/storage/implementation/v3/PersistedBatchV3.js +53 -15
  57. package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
  58. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +2 -1
  59. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +3 -0
  60. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -1
  61. package/dist/storage/implementation/v3/bucket-format.d.ts +10 -1
  62. package/dist/storage/implementation/v3/bucket-format.js +28 -9
  63. package/dist/storage/implementation/v3/bucket-format.js.map +1 -1
  64. package/dist/storage/implementation/v3/chunking.js +3 -1
  65. package/dist/storage/implementation/v3/chunking.js.map +1 -1
  66. package/dist/storage/implementation/v3/models.d.ts +38 -1
  67. package/dist/storage/implementation/v3/models.js.map +1 -1
  68. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.d.ts +21 -0
  69. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js +44 -0
  70. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js.map +1 -0
  71. package/dist/storage/implementation/v3/object-storage/ObjectStorage.d.ts +30 -0
  72. package/dist/storage/implementation/v3/object-storage/ObjectStorage.js +12 -0
  73. package/dist/storage/implementation/v3/object-storage/ObjectStorage.js.map +1 -0
  74. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.d.ts +70 -0
  75. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js +146 -0
  76. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js.map +1 -0
  77. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.d.ts +42 -0
  78. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js +285 -0
  79. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js.map +1 -0
  80. package/dist/types/types.d.ts +35 -0
  81. package/dist/types/types.js +35 -1
  82. package/dist/types/types.js.map +1 -1
  83. package/dist/utils/test-utils.js +3 -1
  84. package/dist/utils/test-utils.js.map +1 -1
  85. package/dist/utils/util.d.ts +8 -1
  86. package/dist/utils/util.js +125 -2
  87. package/dist/utils/util.js.map +1 -1
  88. package/package.json +10 -7
  89. package/src/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.ts +55 -0
  90. package/src/storage/MongoBucketStorage.ts +10 -2
  91. package/src/storage/implementation/MongoBucketBatch.ts +4 -0
  92. package/src/storage/implementation/MongoChecksums.ts +5 -2
  93. package/src/storage/implementation/MongoCompactor.ts +90 -314
  94. package/src/storage/implementation/MongoStorageProvider.ts +32 -3
  95. package/src/storage/implementation/MongoSyncBucketStorage.ts +34 -24
  96. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +164 -37
  97. package/src/storage/implementation/common/MongoSyncBucketStorageCheckpoint.ts +2 -0
  98. package/src/storage/implementation/common/PersistedBatch.ts +22 -10
  99. package/src/storage/implementation/models.ts +16 -0
  100. package/src/storage/implementation/v1/MongoCompactorV1.ts +292 -8
  101. package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +54 -40
  102. package/src/storage/implementation/v1/SingleBucketStoreV1.ts +18 -27
  103. package/src/storage/implementation/v3/BucketDataContextV3.ts +32 -0
  104. package/src/storage/implementation/v3/MongoBucketBatchV3.ts +3 -1
  105. package/src/storage/implementation/v3/MongoChecksumsV3.ts +75 -105
  106. package/src/storage/implementation/v3/MongoCompactorV3.ts +456 -258
  107. package/src/storage/implementation/v3/MongoStoppedSyncConfigCleanup.ts +21 -19
  108. package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +123 -126
  109. package/src/storage/implementation/v3/PersistedBatchV3.ts +69 -16
  110. package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +5 -0
  111. package/src/storage/implementation/v3/bucket-format.ts +36 -9
  112. package/src/storage/implementation/v3/chunking.ts +3 -1
  113. package/src/storage/implementation/v3/models.ts +40 -1
  114. package/src/storage/implementation/v3/object-storage/BucketDataObjectStorage.ts +54 -0
  115. package/src/storage/implementation/v3/object-storage/ObjectStorage.ts +29 -0
  116. package/src/storage/implementation/v3/object-storage/ObjectStorageLifecycle.ts +171 -0
  117. package/src/storage/implementation/v3/object-storage/S3ObjectStorage.ts +343 -0
  118. package/src/types/types.ts +42 -1
  119. package/src/utils/test-utils.ts +3 -1
  120. package/src/utils/util.ts +182 -4
  121. package/test/src/__snapshots__/storage_sync.test.ts.snap +1 -583
  122. package/test/src/cleanup-stopped-sync-configs.test.ts +24 -1
  123. package/test/src/helpers/MemoryObjectStorage.ts +53 -0
  124. package/test/src/helpers/s3TestFactory.ts +69 -0
  125. package/test/src/object_storage_lifecycle.test.ts +59 -0
  126. package/test/src/setup.ts +6 -1
  127. package/test/src/storage.test.ts +216 -2
  128. package/test/src/storage_compacting.test.ts +176 -187
  129. package/test/src/storage_object_storage_inline_threshold.test.ts +116 -0
  130. package/test/src/storage_s3_checksums.test.ts +191 -0
  131. package/test/src/storage_s3_compaction_lifecycle.test.ts +417 -0
  132. package/test/src/storage_s3_reading.test.ts +526 -0
  133. package/test/src/storage_s3_writing.test.ts +135 -0
  134. package/test/src/storage_sync.test.ts +95 -5
  135. package/tsconfig.tsbuildinfo +1 -1
  136. package/dist/storage/implementation/common/SingleBucketStore.d.ts +0 -54
  137. package/dist/storage/implementation/common/SingleBucketStore.js +0 -3
  138. package/dist/storage/implementation/common/SingleBucketStore.js.map +0 -1
  139. package/dist/storage/implementation/v3/SingleBucketStoreV3.d.ts +0 -18
  140. package/dist/storage/implementation/v3/SingleBucketStoreV3.js +0 -84
  141. package/dist/storage/implementation/v3/SingleBucketStoreV3.js.map +0 -1
  142. package/src/storage/implementation/common/SingleBucketStore.ts +0 -63
  143. package/src/storage/implementation/v3/SingleBucketStoreV3.ts +0 -108
@@ -191,7 +191,7 @@ function registerSyncStorageTests(storageConfig: storage.TestStorageConfig, stor
191
191
  const checkpoint = flushResult!.flushed_op;
192
192
 
193
193
  const options: storage.BucketDataBatchOptions = {};
194
- const batch1 = await test_utils.fromAsync(
194
+ const batch1 = await test_utils.getBatchArray(
195
195
  bucketStorage.getBucketDataBatch(
196
196
  test_utils.testCheckpoint(checkpoint),
197
197
  [bucketRequest(syncRulesContent, 'global[]', 0n)],
@@ -208,7 +208,7 @@ function registerSyncStorageTests(storageConfig: storage.TestStorageConfig, stor
208
208
  next_after: '2'
209
209
  });
210
210
 
211
- const batch2 = await test_utils.fromAsync(
211
+ const batch2 = await test_utils.getBatchArray(
212
212
  bucketStorage.getBucketDataBatch(
213
213
  test_utils.testCheckpoint(checkpoint),
214
214
  [bucketRequest(syncRulesContent, 'global[]', batch1[0].chunkData.next_after)],
@@ -224,7 +224,7 @@ function registerSyncStorageTests(storageConfig: storage.TestStorageConfig, stor
224
224
  next_after: '3'
225
225
  });
226
226
 
227
- const batch3 = await test_utils.fromAsync(
227
+ const batch3 = await test_utils.getBatchArray(
228
228
  bucketStorage.getBucketDataBatch(
229
229
  test_utils.testCheckpoint(checkpoint),
230
230
  [bucketRequest(syncRulesContent, 'global[]', batch2[0].chunkData.next_after)],
@@ -1467,6 +1467,96 @@ streams:
1467
1467
  expect(await mongoFactory.db.current_data.countDocuments({ '_id.g': syncRules.replicationStreamId })).toBe(0);
1468
1468
  });
1469
1469
 
1470
+ test.runIf(storageVersion < 3)(
1471
+ 'clear removes large v1 current_data sets without affecting other streams',
1472
+ async () => {
1473
+ await using factory = await storageConfig.factory();
1474
+ const syncRules = await factory.updateSyncRules(
1475
+ updateSyncRulesFromYaml(MINIMAL_SYNC_RULES, {
1476
+ storageVersion
1477
+ })
1478
+ );
1479
+ const bucketStorage = factory.getInstance(syncRules);
1480
+ const mongoFactory = factory as MongoBucketStorage;
1481
+ const sourceTableId = new bson.ObjectId();
1482
+ const documents = Array.from({ length: 10_002 }, (_, index) => ({
1483
+ _id: {
1484
+ g: syncRules.replicationStreamId,
1485
+ t: sourceTableId,
1486
+ k: index
1487
+ },
1488
+ data: new bson.Binary(),
1489
+ buckets: [],
1490
+ lookups: []
1491
+ }));
1492
+ await mongoFactory.db.current_data.insertMany([
1493
+ ...documents,
1494
+ {
1495
+ _id: {
1496
+ g: syncRules.replicationStreamId + 1,
1497
+ t: sourceTableId,
1498
+ k: 0
1499
+ },
1500
+ data: new bson.Binary(),
1501
+ buckets: [],
1502
+ lookups: []
1503
+ }
1504
+ ]);
1505
+
1506
+ await bucketStorage.clear();
1507
+
1508
+ expect(await mongoFactory.db.current_data.countDocuments({ '_id.g': syncRules.replicationStreamId })).toBe(0);
1509
+ expect(await mongoFactory.db.current_data.countDocuments({ '_id.g': syncRules.replicationStreamId + 1 })).toBe(1);
1510
+ }
1511
+ );
1512
+
1513
+ test.runIf(storageVersion < 3)(
1514
+ 'clear removes large v1 parameter index sets without affecting other streams',
1515
+ async () => {
1516
+ await using factory = await storageConfig.factory();
1517
+ const syncRules = await factory.updateSyncRules(
1518
+ updateSyncRulesFromYaml(MINIMAL_SYNC_RULES, {
1519
+ storageVersion
1520
+ })
1521
+ );
1522
+ const bucketStorage = factory.getInstance(syncRules);
1523
+ const mongoFactory = factory as MongoBucketStorage;
1524
+ const sourceTableId = new bson.ObjectId();
1525
+ const documents = Array.from({ length: 10_002 }, (_, index) => ({
1526
+ _id: BigInt(index * 2),
1527
+ key: {
1528
+ g: syncRules.replicationStreamId,
1529
+ t: sourceTableId,
1530
+ k: index
1531
+ },
1532
+ lookup: new bson.Binary(),
1533
+ bucket_parameters: []
1534
+ }));
1535
+ await mongoFactory.db.bucket_parameters.insertMany([
1536
+ ...documents,
1537
+ {
1538
+ _id: 1n,
1539
+ key: {
1540
+ g: syncRules.replicationStreamId + 1,
1541
+ t: sourceTableId,
1542
+ k: 0
1543
+ },
1544
+ lookup: new bson.Binary(),
1545
+ bucket_parameters: []
1546
+ }
1547
+ ]);
1548
+
1549
+ await bucketStorage.clear();
1550
+
1551
+ expect(await mongoFactory.db.bucket_parameters.countDocuments({ 'key.g': syncRules.replicationStreamId })).toBe(
1552
+ 0
1553
+ );
1554
+ expect(
1555
+ await mongoFactory.db.bucket_parameters.countDocuments({ 'key.g': syncRules.replicationStreamId + 1 })
1556
+ ).toBe(1);
1557
+ }
1558
+ );
1559
+
1470
1560
  test.runIf(storageVersion < 3)('storage metrics include v1 current_data', async () => {
1471
1561
  await using factory = await storageConfig.factory();
1472
1562
  const syncRules = await factory.updateSyncRules(
@@ -1747,7 +1837,7 @@ describe('sync - mongodb', () => {
1747
1837
  async function getFilteredOps(start: number, checkpoint: number): Promise<bigint[]> {
1748
1838
  const { syncRules, bucketStorage } = await setupFilteringTest();
1749
1839
  const request = bucketRequest(syncRules.syncConfigContent[0], 'global[]', BigInt(start));
1750
- const batch = await test_utils.fromAsync(
1840
+ const batch = await test_utils.getBatchArray(
1751
1841
  bucketStorage.getBucketDataBatch(test_utils.testCheckpoint(BigInt(checkpoint)), [request])
1752
1842
  );
1753
1843
  const ops = batch.flatMap((b) => b.chunkData.data.map((d) => BigInt(d.op_id)));
@@ -1894,7 +1984,7 @@ describe('sync - mongodb', () => {
1894
1984
  const roundRequests = requests
1895
1985
  .filter((request) => pending.has(request.bucket))
1896
1986
  .map((request) => ({ ...request, start: positions.get(request.bucket)! }));
1897
- const batch = await test_utils.fromAsync(
1987
+ const batch = await test_utils.getBatchArray(
1898
1988
  bucketStorage.getBucketDataBatch(test_utils.testCheckpoint(end), roundRequests)
1899
1989
  );
1900
1990
  let anyHasMore = false;