@powersync/service-module-mongodb-storage 0.0.0-dev-20260602073133 → 0.0.0-dev-20260909133214

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 (275) hide show
  1. package/CHANGELOG.md +177 -5
  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 +71 -18
  6. package/dist/storage/MongoBucketStorage.js +516 -202
  7. package/dist/storage/MongoBucketStorage.js.map +1 -1
  8. package/dist/storage/implementation/MongoBucketBatch.d.ts +71 -29
  9. package/dist/storage/implementation/MongoBucketBatch.js +140 -66
  10. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
  11. package/dist/storage/implementation/MongoChecksums.d.ts +41 -13
  12. package/dist/storage/implementation/MongoChecksums.js +36 -121
  13. package/dist/storage/implementation/MongoChecksums.js.map +1 -1
  14. package/dist/storage/implementation/MongoCompactor.d.ts +28 -90
  15. package/dist/storage/implementation/MongoCompactor.js +87 -481
  16. package/dist/storage/implementation/MongoCompactor.js.map +1 -1
  17. package/dist/storage/implementation/MongoHydrationState.d.ts +11 -0
  18. package/dist/storage/implementation/MongoHydrationState.js +49 -0
  19. package/dist/storage/implementation/MongoHydrationState.js.map +1 -0
  20. package/dist/storage/implementation/MongoParameterCompactor.d.ts +82 -8
  21. package/dist/storage/implementation/MongoParameterCompactor.js +312 -76
  22. package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
  23. package/dist/storage/implementation/MongoParsedSyncConfigSet.d.ts +21 -0
  24. package/dist/storage/implementation/MongoParsedSyncConfigSet.js +93 -0
  25. package/dist/storage/implementation/MongoParsedSyncConfigSet.js.map +1 -0
  26. package/dist/storage/implementation/MongoPersistedReplicationStream.d.ts +36 -0
  27. package/dist/storage/implementation/MongoPersistedReplicationStream.js +90 -0
  28. package/dist/storage/implementation/MongoPersistedReplicationStream.js.map +1 -0
  29. package/dist/storage/implementation/MongoPersistedSyncConfigContent.d.ts +25 -0
  30. package/dist/storage/implementation/MongoPersistedSyncConfigContent.js +125 -0
  31. package/dist/storage/implementation/MongoPersistedSyncConfigContent.js.map +1 -0
  32. package/dist/storage/implementation/MongoStorageProvider.js +41 -4
  33. package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
  34. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +81 -37
  35. package/dist/storage/implementation/MongoSyncBucketStorage.js +159 -86
  36. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  37. package/dist/storage/implementation/MongoSyncRulesLock.d.ts +1 -1
  38. package/dist/storage/implementation/MongoSyncRulesLock.js +23 -11
  39. package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
  40. package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +11 -8
  41. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +159 -33
  42. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
  43. package/dist/storage/implementation/SyncRuleStateUpdate.d.ts +11 -5
  44. package/dist/storage/implementation/SyncRuleStateUpdate.js +14 -6
  45. package/dist/storage/implementation/SyncRuleStateUpdate.js.map +1 -1
  46. package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.d.ts +28 -0
  47. package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.js +2 -0
  48. package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.js.map +1 -0
  49. package/dist/storage/implementation/common/PersistedBatch.d.ts +34 -2
  50. package/dist/storage/implementation/common/PersistedBatch.js +63 -10
  51. package/dist/storage/implementation/common/PersistedBatch.js.map +1 -1
  52. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js +1 -1
  53. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js.map +1 -1
  54. package/dist/storage/implementation/createMongoSyncBucketStorage.d.ts +2 -2
  55. package/dist/storage/implementation/createMongoSyncBucketStorage.js +4 -4
  56. package/dist/storage/implementation/createMongoSyncBucketStorage.js.map +1 -1
  57. package/dist/storage/implementation/db.d.ts +4 -1
  58. package/dist/storage/implementation/db.js +31 -16
  59. package/dist/storage/implementation/db.js.map +1 -1
  60. package/dist/storage/implementation/models.d.ts +55 -13
  61. package/dist/storage/implementation/models.js.map +1 -1
  62. package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +15 -1
  63. package/dist/storage/implementation/v1/MongoBucketBatchV1.js +135 -107
  64. package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -1
  65. package/dist/storage/implementation/v1/MongoChecksumsV1.d.ts +4 -4
  66. package/dist/storage/implementation/v1/MongoChecksumsV1.js +119 -7
  67. package/dist/storage/implementation/v1/MongoChecksumsV1.js.map +1 -1
  68. package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +47 -7
  69. package/dist/storage/implementation/v1/MongoCompactorV1.js +478 -8
  70. package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -1
  71. package/dist/storage/implementation/v1/MongoParameterCompactorV1.d.ts +16 -2
  72. package/dist/storage/implementation/v1/MongoParameterCompactorV1.js +20 -9
  73. package/dist/storage/implementation/v1/MongoParameterCompactorV1.js.map +1 -1
  74. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +33 -27
  75. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +146 -73
  76. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -1
  77. package/dist/storage/implementation/v1/SingleBucketStoreV1.d.ts +9 -10
  78. package/dist/storage/implementation/v1/SingleBucketStoreV1.js +2 -8
  79. package/dist/storage/implementation/v1/SingleBucketStoreV1.js.map +1 -1
  80. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.d.ts +2 -1
  81. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js +3 -0
  82. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js.map +1 -1
  83. package/dist/storage/implementation/v1/models.d.ts +10 -1
  84. package/dist/storage/implementation/v1/models.js +1 -0
  85. package/dist/storage/implementation/v1/models.js.map +1 -1
  86. package/dist/storage/implementation/v3/BucketDataContextV3.d.ts +13 -0
  87. package/dist/storage/implementation/v3/BucketDataContextV3.js +23 -0
  88. package/dist/storage/implementation/v3/BucketDataContextV3.js.map +1 -0
  89. package/dist/storage/implementation/v3/CompactionLease.d.ts +50 -0
  90. package/dist/storage/implementation/v3/CompactionLease.js +131 -0
  91. package/dist/storage/implementation/v3/CompactionLease.js.map +1 -0
  92. package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +16 -5
  93. package/dist/storage/implementation/v3/MongoBucketBatchV3.js +393 -262
  94. package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
  95. package/dist/storage/implementation/v3/MongoChecksumsV3.d.ts +30 -9
  96. package/dist/storage/implementation/v3/MongoChecksumsV3.js +161 -20
  97. package/dist/storage/implementation/v3/MongoChecksumsV3.js.map +1 -1
  98. package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +108 -18
  99. package/dist/storage/implementation/v3/MongoCompactorV3.js +1115 -47
  100. package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -1
  101. package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +12 -3
  102. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +13 -8
  103. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +1 -1
  104. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.d.ts +54 -0
  105. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js +319 -0
  106. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js.map +1 -0
  107. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +61 -36
  108. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +353 -161
  109. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -1
  110. package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.d.ts +26 -0
  111. package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.js +82 -0
  112. package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.js.map +1 -0
  113. package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +8 -3
  114. package/dist/storage/implementation/v3/PersistedBatchV3.js +131 -29
  115. package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
  116. package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +3 -3
  117. package/dist/storage/implementation/v3/SourceRecordStoreV3.js +13 -11
  118. package/dist/storage/implementation/v3/SourceRecordStoreV3.js.map +1 -1
  119. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +26 -14
  120. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +73 -29
  121. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -1
  122. package/dist/storage/implementation/v3/bucket-format.d.ts +13 -0
  123. package/dist/storage/implementation/v3/bucket-format.js +79 -0
  124. package/dist/storage/implementation/v3/bucket-format.js.map +1 -0
  125. package/dist/storage/implementation/v3/chunking.d.ts +10 -0
  126. package/dist/storage/implementation/v3/chunking.js +30 -0
  127. package/dist/storage/implementation/v3/chunking.js.map +1 -0
  128. package/dist/storage/implementation/v3/compact-utils.d.ts +107 -0
  129. package/dist/storage/implementation/v3/compact-utils.js +216 -0
  130. package/dist/storage/implementation/v3/compact-utils.js.map +1 -0
  131. package/dist/storage/implementation/v3/compaction-constants.d.ts +5 -0
  132. package/dist/storage/implementation/v3/compaction-constants.js +6 -0
  133. package/dist/storage/implementation/v3/compaction-constants.js.map +1 -0
  134. package/dist/storage/implementation/v3/models.d.ts +187 -23
  135. package/dist/storage/implementation/v3/models.js +11 -28
  136. package/dist/storage/implementation/v3/models.js.map +1 -1
  137. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.d.ts +20 -0
  138. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js +48 -0
  139. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js.map +1 -0
  140. package/dist/storage/implementation/v3/object-storage/ObjectStorage.d.ts +33 -0
  141. package/dist/storage/implementation/v3/object-storage/ObjectStorage.js +12 -0
  142. package/dist/storage/implementation/v3/object-storage/ObjectStorage.js.map +1 -0
  143. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.d.ts +68 -0
  144. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js +154 -0
  145. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js.map +1 -0
  146. package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.d.ts +37 -0
  147. package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.js +167 -0
  148. package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.js.map +1 -0
  149. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.d.ts +67 -0
  150. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js +439 -0
  151. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js.map +1 -0
  152. package/dist/storage/implementation/v3/source-table-utils.d.ts +63 -0
  153. package/dist/storage/implementation/v3/source-table-utils.js +206 -0
  154. package/dist/storage/implementation/v3/source-table-utils.js.map +1 -0
  155. package/dist/storage/storage-index.d.ts +5 -4
  156. package/dist/storage/storage-index.js +5 -4
  157. package/dist/storage/storage-index.js.map +1 -1
  158. package/dist/types/types.d.ts +71 -3
  159. package/dist/types/types.js +58 -2
  160. package/dist/types/types.js.map +1 -1
  161. package/dist/utils/test-utils.d.ts +1 -2
  162. package/dist/utils/test-utils.js +7 -1
  163. package/dist/utils/test-utils.js.map +1 -1
  164. package/dist/utils/util.d.ts +9 -2
  165. package/dist/utils/util.js +131 -5
  166. package/dist/utils/util.js.map +1 -1
  167. package/package.json +13 -19
  168. package/src/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.ts +55 -0
  169. package/src/storage/MongoBucketStorage.ts +728 -242
  170. package/src/storage/implementation/MongoBucketBatch.ts +196 -91
  171. package/src/storage/implementation/MongoChecksums.ts +93 -153
  172. package/src/storage/implementation/MongoCompactor.ts +106 -631
  173. package/src/storage/implementation/MongoHydrationState.ts +61 -0
  174. package/src/storage/implementation/MongoParameterCompactor.ts +392 -87
  175. package/src/storage/implementation/MongoParsedSyncConfigSet.ts +122 -0
  176. package/src/storage/implementation/MongoPersistedReplicationStream.ts +118 -0
  177. package/src/storage/implementation/MongoPersistedSyncConfigContent.ts +156 -0
  178. package/src/storage/implementation/MongoStorageProvider.ts +48 -4
  179. package/src/storage/implementation/MongoSyncBucketStorage.ts +226 -118
  180. package/src/storage/implementation/MongoSyncRulesLock.ts +28 -12
  181. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +194 -52
  182. package/src/storage/implementation/SyncRuleStateUpdate.ts +13 -6
  183. package/src/storage/implementation/common/MongoSyncBucketStorageCheckpoint.ts +31 -0
  184. package/src/storage/implementation/common/PersistedBatch.ts +83 -15
  185. package/src/storage/implementation/common/VersionedPowerSyncMongoBase.ts +1 -1
  186. package/src/storage/implementation/createMongoSyncBucketStorage.ts +21 -7
  187. package/src/storage/implementation/db.ts +40 -22
  188. package/src/storage/implementation/models.ts +58 -14
  189. package/src/storage/implementation/v1/MongoBucketBatchV1.ts +174 -125
  190. package/src/storage/implementation/v1/MongoChecksumsV1.ts +154 -12
  191. package/src/storage/implementation/v1/MongoCompactorV1.ts +594 -25
  192. package/src/storage/implementation/v1/MongoParameterCompactorV1.ts +25 -9
  193. package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +226 -129
  194. package/src/storage/implementation/v1/SingleBucketStoreV1.ts +18 -27
  195. package/src/storage/implementation/v1/VersionedPowerSyncMongoV1.ts +11 -1
  196. package/src/storage/implementation/v1/models.ts +12 -1
  197. package/src/storage/implementation/v3/BucketDataContextV3.ts +32 -0
  198. package/src/storage/implementation/v3/CompactionLease.ts +152 -0
  199. package/src/storage/implementation/v3/MongoBucketBatchV3.ts +502 -324
  200. package/src/storage/implementation/v3/MongoChecksumsV3.ts +250 -32
  201. package/src/storage/implementation/v3/MongoCompactorV3.ts +1439 -70
  202. package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +19 -9
  203. package/src/storage/implementation/v3/MongoStoppedSyncConfigCleanup.ts +473 -0
  204. package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +453 -211
  205. package/src/storage/implementation/v3/MongoWriteCheckpointAPIV3.ts +111 -0
  206. package/src/storage/implementation/v3/PersistedBatchV3.ts +169 -45
  207. package/src/storage/implementation/v3/SourceRecordStoreV3.ts +12 -12
  208. package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +99 -38
  209. package/src/storage/implementation/v3/bucket-format.ts +99 -0
  210. package/src/storage/implementation/v3/chunking.ts +40 -0
  211. package/src/storage/implementation/v3/compact-utils.ts +320 -0
  212. package/src/storage/implementation/v3/compaction-constants.ts +5 -0
  213. package/src/storage/implementation/v3/models.ts +228 -58
  214. package/src/storage/implementation/v3/object-storage/BucketDataObjectStorage.ts +62 -0
  215. package/src/storage/implementation/v3/object-storage/ObjectStorage.ts +44 -0
  216. package/src/storage/implementation/v3/object-storage/ObjectStorageLifecycle.ts +182 -0
  217. package/src/storage/implementation/v3/object-storage/ObjectStorageUsage.ts +230 -0
  218. package/src/storage/implementation/v3/object-storage/S3ObjectStorage.ts +573 -0
  219. package/src/storage/implementation/v3/source-table-utils.ts +398 -0
  220. package/src/storage/storage-index.ts +14 -4
  221. package/src/types/types.ts +76 -2
  222. package/src/utils/test-utils.ts +8 -3
  223. package/src/utils/util.ts +188 -7
  224. package/test/src/__snapshots__/storage.test.ts.snap +4 -54
  225. package/test/src/__snapshots__/storage_sync.test.ts.snap +652 -645
  226. package/test/src/cleanup-stopped-sync-configs.test.ts +698 -0
  227. package/test/src/compact-utils.test.ts +226 -0
  228. package/test/src/helpers/MemoryObjectStorage.ts +61 -0
  229. package/test/src/helpers/s3TestFactory.ts +69 -0
  230. package/test/src/object_storage_lifecycle.test.ts +59 -0
  231. package/test/src/object_storage_usage.test.ts +255 -0
  232. package/test/src/parameter_compacting_v1.test.ts +120 -0
  233. package/test/src/parameter_compacting_v3.test.ts +192 -0
  234. package/test/src/parameter_compaction_fence.test.ts +276 -0
  235. package/test/src/setup.ts +6 -1
  236. package/test/src/storage.test.ts +478 -13
  237. package/test/src/storage_compacting.test.ts +2516 -107
  238. package/test/src/storage_object_storage_inline_threshold.test.ts +116 -0
  239. package/test/src/storage_s3_checksums.test.ts +190 -0
  240. package/test/src/storage_s3_compaction_lifecycle.test.ts +543 -0
  241. package/test/src/storage_s3_reading.test.ts +768 -0
  242. package/test/src/storage_s3_writing.test.ts +136 -0
  243. package/test/src/storage_sync.test.ts +1507 -76
  244. package/test/src/storeCurrentData.test.ts +227 -0
  245. package/test/src/util.ts +3 -2
  246. package/tsconfig.json +1 -4
  247. package/tsconfig.tsbuildinfo +1 -1
  248. package/dist/storage/implementation/BucketDefinitionMapping.d.ts +0 -15
  249. package/dist/storage/implementation/BucketDefinitionMapping.js +0 -58
  250. package/dist/storage/implementation/BucketDefinitionMapping.js.map +0 -1
  251. package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +0 -14
  252. package/dist/storage/implementation/MongoPersistedSyncRules.js +0 -67
  253. package/dist/storage/implementation/MongoPersistedSyncRules.js.map +0 -1
  254. package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +0 -29
  255. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +0 -76
  256. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +0 -1
  257. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.d.ts +0 -13
  258. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js +0 -2
  259. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js.map +0 -1
  260. package/dist/storage/implementation/common/SingleBucketStore.d.ts +0 -54
  261. package/dist/storage/implementation/common/SingleBucketStore.js +0 -3
  262. package/dist/storage/implementation/common/SingleBucketStore.js.map +0 -1
  263. package/dist/storage/implementation/v3/MongoParameterLookupV3.d.ts +0 -4
  264. package/dist/storage/implementation/v3/MongoParameterLookupV3.js +0 -9
  265. package/dist/storage/implementation/v3/MongoParameterLookupV3.js.map +0 -1
  266. package/dist/storage/implementation/v3/SingleBucketStoreV3.d.ts +0 -18
  267. package/dist/storage/implementation/v3/SingleBucketStoreV3.js +0 -48
  268. package/dist/storage/implementation/v3/SingleBucketStoreV3.js.map +0 -1
  269. package/src/storage/implementation/BucketDefinitionMapping.ts +0 -75
  270. package/src/storage/implementation/MongoPersistedSyncRules.ts +0 -82
  271. package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +0 -101
  272. package/src/storage/implementation/common/MongoSyncBucketStorageContext.ts +0 -15
  273. package/src/storage/implementation/common/SingleBucketStore.ts +0 -63
  274. package/src/storage/implementation/v3/MongoParameterLookupV3.ts +0 -11
  275. package/src/storage/implementation/v3/SingleBucketStoreV3.ts +0 -68
@@ -0,0 +1,122 @@
1
+ import * as sqlite from 'node:sqlite';
2
+
3
+ import { ServiceAssertionError } from '@powersync/lib-services-framework';
4
+ import {
5
+ BucketDefinitionMapping,
6
+ MultiSyncConfigBucketDefinitionMapping,
7
+ SingleSyncConfigBucketDefinitionMapping,
8
+ storage,
9
+ SyncConfigWithMapping,
10
+ SyncConfigWithRequiredMapping
11
+ } from '@powersync/service-core';
12
+ import {
13
+ CompatibilityOption,
14
+ DEFAULT_HYDRATION_STATE,
15
+ EventDefinitionId,
16
+ HydratedEventDescriptor,
17
+ HydratedSyncConfig,
18
+ HydrationState,
19
+ nodeSqlite,
20
+ SyncConfigWithErrors,
21
+ versionedHydrationState
22
+ } from '@powersync/service-sync-rules';
23
+ import { StorageConfig } from './models.js';
24
+ import { MongoHydrationState } from './MongoHydrationState.js';
25
+
26
+ export class MongoParsedSyncConfigSet implements storage.ParsedSyncConfigSet {
27
+ public readonly hydrationState: HydrationState;
28
+ public readonly syncConfigs: SyncConfigWithErrors[];
29
+ public readonly replicationStreamName: string;
30
+ public readonly mapping: BucketDefinitionMapping;
31
+
32
+ readonly #configsWithMapping: readonly SyncConfigWithMapping[];
33
+
34
+ constructor(
35
+ public readonly replicationStreamId: number,
36
+ storageConfig: StorageConfig,
37
+ slotName: string,
38
+ syncConfigs: SyncConfigWithMapping[]
39
+ ) {
40
+ this.replicationStreamName = slotName;
41
+ this.#configsWithMapping = [...syncConfigs];
42
+ this.syncConfigs = syncConfigs.map((config) => config.syncConfig);
43
+ if (this.syncConfigs.length == 0) {
44
+ throw new ServiceAssertionError(`At least one sync config is required`);
45
+ }
46
+ const [firstConfig] = this.syncConfigs;
47
+ const compatibility = firstConfig.config.compatibility;
48
+ for (const config of this.syncConfigs) {
49
+ if (config.config.compatibility.equals(compatibility)) {
50
+ continue;
51
+ }
52
+ throw new ServiceAssertionError(
53
+ `All sync configs in a replication stream must use the same compatibility options`
54
+ );
55
+ }
56
+
57
+ if (storageConfig.incrementalReprocessing) {
58
+ if (syncConfigs.some((c) => c.mapping == null)) {
59
+ throw new ServiceAssertionError(`mapping is required for v3 storage`);
60
+ }
61
+ const mappedConfigs = syncConfigs as SyncConfigWithRequiredMapping[];
62
+ this.hydrationState = new MongoHydrationState(mappedConfigs, this.replicationStreamId);
63
+ this.mapping = new MultiSyncConfigBucketDefinitionMapping(mappedConfigs);
64
+ } else if (!compatibility.isEnabled(CompatibilityOption.versionedBucketIds) && !storageConfig.versionedBuckets) {
65
+ const [syncConfig] = syncConfigs;
66
+ if (syncConfigs.length != 1 || syncConfig == null) {
67
+ throw new ServiceAssertionError(`Non-incremental storage requires exactly one sync config`);
68
+ }
69
+ this.hydrationState = DEFAULT_HYDRATION_STATE;
70
+ this.mapping = syncConfig.mapping ?? new SingleSyncConfigBucketDefinitionMapping();
71
+ } else {
72
+ const [syncConfig] = syncConfigs;
73
+ if (syncConfigs.length != 1 || syncConfig == null) {
74
+ throw new ServiceAssertionError(`Non-incremental storage requires exactly one sync config`);
75
+ }
76
+ this.hydrationState = versionedHydrationState(this.replicationStreamId);
77
+ this.mapping = syncConfig.mapping ?? new SingleSyncConfigBucketDefinitionMapping();
78
+ }
79
+ }
80
+
81
+ #hydratedSyncConfig: HydratedSyncConfig | undefined;
82
+
83
+ get hydratedSyncConfig(): HydratedSyncConfig {
84
+ this.#hydratedSyncConfig ??= new HydratedSyncConfig({
85
+ definitions: this.syncConfigs.map((config) => config.config),
86
+ createParams: {
87
+ hydrationState: this.hydrationState,
88
+ sqlite: nodeSqlite(sqlite)
89
+ }
90
+ });
91
+ return this.#hydratedSyncConfig;
92
+ }
93
+
94
+ #eventById: ReadonlyMap<EventDefinitionId, HydratedEventDescriptor> | undefined;
95
+
96
+ /**
97
+ * Hydrated events for the replication stream, keyed by their assigned storage id.
98
+ *
99
+ * Each config's events are resolved against that config's own (single-config) mapping, so the resolution is
100
+ * unambiguous, and the result is deduplicated by assigned id: unchanged events shared across configs collapse to
101
+ * one entry, while a changed event keeps a separate entry under its new id.
102
+ */
103
+ get eventById(): ReadonlyMap<EventDefinitionId, HydratedEventDescriptor> {
104
+ if (this.#eventById == null) {
105
+ const map = new Map<EventDefinitionId, HydratedEventDescriptor>();
106
+ const byDefinition = this.hydratedSyncConfig.eventDescriptorsByDefinition;
107
+ for (const config of this.#configsWithMapping) {
108
+ if (config.mapping == null) {
109
+ continue;
110
+ }
111
+ for (const event of byDefinition.get(config.syncConfig.config) ?? []) {
112
+ const id = config.mapping.eventId(event);
113
+ if (!map.has(id)) {
114
+ map.set(id, event);
115
+ }
116
+ }
117
+ }
118
+ this.#eventById = map;
119
+ }
120
+ return this.#eventById;
121
+ }
122
+ }
@@ -0,0 +1,118 @@
1
+ import { mongo } from '@powersync/lib-service-mongodb';
2
+ import { ServiceAssertionError } from '@powersync/lib-services-framework';
3
+ import { ReplicationStreamStorageIds, SingleSyncConfigBucketDefinitionMapping, storage } from '@powersync/service-core';
4
+ import * as bson from 'bson';
5
+ import { ReplicationStreamDocumentV3, SyncConfigDefinition } from '../storage-index.js';
6
+ import { PowerSyncMongo } from './db.js';
7
+ import { getMongoStorageConfig } from './models.js';
8
+ import { MongoParsedSyncConfigSet } from './MongoParsedSyncConfigSet.js';
9
+ import {
10
+ MongoPersistedSyncConfigContentBase,
11
+ MongoPersistedSyncConfigContentV1,
12
+ MongoPersistedSyncConfigContentV3
13
+ } from './MongoPersistedSyncConfigContent.js';
14
+ import { MongoSyncRulesLock } from './MongoSyncRulesLock.js';
15
+ import { SyncRuleDocumentV1 } from './v1/models.js';
16
+
17
+ export class MongoPersistedReplicationStream extends storage.PersistedReplicationStream {
18
+ public current_lock: MongoSyncRulesLock | null = null;
19
+ public readonly syncConfigContent: readonly MongoPersistedSyncConfigContentBase[];
20
+
21
+ constructor(
22
+ private readonly db: PowerSyncMongo,
23
+ private readonly doc: SyncRuleDocumentV1 | ReplicationStreamDocumentV3,
24
+ private readonly configs: SyncConfigDefinition[] = []
25
+ ) {
26
+ const storageVersion = doc.storage_version ?? storage.LEGACY_STORAGE_VERSION;
27
+ const replicationJobId =
28
+ configs.length == 0
29
+ ? String(doc._id)
30
+ : `${doc._id}:${configs
31
+ .map((config) => config._id.toHexString())
32
+ .sort()
33
+ .join(',')}`;
34
+
35
+ super({
36
+ replicationStreamId: doc._id,
37
+ replicationStreamName: doc.slot_name ?? `powersync_${doc._id}`,
38
+ state: doc.state,
39
+ storageVersion,
40
+ replicationJobId
41
+ });
42
+
43
+ this.syncConfigContent = this.createSyncConfigContent();
44
+ }
45
+
46
+ getStorageConfig() {
47
+ return getMongoStorageConfig(this.storageVersion);
48
+ }
49
+
50
+ private createSyncConfigContent(): MongoPersistedSyncConfigContentBase[] {
51
+ if (this.getStorageConfig().incrementalReprocessing) {
52
+ if (this.configs.length == 0) {
53
+ throw new ServiceAssertionError(`Cannot create v3 storage without sync config definitions`);
54
+ }
55
+ return this.configs.map(
56
+ (config) => new MongoPersistedSyncConfigContentV3(this.db, this.doc as ReplicationStreamDocumentV3, config)
57
+ );
58
+ }
59
+
60
+ return [new MongoPersistedSyncConfigContentV1(this.db, this.doc as SyncRuleDocumentV1)];
61
+ }
62
+
63
+ get syncConfigIds(): bson.ObjectId[] {
64
+ return this.configs.map((config) => config._id);
65
+ }
66
+
67
+ #storageIds: ReplicationStreamStorageIds | undefined;
68
+
69
+ /**
70
+ * Persisted storage ids for all sync configs in this replication stream.
71
+ *
72
+ * Derived from the persisted rule_mapping documents - requires no parsed sync configs.
73
+ */
74
+ get storageIds(): ReplicationStreamStorageIds {
75
+ this.#storageIds ??= new ReplicationStreamStorageIds(this.syncConfigContent.map((content) => content.mapping));
76
+ return this.#storageIds;
77
+ }
78
+
79
+ /**
80
+ * Parse the sync configs for this replication stream.
81
+ *
82
+ * This creates a fresh parse on every call. Do not call this from operational paths -
83
+ * use {@link MongoSyncBucketStorage.getParsedSyncConfigSet} instead, which returns a
84
+ * canonical instance so that parsed source objects and mappings stay associated.
85
+ */
86
+ parsed(options: storage.ParseSyncConfigOptions): MongoParsedSyncConfigSet {
87
+ const storageConfig = this.getStorageConfig();
88
+ if (!storageConfig.incrementalReprocessing) {
89
+ return this.syncConfigContent[0].parsed(options);
90
+ }
91
+
92
+ const syncConfigs = this.configs.map((config) => {
93
+ return {
94
+ syncConfigId: config._id.toHexString(),
95
+ syncConfig: storage.parsePersistedSyncConfigContent({
96
+ content: config.content,
97
+ compiledPlan: config.serialized_plan ?? null,
98
+ storageVersion: this.storageVersion,
99
+ parseOptions: options
100
+ }),
101
+ mapping: SingleSyncConfigBucketDefinitionMapping.fromPersistedMapping(config.rule_mapping)
102
+ };
103
+ });
104
+
105
+ return new MongoParsedSyncConfigSet(
106
+ this.replicationStreamId,
107
+ storageConfig,
108
+ this.replicationStreamName,
109
+ syncConfigs
110
+ );
111
+ }
112
+
113
+ async lock(session?: mongo.ClientSession) {
114
+ const lock = await MongoSyncRulesLock.createLock(this.db.versioned(this.getStorageConfig()), this, session);
115
+ this.current_lock = lock;
116
+ return lock;
117
+ }
118
+ }
@@ -0,0 +1,156 @@
1
+ import { ServiceAssertionError } from '@powersync/lib-services-framework';
2
+ import { SingleSyncConfigBucketDefinitionMapping, storage } from '@powersync/service-core';
3
+ import * as bson from 'bson';
4
+ import {
5
+ ReplicationStreamDocumentV3,
6
+ SyncConfigDefinition,
7
+ SyncRuleConfigStateV3,
8
+ SyncRuleDocumentV1
9
+ } from '../storage-index.js';
10
+ import { PowerSyncMongo } from './db.js';
11
+ import { getMongoStorageConfig } from './models.js';
12
+ import { MongoParsedSyncConfigSet } from './MongoParsedSyncConfigSet.js';
13
+
14
+ export abstract class MongoPersistedSyncConfigContentBase extends storage.PersistedSyncConfigContent {
15
+ public readonly mapping: SingleSyncConfigBucketDefinitionMapping;
16
+ public readonly syncConfigObjectId: bson.ObjectId | null;
17
+
18
+ protected constructor(
19
+ protected readonly db: PowerSyncMongo,
20
+ options: Omit<storage.PersistedSyncConfigContentData, 'syncConfigId'> & {
21
+ mapping: SingleSyncConfigBucketDefinitionMapping;
22
+ syncConfigId: bson.ObjectId | null;
23
+ }
24
+ ) {
25
+ const { mapping, syncConfigId, ...base } = options;
26
+ super({
27
+ ...base,
28
+ syncConfigId: syncConfigId?.toHexString() ?? null
29
+ });
30
+ this.mapping = mapping;
31
+ this.syncConfigObjectId = syncConfigId;
32
+ }
33
+
34
+ getStorageConfig() {
35
+ return getMongoStorageConfig(this.storageVersion);
36
+ }
37
+
38
+ parsed(options: storage.ParseSyncConfigOptions): MongoParsedSyncConfigSet {
39
+ const parsed = super.parsed(options);
40
+ const storageConfig = this.getStorageConfig();
41
+ const [syncConfig] = parsed.syncConfigs;
42
+ if (syncConfig == null) {
43
+ throw new ServiceAssertionError(`Expected one parsed sync config`);
44
+ }
45
+
46
+ if (storageConfig.incrementalReprocessing) {
47
+ const syncConfigId = this.syncConfigId;
48
+ if (syncConfigId == null) {
49
+ throw new ServiceAssertionError(`syncConfigId is required for v3 sync config content`);
50
+ }
51
+
52
+ return new MongoParsedSyncConfigSet(parsed.replicationStreamId, storageConfig, parsed.replicationStreamName, [
53
+ { syncConfigId, syncConfig, mapping: this.mapping }
54
+ ]);
55
+ }
56
+
57
+ return new MongoParsedSyncConfigSet(parsed.replicationStreamId, storageConfig, parsed.replicationStreamName, [
58
+ { syncConfig, mapping: null }
59
+ ]);
60
+ }
61
+ }
62
+ export class MongoPersistedSyncConfigContentV1 extends MongoPersistedSyncConfigContentBase {
63
+ constructor(db: PowerSyncMongo, doc: SyncRuleDocumentV1) {
64
+ super(db, {
65
+ replicationStreamId: doc._id,
66
+ sync_rules_content: doc.content,
67
+ compiled_plan: doc.serialized_plan ?? null,
68
+ // Handle legacy values
69
+ replicationStreamName: doc.slot_name ?? `powersync_${doc._id}`,
70
+ storageVersion: doc.storage_version ?? storage.LEGACY_STORAGE_VERSION,
71
+ mapping: new SingleSyncConfigBucketDefinitionMapping(),
72
+ syncConfigId: null,
73
+ syncConfigState: doc.state,
74
+ version_label: doc.version_label ?? undefined
75
+ });
76
+ }
77
+
78
+ async getSyncConfigStatus(): Promise<storage.PersistedSyncConfigStatus | null> {
79
+ const doc = await this.db.sync_rules.findOne<SyncRuleDocumentV1>({ _id: this.replicationStreamId });
80
+ if (doc == null) {
81
+ return null;
82
+ }
83
+
84
+ return syncConfigStatusFromV1(doc);
85
+ }
86
+ }
87
+ export class MongoPersistedSyncConfigContentV3 extends MongoPersistedSyncConfigContentBase {
88
+ declare public readonly syncConfigObjectId: bson.ObjectId;
89
+
90
+ constructor(db: PowerSyncMongo, doc: ReplicationStreamDocumentV3, config: SyncConfigDefinition) {
91
+ const state = doc.sync_configs.find((c) => c._id.equals(config._id));
92
+ if (state == null) {
93
+ throw new ServiceAssertionError(`Cannot find sync config ${config._id} in replication stream ${doc._id}`);
94
+ }
95
+ super(db, {
96
+ replicationStreamId: doc._id,
97
+ sync_rules_content: config.content,
98
+ compiled_plan: config.serialized_plan ?? null,
99
+
100
+ replicationStreamName: doc.slot_name ?? `powersync_${doc._id}`,
101
+ storageVersion: doc.storage_version,
102
+ mapping: SingleSyncConfigBucketDefinitionMapping.fromPersistedMapping(config.rule_mapping),
103
+ syncConfigId: config._id,
104
+ syncConfigState: state.state,
105
+ version_label: config.version_label
106
+ });
107
+ }
108
+
109
+ async getSyncConfigStatus(): Promise<storage.PersistedSyncConfigStatus | null> {
110
+ const doc = await this.db.sync_rules.findOne<ReplicationStreamDocumentV3>({
111
+ _id: this.replicationStreamId,
112
+ 'sync_configs._id': this.syncConfigObjectId
113
+ });
114
+ if (doc == null) {
115
+ return null;
116
+ }
117
+
118
+ const state = doc.sync_configs.find((c) => c._id.equals(this.syncConfigObjectId));
119
+ if (state == null) {
120
+ return null;
121
+ }
122
+
123
+ return syncConfigStatusFromV3(doc, state);
124
+ }
125
+ }
126
+
127
+ function syncConfigStatusFromV1(doc: SyncRuleDocumentV1): storage.PersistedSyncConfigStatus {
128
+ return {
129
+ id: String(doc._id),
130
+ replicationStreamId: doc._id,
131
+ state: doc.state,
132
+ snapshot_done: doc.snapshot_done,
133
+ last_checkpoint_lsn: doc.last_checkpoint_lsn,
134
+ last_fatal_error: doc.last_fatal_error,
135
+ last_fatal_error_ts: doc.last_fatal_error_ts,
136
+ last_keepalive_ts: doc.last_keepalive_ts,
137
+ last_checkpoint_ts: doc.last_checkpoint_ts
138
+ };
139
+ }
140
+
141
+ function syncConfigStatusFromV3(
142
+ doc: ReplicationStreamDocumentV3,
143
+ state: SyncRuleConfigStateV3
144
+ ): storage.PersistedSyncConfigStatus {
145
+ return {
146
+ id: state._id.toHexString(),
147
+ replicationStreamId: doc._id,
148
+ state: state.state,
149
+ snapshot_done: state.snapshot_done,
150
+ last_checkpoint_lsn: state.last_checkpoint_lsn,
151
+ last_fatal_error: doc.last_fatal_error,
152
+ last_fatal_error_ts: doc.last_fatal_error_ts,
153
+ last_keepalive_ts: doc.last_keepalive_ts,
154
+ last_checkpoint_ts: doc.last_checkpoint_ts
155
+ };
156
+ }
@@ -1,10 +1,11 @@
1
1
  import * as lib_mongo from '@powersync/lib-service-mongodb';
2
2
  import { ErrorCode, logger, ServiceAssertionError, ServiceError } from '@powersync/lib-services-framework';
3
3
  import { POWERSYNC_VERSION, storage } from '@powersync/service-core';
4
- import { MongoStorageConfig } from '../../types/types.js';
4
+ import { MongoStorageConfig, normalizeClearBatchThrottleRate } from '../../types/types.js';
5
5
  import { MongoBucketStorage } from '../MongoBucketStorage.js';
6
6
  import { MongoReportStorage } from '../MongoReportStorage.js';
7
7
  import { PowerSyncMongo } from './db.js';
8
+ import type { ObjectStorage } from './v3/object-storage/ObjectStorage.js';
8
9
 
9
10
  export class MongoStorageProvider implements storage.StorageProvider {
10
11
  get type() {
@@ -23,6 +24,24 @@ export class MongoStorageProvider implements storage.StorageProvider {
23
24
  }
24
25
 
25
26
  const decodedConfig = MongoStorageConfig.decode(storage as any);
27
+
28
+ let objectStorage: ObjectStorage | undefined;
29
+ if (decodedConfig.object_storage?.type === 's3') {
30
+ // Dynamically import S3ObjectStorage to avoid loading AWS SDK unless needed.
31
+ const { S3ObjectStorage } = await import('./v3/object-storage/S3ObjectStorage.js');
32
+ objectStorage = new S3ObjectStorage({
33
+ bucket: decodedConfig.object_storage.bucket,
34
+ region: decodedConfig.object_storage.region,
35
+ prefix: decodedConfig.object_storage.prefix,
36
+ endpoint: decodedConfig.object_storage.endpoint,
37
+ forcePathStyle: decodedConfig.object_storage.force_path_style,
38
+ accessKeyId: decodedConfig.object_storage.access_key_id,
39
+ secretAccessKey: decodedConfig.object_storage.secret_access_key,
40
+ concurrencyLimit: decodedConfig.object_storage.concurrency_limit,
41
+ defaultsMode: decodedConfig.object_storage.defaults_mode
42
+ });
43
+ }
44
+
26
45
  const client = lib_mongo.db.createMongoClient(decodedConfig, {
27
46
  powersyncVersion: POWERSYNC_VERSION,
28
47
  maxPoolSize: resolvedConfig.storage.max_pool_size ?? 8
@@ -38,9 +57,27 @@ export class MongoStorageProvider implements storage.StorageProvider {
38
57
  await client.connect();
39
58
 
40
59
  const database = new PowerSyncMongo(client, { database: resolvedConfig.storage.database });
60
+ const readPreference =
61
+ decodedConfig.bulk_read_preference == null
62
+ ? undefined
63
+ : new lib_mongo.mongo.ReadPreference(decodedConfig.bulk_read_preference, undefined, {
64
+ // maxStalenessSeconds is relevant for all modes except 'primary'.
65
+ // 90 is the minimum value.
66
+ maxStalenessSeconds: decodedConfig.bulk_read_preference == 'primary' ? undefined : 90
67
+ });
41
68
  const syncStorageFactory = new MongoBucketStorage(database, {
42
- // TODO currently need the entire resolved config due to this
43
- slot_name_prefix: resolvedConfig.slot_name_prefix
69
+ replicationStreamNamePrefix: resolvedConfig.slot_name_prefix,
70
+ readPreference,
71
+ clearBatchThrottleRate: normalizeClearBatchThrottleRate(decodedConfig.clear_batch_throttle_rate),
72
+ checksumCacheTtlMs: resolvedConfig.api_parameters.bucket_count_cache_ttl_minutes * 60_000,
73
+ defaultStorageVersion: decodedConfig.default_storage_version,
74
+ // Right now, only MongoDB source databases supports incremental reprocessing.
75
+ // Remove this filter when we support it for other source databases.
76
+ // This assumes a single source connection - revisit if we ever support multiple connections.
77
+ supportsMultipleSyncConfigs: resolvedConfig.connections?.[0]?.type == lib_mongo.MONGO_CONNECTION_TYPE,
78
+
79
+ objectStorage,
80
+ inlineThresholdBytes: decodedConfig.object_storage?.inline_threshold_bytes
44
81
  });
45
82
 
46
83
  // Storage factory for reports
@@ -53,8 +90,15 @@ export class MongoStorageProvider implements storage.StorageProvider {
53
90
  await syncStorageFactory[Symbol.asyncDispose]();
54
91
  await client.close();
55
92
  },
56
- tearDown: () => {
93
+ tearDown: async () => {
57
94
  logger.info(`Tearing down storage: ${database.db.namespace}...`);
95
+ if (objectStorage != null) {
96
+ logger.info(
97
+ `Clearing object storage: ${decodedConfig.object_storage!.bucket}/${decodedConfig.object_storage!.prefix ?? ''}.`
98
+ );
99
+ const { objectCount } = await objectStorage.deletePrefix('bucket-data/');
100
+ logger.info(`Deleted ${objectCount} objects from object storage.`);
101
+ }
58
102
  return database.db.dropDatabase();
59
103
  },
60
104
  onFatalError: (callback) => {