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

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 (239) hide show
  1. package/CHANGELOG.md +101 -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 +41 -16
  6. package/dist/storage/MongoBucketStorage.js +226 -71
  7. package/dist/storage/MongoBucketStorage.js.map +1 -1
  8. package/dist/storage/implementation/MongoBucketBatch.d.ts +56 -25
  9. package/dist/storage/implementation/MongoBucketBatch.js +110 -62
  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 +17 -19
  15. package/dist/storage/implementation/MongoCompactor.js +80 -263
  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 +7 -6
  21. package/dist/storage/implementation/MongoParameterCompactor.js +25 -2
  22. package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
  23. package/dist/storage/implementation/MongoParsedSyncConfigSet.d.ts +13 -0
  24. package/dist/storage/implementation/MongoParsedSyncConfigSet.js +64 -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 +123 -0
  31. package/dist/storage/implementation/MongoPersistedSyncConfigContent.js.map +1 -0
  32. package/dist/storage/implementation/MongoStorageProvider.js +39 -4
  33. package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
  34. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +57 -29
  35. package/dist/storage/implementation/MongoSyncBucketStorage.js +87 -61
  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 +3 -3
  39. package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
  40. package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +2 -1
  41. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +171 -24
  42. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
  43. package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.d.ts +8 -0
  44. package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.js +2 -0
  45. package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.js.map +1 -0
  46. package/dist/storage/implementation/common/PersistedBatch.d.ts +7 -2
  47. package/dist/storage/implementation/common/PersistedBatch.js +12 -0
  48. package/dist/storage/implementation/common/PersistedBatch.js.map +1 -1
  49. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js +1 -1
  50. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js.map +1 -1
  51. package/dist/storage/implementation/createMongoSyncBucketStorage.d.ts +2 -2
  52. package/dist/storage/implementation/createMongoSyncBucketStorage.js +4 -4
  53. package/dist/storage/implementation/createMongoSyncBucketStorage.js.map +1 -1
  54. package/dist/storage/implementation/db.js +17 -15
  55. package/dist/storage/implementation/db.js.map +1 -1
  56. package/dist/storage/implementation/models.d.ts +20 -13
  57. package/dist/storage/implementation/models.js.map +1 -1
  58. package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +9 -1
  59. package/dist/storage/implementation/v1/MongoBucketBatchV1.js +61 -29
  60. package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -1
  61. package/dist/storage/implementation/v1/MongoChecksumsV1.d.ts +4 -4
  62. package/dist/storage/implementation/v1/MongoChecksumsV1.js +119 -7
  63. package/dist/storage/implementation/v1/MongoChecksumsV1.js.map +1 -1
  64. package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +11 -2
  65. package/dist/storage/implementation/v1/MongoCompactorV1.js +251 -2
  66. package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -1
  67. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +18 -19
  68. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +82 -58
  69. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -1
  70. package/dist/storage/implementation/v1/SingleBucketStoreV1.d.ts +9 -10
  71. package/dist/storage/implementation/v1/SingleBucketStoreV1.js +2 -8
  72. package/dist/storage/implementation/v1/SingleBucketStoreV1.js.map +1 -1
  73. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.d.ts +2 -1
  74. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js +3 -0
  75. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js.map +1 -1
  76. package/dist/storage/implementation/v1/models.d.ts +10 -1
  77. package/dist/storage/implementation/v3/BucketDataContextV3.d.ts +13 -0
  78. package/dist/storage/implementation/v3/BucketDataContextV3.js +23 -0
  79. package/dist/storage/implementation/v3/BucketDataContextV3.js.map +1 -0
  80. package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +8 -4
  81. package/dist/storage/implementation/v3/MongoBucketBatchV3.js +309 -259
  82. package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
  83. package/dist/storage/implementation/v3/MongoChecksumsV3.d.ts +30 -9
  84. package/dist/storage/implementation/v3/MongoChecksumsV3.js +161 -20
  85. package/dist/storage/implementation/v3/MongoChecksumsV3.js.map +1 -1
  86. package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +40 -2
  87. package/dist/storage/implementation/v3/MongoCompactorV3.js +647 -23
  88. package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -1
  89. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.d.ts +67 -0
  90. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js +351 -0
  91. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js.map +1 -0
  92. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +45 -30
  93. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +272 -140
  94. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -1
  95. package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +6 -3
  96. package/dist/storage/implementation/v3/PersistedBatchV3.js +84 -21
  97. package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
  98. package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +3 -3
  99. package/dist/storage/implementation/v3/SourceRecordStoreV3.js +13 -11
  100. package/dist/storage/implementation/v3/SourceRecordStoreV3.js.map +1 -1
  101. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +15 -13
  102. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +32 -27
  103. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -1
  104. package/dist/storage/implementation/v3/bucket-format.d.ts +13 -0
  105. package/dist/storage/implementation/v3/bucket-format.js +77 -0
  106. package/dist/storage/implementation/v3/bucket-format.js.map +1 -0
  107. package/dist/storage/implementation/v3/chunking.d.ts +10 -0
  108. package/dist/storage/implementation/v3/chunking.js +30 -0
  109. package/dist/storage/implementation/v3/chunking.js.map +1 -0
  110. package/dist/storage/implementation/v3/models.d.ts +101 -21
  111. package/dist/storage/implementation/v3/models.js +11 -28
  112. package/dist/storage/implementation/v3/models.js.map +1 -1
  113. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.d.ts +21 -0
  114. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js +44 -0
  115. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js.map +1 -0
  116. package/dist/storage/implementation/v3/object-storage/ObjectStorage.d.ts +30 -0
  117. package/dist/storage/implementation/v3/object-storage/ObjectStorage.js +12 -0
  118. package/dist/storage/implementation/v3/object-storage/ObjectStorage.js.map +1 -0
  119. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.d.ts +70 -0
  120. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js +146 -0
  121. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js.map +1 -0
  122. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.d.ts +42 -0
  123. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js +285 -0
  124. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js.map +1 -0
  125. package/dist/storage/implementation/v3/source-table-utils.d.ts +55 -0
  126. package/dist/storage/implementation/v3/source-table-utils.js +214 -0
  127. package/dist/storage/implementation/v3/source-table-utils.js.map +1 -0
  128. package/dist/storage/storage-index.d.ts +4 -4
  129. package/dist/storage/storage-index.js +4 -4
  130. package/dist/storage/storage-index.js.map +1 -1
  131. package/dist/types/types.d.ts +49 -3
  132. package/dist/types/types.js +48 -1
  133. package/dist/types/types.js.map +1 -1
  134. package/dist/utils/test-utils.d.ts +1 -2
  135. package/dist/utils/test-utils.js +7 -1
  136. package/dist/utils/test-utils.js.map +1 -1
  137. package/dist/utils/util.d.ts +9 -2
  138. package/dist/utils/util.js +128 -4
  139. package/dist/utils/util.js.map +1 -1
  140. package/package.json +12 -19
  141. package/src/migrations/db/migrations/1782950400000-checkpoint-requested-at-index.ts +55 -0
  142. package/src/storage/MongoBucketStorage.ts +347 -88
  143. package/src/storage/implementation/MongoBucketBatch.ts +147 -82
  144. package/src/storage/implementation/MongoChecksums.ts +93 -153
  145. package/src/storage/implementation/MongoCompactor.ts +102 -316
  146. package/src/storage/implementation/MongoHydrationState.ts +61 -0
  147. package/src/storage/implementation/MongoParameterCompactor.ts +25 -6
  148. package/src/storage/implementation/MongoParsedSyncConfigSet.ts +88 -0
  149. package/src/storage/implementation/MongoPersistedReplicationStream.ts +118 -0
  150. package/src/storage/implementation/MongoPersistedSyncConfigContent.ts +154 -0
  151. package/src/storage/implementation/MongoStorageProvider.ts +46 -4
  152. package/src/storage/implementation/MongoSyncBucketStorage.ts +121 -87
  153. package/src/storage/implementation/MongoSyncRulesLock.ts +7 -4
  154. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +202 -28
  155. package/src/storage/implementation/common/MongoSyncBucketStorageCheckpoint.ts +9 -0
  156. package/src/storage/implementation/common/PersistedBatch.ts +19 -3
  157. package/src/storage/implementation/common/VersionedPowerSyncMongoBase.ts +1 -1
  158. package/src/storage/implementation/createMongoSyncBucketStorage.ts +21 -7
  159. package/src/storage/implementation/db.ts +19 -20
  160. package/src/storage/implementation/models.ts +20 -14
  161. package/src/storage/implementation/v1/MongoBucketBatchV1.ts +75 -37
  162. package/src/storage/implementation/v1/MongoChecksumsV1.ts +154 -12
  163. package/src/storage/implementation/v1/MongoCompactorV1.ts +294 -9
  164. package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +142 -105
  165. package/src/storage/implementation/v1/SingleBucketStoreV1.ts +18 -27
  166. package/src/storage/implementation/v1/VersionedPowerSyncMongoV1.ts +11 -1
  167. package/src/storage/implementation/v1/models.ts +11 -1
  168. package/src/storage/implementation/v3/BucketDataContextV3.ts +32 -0
  169. package/src/storage/implementation/v3/MongoBucketBatchV3.ts +382 -321
  170. package/src/storage/implementation/v3/MongoChecksumsV3.ts +250 -32
  171. package/src/storage/implementation/v3/MongoCompactorV3.ts +847 -32
  172. package/src/storage/implementation/v3/MongoStoppedSyncConfigCleanup.ts +482 -0
  173. package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +359 -187
  174. package/src/storage/implementation/v3/PersistedBatchV3.ts +118 -37
  175. package/src/storage/implementation/v3/SourceRecordStoreV3.ts +12 -12
  176. package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +46 -36
  177. package/src/storage/implementation/v3/bucket-format.ts +97 -0
  178. package/src/storage/implementation/v3/chunking.ts +40 -0
  179. package/src/storage/implementation/v3/models.ts +132 -55
  180. package/src/storage/implementation/v3/object-storage/BucketDataObjectStorage.ts +54 -0
  181. package/src/storage/implementation/v3/object-storage/ObjectStorage.ts +29 -0
  182. package/src/storage/implementation/v3/object-storage/ObjectStorageLifecycle.ts +171 -0
  183. package/src/storage/implementation/v3/object-storage/S3ObjectStorage.ts +343 -0
  184. package/src/storage/implementation/v3/source-table-utils.ts +392 -0
  185. package/src/storage/storage-index.ts +12 -4
  186. package/src/types/types.ts +58 -1
  187. package/src/utils/test-utils.ts +8 -3
  188. package/src/utils/util.ts +185 -6
  189. package/test/src/__snapshots__/storage.test.ts.snap +0 -75
  190. package/test/src/__snapshots__/storage_sync.test.ts.snap +35 -617
  191. package/test/src/cleanup-stopped-sync-configs.test.ts +671 -0
  192. package/test/src/helpers/MemoryObjectStorage.ts +53 -0
  193. package/test/src/helpers/s3TestFactory.ts +69 -0
  194. package/test/src/object_storage_lifecycle.test.ts +59 -0
  195. package/test/src/setup.ts +6 -1
  196. package/test/src/storage.test.ts +227 -13
  197. package/test/src/storage_compacting.test.ts +1951 -12
  198. package/test/src/storage_object_storage_inline_threshold.test.ts +116 -0
  199. package/test/src/storage_s3_checksums.test.ts +191 -0
  200. package/test/src/storage_s3_compaction_lifecycle.test.ts +417 -0
  201. package/test/src/storage_s3_reading.test.ts +526 -0
  202. package/test/src/storage_s3_writing.test.ts +135 -0
  203. package/test/src/storage_sync.test.ts +1180 -73
  204. package/test/src/storeCurrentData.test.ts +227 -0
  205. package/test/src/util.ts +2 -1
  206. package/tsconfig.json +1 -4
  207. package/tsconfig.tsbuildinfo +1 -1
  208. package/dist/storage/implementation/BucketDefinitionMapping.d.ts +0 -15
  209. package/dist/storage/implementation/BucketDefinitionMapping.js +0 -58
  210. package/dist/storage/implementation/BucketDefinitionMapping.js.map +0 -1
  211. package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +0 -14
  212. package/dist/storage/implementation/MongoPersistedSyncRules.js +0 -67
  213. package/dist/storage/implementation/MongoPersistedSyncRules.js.map +0 -1
  214. package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +0 -29
  215. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +0 -76
  216. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +0 -1
  217. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.d.ts +0 -13
  218. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js +0 -2
  219. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js.map +0 -1
  220. package/dist/storage/implementation/common/SingleBucketStore.d.ts +0 -54
  221. package/dist/storage/implementation/common/SingleBucketStore.js +0 -3
  222. package/dist/storage/implementation/common/SingleBucketStore.js.map +0 -1
  223. package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +0 -9
  224. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +0 -18
  225. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +0 -1
  226. package/dist/storage/implementation/v3/MongoParameterLookupV3.d.ts +0 -4
  227. package/dist/storage/implementation/v3/MongoParameterLookupV3.js +0 -9
  228. package/dist/storage/implementation/v3/MongoParameterLookupV3.js.map +0 -1
  229. package/dist/storage/implementation/v3/SingleBucketStoreV3.d.ts +0 -18
  230. package/dist/storage/implementation/v3/SingleBucketStoreV3.js +0 -48
  231. package/dist/storage/implementation/v3/SingleBucketStoreV3.js.map +0 -1
  232. package/src/storage/implementation/BucketDefinitionMapping.ts +0 -75
  233. package/src/storage/implementation/MongoPersistedSyncRules.ts +0 -82
  234. package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +0 -101
  235. package/src/storage/implementation/common/MongoSyncBucketStorageContext.ts +0 -15
  236. package/src/storage/implementation/common/SingleBucketStore.ts +0 -63
  237. package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +0 -24
  238. package/src/storage/implementation/v3/MongoParameterLookupV3.ts +0 -11
  239. package/src/storage/implementation/v3/SingleBucketStoreV3.ts +0 -68
@@ -0,0 +1,671 @@
1
+ import { MongoSyncBucketStorageV3 } from '@module/storage/implementation/v3/MongoSyncBucketStorageV3.js';
2
+ import { ObjectStorageLifecycle } from '@module/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js';
3
+ import { mongoTestStorageFactoryGenerator } from '@module/utils/test-utils.js';
4
+ import { storage, updateSyncRulesFromYaml } from '@powersync/service-core';
5
+ import { test_utils } from '@powersync/service-core-tests';
6
+ import * as bson from 'bson';
7
+ import { describe, expect, test } from 'vitest';
8
+ import { VersionedPowerSyncMongoV3 } from '../../src/storage/implementation/v3/VersionedPowerSyncMongoV3.js';
9
+ import { ReplicationStreamDocumentV3 } from '../../src/storage/implementation/v3/models.js';
10
+ import { env } from './env.js';
11
+ import { MemoryObjectStorage } from './helpers/MemoryObjectStorage.js';
12
+ import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
13
+
14
+ function sourceDescriptor(name: string, options: { objectId?: string } = {}): storage.SourceEntityDescriptor {
15
+ return {
16
+ connectionTag: storage.SourceTable.DEFAULT_TAG,
17
+ objectId: options.objectId ?? name,
18
+ schema: 'public',
19
+ name,
20
+ replicaIdColumns: [
21
+ {
22
+ name: 'id',
23
+ type: 'VARCHAR',
24
+ typeId: 25
25
+ }
26
+ ]
27
+ };
28
+ }
29
+
30
+ function objectIdGenerator(id: string) {
31
+ let used = false;
32
+ return () => {
33
+ if (used) {
34
+ throw new Error(`Can only generate a single id using ${id}`);
35
+ }
36
+ used = true;
37
+ return new bson.ObjectId(id);
38
+ };
39
+ }
40
+
41
+ async function collectionExists(db: VersionedPowerSyncMongoV3, name: string) {
42
+ return await db.db.listCollections({ name }, { nameOnly: true }).hasNext();
43
+ }
44
+
45
+ async function cleanupStoppedSyncConfigs(bucketStorage: MongoSyncBucketStorageV3) {
46
+ return bucketStorage.cleanupStoppedSyncConfigs({
47
+ defaultSchema: 'public',
48
+ sourceConnectionTag: storage.SourceTable.DEFAULT_TAG
49
+ });
50
+ }
51
+
52
+ describe('cleanupStoppedSyncConfigs - mongodb', () => {
53
+ test('cleans up unused stopped sync config storage', async () => {
54
+ const objectStorage = new MemoryObjectStorage();
55
+ const storageFactory = mongoTestStorageFactoryGenerator({
56
+ url: env.MONGO_TEST_URL,
57
+ isCI: env.CI,
58
+ supportsMultipleSyncConfigs: true,
59
+ objectStorage
60
+ });
61
+ await using factory = await storageFactory.factory();
62
+
63
+ const first = await factory.updateSyncRules(
64
+ updateSyncRulesFromYaml(
65
+ `
66
+ config:
67
+ edition: 3
68
+
69
+ streams:
70
+ by_project:
71
+ query: |
72
+ SELECT id, project FROM "Scene"
73
+ WHERE project IN (
74
+ SELECT project FROM "ProjectInvitation"
75
+ WHERE project = subscription.parameter('project')
76
+ )
77
+ `,
78
+ { storageVersion: 3 }
79
+ )
80
+ );
81
+ const firstStorage = factory.getInstance(first) as MongoSyncBucketStorageV3;
82
+ const db = firstStorage.db as VersionedPowerSyncMongoV3;
83
+ await using firstWriter = await firstStorage.createWriter(test_utils.BATCH_OPTIONS);
84
+
85
+ const sceneTable = (
86
+ await firstWriter.resolveTables({
87
+ connection_id: 1,
88
+ source: sourceDescriptor('Scene', { objectId: 'scene-relation' }),
89
+ idGenerator: objectIdGenerator('6544e3899293153fa7b38350')
90
+ })
91
+ ).tables[0];
92
+ const invitationTable = (
93
+ await firstWriter.resolveTables({
94
+ connection_id: 1,
95
+ source: sourceDescriptor('ProjectInvitation', { objectId: 'project-invitation-relation' }),
96
+ idGenerator: objectIdGenerator('6544e3899293153fa7b38351')
97
+ })
98
+ ).tables[0];
99
+ await firstWriter.save({
100
+ sourceTable: sceneTable,
101
+ tag: storage.SaveOperationTag.INSERT,
102
+ after: {
103
+ id: 'scene-1',
104
+ project: 'project-1'
105
+ },
106
+ afterReplicaId: test_utils.rid('scene-1')
107
+ });
108
+ await firstWriter.save({
109
+ sourceTable: invitationTable,
110
+ tag: storage.SaveOperationTag.INSERT,
111
+ after: {
112
+ id: 'invitation-1',
113
+ project: 'project-1'
114
+ },
115
+ afterReplicaId: test_utils.rid('invitation-1')
116
+ });
117
+ await firstWriter.markAllSnapshotDone('1/1');
118
+ await firstWriter.commit('1/1');
119
+
120
+ const stoppedMapping = first.syncConfigContent[0].mapping;
121
+ const stoppedBucketDefinitionId = stoppedMapping.allBucketDefinitionIds()[0];
122
+ const stoppedParameterIndexId = stoppedMapping.allParameterIndexIds()[0];
123
+ expect(stoppedBucketDefinitionId).toBeDefined();
124
+ expect(stoppedParameterIndexId).toBeDefined();
125
+ const bucketDataCollection = db.bucketData(first.replicationStreamId, stoppedBucketDefinitionId!).collectionName;
126
+ const parameterIndexCollection = db.parameterIndex(
127
+ first.replicationStreamId,
128
+ stoppedParameterIndexId!
129
+ ).collectionName;
130
+ const sceneRecordsCollection = db.sourceRecords(
131
+ first.replicationStreamId,
132
+ sceneTable.id as bson.ObjectId
133
+ ).collectionName;
134
+ const invitationRecordsCollection = db.sourceRecords(
135
+ first.replicationStreamId,
136
+ invitationTable.id as bson.ObjectId
137
+ ).collectionName;
138
+ expect(await collectionExists(db, bucketDataCollection)).toBe(true);
139
+ expect(await collectionExists(db, parameterIndexCollection)).toBe(true);
140
+ expect(await collectionExists(db, sceneRecordsCollection)).toBe(true);
141
+ expect(await collectionExists(db, invitationRecordsCollection)).toBe(true);
142
+
143
+ const second = await factory.updateSyncRules(
144
+ updateSyncRulesFromYaml(
145
+ `
146
+ config:
147
+ edition: 3
148
+
149
+ streams:
150
+ by_owner:
151
+ query: SELECT * FROM todos WHERE owner_id = subscription.parameter('owner_id')
152
+ `,
153
+ { storageVersion: 3 }
154
+ )
155
+ );
156
+ expect(second.replicationStreamId).toBe(first.replicationStreamId);
157
+ const replicatingStreams = await factory.getReplicatingReplicationStreams();
158
+ const secondStorage = factory.getInstance(replicatingStreams[0]) as MongoSyncBucketStorageV3;
159
+ await using secondWriter = await secondStorage.createWriter(test_utils.BATCH_OPTIONS);
160
+ await secondWriter.markAllSnapshotDone('2/1');
161
+ await secondWriter.commit('2/1');
162
+
163
+ const lifecycle = new ObjectStorageLifecycle(db, first.replicationStreamId, objectStorage);
164
+ const stoppedObject = `${lifecycle.definitionPrefix(stoppedBucketDefinitionId!)}stopped.bson`;
165
+ const similarDefinitionObject = `${lifecycle.definitionPrefix(`${stoppedBucketDefinitionId!}0`)}live.bson`;
166
+ for (const path of [stoppedObject, similarDefinitionObject]) {
167
+ await objectStorage.put(path, new Uint8Array(), {
168
+ contentType: 'application/bson',
169
+ contentEncoding: null
170
+ });
171
+ }
172
+
173
+ const result = await cleanupStoppedSyncConfigs(
174
+ (await factory.getActiveSyncConfig())!.storage as MongoSyncBucketStorageV3
175
+ );
176
+
177
+ expect(result).toEqual({
178
+ stoppedSyncConfigsRemoved: 1,
179
+ bucketDataCollectionsDropped: 1,
180
+ parameterIndexCollectionsDropped: 1,
181
+ bucketStateDocumentsDeleted: 1,
182
+ sourceRecordCollectionsDropped: 2,
183
+ sourceTablesUpdated: 0,
184
+ sourceTablesDeleted: 2
185
+ });
186
+ expect(await collectionExists(db, bucketDataCollection)).toBe(false);
187
+ expect(await collectionExists(db, parameterIndexCollection)).toBe(false);
188
+ expect(await collectionExists(db, sceneRecordsCollection)).toBe(false);
189
+ expect(await collectionExists(db, invitationRecordsCollection)).toBe(false);
190
+ expect(objectStorage.store.has(stoppedObject)).toBe(false);
191
+ expect(objectStorage.store.has(similarDefinitionObject)).toBe(true);
192
+ expect(
193
+ await db.sourceTables(first.replicationStreamId).countDocuments({ _id: sceneTable.id as bson.ObjectId })
194
+ ).toBe(0);
195
+ expect(
196
+ await db.sourceTables(first.replicationStreamId).countDocuments({ _id: invitationTable.id as bson.ObjectId })
197
+ ).toBe(0);
198
+
199
+ const streamDoc = (await db.sync_rules.findOne({ _id: first.replicationStreamId })) as ReplicationStreamDocumentV3;
200
+ expect(streamDoc.sync_configs.map((config) => config.state)).toEqual([storage.SyncRuleState.ACTIVE]);
201
+ });
202
+
203
+ test('keeps source table membership still used by a live sync config', async () => {
204
+ await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY.factory();
205
+
206
+ const first = await factory.updateSyncRules(
207
+ updateSyncRulesFromYaml(
208
+ `
209
+ config:
210
+ edition: 3
211
+
212
+ streams:
213
+ by_owner:
214
+ query: SELECT * FROM todos WHERE owner_id = subscription.parameter('owner_id')
215
+ by_project:
216
+ query: SELECT * FROM todos WHERE project_id = subscription.parameter('project_id')
217
+ `,
218
+ { storageVersion: 3 }
219
+ )
220
+ );
221
+ const firstStorage = factory.getInstance(first) as MongoSyncBucketStorageV3;
222
+ const db = firstStorage.db as VersionedPowerSyncMongoV3;
223
+ await using firstWriter = await firstStorage.createWriter(test_utils.BATCH_OPTIONS);
224
+ const resolved = await firstWriter.resolveTables({
225
+ connection_id: 1,
226
+ source: sourceDescriptor('todos', { objectId: 'todos-relation' }),
227
+ idGenerator: objectIdGenerator('6544e3899293153fa7b38352')
228
+ });
229
+ await firstWriter.save({
230
+ sourceTable: resolved.tables[0],
231
+ tag: storage.SaveOperationTag.INSERT,
232
+ after: {
233
+ id: 'todo-1',
234
+ owner_id: 'user-1',
235
+ project_id: 'project-1'
236
+ },
237
+ afterReplicaId: test_utils.rid('todo-1')
238
+ });
239
+ await firstWriter.markAllSnapshotDone('1/1');
240
+ await firstWriter.commit('1/1');
241
+
242
+ const sourceTableId = resolved.tables[0].id as bson.ObjectId;
243
+ const sourceRecordsCollection = db.sourceRecords(first.replicationStreamId, sourceTableId).collectionName;
244
+ const [ownerDefinitionId, projectDefinitionId] = first.syncConfigContent[0].mapping.allBucketDefinitionIds();
245
+ const ownerBucketDataCollection = db.bucketData(first.replicationStreamId, ownerDefinitionId).collectionName;
246
+ expect(await collectionExists(db, ownerBucketDataCollection)).toBe(true);
247
+ expect(
248
+ (await db.sourceTables(first.replicationStreamId).findOne({ _id: sourceTableId }))?.bucket_data_source_ids
249
+ ).toEqual([ownerDefinitionId, projectDefinitionId]);
250
+
251
+ const second = await factory.updateSyncRules(
252
+ updateSyncRulesFromYaml(
253
+ `
254
+ config:
255
+ edition: 3
256
+
257
+ streams:
258
+ by_project:
259
+ query: SELECT * FROM todos WHERE project_id = subscription.parameter('project_id')
260
+ `,
261
+ { storageVersion: 3 }
262
+ )
263
+ );
264
+ expect(second.replicationStreamId).toBe(first.replicationStreamId);
265
+ const secondDefinitionId = second.syncConfigContent[1].mapping.allBucketDefinitionIds()[0];
266
+ expect(secondDefinitionId).toBe(projectDefinitionId);
267
+
268
+ const replicatingStreams = await factory.getReplicatingReplicationStreams();
269
+ const secondStorage = factory.getInstance(replicatingStreams[0]) as MongoSyncBucketStorageV3;
270
+ await using secondWriter = await secondStorage.createWriter(test_utils.BATCH_OPTIONS);
271
+ await secondWriter.markAllSnapshotDone('2/1');
272
+ await secondWriter.commit('2/1');
273
+
274
+ const result = await cleanupStoppedSyncConfigs(
275
+ (await factory.getActiveSyncConfig())!.storage as MongoSyncBucketStorageV3
276
+ );
277
+
278
+ expect(result).toEqual({
279
+ stoppedSyncConfigsRemoved: 1,
280
+ bucketDataCollectionsDropped: 1,
281
+ parameterIndexCollectionsDropped: 0,
282
+ bucketStateDocumentsDeleted: 1,
283
+ sourceRecordCollectionsDropped: 0,
284
+ sourceTablesUpdated: 1,
285
+ sourceTablesDeleted: 0
286
+ });
287
+ const sourceTable = await db.sourceTables(first.replicationStreamId).findOne({ _id: sourceTableId });
288
+ expect(sourceTable?.bucket_data_source_ids).toEqual([secondDefinitionId]);
289
+ expect(await collectionExists(db, ownerBucketDataCollection)).toBe(false);
290
+ expect(await collectionExists(db, sourceRecordsCollection)).toBe(true);
291
+
292
+ const activeStorage = (await factory.getActiveSyncConfig())!.storage as MongoSyncBucketStorageV3;
293
+ await using activeWriter = await activeStorage.createWriter(test_utils.BATCH_OPTIONS);
294
+ const activeTable = (
295
+ await activeWriter.resolveTables({
296
+ connection_id: 1,
297
+ source: sourceDescriptor('todos', { objectId: 'todos-relation' }),
298
+ idGenerator: objectIdGenerator('6544e3899293153fa7b38354')
299
+ })
300
+ ).tables[0];
301
+ await activeWriter.save({
302
+ sourceTable: activeTable,
303
+ tag: storage.SaveOperationTag.UPDATE,
304
+ before: {
305
+ id: 'todo-1'
306
+ },
307
+ after: {
308
+ id: 'todo-1',
309
+ owner_id: 'user-1',
310
+ project_id: 'project-2'
311
+ },
312
+ beforeReplicaId: test_utils.rid('todo-1'),
313
+ afterReplicaId: test_utils.rid('todo-1')
314
+ });
315
+ await activeWriter.commit('3/1');
316
+
317
+ expect(await collectionExists(db, ownerBucketDataCollection)).toBe(false);
318
+ const sourceRecord = await db.sourceRecords(first.replicationStreamId, sourceTableId).findOne({
319
+ _id: test_utils.rid('todo-1') as any
320
+ });
321
+ expect(sourceRecord?.buckets.map((bucket) => bucket.def)).toEqual([secondDefinitionId]);
322
+ });
323
+
324
+ test('drops current_data when a table becomes event-only but is kept by a live event', async () => {
325
+ await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY.factory();
326
+
327
+ // The first config syncs `todos` data and also fires events for it.
328
+ const first = await factory.updateSyncRules(
329
+ updateSyncRulesFromYaml(
330
+ `
331
+ config:
332
+ edition: 3
333
+
334
+ streams:
335
+ by_owner:
336
+ query: SELECT * FROM todos WHERE owner_id = subscription.parameter('owner_id')
337
+
338
+ event_definitions:
339
+ audit:
340
+ payloads:
341
+ - SELECT id, owner_id FROM todos
342
+ `,
343
+ { storageVersion: 3 }
344
+ )
345
+ );
346
+ const firstStorage = factory.getInstance(first) as MongoSyncBucketStorageV3;
347
+ const db = firstStorage.db as VersionedPowerSyncMongoV3;
348
+ await using firstWriter = await firstStorage.createWriter(test_utils.BATCH_OPTIONS);
349
+ const resolved = await firstWriter.resolveTables({
350
+ connection_id: 1,
351
+ source: sourceDescriptor('todos', { objectId: 'todos-relation' }),
352
+ idGenerator: objectIdGenerator('6544e3899293153fa7b38370')
353
+ });
354
+ await firstWriter.save({
355
+ sourceTable: resolved.tables[0],
356
+ tag: storage.SaveOperationTag.INSERT,
357
+ after: {
358
+ id: 'todo-1',
359
+ owner_id: 'user-1'
360
+ },
361
+ afterReplicaId: test_utils.rid('todo-1')
362
+ });
363
+ await firstWriter.markAllSnapshotDone('1/1');
364
+ await firstWriter.commit('1/1');
365
+
366
+ const sourceTableId = resolved.tables[0].id as bson.ObjectId;
367
+ const sourceRecordsCollection = db.sourceRecords(first.replicationStreamId, sourceTableId).collectionName;
368
+ const ownerDefinitionId = first.syncConfigContent[0].mapping.allBucketDefinitionIds()[0];
369
+ expect(
370
+ (await db.sourceTables(first.replicationStreamId).findOne({ _id: sourceTableId }))?.bucket_data_source_ids
371
+ ).toEqual([ownerDefinitionId]);
372
+ // The prior snapshot is persisted in current_data.
373
+ expect(await collectionExists(db, sourceRecordsCollection)).toBe(true);
374
+
375
+ // The second (active) config drops the `todos` data stream but keeps the audit event, so the
376
+ // `todos` source table becomes event-only while staying alive for the live event.
377
+ const second = await factory.updateSyncRules(
378
+ updateSyncRulesFromYaml(
379
+ `
380
+ config:
381
+ edition: 3
382
+
383
+ streams:
384
+ by_id:
385
+ query: SELECT * FROM other WHERE id = subscription.parameter('id')
386
+
387
+ event_definitions:
388
+ audit:
389
+ payloads:
390
+ - SELECT id, owner_id FROM todos
391
+ `,
392
+ { storageVersion: 3 }
393
+ )
394
+ );
395
+ expect(second.replicationStreamId).toBe(first.replicationStreamId);
396
+
397
+ const replicatingStreams = await factory.getReplicatingReplicationStreams();
398
+ const secondStorage = factory.getInstance(replicatingStreams[0]) as MongoSyncBucketStorageV3;
399
+ await using secondWriter = await secondStorage.createWriter(test_utils.BATCH_OPTIONS);
400
+ await secondWriter.markAllSnapshotDone('2/1');
401
+ await secondWriter.commit('2/1');
402
+
403
+ const result = await cleanupStoppedSyncConfigs(
404
+ (await factory.getActiveSyncConfig())!.storage as MongoSyncBucketStorageV3
405
+ );
406
+
407
+ expect(result).toEqual({
408
+ stoppedSyncConfigsRemoved: 1,
409
+ bucketDataCollectionsDropped: 1,
410
+ parameterIndexCollectionsDropped: 0,
411
+ bucketStateDocumentsDeleted: 1,
412
+ sourceRecordCollectionsDropped: 1,
413
+ sourceTablesUpdated: 1,
414
+ sourceTablesDeleted: 0
415
+ });
416
+
417
+ // The source table row survives (the live event still needs it), narrowed to empty
418
+ // memberships, but its now-unused current_data collection is dropped.
419
+ const sourceTable = await db.sourceTables(first.replicationStreamId).findOne({ _id: sourceTableId });
420
+ expect(sourceTable).not.toBeNull();
421
+ expect(sourceTable?.bucket_data_source_ids).toEqual([]);
422
+ expect(sourceTable?.parameter_lookup_source_ids).toEqual([]);
423
+ expect(await collectionExists(db, sourceRecordsCollection)).toBe(false);
424
+
425
+ const streamDoc = (await db.sync_rules.findOne({ _id: first.replicationStreamId })) as ReplicationStreamDocumentV3;
426
+ expect(streamDoc.sync_configs.map((config) => config.state)).toEqual([storage.SyncRuleState.ACTIVE]);
427
+ });
428
+
429
+ test('cleans up event-only source tables no longer triggered by a live sync config', async () => {
430
+ await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY.factory();
431
+
432
+ // The first config syncs `todos` and additionally fires events for `audit_log`. The
433
+ // `audit_log` table is referenced only by the event trigger, so its source table carries
434
+ // empty membership arrays (an event-only table).
435
+ const first = await factory.updateSyncRules(
436
+ updateSyncRulesFromYaml(
437
+ `
438
+ config:
439
+ edition: 3
440
+
441
+ streams:
442
+ by_owner:
443
+ query: SELECT * FROM todos WHERE owner_id = subscription.parameter('owner_id')
444
+
445
+ event_definitions:
446
+ audit:
447
+ payloads:
448
+ - SELECT id FROM audit_log
449
+ `,
450
+ { storageVersion: 3 }
451
+ )
452
+ );
453
+ const firstStorage = factory.getInstance(first) as MongoSyncBucketStorageV3;
454
+ const db = firstStorage.db as VersionedPowerSyncMongoV3;
455
+ await using firstWriter = await firstStorage.createWriter(test_utils.BATCH_OPTIONS);
456
+
457
+ const todosTable = (
458
+ await firstWriter.resolveTables({
459
+ connection_id: 1,
460
+ source: sourceDescriptor('todos', { objectId: 'todos-relation' }),
461
+ idGenerator: objectIdGenerator('6544e3899293153fa7b38360')
462
+ })
463
+ ).tables[0];
464
+ const auditTable = (
465
+ await firstWriter.resolveTables({
466
+ connection_id: 1,
467
+ source: sourceDescriptor('audit_log', { objectId: 'audit-log-relation' }),
468
+ idGenerator: objectIdGenerator('6544e3899293153fa7b38361')
469
+ })
470
+ ).tables[0];
471
+ await firstWriter.save({
472
+ sourceTable: todosTable,
473
+ tag: storage.SaveOperationTag.INSERT,
474
+ after: {
475
+ id: 'todo-1',
476
+ owner_id: 'user-1'
477
+ },
478
+ afterReplicaId: test_utils.rid('todo-1')
479
+ });
480
+ await firstWriter.save({
481
+ sourceTable: auditTable,
482
+ tag: storage.SaveOperationTag.INSERT,
483
+ after: {
484
+ id: 'audit-1'
485
+ },
486
+ afterReplicaId: test_utils.rid('audit-1')
487
+ });
488
+ await firstWriter.markAllSnapshotDone('1/1');
489
+ await firstWriter.commit('1/1');
490
+
491
+ const todosTableId = todosTable.id as bson.ObjectId;
492
+ const auditTableId = auditTable.id as bson.ObjectId;
493
+ // The event-only table is persisted with empty membership arrays, so the membership filter
494
+ // never selects it.
495
+ const auditSourceTable = await db.sourceTables(first.replicationStreamId).findOne({ _id: auditTableId });
496
+ expect(auditSourceTable?.bucket_data_source_ids).toEqual([]);
497
+ expect(auditSourceTable?.parameter_lookup_source_ids).toEqual([]);
498
+ const auditRecordsCollection = db.sourceRecords(first.replicationStreamId, auditTableId).collectionName;
499
+
500
+ // The second (active) config keeps the same `by_owner` stream but drops the audit event.
501
+ // The shared bucket definition stays in use, so no bucket/parameter ids become unused and
502
+ // the membership-cleanup block is skipped entirely.
503
+ const second = await factory.updateSyncRules(
504
+ updateSyncRulesFromYaml(
505
+ `
506
+ config:
507
+ edition: 3
508
+
509
+ streams:
510
+ by_owner:
511
+ query: SELECT * FROM todos WHERE owner_id = subscription.parameter('owner_id')
512
+ `,
513
+ { storageVersion: 3 }
514
+ )
515
+ );
516
+ expect(second.replicationStreamId).toBe(first.replicationStreamId);
517
+
518
+ const replicatingStreams = await factory.getReplicatingReplicationStreams();
519
+ const secondStorage = factory.getInstance(replicatingStreams[0]) as MongoSyncBucketStorageV3;
520
+ await using secondWriter = await secondStorage.createWriter(test_utils.BATCH_OPTIONS);
521
+ await secondWriter.markAllSnapshotDone('2/1');
522
+ await secondWriter.commit('2/1');
523
+
524
+ const result = await cleanupStoppedSyncConfigs(
525
+ (await factory.getActiveSyncConfig())!.storage as MongoSyncBucketStorageV3
526
+ );
527
+
528
+ expect(result).toEqual({
529
+ stoppedSyncConfigsRemoved: 1,
530
+ bucketDataCollectionsDropped: 0,
531
+ parameterIndexCollectionsDropped: 0,
532
+ bucketStateDocumentsDeleted: 0,
533
+ sourceRecordCollectionsDropped: 1,
534
+ sourceTablesUpdated: 0,
535
+ sourceTablesDeleted: 1
536
+ });
537
+ // The orphaned event-only table and its source records are removed...
538
+ expect(await db.sourceTables(first.replicationStreamId).countDocuments({ _id: auditTableId })).toBe(0);
539
+ expect(await collectionExists(db, auditRecordsCollection)).toBe(false);
540
+ // ...while the data table still used by the live config is retained.
541
+ expect(await db.sourceTables(first.replicationStreamId).countDocuments({ _id: todosTableId })).toBe(1);
542
+
543
+ const streamDoc = (await db.sync_rules.findOne({ _id: first.replicationStreamId })) as ReplicationStreamDocumentV3;
544
+ expect(streamDoc.sync_configs.map((config) => config.state)).toEqual([storage.SyncRuleState.ACTIVE]);
545
+ });
546
+
547
+ test('does not recreate dropped parameter indexes from stale source record lookups', async () => {
548
+ await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY.factory();
549
+
550
+ const first = await factory.updateSyncRules(
551
+ updateSyncRulesFromYaml(
552
+ `
553
+ config:
554
+ edition: 3
555
+
556
+ streams:
557
+ by_role:
558
+ query: |
559
+ SELECT * FROM todos
560
+ WHERE role_id IN (
561
+ SELECT role_id FROM memberships
562
+ WHERE role_id = subscription.parameter('role_id')
563
+ )
564
+ by_org:
565
+ query: |
566
+ SELECT * FROM todos
567
+ WHERE org_id IN (
568
+ SELECT org_id FROM memberships
569
+ WHERE org_id = subscription.parameter('org_id')
570
+ )
571
+ `,
572
+ { storageVersion: 3 }
573
+ )
574
+ );
575
+ const firstStorage = factory.getInstance(first) as MongoSyncBucketStorageV3;
576
+ const db = firstStorage.db as VersionedPowerSyncMongoV3;
577
+ await using firstWriter = await firstStorage.createWriter(test_utils.BATCH_OPTIONS);
578
+ const resolved = await firstWriter.resolveTables({
579
+ connection_id: 1,
580
+ source: sourceDescriptor('memberships', { objectId: 'memberships-relation' }),
581
+ idGenerator: objectIdGenerator('6544e3899293153fa7b38355')
582
+ });
583
+ await firstWriter.save({
584
+ sourceTable: resolved.tables[0],
585
+ tag: storage.SaveOperationTag.INSERT,
586
+ after: {
587
+ id: 'membership-1',
588
+ org_id: 'org-1',
589
+ role_id: 'role-1'
590
+ },
591
+ afterReplicaId: test_utils.rid('membership-1')
592
+ });
593
+ await firstWriter.markAllSnapshotDone('1/1');
594
+ await firstWriter.commit('1/1');
595
+
596
+ const sourceTableId = resolved.tables[0].id as bson.ObjectId;
597
+ const sourceRecordsCollection = db.sourceRecords(first.replicationStreamId, sourceTableId).collectionName;
598
+ const [roleIndexId, orgIndexId] = first.syncConfigContent[0].mapping.allParameterIndexIds();
599
+ const orgParameterIndexCollection = db.parameterIndex(first.replicationStreamId, orgIndexId).collectionName;
600
+ expect(await collectionExists(db, orgParameterIndexCollection)).toBe(true);
601
+ expect(
602
+ (await db.sourceTables(first.replicationStreamId).findOne({ _id: sourceTableId }))?.parameter_lookup_source_ids
603
+ ).toEqual([roleIndexId, orgIndexId]);
604
+
605
+ const second = await factory.updateSyncRules(
606
+ updateSyncRulesFromYaml(
607
+ `
608
+ config:
609
+ edition: 3
610
+
611
+ streams:
612
+ by_role:
613
+ query: |
614
+ SELECT * FROM todos
615
+ WHERE role_id IN (
616
+ SELECT role_id FROM memberships
617
+ WHERE role_id = subscription.parameter('role_id')
618
+ )
619
+ `,
620
+ { storageVersion: 3 }
621
+ )
622
+ );
623
+ expect(second.replicationStreamId).toBe(first.replicationStreamId);
624
+ const secondIndexId = second.syncConfigContent[1].mapping.allParameterIndexIds()[0];
625
+ expect(secondIndexId).toBe(roleIndexId);
626
+
627
+ const replicatingStreams = await factory.getReplicatingReplicationStreams();
628
+ const secondStorage = factory.getInstance(replicatingStreams[0]) as MongoSyncBucketStorageV3;
629
+ await using secondWriter = await secondStorage.createWriter(test_utils.BATCH_OPTIONS);
630
+ await secondWriter.markAllSnapshotDone('2/1');
631
+ await secondWriter.commit('2/1');
632
+
633
+ const result = await cleanupStoppedSyncConfigs(
634
+ (await factory.getActiveSyncConfig())!.storage as MongoSyncBucketStorageV3
635
+ );
636
+ expect(result.parameterIndexCollectionsDropped).toBe(1);
637
+ expect(await collectionExists(db, orgParameterIndexCollection)).toBe(false);
638
+ expect(await collectionExists(db, sourceRecordsCollection)).toBe(true);
639
+
640
+ const activeStorage = (await factory.getActiveSyncConfig())!.storage as MongoSyncBucketStorageV3;
641
+ await using activeWriter = await activeStorage.createWriter(test_utils.BATCH_OPTIONS);
642
+ const activeTable = (
643
+ await activeWriter.resolveTables({
644
+ connection_id: 1,
645
+ source: sourceDescriptor('memberships', { objectId: 'memberships-relation' }),
646
+ idGenerator: objectIdGenerator('6544e3899293153fa7b38357')
647
+ })
648
+ ).tables[0];
649
+ await activeWriter.save({
650
+ sourceTable: activeTable,
651
+ tag: storage.SaveOperationTag.UPDATE,
652
+ before: {
653
+ id: 'membership-1'
654
+ },
655
+ after: {
656
+ id: 'membership-1',
657
+ org_id: 'org-1',
658
+ role_id: 'role-2'
659
+ },
660
+ beforeReplicaId: test_utils.rid('membership-1'),
661
+ afterReplicaId: test_utils.rid('membership-1')
662
+ });
663
+ await activeWriter.commit('3/1');
664
+
665
+ expect(await collectionExists(db, orgParameterIndexCollection)).toBe(false);
666
+ const sourceRecord = await db.sourceRecords(first.replicationStreamId, sourceTableId).findOne({
667
+ _id: test_utils.rid('membership-1') as any
668
+ });
669
+ expect(sourceRecord?.lookups.map((lookup) => lookup.i)).toEqual([secondIndexId]);
670
+ });
671
+ });