@powersync/service-module-mongodb-storage 0.15.4 → 0.16.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 (193) hide show
  1. package/CHANGELOG.md +35 -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 +2 -2
  7. package/dist/storage/MongoBucketStorage.js +47 -34
  8. package/dist/storage/MongoBucketStorage.js.map +1 -1
  9. package/dist/storage/implementation/BucketDefinitionMapping.d.ts +17 -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/MongoBucketBatch.d.ts +16 -14
  13. package/dist/storage/implementation/MongoBucketBatch.js +80 -115
  14. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
  15. package/dist/storage/implementation/MongoBucketBatchShared.d.ts +5 -0
  16. package/dist/storage/implementation/MongoBucketBatchShared.js +8 -0
  17. package/dist/storage/implementation/MongoBucketBatchShared.js.map +1 -0
  18. package/dist/storage/implementation/MongoChecksums.d.ts +28 -17
  19. package/dist/storage/implementation/MongoChecksums.js +13 -72
  20. package/dist/storage/implementation/MongoChecksums.js.map +1 -1
  21. package/dist/storage/implementation/MongoCompactor.d.ts +98 -58
  22. package/dist/storage/implementation/MongoCompactor.js +229 -296
  23. package/dist/storage/implementation/MongoCompactor.js.map +1 -1
  24. package/dist/storage/implementation/MongoParameterCompactor.d.ts +11 -6
  25. package/dist/storage/implementation/MongoParameterCompactor.js +11 -8
  26. package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
  27. package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +14 -0
  28. package/dist/storage/implementation/MongoPersistedSyncRules.js +64 -0
  29. package/dist/storage/implementation/MongoPersistedSyncRules.js.map +1 -0
  30. package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +3 -0
  31. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +9 -0
  32. package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +1 -1
  33. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +47 -29
  34. package/dist/storage/implementation/MongoSyncBucketStorage.js +94 -387
  35. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  36. package/dist/storage/implementation/MongoSyncRulesLock.d.ts +5 -3
  37. package/dist/storage/implementation/MongoSyncRulesLock.js +12 -10
  38. package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
  39. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +1 -1
  40. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
  41. package/dist/storage/implementation/OperationBatch.js +1 -1
  42. package/dist/storage/implementation/common/BucketDataDoc.d.ts +35 -0
  43. package/dist/storage/implementation/common/BucketDataDoc.js +2 -0
  44. package/dist/storage/implementation/common/BucketDataDoc.js.map +1 -0
  45. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.d.ts +13 -0
  46. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js +2 -0
  47. package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js.map +1 -0
  48. package/dist/storage/implementation/common/PersistedBatch.d.ts +108 -0
  49. package/dist/storage/implementation/common/PersistedBatch.js +237 -0
  50. package/dist/storage/implementation/common/PersistedBatch.js.map +1 -0
  51. package/dist/storage/implementation/common/SingleBucketStore.d.ts +54 -0
  52. package/dist/storage/implementation/common/SingleBucketStore.js +3 -0
  53. package/dist/storage/implementation/common/SingleBucketStore.js.map +1 -0
  54. package/dist/storage/implementation/common/SourceRecordStore.d.ts +36 -0
  55. package/dist/storage/implementation/common/SourceRecordStore.js +2 -0
  56. package/dist/storage/implementation/common/SourceRecordStore.js.map +1 -0
  57. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.d.ts +27 -0
  58. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js +57 -0
  59. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js.map +1 -0
  60. package/dist/storage/implementation/createMongoSyncBucketStorage.d.ts +7 -0
  61. package/dist/storage/implementation/createMongoSyncBucketStorage.js +9 -0
  62. package/dist/storage/implementation/createMongoSyncBucketStorage.js.map +1 -0
  63. package/dist/storage/implementation/db.d.ts +32 -35
  64. package/dist/storage/implementation/db.js +77 -99
  65. package/dist/storage/implementation/db.js.map +1 -1
  66. package/dist/storage/implementation/models.d.ts +62 -33
  67. package/dist/storage/implementation/models.js +20 -1
  68. package/dist/storage/implementation/models.js.map +1 -1
  69. package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +13 -0
  70. package/dist/storage/implementation/v1/MongoBucketBatchV1.js +22 -0
  71. package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -0
  72. package/dist/storage/implementation/v1/MongoChecksumsV1.d.ts +12 -0
  73. package/dist/storage/implementation/v1/MongoChecksumsV1.js +56 -0
  74. package/dist/storage/implementation/v1/MongoChecksumsV1.js.map +1 -0
  75. package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +23 -0
  76. package/dist/storage/implementation/v1/MongoCompactorV1.js +52 -0
  77. package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -0
  78. package/dist/storage/implementation/v1/MongoParameterCompactorV1.d.ts +9 -0
  79. package/dist/storage/implementation/v1/MongoParameterCompactorV1.js +20 -0
  80. package/dist/storage/implementation/v1/MongoParameterCompactorV1.js.map +1 -0
  81. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +41 -0
  82. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +283 -0
  83. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -0
  84. package/dist/storage/implementation/v1/PersistedBatchV1.d.ts +26 -0
  85. package/dist/storage/implementation/v1/PersistedBatchV1.js +183 -0
  86. package/dist/storage/implementation/v1/PersistedBatchV1.js.map +1 -0
  87. package/dist/storage/implementation/v1/SingleBucketStoreV1.d.ts +18 -0
  88. package/dist/storage/implementation/v1/SingleBucketStoreV1.js +57 -0
  89. package/dist/storage/implementation/v1/SingleBucketStoreV1.js.map +1 -0
  90. package/dist/storage/implementation/v1/SourceRecordStoreV1.d.ts +19 -0
  91. package/dist/storage/implementation/v1/SourceRecordStoreV1.js +105 -0
  92. package/dist/storage/implementation/v1/SourceRecordStoreV1.js.map +1 -0
  93. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.d.ts +12 -0
  94. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js +20 -0
  95. package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js.map +1 -0
  96. package/dist/storage/implementation/v1/models.d.ts +34 -0
  97. package/dist/storage/implementation/v1/models.js +37 -0
  98. package/dist/storage/implementation/v1/models.js.map +1 -0
  99. package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +13 -0
  100. package/dist/storage/implementation/v3/MongoBucketBatchV3.js +34 -0
  101. package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -0
  102. package/dist/storage/implementation/v3/MongoChecksumsV3.d.ts +15 -0
  103. package/dist/storage/implementation/v3/MongoChecksumsV3.js +84 -0
  104. package/dist/storage/implementation/v3/MongoChecksumsV3.js.map +1 -0
  105. package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +23 -0
  106. package/dist/storage/implementation/v3/MongoCompactorV3.js +68 -0
  107. package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -0
  108. package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +9 -0
  109. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +18 -0
  110. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +1 -0
  111. package/dist/storage/implementation/v3/MongoParameterLookupV3.d.ts +5 -0
  112. package/dist/storage/implementation/v3/MongoParameterLookupV3.js +9 -0
  113. package/dist/storage/implementation/v3/MongoParameterLookupV3.js.map +1 -0
  114. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +41 -0
  115. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +407 -0
  116. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -0
  117. package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +29 -0
  118. package/dist/storage/implementation/v3/PersistedBatchV3.js +259 -0
  119. package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -0
  120. package/dist/storage/implementation/v3/SingleBucketStoreV3.d.ts +18 -0
  121. package/dist/storage/implementation/v3/SingleBucketStoreV3.js +48 -0
  122. package/dist/storage/implementation/v3/SingleBucketStoreV3.js.map +1 -0
  123. package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +22 -0
  124. package/dist/storage/implementation/v3/SourceRecordStoreV3.js +164 -0
  125. package/dist/storage/implementation/v3/SourceRecordStoreV3.js.map +1 -0
  126. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +21 -0
  127. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +71 -0
  128. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -0
  129. package/dist/storage/implementation/v3/models.d.ts +43 -0
  130. package/dist/storage/implementation/v3/models.js +34 -0
  131. package/dist/storage/implementation/v3/models.js.map +1 -0
  132. package/dist/storage/storage-index.d.ts +6 -3
  133. package/dist/storage/storage-index.js +6 -3
  134. package/dist/storage/storage-index.js.map +1 -1
  135. package/dist/utils/util.d.ts +10 -3
  136. package/dist/utils/util.js +24 -3
  137. package/dist/utils/util.js.map +1 -1
  138. package/package.json +9 -9
  139. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +1 -1
  140. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +6 -6
  141. package/src/storage/MongoBucketStorage.ts +92 -59
  142. package/src/storage/implementation/BucketDefinitionMapping.ts +72 -0
  143. package/src/storage/implementation/MongoBucketBatch.ts +110 -144
  144. package/src/storage/implementation/MongoBucketBatchShared.ts +11 -0
  145. package/src/storage/implementation/MongoChecksums.ts +52 -75
  146. package/src/storage/implementation/MongoCompactor.ts +374 -404
  147. package/src/storage/implementation/MongoParameterCompactor.ts +37 -24
  148. package/src/storage/implementation/MongoPersistedSyncRules.ts +76 -0
  149. package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +17 -0
  150. package/src/storage/implementation/MongoSyncBucketStorage.ts +181 -455
  151. package/src/storage/implementation/MongoSyncRulesLock.ts +11 -13
  152. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +3 -1
  153. package/src/storage/implementation/OperationBatch.ts +1 -1
  154. package/src/storage/implementation/common/BucketDataDoc.ts +37 -0
  155. package/src/storage/implementation/common/MongoSyncBucketStorageContext.ts +15 -0
  156. package/src/storage/implementation/common/PersistedBatch.ts +364 -0
  157. package/src/storage/implementation/common/SingleBucketStore.ts +63 -0
  158. package/src/storage/implementation/common/SourceRecordStore.ts +49 -0
  159. package/src/storage/implementation/common/VersionedPowerSyncMongoBase.ts +80 -0
  160. package/src/storage/implementation/createMongoSyncBucketStorage.ts +25 -0
  161. package/src/storage/implementation/db.ts +105 -129
  162. package/src/storage/implementation/models.ts +82 -36
  163. package/src/storage/implementation/v1/MongoBucketBatchV1.ts +32 -0
  164. package/src/storage/implementation/v1/MongoChecksumsV1.ts +75 -0
  165. package/src/storage/implementation/v1/MongoCompactorV1.ts +93 -0
  166. package/src/storage/implementation/v1/MongoParameterCompactorV1.ts +26 -0
  167. package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +448 -0
  168. package/src/storage/implementation/v1/PersistedBatchV1.ts +230 -0
  169. package/src/storage/implementation/v1/SingleBucketStoreV1.ts +74 -0
  170. package/src/storage/implementation/v1/SourceRecordStoreV1.ts +156 -0
  171. package/src/storage/implementation/v1/VersionedPowerSyncMongoV1.ts +28 -0
  172. package/src/storage/implementation/v1/models.ts +84 -0
  173. package/src/storage/implementation/v3/MongoBucketBatchV3.ts +44 -0
  174. package/src/storage/implementation/v3/MongoChecksumsV3.ts +120 -0
  175. package/src/storage/implementation/v3/MongoCompactorV3.ts +107 -0
  176. package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +24 -0
  177. package/src/storage/implementation/v3/MongoParameterLookupV3.ts +12 -0
  178. package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +550 -0
  179. package/src/storage/implementation/v3/PersistedBatchV3.ts +318 -0
  180. package/src/storage/implementation/v3/SingleBucketStoreV3.ts +68 -0
  181. package/src/storage/implementation/v3/SourceRecordStoreV3.ts +226 -0
  182. package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +112 -0
  183. package/src/storage/implementation/v3/models.ts +96 -0
  184. package/src/storage/storage-index.ts +6 -3
  185. package/src/utils/util.ts +34 -5
  186. package/test/src/storage_compacting.test.ts +57 -29
  187. package/test/src/storage_sync.test.ts +351 -5
  188. package/test/tsconfig.json +0 -1
  189. package/tsconfig.tsbuildinfo +1 -1
  190. package/dist/storage/implementation/PersistedBatch.d.ts +0 -71
  191. package/dist/storage/implementation/PersistedBatch.js +0 -354
  192. package/dist/storage/implementation/PersistedBatch.js.map +0 -1
  193. package/src/storage/implementation/PersistedBatch.ts +0 -432
@@ -0,0 +1,259 @@
1
+ import { ReplicationAssertionError } from '@powersync/lib-services-framework';
2
+ import * as bson from 'bson';
3
+ import { mongoTableId } from '../../../utils/util.js';
4
+ import { PersistedBatch } from '../common/PersistedBatch.js';
5
+ import { serializeBucketDataV3, taggedBucketParameterDocumentToV3 } from './models.js';
6
+ import { serializeParameterLookupV3 } from './MongoParameterLookupV3.js';
7
+ export class PersistedBatchV3 extends PersistedBatch {
8
+ currentData = [];
9
+ sourceTablePendingDeletes = new Map();
10
+ checkDefinitionId(definitionId) {
11
+ if (definitionId == null) {
12
+ // This is required for V3 storage.
13
+ throw new ReplicationAssertionError('Expected v3 bucket when incrementalReprocessing is enabled');
14
+ }
15
+ return definitionId;
16
+ }
17
+ getBucketDefinitionId(bucketSource) {
18
+ return this.mapping.bucketSourceId(bucketSource);
19
+ }
20
+ saveParameterData(data) {
21
+ const { sourceTable, sourceKey, evaluated } = data;
22
+ const remaining_lookups = new Map();
23
+ for (let lookup of data.existing_lookups) {
24
+ if (lookup.indexId == null) {
25
+ throw new ReplicationAssertionError('Expected v3 lookup when incrementalReprocessing is enabled');
26
+ }
27
+ remaining_lookups.set(`${lookup.indexId}.${lookup.lookup.toString('base64')}`, lookup);
28
+ }
29
+ for (let result of evaluated) {
30
+ const sourceDefinitionId = this.mapping.parameterLookupId(result.lookup.source);
31
+ const binLookup = serializeParameterLookupV3(result.lookup);
32
+ remaining_lookups.delete(`${sourceDefinitionId}.${binLookup.toString('base64')}`);
33
+ const op_id = data.op_seq.next();
34
+ this.debugLastOpId = op_id;
35
+ const values = {
36
+ _id: op_id,
37
+ key: {
38
+ t: mongoTableId(sourceTable.id),
39
+ k: sourceKey
40
+ },
41
+ lookup: binLookup,
42
+ bucket_parameters: result.bucketParameters
43
+ };
44
+ this.bucketParameters.push({
45
+ ...values,
46
+ index: sourceDefinitionId
47
+ });
48
+ this.currentSize += 200;
49
+ }
50
+ for (let lookup of remaining_lookups.values()) {
51
+ const op_id = data.op_seq.next();
52
+ this.debugLastOpId = op_id;
53
+ const indexId = lookup.indexId;
54
+ if (indexId == null) {
55
+ throw new ReplicationAssertionError('Expected v3 lookup when incrementalReprocessing is enabled');
56
+ }
57
+ const values = {
58
+ _id: op_id,
59
+ key: {
60
+ t: mongoTableId(sourceTable.id),
61
+ k: sourceKey
62
+ },
63
+ lookup: lookup.lookup,
64
+ bucket_parameters: []
65
+ };
66
+ this.bucketParameters.push({
67
+ ...values,
68
+ index: indexId
69
+ });
70
+ this.currentSize += 200;
71
+ }
72
+ }
73
+ hardDeleteCurrentData(sourceTableId, replicaId) {
74
+ this.currentData.push({
75
+ sourceTableId,
76
+ operation: {
77
+ deleteOne: {
78
+ filter: { _id: replicaId }
79
+ }
80
+ }
81
+ });
82
+ this.currentSize += 50;
83
+ }
84
+ softDeleteCurrentData(sourceTableId, replicaId, checkpointGreaterThan) {
85
+ this.currentData.push({
86
+ sourceTableId,
87
+ operation: {
88
+ updateOne: {
89
+ filter: { _id: replicaId },
90
+ update: {
91
+ $set: {
92
+ data: null,
93
+ buckets: [],
94
+ lookups: [],
95
+ pending_delete: checkpointGreaterThan
96
+ }
97
+ },
98
+ upsert: true
99
+ }
100
+ }
101
+ });
102
+ const sourceTableKey = sourceTableId.toHexString();
103
+ const existingPendingDelete = this.sourceTablePendingDeletes.get(sourceTableKey);
104
+ if (existingPendingDelete == null || checkpointGreaterThan > existingPendingDelete) {
105
+ this.sourceTablePendingDeletes.set(sourceTableKey, checkpointGreaterThan);
106
+ }
107
+ this.currentSize += 50;
108
+ }
109
+ upsertCurrentData(values) {
110
+ const buckets = values.buckets.map((bucket) => {
111
+ if (bucket.definitionId == null) {
112
+ throw new ReplicationAssertionError('Expected v3 bucket when incrementalReprocessing is enabled');
113
+ }
114
+ return {
115
+ def: bucket.definitionId,
116
+ bucket: bucket.bucket,
117
+ table: bucket.table,
118
+ id: bucket.id
119
+ };
120
+ });
121
+ const lookups = values.lookups.map((lookup) => {
122
+ if (lookup.indexId == null) {
123
+ throw new ReplicationAssertionError('Expected v3 lookup when incrementalReprocessing is enabled');
124
+ }
125
+ return {
126
+ i: lookup.indexId,
127
+ l: lookup.lookup
128
+ };
129
+ });
130
+ this.currentData.push({
131
+ sourceTableId: values.sourceTableId,
132
+ operation: {
133
+ updateOne: {
134
+ filter: { _id: values.replicaId },
135
+ update: {
136
+ $set: {
137
+ data: values.data,
138
+ buckets,
139
+ lookups
140
+ },
141
+ $unset: { pending_delete: 1 }
142
+ },
143
+ upsert: true
144
+ }
145
+ }
146
+ });
147
+ this.currentSize += (values.data?.length() ?? 0) + 100;
148
+ }
149
+ get currentDataCount() {
150
+ return this.currentData.length;
151
+ }
152
+ async flushBucketData(session) {
153
+ const operationsByDefinition = new Map();
154
+ for (const document of this.bucketData) {
155
+ const existing = operationsByDefinition.get(document.bucketKey.definitionId) ?? [];
156
+ existing.push(document);
157
+ operationsByDefinition.set(document.bucketKey.definitionId, existing);
158
+ }
159
+ for (const [definitionId, documents] of operationsByDefinition.entries()) {
160
+ await this.db.bucketDataV3(this.group_id, definitionId).bulkWrite(documents.map((document) => ({
161
+ insertOne: {
162
+ document: serializeBucketDataV3(document)
163
+ }
164
+ })), {
165
+ session,
166
+ ordered: false
167
+ });
168
+ }
169
+ }
170
+ async flushBucketParameters(session) {
171
+ const operationsByIndex = new Map();
172
+ for (const document of this.bucketParameters) {
173
+ const existing = operationsByIndex.get(document.index) ?? [];
174
+ existing.push(document);
175
+ operationsByIndex.set(document.index, existing);
176
+ }
177
+ for (const [indexId, documents] of operationsByIndex.entries()) {
178
+ await this.db.parameterIndexV3(this.group_id, indexId).bulkWrite(documents.map((document) => ({
179
+ insertOne: {
180
+ document: taggedBucketParameterDocumentToV3(document)
181
+ }
182
+ })), {
183
+ session,
184
+ ordered: false
185
+ });
186
+ }
187
+ }
188
+ async flushCurrentData(session) {
189
+ const operationsBySourceTable = new Map();
190
+ for (const operation of this.currentData) {
191
+ const sourceTableId = operation.sourceTableId.toHexString();
192
+ const existing = operationsBySourceTable.get(sourceTableId) ?? [];
193
+ existing.push(operation);
194
+ operationsBySourceTable.set(sourceTableId, existing);
195
+ }
196
+ const sourceTableUpdates = [
197
+ ...this.sourceTablePendingDeletes.entries()
198
+ ].map(([key, value]) => {
199
+ return {
200
+ updateOne: {
201
+ filter: { _id: new bson.ObjectId(key) },
202
+ update: {
203
+ $max: {
204
+ latest_pending_delete: value
205
+ }
206
+ }
207
+ }
208
+ };
209
+ });
210
+ if (sourceTableUpdates.length > 0) {
211
+ await this.db.sourceTablesV3(this.group_id).bulkWrite(sourceTableUpdates, { session, ordered: false });
212
+ }
213
+ for (const operations of operationsBySourceTable.values()) {
214
+ const sourceTableId = operations[0].sourceTableId;
215
+ await this.db.sourceRecordsV3(this.group_id, sourceTableId).bulkWrite(operations.map((entry) => entry.operation), {
216
+ session,
217
+ ordered: true
218
+ });
219
+ }
220
+ }
221
+ async flushBucketStates(session) {
222
+ await this.db.bucketStateV3(this.group_id).bulkWrite(this.getBucketStateUpdates(), {
223
+ session,
224
+ ordered: false
225
+ });
226
+ }
227
+ resetCurrentData() {
228
+ this.currentData = [];
229
+ this.sourceTablePendingDeletes.clear();
230
+ }
231
+ getBucketStateUpdates() {
232
+ return Array.from(this.bucketStates.values()).map((state) => {
233
+ if (state.definitionId == null) {
234
+ throw new ReplicationAssertionError('Expected bucket definition id when incrementalReprocessing is enabled');
235
+ }
236
+ return {
237
+ updateOne: {
238
+ filter: {
239
+ _id: {
240
+ d: state.definitionId,
241
+ b: state.bucket
242
+ }
243
+ },
244
+ update: {
245
+ $set: {
246
+ last_op: state.lastOp
247
+ },
248
+ $inc: {
249
+ 'estimate_since_compact.count': state.incrementCount,
250
+ 'estimate_since_compact.bytes': state.incrementBytes
251
+ }
252
+ },
253
+ upsert: true
254
+ }
255
+ };
256
+ });
257
+ }
258
+ }
259
+ //# sourceMappingURL=PersistedBatchV3.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PersistedBatchV3.js","sourceRoot":"","sources":["../../../../src/storage/implementation/v3/PersistedBatchV3.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,yBAAyB,EAAE,MAAM,mCAAmC,CAAC;AAG9E,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,EAEL,cAAc,EAGf,MAAM,6BAA6B,CAAC;AAErC,OAAO,EAIL,qBAAqB,EAErB,iCAAiC,EAClC,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAC;AAGzE,MAAM,OAAO,gBAAiB,SAAQ,cA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C,EAAE,KAAK,CAAC,MAAM;yBAChB;qBACF;oBACD,MAAM,EAAE;wBACN,IAAI,EAAE;4BACJ,OAAO,EAAE,KAAK,CAAC,MAAM;yBACtB;wBACD,IAAI,EAAE;4BACJ,8BAA8B,EAAE,KAAK,CAAC,cAAc;4BACpD,8BAA8B,EAAE,KAAK,CAAC,cAAc;yBACrD;qBACF;oBACD,MAAM,EAAE,IAAI;iBACb;aAC2D,CAAC;QACjE,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,18 @@
1
+ import { mongo } from '@powersync/lib-service-mongodb';
2
+ import { InternalOpId } from '@powersync/service-core';
3
+ import { BucketDataDoc, BucketKey } from '../common/BucketDataDoc.js';
4
+ import { BucketDataDocumentGeneric, BucketDataDocumentGenericId, SingleBucketStore } from '../common/SingleBucketStore.js';
5
+ import { BucketDataProperties } from '../models.js';
6
+ import { VersionedPowerSyncMongoV3 } from './VersionedPowerSyncMongoV3.js';
7
+ export declare class SingleBucketStoreV3 implements SingleBucketStore {
8
+ private db;
9
+ readonly key: BucketKey;
10
+ readonly collection: mongo.Collection<BucketDataDocumentGeneric>;
11
+ constructor(db: VersionedPowerSyncMongoV3, key: BucketKey);
12
+ docId(o: InternalOpId): BucketDataDocumentGenericId;
13
+ get minId(): BucketDataDocumentGenericId;
14
+ get maxId(): BucketDataDocumentGenericId;
15
+ toPersistedDocument(source: Omit<BucketDataDoc, 'bucketKey'>): BucketDataDocumentGeneric;
16
+ fromPersistedDocument(doc: BucketDataDocumentGeneric): BucketDataDoc;
17
+ fromPartialPersistedDocument<T extends keyof BucketDataProperties>(doc: Pick<BucketDataDocumentGeneric, '_id' | T>): Pick<BucketDataDoc, 'bucketKey' | 'o' | T>;
18
+ }
@@ -0,0 +1,48 @@
1
+ import { mongo } from '@powersync/lib-service-mongodb';
2
+ import { loadBucketDataDocumentV3, serializeBucketDataV3 } from './models.js';
3
+ export class SingleBucketStoreV3 {
4
+ db;
5
+ key;
6
+ collection;
7
+ constructor(db, key) {
8
+ this.db = db;
9
+ this.key = key;
10
+ this.collection = db.bucketDataV3(key.replicationStreamId, key.definitionId);
11
+ }
12
+ docId(o) {
13
+ // `satisfies BucketDataKeyV3` checks that we use the correct type for V3 storage
14
+ // `as BucketDataDocumentGenericId` does a cast to get the interface virtual type
15
+ return {
16
+ b: this.key.bucket,
17
+ o
18
+ };
19
+ }
20
+ get minId() {
21
+ return {
22
+ b: this.key.bucket,
23
+ o: new mongo.MinKey()
24
+ }; // No way to properly type this
25
+ }
26
+ get maxId() {
27
+ return {
28
+ b: this.key.bucket,
29
+ o: new mongo.MaxKey()
30
+ }; // No way to properly type this
31
+ }
32
+ toPersistedDocument(source) {
33
+ return serializeBucketDataV3({ bucketKey: this.key, ...source });
34
+ }
35
+ fromPersistedDocument(doc) {
36
+ return loadBucketDataDocumentV3(this.key, doc);
37
+ }
38
+ fromPartialPersistedDocument(doc) {
39
+ const document = doc;
40
+ const { _id, ...rest } = document;
41
+ return {
42
+ bucketKey: this.key,
43
+ o: _id.o,
44
+ ...rest
45
+ };
46
+ }
47
+ }
48
+ //# sourceMappingURL=SingleBucketStoreV3.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SingleBucketStoreV3.js","sourceRoot":"","sources":["../../../../src/storage/implementation/v3/SingleBucketStoreV3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,gCAAgC,CAAC;AAUvD,OAAO,EAAyC,wBAAwB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAErH,MAAM,OAAO,mBAAmB;IAIpB;IACQ;IAJF,UAAU,CAA8C;IAExE,YACU,EAA6B,EACrB,GAAc;QADtB,OAAE,GAAF,EAAE,CAA2B;QACrB,QAAG,GAAH,GAAG,CAAW;QAE9B,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC,YAAY,CAC/B,GAAG,CAAC,mBAAmB,EACvB,GAAG,CAAC,YAAY,CACyC,CAAC;IAC9D,CAAC;IAED,KAAK,CAAC,CAAe;QACnB,iFAAiF;QACjF,iFAAiF;QACjF,OAAO;YACL,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,MAAM;YAClB,CAAC;SACuD,CAAC;IAC7D,CAAC;IAED,IAAI,KAAK;QACP,OAAO;YACL,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,MAAM;YAClB,CAAC,EAAE,IAAI,KAAK,CAAC,MAAM,EAAE;SACf,CAAC,CAAC,+BAA+B;IAC3C,CAAC;IAED,IAAI,KAAK;QACP,OAAO;YACL,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,MAAM;YAClB,CAAC,EAAE,IAAI,KAAK,CAAC,MAAM,EAAE;SACf,CAAC,CAAC,+BAA+B;IAC3C,CAAC;IAED,mBAAmB,CAAC,MAAwC;QAC1D,OAAO,qBAAqB,CAAC,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,EAAE,CAA8B,CAAC;IAChG,CAAC;IAED,qBAAqB,CAAC,GAA8B;QAClD,OAAO,wBAAwB,CAAC,IAAI,CAAC,GAAG,EAAE,GAA2B,CAAC,CAAC;IACzE,CAAC;IAED,4BAA4B,CAC1B,GAA+C;QAE/C,MAAM,QAAQ,GAAG,GAA4C,CAAC;QAC9D,MAAM,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,QAAQ,CAAC;QAClC,OAAO;YACL,SAAS,EAAE,IAAI,CAAC,GAAG;YACnB,CAAC,EAAE,GAAG,CAAC,CAAC;YACR,GAAG,IAAI;SACsC,CAAC;IAClD,CAAC;CACF"}
@@ -0,0 +1,22 @@
1
+ import { mongo } from '@powersync/lib-service-mongodb';
2
+ import { Logger } from '@powersync/lib-services-framework';
3
+ import { EvaluatedParameters, EvaluatedRow } from '@powersync/service-sync-rules';
4
+ import * as bson from 'bson';
5
+ import { BucketDefinitionMapping } from '../BucketDefinitionMapping.js';
6
+ import { LoadedSourceRecord, SourceRecordLookupEntry, SourceRecordStore } from '../common/SourceRecordStore.js';
7
+ import { VersionedPowerSyncMongoV3 } from './VersionedPowerSyncMongoV3.js';
8
+ export declare class SourceRecordStoreV3 implements SourceRecordStore {
9
+ private readonly db;
10
+ private readonly groupId;
11
+ private readonly mapping;
12
+ constructor(db: VersionedPowerSyncMongoV3, groupId: number, mapping: BucketDefinitionMapping);
13
+ mapEvaluatedBuckets(evaluated: EvaluatedRow[]): LoadedSourceRecord['buckets'];
14
+ mapParameterLookups(paramEvaluated: EvaluatedParameters[]): LoadedSourceRecord['lookups'];
15
+ private createLoadedDocument;
16
+ loadSizes(session: mongo.ClientSession, entries: SourceRecordLookupEntry[]): Promise<Map<string, number>>;
17
+ loadDocuments(session: mongo.ClientSession, entries: SourceRecordLookupEntry[], idsOnly: boolean): Promise<Map<string, LoadedSourceRecord>>;
18
+ loadTruncateBatch(session: mongo.ClientSession, sourceTableId: bson.ObjectId, limit: number): Promise<LoadedSourceRecord[]>;
19
+ postCommitCleanup(lastCheckpoint: bigint, logger: Logger): Promise<void>;
20
+ private deletePendingDeletes;
21
+ private groupEntries;
22
+ }
@@ -0,0 +1,164 @@
1
+ import * as lib_mongo from '@powersync/lib-service-mongodb';
2
+ import { retryOnMongoMaxTimeMSExpired } from '../../../utils/util.js';
3
+ import { cacheKey } from '../OperationBatch.js';
4
+ import { serializeParameterLookupV3 } from './MongoParameterLookupV3.js';
5
+ export class SourceRecordStoreV3 {
6
+ db;
7
+ groupId;
8
+ mapping;
9
+ constructor(db, groupId, mapping) {
10
+ this.db = db;
11
+ this.groupId = groupId;
12
+ this.mapping = mapping;
13
+ }
14
+ mapEvaluatedBuckets(evaluated) {
15
+ return evaluated.map((entry) => ({
16
+ definitionId: this.mapping.bucketSourceId(entry.source),
17
+ bucket: entry.bucket,
18
+ table: entry.table,
19
+ id: entry.id
20
+ }));
21
+ }
22
+ mapParameterLookups(paramEvaluated) {
23
+ return paramEvaluated.map((entry) => ({
24
+ indexId: this.mapping.parameterLookupId(entry.lookup.source),
25
+ lookup: serializeParameterLookupV3(entry.lookup)
26
+ }));
27
+ }
28
+ createLoadedDocument(sourceTableId, id, data, buckets, lookups) {
29
+ return {
30
+ sourceTableId,
31
+ replicaId: id,
32
+ data,
33
+ buckets: buckets.map((bucket) => ({
34
+ definitionId: bucket.def,
35
+ bucket: bucket.bucket,
36
+ table: bucket.table,
37
+ id: bucket.id
38
+ })),
39
+ lookups: lookups.map((lookup) => ({
40
+ indexId: lookup.i,
41
+ lookup: lookup.l
42
+ })),
43
+ cacheKey: cacheKey(sourceTableId, id)
44
+ };
45
+ }
46
+ async loadSizes(session, entries) {
47
+ const sizes = new Map();
48
+ for (const [sourceTableId, replicaIds] of this.groupEntries(entries)) {
49
+ const filter = {
50
+ _id: { $in: replicaIds }
51
+ };
52
+ const sizeCursor = this.db
53
+ .sourceRecordsV3(this.groupId, sourceTableId)
54
+ .aggregate([
55
+ {
56
+ $match: filter
57
+ },
58
+ {
59
+ $project: {
60
+ _id: 1,
61
+ size: { $bsonSize: '$$ROOT' }
62
+ }
63
+ }
64
+ ], { session });
65
+ for await (const doc of sizeCursor.stream()) {
66
+ sizes.set(cacheKey(sourceTableId, doc._id), doc.size);
67
+ }
68
+ }
69
+ return sizes;
70
+ }
71
+ async loadDocuments(session, entries, idsOnly) {
72
+ const documents = new Map();
73
+ const projection = idsOnly ? { _id: 1 } : undefined;
74
+ for (const [sourceTableId, replicaIds] of this.groupEntries(entries)) {
75
+ const filter = {
76
+ _id: { $in: replicaIds }
77
+ };
78
+ const cursor = this.db.sourceRecordsV3(this.groupId, sourceTableId).find(filter, { session, projection });
79
+ for await (const doc of cursor.stream()) {
80
+ const loaded = this.createLoadedDocument(sourceTableId, doc._id, idsOnly ? null : doc.data, idsOnly ? [] : doc.buckets, idsOnly ? [] : doc.lookups);
81
+ documents.set(loaded.cacheKey, loaded);
82
+ }
83
+ }
84
+ return documents;
85
+ }
86
+ async loadTruncateBatch(session, sourceTableId, limit) {
87
+ const cursor = this.db.sourceRecordsV3(this.groupId, sourceTableId).find({
88
+ pending_delete: { $exists: false }
89
+ }, {
90
+ projection: {
91
+ _id: 1,
92
+ buckets: 1,
93
+ lookups: 1
94
+ },
95
+ limit,
96
+ session
97
+ });
98
+ return (await cursor.toArray()).map((doc) => this.createLoadedDocument(sourceTableId, doc._id, null, doc.buckets, doc.lookups));
99
+ }
100
+ async postCommitCleanup(lastCheckpoint, logger) {
101
+ // This cleans up soft deletes in source_records collections.
102
+ // Since there may be a lot (100+) of these collections in some cases, we track which
103
+ // ones have dirty deletes in source_tables.
104
+ const dirtySourceTables = await this.db
105
+ .sourceTablesV3(this.groupId)
106
+ .find({
107
+ latest_pending_delete: { $exists: true }
108
+ }, {
109
+ projection: { _id: 1, latest_pending_delete: 1 }
110
+ })
111
+ .toArray();
112
+ let deletedCount = 0;
113
+ const sourceTableUpdates = [];
114
+ for (const sourceTable of dirtySourceTables) {
115
+ const collection = this.db.sourceRecordsV3(this.groupId, sourceTable._id);
116
+ const result = await this.deletePendingDeletes(collection, sourceTable._id, lastCheckpoint, logger);
117
+ deletedCount += result.deletedCount;
118
+ if (sourceTable.latest_pending_delete != null && sourceTable.latest_pending_delete <= lastCheckpoint) {
119
+ sourceTableUpdates.push({
120
+ updateOne: {
121
+ filter: {
122
+ _id: sourceTable._id,
123
+ // If the source table received more writes in the meantime, this will filter it out
124
+ latest_pending_delete: sourceTable.latest_pending_delete
125
+ },
126
+ update: {
127
+ $unset: {
128
+ latest_pending_delete: 1
129
+ }
130
+ }
131
+ }
132
+ });
133
+ }
134
+ }
135
+ if (sourceTableUpdates.length > 0) {
136
+ await this.db.sourceTablesV3(this.groupId).bulkWrite(sourceTableUpdates, { ordered: false });
137
+ }
138
+ if (deletedCount > 0) {
139
+ logger.info(`Cleaned up ${deletedCount} pending delete current_data records for checkpoint ${lastCheckpoint}`);
140
+ }
141
+ }
142
+ async deletePendingDeletes(collection, sourceTableId, lastCheckpoint, logger) {
143
+ return retryOnMongoMaxTimeMSExpired(() => collection.deleteMany({
144
+ pending_delete: { $exists: true, $lte: lastCheckpoint }
145
+ }, {
146
+ maxTimeMS: lib_mongo.db.MONGO_CLEAR_OPERATION_TIMEOUT_MS
147
+ }), {
148
+ retryDelayMs: lib_mongo.db.MONGO_OPERATION_TIMEOUT_MS / 5,
149
+ onRetry: (n) => {
150
+ logger.warn(`Cleared batch ${n} of pending deletes for source table ${sourceTableId}, continuing...`);
151
+ }
152
+ });
153
+ }
154
+ groupEntries(entries) {
155
+ const grouped = new Map();
156
+ for (const entry of entries) {
157
+ const existing = grouped.get(entry.sourceTableId) ?? [];
158
+ existing.push(entry.replicaId);
159
+ grouped.set(entry.sourceTableId, existing);
160
+ }
161
+ return grouped;
162
+ }
163
+ }
164
+ //# sourceMappingURL=SourceRecordStoreV3.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SourceRecordStoreV3.js","sourceRoot":"","sources":["../../../../src/storage/implementation/v3/SourceRecordStoreV3.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,gCAAgC,CAAC;AAM5D,OAAO,EAAE,4BAA4B,EAAE,MAAM,wBAAwB,CAAC;AAEtE,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,OAAO,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAC;AAIzE,MAAM,OAAO,mBAAmB;IAEX;IACA;IACA;IAHnB,YACmB,EAA6B,EAC7B,OAAe,EACf,OAAgC;QAFhC,OAAE,GAAF,EAAE,CAA2B;QAC7B,YAAO,GAAP,OAAO,CAAQ;QACf,YAAO,GAAP,OAAO,CAAyB;IAChD,CAAC;IAEJ,mBAAmB,CAAC,SAAyB;QAC3C,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;YAC/B,YAAY,EAAE,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,MAAM,CAAC;YACvD,MAAM,EAAE,KAAK,CAAC,MAAM;YACpB,KAAK,EAAE,KAAK,CAAC,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,CAAC;QAC/F,CAAC;QACD,IAAI,YAAY,GAAG,CAAC,EAAE,CAAC;YACrB,MAAM,CAAC,IAAI,CAAC,cAAc,YAAY,uDAAuD,cAAc,EAAE,CAAC,CAAC;QACjH,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,oBAAoB,CAChC,UAAmD,EACnD,aAA4B,EAC5B,cAAsB,EACtB,MAAc;QAEd,OAAO,4BAA4B,CACjC,GAAG,EAAE,CACH,UAAU,CAAC,UAAU,CACnB;YACE,cAAc,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE;SACxD,EACD;YACE,SAAS,EAAE,SAAS,CAAC,EAAE,CAAC,gCAAgC;SACzD,CACF,EACH;YACE,YAAY,EAAE,SAAS,CAAC,EAAE,CAAC,0BAA0B,GAAG,CAAC;YACzD,OAAO,EAAE,CAAC,CAAS,EAAE,EAAE;gBACrB,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,wCAAwC,aAAa,iBAAiB,CAAC,CAAC;YACxG,CAAC;SACF,CACF,CAAC;IACJ,CAAC;IAEO,YAAY,CAAC,OAAkC;QACrD,MAAM,OAAO,GAAG,IAAI,GAAG,EAAsC,CAAC;QAC9D,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;YAC5B,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;YACxD,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;YAC/B,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
@@ -0,0 +1,21 @@
1
+ import { mongo } from '@powersync/lib-service-mongodb';
2
+ import { BucketDefinitionId, ParameterIndexId } from '../BucketDefinitionMapping.js';
3
+ import { BaseVersionedPowerSyncMongo } from '../common/VersionedPowerSyncMongoBase.js';
4
+ import { CommonSourceTableDocument } from '../models.js';
5
+ import { BucketDataDocumentV3, BucketParameterDocumentV3, BucketStateDocumentV3, CurrentDataDocumentV3, SourceTableDocumentV3 } from './models.js';
6
+ export declare class VersionedPowerSyncMongoV3 extends BaseVersionedPowerSyncMongo {
7
+ sourceRecordsV3(replicationStreamId: number, sourceTableId: mongo.ObjectId): mongo.Collection<CurrentDataDocumentV3>;
8
+ listSourceRecordCollectionsV3(replicationStreamId: number): Promise<mongo.Collection<CurrentDataDocumentV3>[]>;
9
+ initializeSourceRecordsCollection(replicationStreamId: number, sourceTableId: mongo.ObjectId): Promise<void>;
10
+ commonSourceTables(replicationStreamId: number): mongo.Collection<CommonSourceTableDocument>;
11
+ bucketStateV3(replicationStreamId: number): mongo.Collection<BucketStateDocumentV3>;
12
+ parameterIndexV3(replicationStreamId: number, indexId: ParameterIndexId): mongo.Collection<BucketParameterDocumentV3>;
13
+ sourceTablesV3(replicationStreamId: number): mongo.Collection<SourceTableDocumentV3>;
14
+ initializeStreamStorage(replicationStreamId: number): Promise<void>;
15
+ bucketDataV3(replicationStreamId: number, definitionId: BucketDefinitionId): mongo.Collection<BucketDataDocumentV3>;
16
+ listBucketDataCollectionsV3(replicationStreamId: number): Promise<mongo.Collection<BucketDataDocumentV3>[]>;
17
+ listParameterIndexCollectionsV3(replicationStreamId: number): Promise<{
18
+ collection: mongo.Collection<BucketParameterDocumentV3>;
19
+ indexId: ParameterIndexId;
20
+ }[]>;
21
+ }
@@ -0,0 +1,71 @@
1
+ import { BaseVersionedPowerSyncMongo } from '../common/VersionedPowerSyncMongoBase.js';
2
+ export class VersionedPowerSyncMongoV3 extends BaseVersionedPowerSyncMongo {
3
+ sourceRecordsV3(replicationStreamId, sourceTableId) {
4
+ const collectionName = this.sourceRecordsCollectionName(replicationStreamId, sourceTableId);
5
+ return this.db.collection(collectionName);
6
+ }
7
+ async listSourceRecordCollectionsV3(replicationStreamId) {
8
+ return this.listCollectionsByPrefix(`source_records_${replicationStreamId}_`);
9
+ }
10
+ async initializeSourceRecordsCollection(replicationStreamId, sourceTableId) {
11
+ await this.sourceRecordsV3(replicationStreamId, sourceTableId).createIndex({
12
+ pending_delete: 1
13
+ }, {
14
+ partialFilterExpression: { pending_delete: { $exists: true } },
15
+ name: 'pending_delete'
16
+ });
17
+ }
18
+ commonSourceTables(replicationStreamId) {
19
+ return this.sourceTablesV3(replicationStreamId);
20
+ }
21
+ bucketStateV3(replicationStreamId) {
22
+ return this.db.collection(`bucket_state_${replicationStreamId}`);
23
+ }
24
+ parameterIndexV3(replicationStreamId, indexId) {
25
+ return this.db.collection(`parameter_index_${replicationStreamId}_${indexId}`);
26
+ }
27
+ sourceTablesV3(replicationStreamId) {
28
+ return this.db.collection(this.sourceTableCollectionName(replicationStreamId));
29
+ }
30
+ async initializeStreamStorage(replicationStreamId) {
31
+ const sourceTables = this.sourceTablesV3(replicationStreamId);
32
+ const bucketState = this.bucketStateV3(replicationStreamId);
33
+ await sourceTables.createIndex({
34
+ connection_id: 1,
35
+ schema_name: 1,
36
+ table_name: 1,
37
+ relation_id: 1
38
+ }, {
39
+ name: 'source_lookup'
40
+ });
41
+ await sourceTables.createIndex({
42
+ latest_pending_delete: 1
43
+ }, {
44
+ partialFilterExpression: { latest_pending_delete: { $exists: true } },
45
+ name: 'latest_pending_delete'
46
+ });
47
+ await bucketState.createIndex({
48
+ last_op: 1
49
+ }, { name: 'bucket_updates', unique: true });
50
+ await bucketState.createIndex({
51
+ 'estimate_since_compact.count': -1
52
+ }, { name: 'dirty_count' });
53
+ }
54
+ bucketDataV3(replicationStreamId, definitionId) {
55
+ return this.db.collection(`bucket_data_${replicationStreamId}_${definitionId}`);
56
+ }
57
+ listBucketDataCollectionsV3(replicationStreamId) {
58
+ return this.upstream.listBucketDataCollectionsV3(replicationStreamId);
59
+ }
60
+ async listParameterIndexCollectionsV3(replicationStreamId) {
61
+ const prefix = `parameter_index_${replicationStreamId}_`;
62
+ const collections = await this.db.listCollections({ name: new RegExp(`^${prefix}`) }, { nameOnly: true }).toArray();
63
+ return collections
64
+ .filter((collection) => collection.name.startsWith(prefix))
65
+ .map((collection) => ({
66
+ collection: this.db.collection(collection.name),
67
+ indexId: collection.name.slice(prefix.length)
68
+ }));
69
+ }
70
+ }
71
+ //# sourceMappingURL=VersionedPowerSyncMongoV3.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VersionedPowerSyncMongoV3.js","sourceRoot":"","sources":["../../../../src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,2BAA2B,EAAE,MAAM,0CAA0C,CAAC;AAUvF,MAAM,OAAO,yBAA0B,SAAQ,2BAA2B;IACxE,eAAe,CAAC,mBAA2B,EAAE,aAA6B;QACxE,MAAM,cAAc,GAAG,IAAI,CAAC,2BAA2B,CAAC,mBAAmB,EAAE,aAAa,CAAC,CAAC;QAC5F,OAAO,IAAI,CAAC,EAAE,CAAC,UAAU,CAAwB,cAAc,CAAC,CAAC;IACnE,CAAC;IAED,KAAK,CAAC,6BAA6B,CAAC,mBAA2B;QAC7D,OAAO,IAAI,CAAC,uBAAuB,CAAwB,kBAAkB,mBAAmB,GAAG,CAAC,CAAC;IACvG,CAAC;IAED,KAAK,CAAC,iCAAiC,CAAC,mBAA2B,EAAE,aAA6B;QAChG,MAAM,IAAI,CAAC,eAAe,CAAC,mBAAmB,EAAE,aAAa,CAAC,CAAC,WAAW,CACxE;YACE,cAAc,EAAE,CAAC;SAClB,EACD;YACE,uBAAuB,EAAE,EAAE,cAAc,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;YAC9D,IAAI,EAAE,gBAAgB;SACvB,CACF,CAAC;IACJ,CAAC;IAED,kBAAkB,CAAC,mBAA2B;QAC5C,OAAO,IAAI,CAAC,cAAc,CAAC,mBAAmB,CAAgD,CAAC;IACjG,CAAC;IAED,aAAa,CAAC,mBAA2B;QACvC,OAAO,IAAI,CAAC,EAAE,CAAC,UA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