@powersync/service-module-mongodb-storage 0.20.0 → 0.21.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 (176) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/dist/storage/MongoBucketStorage.d.ts +31 -3
  3. package/dist/storage/MongoBucketStorage.js +365 -206
  4. package/dist/storage/MongoBucketStorage.js.map +1 -1
  5. package/dist/storage/implementation/MongoBucketBatch.d.ts +15 -4
  6. package/dist/storage/implementation/MongoBucketBatch.js +35 -9
  7. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
  8. package/dist/storage/implementation/MongoCompactor.d.ts +19 -79
  9. package/dist/storage/implementation/MongoCompactor.js +27 -238
  10. package/dist/storage/implementation/MongoCompactor.js.map +1 -1
  11. package/dist/storage/implementation/MongoParameterCompactor.d.ts +83 -10
  12. package/dist/storage/implementation/MongoParameterCompactor.js +309 -96
  13. package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
  14. package/dist/storage/implementation/MongoParsedSyncConfigSet.d.ts +9 -1
  15. package/dist/storage/implementation/MongoParsedSyncConfigSet.js +29 -0
  16. package/dist/storage/implementation/MongoParsedSyncConfigSet.js.map +1 -1
  17. package/dist/storage/implementation/MongoPersistedSyncConfigContent.js +4 -2
  18. package/dist/storage/implementation/MongoPersistedSyncConfigContent.js.map +1 -1
  19. package/dist/storage/implementation/MongoStorageProvider.js +3 -1
  20. package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
  21. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +27 -11
  22. package/dist/storage/implementation/MongoSyncBucketStorage.js +78 -31
  23. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  24. package/dist/storage/implementation/MongoSyncRulesLock.js +21 -9
  25. package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
  26. package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +9 -7
  27. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +7 -28
  28. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
  29. package/dist/storage/implementation/SyncRuleStateUpdate.d.ts +11 -5
  30. package/dist/storage/implementation/SyncRuleStateUpdate.js +14 -6
  31. package/dist/storage/implementation/SyncRuleStateUpdate.js.map +1 -1
  32. package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.d.ts +22 -2
  33. package/dist/storage/implementation/common/PersistedBatch.d.ts +27 -0
  34. package/dist/storage/implementation/common/PersistedBatch.js +43 -2
  35. package/dist/storage/implementation/common/PersistedBatch.js.map +1 -1
  36. package/dist/storage/implementation/db.d.ts +4 -1
  37. package/dist/storage/implementation/db.js +14 -1
  38. package/dist/storage/implementation/db.js.map +1 -1
  39. package/dist/storage/implementation/models.d.ts +31 -0
  40. package/dist/storage/implementation/models.js.map +1 -1
  41. package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +2 -0
  42. package/dist/storage/implementation/v1/MongoBucketBatchV1.js +22 -0
  43. package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -1
  44. package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +36 -5
  45. package/dist/storage/implementation/v1/MongoCompactorV1.js +229 -8
  46. package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -1
  47. package/dist/storage/implementation/v1/MongoParameterCompactorV1.d.ts +16 -2
  48. package/dist/storage/implementation/v1/MongoParameterCompactorV1.js +20 -9
  49. package/dist/storage/implementation/v1/MongoParameterCompactorV1.js.map +1 -1
  50. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +18 -11
  51. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +65 -16
  52. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -1
  53. package/dist/storage/implementation/v1/models.js +1 -0
  54. package/dist/storage/implementation/v1/models.js.map +1 -1
  55. package/dist/storage/implementation/v3/CompactionLease.d.ts +50 -0
  56. package/dist/storage/implementation/v3/CompactionLease.js +131 -0
  57. package/dist/storage/implementation/v3/CompactionLease.js.map +1 -0
  58. package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +8 -1
  59. package/dist/storage/implementation/v3/MongoBucketBatchV3.js +90 -19
  60. package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
  61. package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +77 -25
  62. package/dist/storage/implementation/v3/MongoCompactorV3.js +585 -141
  63. package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -1
  64. package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +18 -0
  65. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +23 -0
  66. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +1 -0
  67. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.d.ts +2 -15
  68. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js +60 -92
  69. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js.map +1 -1
  70. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +19 -9
  71. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +91 -31
  72. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -1
  73. package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.d.ts +26 -0
  74. package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.js +82 -0
  75. package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.js.map +1 -0
  76. package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +2 -0
  77. package/dist/storage/implementation/v3/PersistedBatchV3.js +52 -11
  78. package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
  79. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +12 -2
  80. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +42 -3
  81. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -1
  82. package/dist/storage/implementation/v3/bucket-format.js +2 -0
  83. package/dist/storage/implementation/v3/bucket-format.js.map +1 -1
  84. package/dist/storage/implementation/v3/compact-utils.d.ts +107 -0
  85. package/dist/storage/implementation/v3/compact-utils.js +216 -0
  86. package/dist/storage/implementation/v3/compact-utils.js.map +1 -0
  87. package/dist/storage/implementation/v3/compaction-constants.d.ts +5 -0
  88. package/dist/storage/implementation/v3/compaction-constants.js +6 -0
  89. package/dist/storage/implementation/v3/compaction-constants.js.map +1 -0
  90. package/dist/storage/implementation/v3/models.d.ts +84 -4
  91. package/dist/storage/implementation/v3/models.js.map +1 -1
  92. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.d.ts +7 -8
  93. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js +8 -4
  94. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js.map +1 -1
  95. package/dist/storage/implementation/v3/object-storage/ObjectStorage.d.ts +14 -11
  96. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.d.ts +3 -5
  97. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js +10 -2
  98. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js.map +1 -1
  99. package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.d.ts +37 -0
  100. package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.js +167 -0
  101. package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.js.map +1 -0
  102. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.d.ts +37 -12
  103. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js +177 -23
  104. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js.map +1 -1
  105. package/dist/storage/implementation/v3/source-table-utils.d.ts +6 -6
  106. package/dist/storage/implementation/v3/source-table-utils.js +30 -37
  107. package/dist/storage/implementation/v3/source-table-utils.js.map +1 -1
  108. package/dist/storage/storage-index.d.ts +2 -1
  109. package/dist/storage/storage-index.js +1 -0
  110. package/dist/storage/storage-index.js.map +1 -1
  111. package/dist/types/types.d.ts +23 -1
  112. package/dist/types/types.js +16 -7
  113. package/dist/types/types.js.map +1 -1
  114. package/dist/utils/util.js +3 -1
  115. package/dist/utils/util.js.map +1 -1
  116. package/package.json +10 -9
  117. package/src/storage/MongoBucketStorage.ts +495 -268
  118. package/src/storage/implementation/MongoBucketBatch.ts +54 -14
  119. package/src/storage/implementation/MongoCompactor.ts +35 -346
  120. package/src/storage/implementation/MongoParameterCompactor.ts +388 -102
  121. package/src/storage/implementation/MongoParsedSyncConfigSet.ts +34 -0
  122. package/src/storage/implementation/MongoPersistedSyncConfigContent.ts +4 -2
  123. package/src/storage/implementation/MongoStorageProvider.ts +3 -1
  124. package/src/storage/implementation/MongoSyncBucketStorage.ts +114 -40
  125. package/src/storage/implementation/MongoSyncRulesLock.ts +22 -9
  126. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +12 -44
  127. package/src/storage/implementation/SyncRuleStateUpdate.ts +13 -6
  128. package/src/storage/implementation/common/MongoSyncBucketStorageCheckpoint.ts +24 -2
  129. package/src/storage/implementation/common/PersistedBatch.ts +54 -2
  130. package/src/storage/implementation/db.ts +21 -2
  131. package/src/storage/implementation/models.ts +34 -0
  132. package/src/storage/implementation/v1/MongoBucketBatchV1.ts +31 -0
  133. package/src/storage/implementation/v1/MongoCompactorV1.ts +303 -19
  134. package/src/storage/implementation/v1/MongoParameterCompactorV1.ts +25 -9
  135. package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +87 -27
  136. package/src/storage/implementation/v1/models.ts +1 -0
  137. package/src/storage/implementation/v3/CompactionLease.ts +152 -0
  138. package/src/storage/implementation/v3/MongoBucketBatchV3.ts +124 -22
  139. package/src/storage/implementation/v3/MongoCompactorV3.ts +744 -190
  140. package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +34 -0
  141. package/src/storage/implementation/v3/MongoStoppedSyncConfigCleanup.ts +100 -109
  142. package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +111 -41
  143. package/src/storage/implementation/v3/MongoWriteCheckpointAPIV3.ts +111 -0
  144. package/src/storage/implementation/v3/PersistedBatchV3.ts +57 -12
  145. package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +55 -4
  146. package/src/storage/implementation/v3/bucket-format.ts +2 -0
  147. package/src/storage/implementation/v3/compact-utils.ts +320 -0
  148. package/src/storage/implementation/v3/compaction-constants.ts +5 -0
  149. package/src/storage/implementation/v3/models.ts +91 -3
  150. package/src/storage/implementation/v3/object-storage/BucketDataObjectStorage.ts +16 -8
  151. package/src/storage/implementation/v3/object-storage/ObjectStorage.ts +20 -5
  152. package/src/storage/implementation/v3/object-storage/ObjectStorageLifecycle.ts +15 -4
  153. package/src/storage/implementation/v3/object-storage/ObjectStorageUsage.ts +230 -0
  154. package/src/storage/implementation/v3/object-storage/S3ObjectStorage.ts +257 -27
  155. package/src/storage/implementation/v3/source-table-utils.ts +39 -41
  156. package/src/storage/storage-index.ts +2 -0
  157. package/src/types/types.ts +24 -7
  158. package/src/utils/util.ts +3 -1
  159. package/test/src/__snapshots__/storage.test.ts.snap +25 -0
  160. package/test/src/__snapshots__/storage_sync.test.ts.snap +605 -16
  161. package/test/src/cleanup-stopped-sync-configs.test.ts +40 -13
  162. package/test/src/compact-utils.test.ts +226 -0
  163. package/test/src/helpers/MemoryObjectStorage.ts +14 -6
  164. package/test/src/object_storage_usage.test.ts +255 -0
  165. package/test/src/parameter_compacting_v1.test.ts +120 -0
  166. package/test/src/parameter_compacting_v3.test.ts +192 -0
  167. package/test/src/parameter_compaction_fence.test.ts +276 -0
  168. package/test/src/storage.test.ts +273 -22
  169. package/test/src/storage_compacting.test.ts +714 -244
  170. package/test/src/storage_s3_checksums.test.ts +3 -4
  171. package/test/src/storage_s3_compaction_lifecycle.test.ts +138 -12
  172. package/test/src/storage_s3_reading.test.ts +243 -1
  173. package/test/src/storage_s3_writing.test.ts +1 -0
  174. package/test/src/storage_sync.test.ts +336 -41
  175. package/test/src/util.ts +1 -1
  176. package/tsconfig.tsbuildinfo +1 -1
@@ -1,8 +1,10 @@
1
1
  import { mongoTestStorageFactoryGenerator } from '@module/utils/test-utils.js';
2
2
  import { mongo } from '@powersync/lib-service-mongodb';
3
3
  import { storage, updateSyncRulesFromYaml } from '@powersync/service-core';
4
- import { register, test_utils } from '@powersync/service-core-tests';
4
+ import { compactActive, register, test_utils } from '@powersync/service-core-tests';
5
5
  import { describe, expect, test } from 'vitest';
6
+ import { MongoSyncBucketStorage } from '../../src/storage/implementation/createMongoSyncBucketStorage.js';
7
+ import { VersionedPowerSyncMongoV3 } from '../../src/storage/implementation/v3/VersionedPowerSyncMongoV3.js';
6
8
  import { env } from './env.js';
7
9
  import { INITIALIZED_MONGO_STORAGE_FACTORY, TEST_STORAGE_VERSIONS } from './util.js';
8
10
 
@@ -115,36 +117,285 @@ bucket_definitions:
115
117
  await managedWriter.markAllSnapshotDone('1/1');
116
118
  await managedWriter.keepalive('8/0');
117
119
 
118
- await bucketStorage.compact({
120
+ await compactActive(factory, {
119
121
  compactBuckets: [],
120
122
  deleteCheckpointRequestsBefore: new Date('2024-02-01T00:00:00.000Z')
121
123
  });
122
124
  // The request is now both expired and processed, so it can be removed.
123
125
  await expect(factory.db.write_checkpoints.findOne({ user_id: 'user2' })).resolves.toBeNull();
124
126
 
125
- bucketStorage.setWriteCheckpointMode(storage.WriteCheckpointMode.CUSTOM);
126
- await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
127
- await writer.markAllSnapshotDone('1/1');
128
- const customCheckpointRequestedAt = new Date('2024-01-01T00:00:00.000Z');
129
- writer.addCustomWriteCheckpoint({
130
- user_id: 'custom1',
131
- checkpoint: 51n,
132
- checkpoint_requested_at: customCheckpointRequestedAt
133
- });
134
- await writer.flush();
135
- const customRequested = await factory.db.custom_write_checkpoints.findOne({ user_id: 'custom1' });
136
- expect(customRequested?.checkpoint_requested_at).toEqual(customCheckpointRequestedAt);
127
+ if (storageVersion < 3) {
128
+ bucketStorage.setWriteCheckpointMode({
129
+ mode: storage.WriteCheckpointMode.CUSTOM
130
+ });
131
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
132
+ await writer.markAllSnapshotDone('1/1');
133
+ const customCheckpointRequestedAt = new Date('2024-01-01T00:00:00.000Z');
134
+ writer.addCustomWriteCheckpoint({
135
+ user_id: 'custom1',
136
+ checkpoint: 51n,
137
+ checkpoint_requested_at: customCheckpointRequestedAt
138
+ });
139
+ await writer.flush();
140
+ const customRequested = await factory.db.custom_write_checkpoints.findOne({ user_id: 'custom1' });
141
+ expect(customRequested?.checkpoint_requested_at).toEqual(customCheckpointRequestedAt);
137
142
 
138
- writer.addCustomWriteCheckpoint({
139
- user_id: 'custom1',
140
- checkpoint: 52n
141
- });
142
- await writer.flush();
143
- const customGenerated = await factory.db.custom_write_checkpoints.findOne({ user_id: 'custom1' });
144
- expect(customGenerated).not.toBeNull();
145
- expect(customGenerated?.checkpoint_requested_at).toBeUndefined();
143
+ writer.addCustomWriteCheckpoint({
144
+ user_id: 'custom1',
145
+ checkpoint: 52n
146
+ });
147
+ await writer.flush();
148
+ const customGenerated = await factory.db.custom_write_checkpoints.findOne({ user_id: 'custom1' });
149
+ expect(customGenerated).not.toBeNull();
150
+ expect(customGenerated?.checkpoint_requested_at).toBeUndefined();
151
+ }
146
152
  });
147
153
 
154
+ test.runIf(storageVersion >= 3)(
155
+ 'stores and streams checkpoints from different event definitions independently',
156
+ async (context) => {
157
+ await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY.factory();
158
+ const syncRules = await factory.updateSyncRules(
159
+ updateSyncRulesFromYaml(
160
+ `
161
+ config:
162
+ edition: 3
163
+
164
+ streams:
165
+ global:
166
+ query: SELECT * FROM checkpoints
167
+
168
+ event_definitions:
169
+ checkpoint_a:
170
+ payloads:
171
+ - SELECT id FROM checkpoints WHERE kind = 'a'
172
+ checkpoint_b:
173
+ payloads:
174
+ - SELECT id FROM checkpoints WHERE kind = 'b'
175
+ unrelated_event:
176
+ payloads:
177
+ - SELECT id FROM checkpoints WHERE kind = 'unrelated'
178
+ `,
179
+ { storageVersion }
180
+ )
181
+ );
182
+ const bucketStorage = factory.getInstance(syncRules);
183
+ const db = bucketStorage.db as VersionedPowerSyncMongoV3;
184
+ const activeSyncConfig = bucketStorage.getParsedSyncRules({ defaultSchema: 'public' });
185
+ const eventA = activeSyncConfig.eventDescriptors.find((event) => event.name == 'checkpoint_a')!;
186
+ const eventB = activeSyncConfig.eventDescriptors.find((event) => event.name == 'checkpoint_b')!;
187
+ const unrelatedEvent = activeSyncConfig.eventDescriptors.find((event) => event.name == 'unrelated_event')!;
188
+ const mapping = syncRules.syncConfigContent[0].mapping;
189
+ const eventAId = mapping.eventId(eventA);
190
+ const eventBId = mapping.eventId(eventB);
191
+ const unrelatedEventId = mapping.eventId(unrelatedEvent);
192
+ expect(() =>
193
+ bucketStorage.setWriteCheckpointMode({
194
+ mode: storage.WriteCheckpointMode.CUSTOM
195
+ })
196
+ ).toThrow('eventName');
197
+ bucketStorage.setWriteCheckpointMode({
198
+ mode: storage.WriteCheckpointMode.CUSTOM,
199
+ eventName: 'unknown-event'
200
+ });
201
+ await expect(bucketStorage.lastWriteCheckpoint({ user_id: 'user1' })).rejects.toThrow(
202
+ 'No mapping found for event definition unknown-event'
203
+ );
204
+
205
+ bucketStorage.setWriteCheckpointMode({
206
+ mode: storage.WriteCheckpointMode.CUSTOM,
207
+ eventName: eventA.name
208
+ });
209
+ // Reads before the first checkpoint must behave like an empty result;
210
+ // they must not require the lazily-created collection to exist.
211
+ await expect(bucketStorage.lastWriteCheckpoint({ user_id: 'user1' })).resolves.toBeNull();
212
+
213
+ await using invalidWriter = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
214
+ invalidWriter.addCustomWriteCheckpoint({ user_id: 'invalid', checkpoint: 1n });
215
+ await expect(invalidWriter.flush()).rejects.toThrow('require an event definition id');
216
+ await expect(db.listCustomCheckpointRequestCollections(syncRules.replicationStreamId)).resolves.toEqual([]);
217
+
218
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
219
+ await writer.markAllSnapshotDone('1/1');
220
+ const abortController = new AbortController();
221
+ context.onTestFinished(() => abortController.abort());
222
+ const iter = bucketStorage
223
+ .watchCheckpointChanges({
224
+ user_id: 'user1',
225
+ signal: abortController.signal
226
+ })
227
+ [Symbol.asyncIterator]();
228
+
229
+ writer.addCustomWriteCheckpoint({ user_id: 'user1', checkpoint: 5n, event_id: eventAId });
230
+ writer.addCustomWriteCheckpoint({ user_id: 'user1', checkpoint: 8n, event_id: eventBId });
231
+ await writer.flush();
232
+ await writer.keepalive('5/0');
233
+
234
+ const eventACollection = db.customCheckpointRequests({
235
+ replicationStreamId: syncRules.replicationStreamId,
236
+ eventId: eventAId
237
+ });
238
+ const eventBCollection = db.customCheckpointRequests({
239
+ replicationStreamId: syncRules.replicationStreamId,
240
+ eventId: eventBId
241
+ });
242
+ await expect(eventACollection.findOne({ user_id: 'user1' })).resolves.toMatchObject({ checkpoint: 5n });
243
+ await expect(eventBCollection.findOne({ user_id: 'user1' })).resolves.toMatchObject({ checkpoint: 8n });
244
+ const checkpointCollectionNames = (
245
+ await db.listCustomCheckpointRequestCollections(syncRules.replicationStreamId)
246
+ )
247
+ .map((collection) => collection.collectionName)
248
+ .sort();
249
+ expect(checkpointCollectionNames).toEqual(
250
+ [eventACollection.collectionName, eventBCollection.collectionName].sort()
251
+ );
252
+ expect(checkpointCollectionNames).not.toContain(
253
+ db.customCheckpointRequests({
254
+ replicationStreamId: syncRules.replicationStreamId,
255
+ eventId: unrelatedEventId
256
+ }).collectionName
257
+ );
258
+ await expect(eventACollection.indexExists(['user_unique', 'op_id', 'checkpoint_requested_at'])).resolves.toBe(
259
+ true
260
+ );
261
+ await expect(bucketStorage.lastWriteCheckpoint({ user_id: 'user1' })).resolves.toEqual(5n);
262
+ await expect(iter.next()).resolves.toMatchObject({
263
+ done: false,
264
+ value: { writeCheckpoint: 5n }
265
+ });
266
+
267
+ writer.addCustomWriteCheckpoint({ user_id: 'user1', checkpoint: 9n, event_id: eventBId });
268
+ await writer.flush();
269
+ await writer.keepalive('6/0');
270
+ // The processing event's record does not advance clients still served by
271
+ // the active event definition.
272
+ await expect(iter.next()).resolves.toMatchObject({
273
+ done: false,
274
+ value: { writeCheckpoint: 5n }
275
+ });
276
+
277
+ bucketStorage.setWriteCheckpointMode({
278
+ mode: storage.WriteCheckpointMode.CUSTOM,
279
+ eventName: eventB.name
280
+ });
281
+ await expect(bucketStorage.lastWriteCheckpoint({ user_id: 'user1' })).resolves.toEqual(9n);
282
+
283
+ writer.addCustomWriteCheckpoint({
284
+ user_id: 'temporary',
285
+ checkpoint: 10n,
286
+ event_id: eventBId,
287
+ checkpoint_requested_at: new Date('2024-01-01T00:00:00.000Z')
288
+ });
289
+ await writer.flush();
290
+ await compactActive(factory, {
291
+ compactBuckets: [],
292
+ deleteCheckpointRequestsBefore: new Date('2024-01-02T00:00:00.000Z')
293
+ });
294
+ await expect(eventBCollection.findOne({ user_id: 'temporary' })).resolves.toBeNull();
295
+
296
+ // A fresh storage instance has an empty initialization cache, just like
297
+ // one created after a service restart. Recreating the existing indexes
298
+ // must be idempotent before another checkpoint is written.
299
+ const freshBucketStorage = factory.getInstance(syncRules);
300
+ await using freshWriter = await freshBucketStorage.createWriter(test_utils.BATCH_OPTIONS);
301
+ freshWriter.addCustomWriteCheckpoint({
302
+ user_id: 'after-restart',
303
+ checkpoint: 11n,
304
+ event_id: eventAId
305
+ });
306
+ await freshWriter.flush();
307
+ await expect(eventACollection.findOne({ user_id: 'after-restart' })).resolves.toMatchObject({
308
+ checkpoint: 11n
309
+ });
310
+
311
+ await bucketStorage.clear();
312
+ await expect(db.listCustomCheckpointRequestCollections(syncRules.replicationStreamId)).resolves.toEqual([]);
313
+ }
314
+ );
315
+
316
+ test.runIf(storageVersion >= 3)(
317
+ 'resolves custom checkpoints through the active event mapping across deployments',
318
+ async () => {
319
+ await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY.factory();
320
+ const syncConfigYaml = (predicate: string, includeExtraStream: boolean) => `
321
+ config:
322
+ edition: 3
323
+
324
+ streams:
325
+ global:
326
+ query: SELECT * FROM checkpoints
327
+ ${includeExtraStream ? ' todos:\n query: SELECT * FROM todos' : ''}
328
+
329
+ event_definitions:
330
+ write_checkpoints:
331
+ payloads:
332
+ - SELECT user_id, checkpoint FROM checkpoints WHERE ${predicate}
333
+ `;
334
+
335
+ const first = await factory.updateSyncRules(
336
+ updateSyncRulesFromYaml(syncConfigYaml("kind = 'write'", false), { storageVersion })
337
+ );
338
+ const firstEventId = first.syncConfigContent[0].mapping.eventDefinitionIdByName('write_checkpoints');
339
+ const firstStorage = factory.getInstance(first);
340
+ await using firstWriter = await firstStorage.createWriter(test_utils.BATCH_OPTIONS);
341
+ firstWriter.addCustomWriteCheckpoint({ user_id: 'user1', checkpoint: 5n, event_id: firstEventId });
342
+ await firstWriter.markAllSnapshotDone('1/1');
343
+ await firstWriter.commit('1/1');
344
+
345
+ const unchanged = await factory.updateSyncRules(
346
+ updateSyncRulesFromYaml(syncConfigYaml("kind = 'write'", true), { storageVersion })
347
+ );
348
+ const unchangedEventId = unchanged.syncConfigContent[1].mapping.eventDefinitionIdByName('write_checkpoints');
349
+ expect(unchangedEventId).toBe(firstEventId);
350
+
351
+ await using unchangedWriter = await factory.getInstance(unchanged).createWriter(test_utils.BATCH_OPTIONS);
352
+ await unchangedWriter.markAllSnapshotDone('2/1');
353
+ await unchangedWriter.commit('2/1');
354
+
355
+ const unchangedActiveStorage = (await factory.getActiveSyncConfig())!.storage as MongoSyncBucketStorage;
356
+ unchangedActiveStorage.setWriteCheckpointMode({
357
+ mode: storage.WriteCheckpointMode.CUSTOM,
358
+ eventName: 'write_checkpoints'
359
+ });
360
+ await expect(
361
+ unchangedActiveStorage.lastWriteCheckpoint({
362
+ user_id: 'user1'
363
+ })
364
+ ).resolves.toBe(5n);
365
+
366
+ const changed = await factory.updateSyncRules(
367
+ updateSyncRulesFromYaml(syncConfigYaml("kind = 'processed'", true), { storageVersion })
368
+ );
369
+ const changedEventId = changed.syncConfigContent[1].mapping.eventDefinitionIdByName('write_checkpoints');
370
+ expect(changedEventId).not.toBe(unchangedEventId);
371
+
372
+ await using changedWriter = await factory.getInstance(changed).createWriter(test_utils.BATCH_OPTIONS);
373
+ changedWriter.addCustomWriteCheckpoint({ user_id: 'user1', checkpoint: 9n, event_id: changedEventId });
374
+ await changedWriter.flush();
375
+
376
+ // Until activation, reads still use the previous config's assigned id and collection.
377
+ await expect(
378
+ unchangedActiveStorage.lastWriteCheckpoint({
379
+ user_id: 'user1'
380
+ })
381
+ ).resolves.toBe(5n);
382
+
383
+ await changedWriter.markAllSnapshotDone('3/1');
384
+ await changedWriter.commit('3/1');
385
+
386
+ const changedActiveStorage = (await factory.getActiveSyncConfig())!.storage as MongoSyncBucketStorage;
387
+ changedActiveStorage.setWriteCheckpointMode({
388
+ mode: storage.WriteCheckpointMode.CUSTOM,
389
+ eventName: 'write_checkpoints'
390
+ });
391
+ await expect(
392
+ changedActiveStorage.lastWriteCheckpoint({
393
+ user_id: 'user1'
394
+ })
395
+ ).resolves.toBe(9n);
396
+ }
397
+ );
398
+
148
399
  /**
149
400
  * It's extremely rare (but technically possible) to have duplicate write checkpoint records
150
401
  * for a full user_id. This is normally not an issue for checkpoints requested from `write-checkpoint2.json`,