@powersync/service-module-mongodb-storage 0.15.3 → 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.
- package/CHANGELOG.md +54 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +1 -1
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -1
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +3 -3
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -1
- package/dist/migrations/db/migrations/1770213298299-storage-version.js.map +1 -1
- package/dist/storage/MongoBucketStorage.d.ts +5 -3
- package/dist/storage/MongoBucketStorage.js +50 -36
- package/dist/storage/MongoBucketStorage.js.map +1 -1
- package/dist/storage/MongoReportStorage.js.map +1 -1
- package/dist/storage/implementation/BucketDefinitionMapping.d.ts +17 -0
- package/dist/storage/implementation/BucketDefinitionMapping.js +58 -0
- package/dist/storage/implementation/BucketDefinitionMapping.js.map +1 -0
- package/dist/storage/implementation/MongoBucketBatch.d.ts +16 -14
- package/dist/storage/implementation/MongoBucketBatch.js +80 -115
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
- package/dist/storage/implementation/MongoBucketBatchShared.d.ts +5 -0
- package/dist/storage/implementation/MongoBucketBatchShared.js +8 -0
- package/dist/storage/implementation/MongoBucketBatchShared.js.map +1 -0
- package/dist/storage/implementation/MongoChecksums.d.ts +28 -17
- package/dist/storage/implementation/MongoChecksums.js +13 -72
- package/dist/storage/implementation/MongoChecksums.js.map +1 -1
- package/dist/storage/implementation/MongoCompactor.d.ts +98 -58
- package/dist/storage/implementation/MongoCompactor.js +229 -296
- package/dist/storage/implementation/MongoCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoParameterCompactor.d.ts +11 -6
- package/dist/storage/implementation/MongoParameterCompactor.js +11 -8
- package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +14 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.js +64 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +3 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +9 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +1 -1
- package/dist/storage/implementation/MongoStorageProvider.js +1 -1
- package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +49 -30
- package/dist/storage/implementation/MongoSyncBucketStorage.js +96 -388
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +5 -3
- package/dist/storage/implementation/MongoSyncRulesLock.js +12 -10
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js +1 -1
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
- package/dist/storage/implementation/OperationBatch.js +1 -1
- package/dist/storage/implementation/common/BucketDataDoc.d.ts +35 -0
- package/dist/storage/implementation/common/BucketDataDoc.js +2 -0
- package/dist/storage/implementation/common/BucketDataDoc.js.map +1 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.d.ts +13 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js +2 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js.map +1 -0
- package/dist/storage/implementation/common/PersistedBatch.d.ts +108 -0
- package/dist/storage/implementation/common/PersistedBatch.js +237 -0
- package/dist/storage/implementation/common/PersistedBatch.js.map +1 -0
- package/dist/storage/implementation/common/SingleBucketStore.d.ts +54 -0
- package/dist/storage/implementation/common/SingleBucketStore.js +3 -0
- package/dist/storage/implementation/common/SingleBucketStore.js.map +1 -0
- package/dist/storage/implementation/common/SourceRecordStore.d.ts +36 -0
- package/dist/storage/implementation/common/SourceRecordStore.js +2 -0
- package/dist/storage/implementation/common/SourceRecordStore.js.map +1 -0
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.d.ts +27 -0
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js +57 -0
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js.map +1 -0
- package/dist/storage/implementation/createMongoSyncBucketStorage.d.ts +7 -0
- package/dist/storage/implementation/createMongoSyncBucketStorage.js +9 -0
- package/dist/storage/implementation/createMongoSyncBucketStorage.js.map +1 -0
- package/dist/storage/implementation/db.d.ts +34 -34
- package/dist/storage/implementation/db.js +78 -98
- package/dist/storage/implementation/db.js.map +1 -1
- package/dist/storage/implementation/models.d.ts +63 -34
- package/dist/storage/implementation/models.js +21 -2
- package/dist/storage/implementation/models.js.map +1 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +13 -0
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js +22 -0
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -0
- package/dist/storage/implementation/v1/MongoChecksumsV1.d.ts +12 -0
- package/dist/storage/implementation/v1/MongoChecksumsV1.js +56 -0
- package/dist/storage/implementation/v1/MongoChecksumsV1.js.map +1 -0
- package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +23 -0
- package/dist/storage/implementation/v1/MongoCompactorV1.js +52 -0
- package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -0
- package/dist/storage/implementation/v1/MongoParameterCompactorV1.d.ts +9 -0
- package/dist/storage/implementation/v1/MongoParameterCompactorV1.js +20 -0
- package/dist/storage/implementation/v1/MongoParameterCompactorV1.js.map +1 -0
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +41 -0
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +283 -0
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -0
- package/dist/storage/implementation/v1/PersistedBatchV1.d.ts +26 -0
- package/dist/storage/implementation/v1/PersistedBatchV1.js +183 -0
- package/dist/storage/implementation/v1/PersistedBatchV1.js.map +1 -0
- package/dist/storage/implementation/v1/SingleBucketStoreV1.d.ts +18 -0
- package/dist/storage/implementation/v1/SingleBucketStoreV1.js +57 -0
- package/dist/storage/implementation/v1/SingleBucketStoreV1.js.map +1 -0
- package/dist/storage/implementation/v1/SourceRecordStoreV1.d.ts +19 -0
- package/dist/storage/implementation/v1/SourceRecordStoreV1.js +105 -0
- package/dist/storage/implementation/v1/SourceRecordStoreV1.js.map +1 -0
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.d.ts +12 -0
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js +20 -0
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js.map +1 -0
- package/dist/storage/implementation/v1/models.d.ts +34 -0
- package/dist/storage/implementation/v1/models.js +37 -0
- package/dist/storage/implementation/v1/models.js.map +1 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +13 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js +34 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoChecksumsV3.d.ts +15 -0
- package/dist/storage/implementation/v3/MongoChecksumsV3.js +84 -0
- package/dist/storage/implementation/v3/MongoChecksumsV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +23 -0
- package/dist/storage/implementation/v3/MongoCompactorV3.js +68 -0
- package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +9 -0
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +18 -0
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoParameterLookupV3.d.ts +5 -0
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js +9 -0
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js.map +1 -0
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +41 -0
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +407 -0
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -0
- package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +29 -0
- package/dist/storage/implementation/v3/PersistedBatchV3.js +259 -0
- package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -0
- package/dist/storage/implementation/v3/SingleBucketStoreV3.d.ts +18 -0
- package/dist/storage/implementation/v3/SingleBucketStoreV3.js +48 -0
- package/dist/storage/implementation/v3/SingleBucketStoreV3.js.map +1 -0
- package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +22 -0
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js +164 -0
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js.map +1 -0
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +21 -0
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +71 -0
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -0
- package/dist/storage/implementation/v3/models.d.ts +43 -0
- package/dist/storage/implementation/v3/models.js +34 -0
- package/dist/storage/implementation/v3/models.js.map +1 -0
- package/dist/storage/storage-index.d.ts +8 -5
- package/dist/storage/storage-index.js +8 -5
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/utils/util.d.ts +11 -4
- package/dist/utils/util.js +25 -4
- package/dist/utils/util.js.map +1 -1
- package/package.json +9 -9
- package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +1 -1
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +7 -7
- package/src/migrations/db/migrations/1770213298299-storage-version.ts +1 -1
- package/src/storage/MongoBucketStorage.ts +97 -62
- package/src/storage/MongoReportStorage.ts +2 -2
- package/src/storage/implementation/BucketDefinitionMapping.ts +72 -0
- package/src/storage/implementation/MongoBucketBatch.ts +110 -144
- package/src/storage/implementation/MongoBucketBatchShared.ts +11 -0
- package/src/storage/implementation/MongoChecksums.ts +53 -76
- package/src/storage/implementation/MongoCompactor.ts +374 -404
- package/src/storage/implementation/MongoParameterCompactor.ts +37 -24
- package/src/storage/implementation/MongoPersistedSyncRules.ts +76 -0
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +18 -1
- package/src/storage/implementation/MongoStorageProvider.ts +1 -1
- package/src/storage/implementation/MongoSyncBucketStorage.ts +190 -457
- package/src/storage/implementation/MongoSyncRulesLock.ts +12 -14
- package/src/storage/implementation/MongoWriteCheckpointAPI.ts +4 -2
- package/src/storage/implementation/OperationBatch.ts +1 -1
- package/src/storage/implementation/common/BucketDataDoc.ts +37 -0
- package/src/storage/implementation/common/MongoSyncBucketStorageContext.ts +15 -0
- package/src/storage/implementation/common/PersistedBatch.ts +364 -0
- package/src/storage/implementation/common/SingleBucketStore.ts +63 -0
- package/src/storage/implementation/common/SourceRecordStore.ts +49 -0
- package/src/storage/implementation/common/VersionedPowerSyncMongoBase.ts +80 -0
- package/src/storage/implementation/createMongoSyncBucketStorage.ts +25 -0
- package/src/storage/implementation/db.ts +107 -128
- package/src/storage/implementation/models.ts +84 -38
- package/src/storage/implementation/v1/MongoBucketBatchV1.ts +32 -0
- package/src/storage/implementation/v1/MongoChecksumsV1.ts +75 -0
- package/src/storage/implementation/v1/MongoCompactorV1.ts +93 -0
- package/src/storage/implementation/v1/MongoParameterCompactorV1.ts +26 -0
- package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +448 -0
- package/src/storage/implementation/v1/PersistedBatchV1.ts +230 -0
- package/src/storage/implementation/v1/SingleBucketStoreV1.ts +74 -0
- package/src/storage/implementation/v1/SourceRecordStoreV1.ts +156 -0
- package/src/storage/implementation/v1/VersionedPowerSyncMongoV1.ts +28 -0
- package/src/storage/implementation/v1/models.ts +84 -0
- package/src/storage/implementation/v3/MongoBucketBatchV3.ts +44 -0
- package/src/storage/implementation/v3/MongoChecksumsV3.ts +120 -0
- package/src/storage/implementation/v3/MongoCompactorV3.ts +107 -0
- package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +24 -0
- package/src/storage/implementation/v3/MongoParameterLookupV3.ts +12 -0
- package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +550 -0
- package/src/storage/implementation/v3/PersistedBatchV3.ts +318 -0
- package/src/storage/implementation/v3/SingleBucketStoreV3.ts +68 -0
- package/src/storage/implementation/v3/SourceRecordStoreV3.ts +226 -0
- package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +112 -0
- package/src/storage/implementation/v3/models.ts +96 -0
- package/src/storage/storage-index.ts +8 -5
- package/src/utils/util.ts +36 -7
- package/test/src/__snapshots__/storage_sync.test.ts.snap +282 -0
- package/test/src/connection-report-storage.test.ts +3 -3
- package/test/src/setup.ts +1 -1
- package/test/src/storage.test.ts +2 -2
- package/test/src/storage_compacting.test.ts +57 -29
- package/test/src/storage_sync.test.ts +351 -5
- package/test/tsconfig.json +0 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/storage/implementation/PersistedBatch.d.ts +0 -71
- package/dist/storage/implementation/PersistedBatch.js +0 -354
- package/dist/storage/implementation/PersistedBatch.js.map +0 -1
- package/src/storage/implementation/PersistedBatch.ts +0 -432
|
@@ -2,8 +2,14 @@ import { mongo } from '@powersync/lib-service-mongodb';
|
|
|
2
2
|
import { logger } from '@powersync/lib-services-framework';
|
|
3
3
|
import { bson, CompactOptions, InternalOpId } from '@powersync/service-core';
|
|
4
4
|
import { LRUCache } from 'lru-cache';
|
|
5
|
-
import { VersionedPowerSyncMongo } from './db.js';
|
|
6
|
-
|
|
5
|
+
import type { VersionedPowerSyncMongo } from './db.js';
|
|
6
|
+
|
|
7
|
+
type ParameterCompactionReadDocument = {
|
|
8
|
+
_id: InternalOpId;
|
|
9
|
+
key: mongo.Document;
|
|
10
|
+
lookup: unknown;
|
|
11
|
+
bucket_parameters?: unknown[] | null;
|
|
12
|
+
};
|
|
7
13
|
|
|
8
14
|
/**
|
|
9
15
|
* Compacts parameter lookup data (the bucket_parameters collection).
|
|
@@ -12,16 +18,28 @@ import { BucketParameterDocument } from './models.js';
|
|
|
12
18
|
*
|
|
13
19
|
* For background, see the `/docs/parameters-lookups.md` file.
|
|
14
20
|
*/
|
|
15
|
-
export class MongoParameterCompactor {
|
|
21
|
+
export abstract class MongoParameterCompactor {
|
|
16
22
|
constructor(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
protected readonly db: VersionedPowerSyncMongo,
|
|
24
|
+
protected readonly group_id: number,
|
|
25
|
+
protected readonly checkpoint: InternalOpId,
|
|
26
|
+
protected readonly options: CompactOptions
|
|
21
27
|
) {}
|
|
22
28
|
|
|
23
29
|
async compact() {
|
|
24
30
|
logger.info(`Compacting parameters for sync config ${this.group_id} up to checkpoint ${this.checkpoint}`);
|
|
31
|
+
for (const collection of await this.getCollections()) {
|
|
32
|
+
await this.compactCollection(collection);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
protected abstract getCollections(): Promise<mongo.Collection<mongo.Document>[]>;
|
|
37
|
+
|
|
38
|
+
protected abstract collectionFilter(): mongo.Document;
|
|
39
|
+
|
|
40
|
+
protected abstract deleteFilter(doc: mongo.Document): mongo.Document;
|
|
41
|
+
|
|
42
|
+
protected async compactCollection(collection: mongo.Collection<mongo.Document>) {
|
|
25
43
|
// This is the currently-active checkpoint.
|
|
26
44
|
// We do not remove any data that may be used by this checkpoint.
|
|
27
45
|
// snapshot queries ensure that if any clients are still using older checkpoints, they would
|
|
@@ -32,43 +50,38 @@ export class MongoParameterCompactor {
|
|
|
32
50
|
// In theory, we could let MongoDB do more of the work here, by grouping by (key, lookup)
|
|
33
51
|
// in MongoDB already. However, that risks running into cases where MongoDB needs to process
|
|
34
52
|
// very large amounts of data before returning results, which could lead to timeouts.
|
|
35
|
-
const cursor =
|
|
36
|
-
{
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
sort: { lookup: 1, _id: 1 },
|
|
41
|
-
batchSize: 10_000,
|
|
42
|
-
projection: { _id: 1, key: 1, lookup: 1, bucket_parameters: 1 }
|
|
43
|
-
}
|
|
44
|
-
);
|
|
53
|
+
const cursor = collection.find(this.collectionFilter(), {
|
|
54
|
+
sort: { lookup: 1, _id: 1 },
|
|
55
|
+
batchSize: 10_000,
|
|
56
|
+
projection: { _id: 1, key: 1, lookup: 1, bucket_parameters: 1 }
|
|
57
|
+
});
|
|
45
58
|
|
|
46
59
|
// The index doesn't cover sorting by key, so we keep our own cache of the last seen key.
|
|
47
60
|
let lastByKey = new LRUCache<string, InternalOpId>({
|
|
48
61
|
max: this.options.compactParameterCacheLimit ?? 10_000
|
|
49
62
|
});
|
|
50
63
|
let removeIds: InternalOpId[] = [];
|
|
51
|
-
let removeDeleted: mongo.AnyBulkWriteOperation<
|
|
64
|
+
let removeDeleted: mongo.AnyBulkWriteOperation<mongo.Document>[] = [];
|
|
52
65
|
let checkedEntries = 0;
|
|
53
66
|
let checkedEntriesAtLastLog = 0;
|
|
54
67
|
let lastProgressLogTime = Date.now();
|
|
55
68
|
|
|
56
69
|
const flush = async (force: boolean) => {
|
|
57
70
|
if (removeIds.length >= 1000 || (force && removeIds.length > 0)) {
|
|
58
|
-
const results = await
|
|
71
|
+
const results = await collection.deleteMany({ _id: { $in: removeIds } } as any);
|
|
59
72
|
logger.info(`Removed ${results.deletedCount} (${removeIds.length}) superseded parameter entries`);
|
|
60
73
|
removeIds = [];
|
|
61
74
|
}
|
|
62
75
|
|
|
63
76
|
if (removeDeleted.length > 10 || (force && removeDeleted.length > 0)) {
|
|
64
|
-
const results = await
|
|
77
|
+
const results = await collection.bulkWrite(removeDeleted);
|
|
65
78
|
logger.info(`Removed ${results.deletedCount} (${removeDeleted.length}) deleted parameter entries`);
|
|
66
79
|
removeDeleted = [];
|
|
67
80
|
}
|
|
68
81
|
};
|
|
69
82
|
|
|
70
83
|
while (await cursor.hasNext()) {
|
|
71
|
-
const batch = cursor.readBufferedDocuments();
|
|
84
|
+
const batch = cursor.readBufferedDocuments() as unknown as ParameterCompactionReadDocument[];
|
|
72
85
|
checkedEntries += batch.length;
|
|
73
86
|
const now = Date.now();
|
|
74
87
|
if (now - lastProgressLogTime >= 60_000) {
|
|
@@ -79,7 +92,7 @@ export class MongoParameterCompactor {
|
|
|
79
92
|
checkedEntriesAtLastLog = checkedEntries;
|
|
80
93
|
}
|
|
81
94
|
|
|
82
|
-
for (
|
|
95
|
+
for (const doc of batch) {
|
|
83
96
|
if (doc._id >= checkpoint) {
|
|
84
97
|
continue;
|
|
85
98
|
}
|
|
@@ -103,7 +116,7 @@ export class MongoParameterCompactor {
|
|
|
103
116
|
// in the cache due to cache size limits. So we need to explicitly remove all earlier operations.
|
|
104
117
|
removeDeleted.push({
|
|
105
118
|
deleteMany: {
|
|
106
|
-
filter:
|
|
119
|
+
filter: this.deleteFilter(doc)
|
|
107
120
|
}
|
|
108
121
|
});
|
|
109
122
|
}
|
|
@@ -113,6 +126,6 @@ export class MongoParameterCompactor {
|
|
|
113
126
|
}
|
|
114
127
|
|
|
115
128
|
await flush(true);
|
|
116
|
-
logger.info(
|
|
129
|
+
logger.info(`Parameter compaction completed for ${collection.collectionName}`);
|
|
117
130
|
}
|
|
118
131
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ServiceAssertionError } from '@powersync/lib-services-framework';
|
|
2
|
+
import { storage } from '@powersync/service-core';
|
|
3
|
+
import {
|
|
4
|
+
BucketDataScope,
|
|
5
|
+
BucketDataSource,
|
|
6
|
+
CompatibilityOption,
|
|
7
|
+
DEFAULT_HYDRATION_STATE,
|
|
8
|
+
HydratedSyncRules,
|
|
9
|
+
HydrationState,
|
|
10
|
+
ParameterIndexLookupCreator,
|
|
11
|
+
SyncConfigWithErrors,
|
|
12
|
+
versionedHydrationState
|
|
13
|
+
} from '@powersync/service-sync-rules';
|
|
14
|
+
import { BucketDefinitionMapping } from './BucketDefinitionMapping.js';
|
|
15
|
+
import { StorageConfig } from './models.js';
|
|
16
|
+
|
|
17
|
+
export class MongoPersistedSyncRules implements storage.PersistedSyncRules {
|
|
18
|
+
public readonly hydrationState: HydrationState;
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
public readonly id: number,
|
|
22
|
+
public readonly sync_rules: SyncConfigWithErrors,
|
|
23
|
+
public readonly slot_name: string,
|
|
24
|
+
private readonly mapping: BucketDefinitionMapping | null,
|
|
25
|
+
private readonly storageConfig: StorageConfig
|
|
26
|
+
) {
|
|
27
|
+
if (this.storageConfig.incrementalReprocessing) {
|
|
28
|
+
if (this.mapping == null) {
|
|
29
|
+
throw new ServiceAssertionError(`mapping is required for v3 storage`);
|
|
30
|
+
}
|
|
31
|
+
this.hydrationState = new MongoHydrationState(this.mapping, this.id);
|
|
32
|
+
} else if (
|
|
33
|
+
!this.sync_rules.config.compatibility.isEnabled(CompatibilityOption.versionedBucketIds) &&
|
|
34
|
+
!this.storageConfig.versionedBuckets
|
|
35
|
+
) {
|
|
36
|
+
this.hydrationState = DEFAULT_HYDRATION_STATE;
|
|
37
|
+
} else {
|
|
38
|
+
this.hydrationState = versionedHydrationState(this.id);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
hydratedSyncRules(): HydratedSyncRules {
|
|
43
|
+
return this.sync_rules.config.hydrate({ hydrationState: this.hydrationState });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class MongoHydrationState implements HydrationState {
|
|
48
|
+
constructor(
|
|
49
|
+
private readonly mapping: BucketDefinitionMapping,
|
|
50
|
+
private readonly version: number
|
|
51
|
+
) {}
|
|
52
|
+
|
|
53
|
+
getBucketSourceScope(source: BucketDataSource): BucketDataScope {
|
|
54
|
+
// Keep this aligned with versionedHydrationState() for now.
|
|
55
|
+
//
|
|
56
|
+
// Previous Mongo-specific behavior:
|
|
57
|
+
// const defId = this.mapping.bucketSourceId(source);
|
|
58
|
+
// return {
|
|
59
|
+
// bucketPrefix: defId,
|
|
60
|
+
// source
|
|
61
|
+
// };
|
|
62
|
+
return {
|
|
63
|
+
bucketPrefix: `${this.version}#${source.uniqueName}`,
|
|
64
|
+
source
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getParameterIndexLookupScope(source: ParameterIndexLookupCreator) {
|
|
69
|
+
const defId = this.mapping.parameterLookupId(source);
|
|
70
|
+
return {
|
|
71
|
+
lookupName: defId,
|
|
72
|
+
queryId: '',
|
|
73
|
+
source
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
2
|
import { storage } from '@powersync/service-core';
|
|
3
|
+
import { BucketDefinitionMapping } from './BucketDefinitionMapping.js';
|
|
4
|
+
import { MongoPersistedSyncRules } from './MongoPersistedSyncRules.js';
|
|
3
5
|
import { MongoSyncRulesLock } from './MongoSyncRulesLock.js';
|
|
4
|
-
import { PowerSyncMongo
|
|
6
|
+
import { PowerSyncMongo } from './db.js';
|
|
5
7
|
import { getMongoStorageConfig, SyncRuleDocument } from './models.js';
|
|
6
8
|
|
|
7
9
|
export class MongoPersistedSyncRulesContent extends storage.PersistedSyncRulesContent {
|
|
8
10
|
public current_lock: MongoSyncRulesLock | null = null;
|
|
11
|
+
public readonly mapping: BucketDefinitionMapping;
|
|
9
12
|
|
|
10
13
|
constructor(
|
|
11
14
|
private db: PowerSyncMongo,
|
|
@@ -25,12 +28,26 @@ export class MongoPersistedSyncRulesContent extends storage.PersistedSyncRulesCo
|
|
|
25
28
|
active: doc.state == 'ACTIVE',
|
|
26
29
|
storageVersion: doc.storage_version ?? storage.LEGACY_STORAGE_VERSION
|
|
27
30
|
});
|
|
31
|
+
this.mapping = BucketDefinitionMapping.fromSyncRules(doc);
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
getStorageConfig() {
|
|
31
35
|
return getMongoStorageConfig(this.storageVersion);
|
|
32
36
|
}
|
|
33
37
|
|
|
38
|
+
parsed(options: storage.ParseSyncRulesOptions): storage.PersistedSyncRules {
|
|
39
|
+
const parsed = super.parsed(options);
|
|
40
|
+
const storageConfig = this.getStorageConfig();
|
|
41
|
+
|
|
42
|
+
return new MongoPersistedSyncRules(
|
|
43
|
+
parsed.id,
|
|
44
|
+
parsed.sync_rules,
|
|
45
|
+
parsed.slot_name,
|
|
46
|
+
storageConfig.incrementalReprocessing ? this.mapping : null,
|
|
47
|
+
storageConfig
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
34
51
|
async lock() {
|
|
35
52
|
const lock = await MongoSyncRulesLock.createLock(this.db.versioned(this.getStorageConfig()), this);
|
|
36
53
|
this.current_lock = lock;
|
|
@@ -3,8 +3,8 @@ import { ErrorCode, logger, ServiceAssertionError, ServiceError } from '@powersy
|
|
|
3
3
|
import { POWERSYNC_VERSION, storage } from '@powersync/service-core';
|
|
4
4
|
import { MongoStorageConfig } from '../../types/types.js';
|
|
5
5
|
import { MongoBucketStorage } from '../MongoBucketStorage.js';
|
|
6
|
-
import { PowerSyncMongo } from './db.js';
|
|
7
6
|
import { MongoReportStorage } from '../MongoReportStorage.js';
|
|
7
|
+
import { PowerSyncMongo } from './db.js';
|
|
8
8
|
|
|
9
9
|
export class MongoStorageProvider implements storage.StorageProvider {
|
|
10
10
|
get type() {
|