@powersync/service-module-mongodb-storage 0.15.4 → 0.17.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 (202) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +1 -1
  3. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -1
  4. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +2 -2
  5. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -1
  6. package/dist/storage/MongoBucketStorage.d.ts +8 -6
  7. package/dist/storage/MongoBucketStorage.js +153 -66
  8. package/dist/storage/MongoBucketStorage.js.map +1 -1
  9. package/dist/storage/implementation/BucketDefinitionMapping.d.ts +15 -0
  10. package/dist/storage/implementation/BucketDefinitionMapping.js +58 -0
  11. package/dist/storage/implementation/BucketDefinitionMapping.js.map +1 -0
  12. package/dist/storage/implementation/CheckpointState.d.ts +20 -0
  13. package/dist/storage/implementation/CheckpointState.js +31 -0
  14. package/dist/storage/implementation/CheckpointState.js.map +1 -0
  15. package/dist/storage/implementation/MongoBucketBatch.d.ts +48 -35
  16. package/dist/storage/implementation/MongoBucketBatch.js +118 -379
  17. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
  18. package/dist/storage/implementation/MongoBucketBatchShared.d.ts +5 -0
  19. package/dist/storage/implementation/MongoBucketBatchShared.js +8 -0
  20. package/dist/storage/implementation/MongoBucketBatchShared.js.map +1 -0
  21. package/dist/storage/implementation/MongoChecksums.d.ts +29 -17
  22. package/dist/storage/implementation/MongoChecksums.js +13 -72
  23. package/dist/storage/implementation/MongoChecksums.js.map +1 -1
  24. package/dist/storage/implementation/MongoCompactor.d.ts +98 -58
  25. package/dist/storage/implementation/MongoCompactor.js +229 -296
  26. package/dist/storage/implementation/MongoCompactor.js.map +1 -1
  27. package/dist/storage/implementation/MongoParameterCompactor.d.ts +11 -6
  28. package/dist/storage/implementation/MongoParameterCompactor.js +11 -8
  29. package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
  30. package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +14 -0
  31. package/dist/storage/implementation/MongoPersistedSyncRules.js +67 -0
  32. package/dist/storage/implementation/MongoPersistedSyncRules.js.map +1 -0
  33. package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +22 -5
  34. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +56 -13
  35. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +1 -1
  36. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +61 -32
  37. package/dist/storage/implementation/MongoSyncBucketStorage.js +85 -523
  38. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  39. package/dist/storage/implementation/MongoSyncRulesLock.d.ts +10 -4
  40. package/dist/storage/implementation/MongoSyncRulesLock.js +19 -13
  41. package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
  42. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +1 -1
  43. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
  44. package/dist/storage/implementation/OperationBatch.js +1 -1
  45. package/dist/storage/implementation/SyncRuleStateUpdate.d.ts +14 -0
  46. package/dist/storage/implementation/SyncRuleStateUpdate.js +36 -0
  47. package/dist/storage/implementation/SyncRuleStateUpdate.js.map +1 -0
  48. package/dist/storage/implementation/common/BucketDataDoc.d.ts +35 -0
  49. package/dist/storage/implementation/common/BucketDataDoc.js +2 -0
  50. package/dist/storage/implementation/common/BucketDataDoc.js.map +1 -0
  51. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.d.ts +13 -0
  52. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js +2 -0
  53. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js.map +1 -0
  54. package/dist/storage/implementation/common/PersistedBatch.d.ts +108 -0
  55. package/dist/storage/implementation/common/PersistedBatch.js +237 -0
  56. package/dist/storage/implementation/common/PersistedBatch.js.map +1 -0
  57. package/dist/storage/implementation/common/SingleBucketStore.d.ts +54 -0
  58. package/dist/storage/implementation/common/SingleBucketStore.js +3 -0
  59. package/dist/storage/implementation/common/SingleBucketStore.js.map +1 -0
  60. package/dist/storage/implementation/common/SourceRecordStore.d.ts +35 -0
  61. package/dist/storage/implementation/common/SourceRecordStore.js +2 -0
  62. package/dist/storage/implementation/common/SourceRecordStore.js.map +1 -0
  63. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.d.ts +27 -0
  64. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js +57 -0
  65. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js.map +1 -0
  66. package/dist/storage/implementation/createMongoSyncBucketStorage.d.ts +7 -0
  67. package/dist/storage/implementation/createMongoSyncBucketStorage.js +9 -0
  68. package/dist/storage/implementation/createMongoSyncBucketStorage.js.map +1 -0
  69. package/dist/storage/implementation/db.d.ts +41 -36
  70. package/dist/storage/implementation/db.js +77 -99
  71. package/dist/storage/implementation/db.js.map +1 -1
  72. package/dist/storage/implementation/models.d.ts +79 -66
  73. package/dist/storage/implementation/models.js +20 -1
  74. package/dist/storage/implementation/models.js.map +1 -1
  75. package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +27 -0
  76. package/dist/storage/implementation/v1/MongoBucketBatchV1.js +407 -0
  77. package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -0
  78. package/dist/storage/implementation/v1/MongoChecksumsV1.d.ts +12 -0
  79. package/dist/storage/implementation/v1/MongoChecksumsV1.js +56 -0
  80. package/dist/storage/implementation/v1/MongoChecksumsV1.js.map +1 -0
  81. package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +23 -0
  82. package/dist/storage/implementation/v1/MongoCompactorV1.js +52 -0
  83. package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -0
  84. package/dist/storage/implementation/v1/MongoParameterCompactorV1.d.ts +9 -0
  85. package/dist/storage/implementation/v1/MongoParameterCompactorV1.js +20 -0
  86. package/dist/storage/implementation/v1/MongoParameterCompactorV1.js.map +1 -0
  87. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +50 -0
  88. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +354 -0
  89. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -0
  90. package/dist/storage/implementation/v1/PersistedBatchV1.d.ts +25 -0
  91. package/dist/storage/implementation/v1/PersistedBatchV1.js +183 -0
  92. package/dist/storage/implementation/v1/PersistedBatchV1.js.map +1 -0
  93. package/dist/storage/implementation/v1/SingleBucketStoreV1.d.ts +18 -0
  94. package/dist/storage/implementation/v1/SingleBucketStoreV1.js +57 -0
  95. package/dist/storage/implementation/v1/SingleBucketStoreV1.js.map +1 -0
  96. package/dist/storage/implementation/v1/SourceRecordStoreV1.d.ts +19 -0
  97. package/dist/storage/implementation/v1/SourceRecordStoreV1.js +105 -0
  98. package/dist/storage/implementation/v1/SourceRecordStoreV1.js.map +1 -0
  99. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.d.ts +12 -0
  100. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js +20 -0
  101. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js.map +1 -0
  102. package/dist/storage/implementation/v1/models.d.ts +45 -0
  103. package/dist/storage/implementation/v1/models.js +37 -0
  104. package/dist/storage/implementation/v1/models.js.map +1 -0
  105. package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +30 -0
  106. package/dist/storage/implementation/v3/MongoBucketBatchV3.js +463 -0
  107. package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -0
  108. package/dist/storage/implementation/v3/MongoChecksumsV3.d.ts +15 -0
  109. package/dist/storage/implementation/v3/MongoChecksumsV3.js +84 -0
  110. package/dist/storage/implementation/v3/MongoChecksumsV3.js.map +1 -0
  111. package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +23 -0
  112. package/dist/storage/implementation/v3/MongoCompactorV3.js +68 -0
  113. package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -0
  114. package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +9 -0
  115. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +18 -0
  116. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +1 -0
  117. package/dist/storage/implementation/v3/MongoParameterLookupV3.d.ts +4 -0
  118. package/dist/storage/implementation/v3/MongoParameterLookupV3.js +9 -0
  119. package/dist/storage/implementation/v3/MongoParameterLookupV3.js.map +1 -0
  120. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +63 -0
  121. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +508 -0
  122. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -0
  123. package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +28 -0
  124. package/dist/storage/implementation/v3/PersistedBatchV3.js +259 -0
  125. package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -0
  126. package/dist/storage/implementation/v3/SingleBucketStoreV3.d.ts +18 -0
  127. package/dist/storage/implementation/v3/SingleBucketStoreV3.js +48 -0
  128. package/dist/storage/implementation/v3/SingleBucketStoreV3.js.map +1 -0
  129. package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +22 -0
  130. package/dist/storage/implementation/v3/SourceRecordStoreV3.js +164 -0
  131. package/dist/storage/implementation/v3/SourceRecordStoreV3.js.map +1 -0
  132. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +22 -0
  133. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +74 -0
  134. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -0
  135. package/dist/storage/implementation/v3/models.d.ts +101 -0
  136. package/dist/storage/implementation/v3/models.js +34 -0
  137. package/dist/storage/implementation/v3/models.js.map +1 -0
  138. package/dist/storage/storage-index.d.ts +6 -3
  139. package/dist/storage/storage-index.js +6 -3
  140. package/dist/storage/storage-index.js.map +1 -1
  141. package/dist/utils/util.d.ts +10 -3
  142. package/dist/utils/util.js +24 -3
  143. package/dist/utils/util.js.map +1 -1
  144. package/package.json +9 -9
  145. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +1 -1
  146. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +7 -7
  147. package/src/storage/MongoBucketStorage.ts +254 -99
  148. package/src/storage/implementation/BucketDefinitionMapping.ts +75 -0
  149. package/src/storage/implementation/CheckpointState.ts +59 -0
  150. package/src/storage/implementation/MongoBucketBatch.ts +182 -490
  151. package/src/storage/implementation/MongoBucketBatchShared.ts +11 -0
  152. package/src/storage/implementation/MongoChecksums.ts +53 -75
  153. package/src/storage/implementation/MongoCompactor.ts +374 -404
  154. package/src/storage/implementation/MongoParameterCompactor.ts +37 -24
  155. package/src/storage/implementation/MongoPersistedSyncRules.ts +82 -0
  156. package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +78 -16
  157. package/src/storage/implementation/MongoSyncBucketStorage.ts +179 -628
  158. package/src/storage/implementation/MongoSyncRulesLock.ts +20 -16
  159. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +3 -1
  160. package/src/storage/implementation/OperationBatch.ts +1 -1
  161. package/src/storage/implementation/SyncRuleStateUpdate.ts +38 -0
  162. package/src/storage/implementation/common/BucketDataDoc.ts +37 -0
  163. package/src/storage/implementation/common/MongoSyncBucketStorageContext.ts +15 -0
  164. package/src/storage/implementation/common/PersistedBatch.ts +364 -0
  165. package/src/storage/implementation/common/SingleBucketStore.ts +63 -0
  166. package/src/storage/implementation/common/SourceRecordStore.ts +48 -0
  167. package/src/storage/implementation/common/VersionedPowerSyncMongoBase.ts +80 -0
  168. package/src/storage/implementation/createMongoSyncBucketStorage.ts +25 -0
  169. package/src/storage/implementation/db.ts +110 -131
  170. package/src/storage/implementation/models.ts +102 -79
  171. package/src/storage/implementation/v1/MongoBucketBatchV1.ts +509 -0
  172. package/src/storage/implementation/v1/MongoChecksumsV1.ts +75 -0
  173. package/src/storage/implementation/v1/MongoCompactorV1.ts +93 -0
  174. package/src/storage/implementation/v1/MongoParameterCompactorV1.ts +26 -0
  175. package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +543 -0
  176. package/src/storage/implementation/v1/PersistedBatchV1.ts +229 -0
  177. package/src/storage/implementation/v1/SingleBucketStoreV1.ts +74 -0
  178. package/src/storage/implementation/v1/SourceRecordStoreV1.ts +156 -0
  179. package/src/storage/implementation/v1/VersionedPowerSyncMongoV1.ts +28 -0
  180. package/src/storage/implementation/v1/models.ts +99 -0
  181. package/src/storage/implementation/v3/MongoBucketBatchV3.ts +607 -0
  182. package/src/storage/implementation/v3/MongoChecksumsV3.ts +120 -0
  183. package/src/storage/implementation/v3/MongoCompactorV3.ts +107 -0
  184. package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +24 -0
  185. package/src/storage/implementation/v3/MongoParameterLookupV3.ts +11 -0
  186. package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +678 -0
  187. package/src/storage/implementation/v3/PersistedBatchV3.ts +317 -0
  188. package/src/storage/implementation/v3/SingleBucketStoreV3.ts +68 -0
  189. package/src/storage/implementation/v3/SourceRecordStoreV3.ts +226 -0
  190. package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +117 -0
  191. package/src/storage/implementation/v3/models.ts +164 -0
  192. package/src/storage/storage-index.ts +6 -3
  193. package/src/utils/util.ts +34 -5
  194. package/test/src/storage_compacting.test.ts +57 -29
  195. package/test/src/storage_sync.test.ts +767 -5
  196. package/test/src/storeCurrentData.test.ts +211 -0
  197. package/test/tsconfig.json +0 -1
  198. package/tsconfig.tsbuildinfo +1 -1
  199. package/dist/storage/implementation/PersistedBatch.d.ts +0 -71
  200. package/dist/storage/implementation/PersistedBatch.js +0 -354
  201. package/dist/storage/implementation/PersistedBatch.js.map +0 -1
  202. package/src/storage/implementation/PersistedBatch.ts +0 -432
@@ -0,0 +1,80 @@
1
+ import { mongo } from '@powersync/lib-service-mongodb';
2
+ import { DO_NOT_LOG } from '@powersync/lib-services-framework';
3
+ import { PowerSyncMongo } from '../db.js';
4
+ import { CommonSourceTableDocument, StorageConfig } from '../models.js';
5
+
6
+ export abstract class BaseVersionedPowerSyncMongo {
7
+ readonly client: mongo.MongoClient;
8
+ readonly db: mongo.Db;
9
+ readonly storageConfig: StorageConfig;
10
+ [DO_NOT_LOG] = true;
11
+
12
+ constructor(
13
+ protected readonly upstream: PowerSyncMongo,
14
+ storageConfig: StorageConfig
15
+ ) {
16
+ this.client = upstream.client;
17
+ this.db = upstream.db;
18
+ this.storageConfig = storageConfig;
19
+ }
20
+
21
+ get bucket_data() {
22
+ return this.upstream.bucket_data;
23
+ }
24
+
25
+ get op_id_sequence() {
26
+ return this.upstream.op_id_sequence;
27
+ }
28
+
29
+ get sync_rules() {
30
+ return this.upstream.sync_rules;
31
+ }
32
+
33
+ get custom_write_checkpoints() {
34
+ return this.upstream.custom_write_checkpoints;
35
+ }
36
+
37
+ get write_checkpoints() {
38
+ return this.upstream.write_checkpoints;
39
+ }
40
+
41
+ get instance() {
42
+ return this.upstream.instance;
43
+ }
44
+
45
+ get locks() {
46
+ return this.upstream.locks;
47
+ }
48
+
49
+ get checkpoint_events() {
50
+ return this.upstream.checkpoint_events;
51
+ }
52
+
53
+ get connection_report_events() {
54
+ return this.upstream.connection_report_events;
55
+ }
56
+
57
+ notifyCheckpoint() {
58
+ return this.upstream.notifyCheckpoint();
59
+ }
60
+
61
+ protected sourceRecordsCollectionName(replicationStreamId: number, sourceTableId: mongo.ObjectId) {
62
+ return this.upstream.sourceRecordsCollectionName(replicationStreamId, sourceTableId);
63
+ }
64
+
65
+ protected sourceTableCollectionName(replicationStreamId: number) {
66
+ return this.upstream.sourceTableCollectionName(replicationStreamId);
67
+ }
68
+
69
+ protected async listCollectionsByPrefix<T extends mongo.Document>(prefix: string): Promise<mongo.Collection<T>[]> {
70
+ const collections = await this.db.listCollections({ name: new RegExp(`^${prefix}`) }, { nameOnly: true }).toArray();
71
+
72
+ return collections
73
+ .filter((collection) => collection.name.startsWith(prefix))
74
+ .map((collection) => this.db.collection<T>(collection.name));
75
+ }
76
+
77
+ abstract commonSourceTables(replicationStreamId: number): mongo.Collection<CommonSourceTableDocument>;
78
+
79
+ abstract initializeStreamStorage(replicationStreamId: number): Promise<void>;
80
+ }
@@ -0,0 +1,25 @@
1
+ import { storage } from '@powersync/service-core';
2
+ import { MongoBucketStorage } from '../MongoBucketStorage.js';
3
+ import { MongoPersistedSyncRulesContentV1 } from './MongoPersistedSyncRulesContent.js';
4
+ import { MongoSyncBucketStorage, MongoSyncBucketStorageOptions } from './MongoSyncBucketStorage.js';
5
+ import { MongoSyncBucketStorageV1 } from './v1/MongoSyncBucketStorageV1.js';
6
+ import { MongoSyncBucketStorageV3 } from './v3/MongoSyncBucketStorageV3.js';
7
+
8
+ export { MongoSyncBucketStorageOptions } from './MongoSyncBucketStorage.js';
9
+
10
+ export type { MongoSyncBucketStorage };
11
+
12
+ export function createMongoSyncBucketStorage(
13
+ factory: MongoBucketStorage,
14
+ group_id: number,
15
+ sync_rules: MongoPersistedSyncRulesContentV1,
16
+ slot_name: string,
17
+ writeCheckpointMode: storage.WriteCheckpointMode | undefined,
18
+ options: MongoSyncBucketStorageOptions
19
+ ): MongoSyncBucketStorage {
20
+ if (sync_rules.getStorageConfig().incrementalReprocessing) {
21
+ return new MongoSyncBucketStorageV3(factory, group_id, sync_rules, slot_name, writeCheckpointMode, options);
22
+ }
23
+
24
+ return new MongoSyncBucketStorageV1(factory, group_id, sync_rules, slot_name, writeCheckpointMode, options);
25
+ }
@@ -2,24 +2,30 @@ import * as lib_mongo from '@powersync/lib-service-mongodb';
2
2
  import { mongo } from '@powersync/lib-service-mongodb';
3
3
  import { POWERSYNC_VERSION, storage } from '@powersync/service-core';
4
4
 
5
- import { DO_NOT_LOG, ServiceAssertionError } from '@powersync/lib-services-framework';
5
+ import { DO_NOT_LOG } from '@powersync/lib-services-framework';
6
6
  import { MongoStorageConfig } from '../../types/types.js';
7
+ import { BaseVersionedPowerSyncMongo } from './common/VersionedPowerSyncMongoBase.js';
7
8
  import {
8
- BucketDataDocument,
9
- BucketParameterDocument,
10
- BucketStateDocument,
11
9
  CheckpointEventDocument,
12
10
  ClientConnectionDocument,
13
- CurrentDataDocument,
14
- CurrentDataDocumentV3,
11
+ CommonSourceTableDocument,
15
12
  CustomWriteCheckpointDocument,
16
13
  IdSequenceDocument,
17
14
  InstanceDocument,
18
15
  SourceTableDocument,
19
16
  StorageConfig,
20
- SyncRuleDocument,
17
+ SyncRuleDocumentBase,
21
18
  WriteCheckpointDocument
22
19
  } from './models.js';
20
+ import {
21
+ BucketDataDocumentV1,
22
+ BucketParameterDocument,
23
+ BucketStateDocumentV1,
24
+ CurrentDataDocument
25
+ } from './v1/models.js';
26
+ import { VersionedPowerSyncMongoV1 } from './v1/VersionedPowerSyncMongoV1.js';
27
+ import { BucketDataDocumentV3 } from './v3/models.js';
28
+ import { VersionedPowerSyncMongoV3 } from './v3/VersionedPowerSyncMongoV3.js';
23
29
 
24
30
  export interface PowerSyncMongoOptions {
25
31
  /**
@@ -32,17 +38,16 @@ export class PowerSyncMongo {
32
38
  [DO_NOT_LOG] = true;
33
39
 
34
40
  readonly current_data: mongo.Collection<CurrentDataDocument>;
35
- readonly v3_current_data: mongo.Collection<CurrentDataDocumentV3>;
36
- readonly bucket_data: mongo.Collection<BucketDataDocument>;
41
+ readonly bucket_data: mongo.Collection<BucketDataDocumentV1>;
37
42
  readonly bucket_parameters: mongo.Collection<BucketParameterDocument>;
38
43
  readonly op_id_sequence: mongo.Collection<IdSequenceDocument>;
39
- readonly sync_rules: mongo.Collection<SyncRuleDocument>;
44
+ readonly sync_rules: mongo.Collection<SyncRuleDocumentBase>;
40
45
  readonly source_tables: mongo.Collection<SourceTableDocument>;
41
46
  readonly custom_write_checkpoints: mongo.Collection<CustomWriteCheckpointDocument>;
42
47
  readonly write_checkpoints: mongo.Collection<WriteCheckpointDocument>;
43
48
  readonly instance: mongo.Collection<InstanceDocument>;
44
49
  readonly locks: mongo.Collection<lib_mongo.locks.Lock>;
45
- readonly bucket_state: mongo.Collection<BucketStateDocument>;
50
+ readonly bucket_state: mongo.Collection<BucketStateDocumentV1>;
46
51
  readonly checkpoint_events: mongo.Collection<CheckpointEventDocument>;
47
52
  readonly connection_report_events: mongo.Collection<ClientConnectionDocument>;
48
53
 
@@ -58,7 +63,6 @@ export class PowerSyncMongo {
58
63
  this.db = db;
59
64
 
60
65
  this.current_data = db.collection('current_data');
61
- this.v3_current_data = db.collection('v3_current_data');
62
66
  this.bucket_data = db.collection('bucket_data');
63
67
  this.bucket_parameters = db.collection('bucket_parameters');
64
68
  this.op_id_sequence = db.collection('op_id_sequence');
@@ -73,8 +77,83 @@ export class PowerSyncMongo {
73
77
  this.connection_report_events = this.db.collection('connection_report_events');
74
78
  }
75
79
 
76
- versioned(storageConfig: StorageConfig) {
77
- return new VersionedPowerSyncMongo(this, storageConfig);
80
+ versioned(storageConfig: StorageConfig & { incrementalReprocessing: true }): VersionedPowerSyncMongoV3;
81
+ versioned(storageConfig: StorageConfig & { incrementalReprocessing: false }): VersionedPowerSyncMongoV1;
82
+ versioned(storageConfig: StorageConfig): VersionedPowerSyncMongo;
83
+ versioned(storageConfig: StorageConfig): VersionedPowerSyncMongo {
84
+ if (storageConfig.incrementalReprocessing) {
85
+ return new VersionedPowerSyncMongoV3(this, storageConfig);
86
+ }
87
+
88
+ return new VersionedPowerSyncMongoV1(this, storageConfig);
89
+ }
90
+
91
+ /**
92
+ * Not safe for user-provided prefix - only for hardcoded values.
93
+ */
94
+ async listBucketDataCollectionsV3(groupId?: number): Promise<mongo.Collection<BucketDataDocumentV3>[]> {
95
+ const prefix = groupId == null ? 'bucket_data_' : `bucket_data_${groupId}_`;
96
+ const collections = await this.db.listCollections({ name: new RegExp(`^${prefix}`) }, { nameOnly: true }).toArray();
97
+
98
+ return collections
99
+ .filter((collection) => collection.name.startsWith(prefix))
100
+ .map((collection) => this.db.collection<BucketDataDocumentV3>(collection.name));
101
+ }
102
+
103
+ /**
104
+ * Not safe for user-provided prefix - only for hardcoded values.
105
+ */
106
+ private async collectionsByPrefix(prefix: string): Promise<mongo.Collection<never>[]> {
107
+ const collections = await this.db.listCollections({ name: new RegExp(`^${prefix}`) }, { nameOnly: true }).toArray();
108
+
109
+ return collections
110
+ .filter((collection) => collection.name.startsWith(prefix))
111
+ .map((collection) => this.db.collection<never>(collection.name));
112
+ }
113
+
114
+ /**
115
+ * List all parameter index collections across all replication streams.
116
+ *
117
+ * Primarily used to clear the db.
118
+ */
119
+ async listAllParameterIndexCollectionsV3(): Promise<mongo.Collection<never>[]> {
120
+ return this.collectionsByPrefix(`parameter_index_`);
121
+ }
122
+
123
+ /**
124
+ * List all parameter index collections across all replication streams.
125
+ *
126
+ * Primarily used to clear the db.
127
+ */
128
+ async listAllSourceRecordCollectionsV3(): Promise<mongo.Collection<never>[]> {
129
+ return this.collectionsByPrefix(`source_records_`);
130
+ }
131
+
132
+ async listAllBucketStateCollectionsV3(): Promise<mongo.Collection<never>[]> {
133
+ return this.collectionsByPrefix(`bucket_state_`);
134
+ }
135
+
136
+ sourceRecordsCollectionName(replicationStreamId: number, sourceTableId: mongo.ObjectId) {
137
+ return `source_records_${replicationStreamId}_${sourceTableId.toHexString()}`;
138
+ }
139
+
140
+ sourceTableCollectionName(replicationStreamId: number) {
141
+ return `source_table_${replicationStreamId}`;
142
+ }
143
+
144
+ async listSourceTableCollections(
145
+ replicationStreamId?: number
146
+ ): Promise<mongo.Collection<CommonSourceTableDocument>[]> {
147
+ const filter =
148
+ replicationStreamId == null
149
+ ? { name: new RegExp('^source_table_') }
150
+ : { name: this.sourceTableCollectionName(replicationStreamId) };
151
+ const prefix = replicationStreamId == null ? 'source_table_' : this.sourceTableCollectionName(replicationStreamId);
152
+ const collections = await this.db.listCollections(filter, { nameOnly: true }).toArray();
153
+
154
+ return collections
155
+ .filter((collection) => collection.name.startsWith(prefix))
156
+ .map((collection) => this.db.collection<CommonSourceTableDocument>(collection.name));
78
157
  }
79
158
 
80
159
  /**
@@ -82,11 +161,25 @@ export class PowerSyncMongo {
82
161
  */
83
162
  async clear() {
84
163
  await this.current_data.deleteMany({});
85
- await this.v3_current_data.deleteMany({});
164
+ for (const collection of await this.listAllSourceRecordCollectionsV3()) {
165
+ await collection.drop();
166
+ }
86
167
  await this.bucket_data.deleteMany({});
168
+ for (const collection of await this.listBucketDataCollectionsV3()) {
169
+ await collection.drop();
170
+ }
87
171
  await this.bucket_parameters.deleteMany({});
172
+ for (const collection of await this.listAllParameterIndexCollectionsV3()) {
173
+ await collection.drop();
174
+ }
175
+ for (const collection of await this.listAllBucketStateCollectionsV3()) {
176
+ await collection.drop();
177
+ }
88
178
  await this.op_id_sequence.deleteMany({});
89
179
  await this.sync_rules.deleteMany({});
180
+ for (const collection of await this.listSourceTableCollections()) {
181
+ await collection.drop();
182
+ }
90
183
  await this.source_tables.deleteMany({});
91
184
  await this.write_checkpoints.deleteMany({});
92
185
  await this.instance.deleteOne({});
@@ -105,7 +198,7 @@ export class PowerSyncMongo {
105
198
  }
106
199
 
107
200
  /**
108
- * Call this after every checkpoint or sync rules status update. Rather call too often than too rarely.
201
+ * Call this after every checkpoint or replication stream status update. Rather call too often than too rarely.
109
202
  *
110
203
  * This is used in a similar way to the Postgres NOTIFY functionality.
111
204
  */
@@ -183,126 +276,12 @@ export class PowerSyncMongo {
183
276
  { name: 'dirty_count' }
184
277
  );
185
278
  }
186
-
187
- async initializeStorageVersion(storageConfig: StorageConfig) {
188
- if (storageConfig.softDeleteCurrentData) {
189
- // Initialize the v3_current_data collection, which is used for the new storage version.
190
- // No-op if this already exists
191
- await this.v3_current_data.createIndex(
192
- {
193
- '_id.g': 1,
194
- pending_delete: 1
195
- },
196
- {
197
- partialFilterExpression: { pending_delete: { $exists: true } },
198
- name: 'pending_delete'
199
- }
200
- );
201
- }
202
- }
203
279
  }
204
280
 
205
281
  /**
206
282
  * This is similar to PowerSyncMongo, but blocks access to certain collections based on the storage version.
207
283
  */
208
- export class VersionedPowerSyncMongo {
209
- readonly client: mongo.MongoClient;
210
- readonly db: mongo.Db;
211
- [DO_NOT_LOG] = true;
212
-
213
- readonly storageConfig: StorageConfig;
214
- #upstream: PowerSyncMongo;
215
-
216
- constructor(upstream: PowerSyncMongo, storageConfig: StorageConfig) {
217
- this.#upstream = upstream;
218
- this.client = upstream.client;
219
- this.db = upstream.db;
220
- this.storageConfig = storageConfig;
221
- }
222
-
223
- /**
224
- * Uses either `current_data` or `v3_current_data` collection based on the storage version.
225
- *
226
- * Use in places where it does not matter which version is used.
227
- */
228
- get common_current_data(): mongo.Collection<CurrentDataDocument> {
229
- if (this.storageConfig.softDeleteCurrentData) {
230
- return this.#upstream.v3_current_data;
231
- } else {
232
- return this.#upstream.current_data;
233
- }
234
- }
235
-
236
- get v1_current_data() {
237
- if (this.storageConfig.softDeleteCurrentData) {
238
- throw new ServiceAssertionError(
239
- 'current_data collection should not be used when softDeleteCurrentData is enabled'
240
- );
241
- }
242
- return this.#upstream.current_data;
243
- }
244
-
245
- get v3_current_data() {
246
- if (!this.storageConfig.softDeleteCurrentData) {
247
- throw new ServiceAssertionError(
248
- 'v3_current_data collection should not be used when softDeleteCurrentData is disabled'
249
- );
250
- }
251
- return this.#upstream.v3_current_data;
252
- }
253
-
254
- get bucket_data() {
255
- return this.#upstream.bucket_data;
256
- }
257
-
258
- get bucket_parameters() {
259
- return this.#upstream.bucket_parameters;
260
- }
261
-
262
- get op_id_sequence() {
263
- return this.#upstream.op_id_sequence;
264
- }
265
-
266
- get sync_rules() {
267
- return this.#upstream.sync_rules;
268
- }
269
-
270
- get source_tables() {
271
- return this.#upstream.source_tables;
272
- }
273
-
274
- get custom_write_checkpoints() {
275
- return this.#upstream.custom_write_checkpoints;
276
- }
277
-
278
- get write_checkpoints() {
279
- return this.#upstream.write_checkpoints;
280
- }
281
-
282
- get instance() {
283
- return this.#upstream.instance;
284
- }
285
-
286
- get locks() {
287
- return this.#upstream.locks;
288
- }
289
-
290
- get bucket_state() {
291
- return this.#upstream.bucket_state;
292
- }
293
-
294
- get checkpoint_events() {
295
- return this.#upstream.checkpoint_events;
296
- }
297
-
298
- get connection_report_events() {
299
- return this.#upstream.connection_report_events;
300
- }
301
-
302
- notifyCheckpoint() {
303
- return this.#upstream.notifyCheckpoint();
304
- }
305
- }
284
+ export type VersionedPowerSyncMongo = BaseVersionedPowerSyncMongo;
306
285
 
307
286
  export function createPowerSyncMongo(config: MongoStorageConfig, options?: lib_mongo.MongoConnectionOptions) {
308
287
  return new PowerSyncMongo(