@powersync/service-module-mongodb-storage 0.16.0 → 0.18.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 +53 -0
- package/dist/storage/MongoBucketStorage.d.ts +35 -15
- package/dist/storage/MongoBucketStorage.js +309 -86
- package/dist/storage/MongoBucketStorage.js.map +1 -1
- package/dist/storage/implementation/CheckpointState.d.ts +20 -0
- package/dist/storage/implementation/CheckpointState.js +31 -0
- package/dist/storage/implementation/CheckpointState.js.map +1 -0
- package/dist/storage/implementation/MongoBucketBatch.d.ts +69 -39
- package/dist/storage/implementation/MongoBucketBatch.js +104 -313
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
- package/dist/storage/implementation/MongoChecksums.d.ts +11 -9
- package/dist/storage/implementation/MongoChecksums.js +3 -111
- package/dist/storage/implementation/MongoChecksums.js.map +1 -1
- package/dist/storage/implementation/MongoCompactor.d.ts +3 -1
- package/dist/storage/implementation/MongoCompactor.js +18 -9
- package/dist/storage/implementation/MongoCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoHydrationState.d.ts +11 -0
- package/dist/storage/implementation/MongoHydrationState.js +49 -0
- package/dist/storage/implementation/MongoHydrationState.js.map +1 -0
- package/dist/storage/implementation/MongoParameterCompactor.d.ts +6 -5
- package/dist/storage/implementation/MongoParameterCompactor.js +24 -1
- package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
- package/dist/storage/implementation/MongoParsedSyncConfigSet.d.ts +13 -0
- package/dist/storage/implementation/MongoParsedSyncConfigSet.js +64 -0
- package/dist/storage/implementation/MongoParsedSyncConfigSet.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedReplicationStream.d.ts +36 -0
- package/dist/storage/implementation/MongoPersistedReplicationStream.js +90 -0
- package/dist/storage/implementation/MongoPersistedReplicationStream.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncConfigContent.d.ts +25 -0
- package/dist/storage/implementation/MongoPersistedSyncConfigContent.js +121 -0
- package/dist/storage/implementation/MongoPersistedSyncConfigContent.js.map +1 -0
- package/dist/storage/implementation/MongoStorageProvider.js +5 -2
- package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +50 -21
- package/dist/storage/implementation/MongoSyncBucketStorage.js +62 -199
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +5 -1
- package/dist/storage/implementation/MongoSyncRulesLock.js +9 -5
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
- package/dist/storage/implementation/SyncRuleStateUpdate.d.ts +14 -0
- package/dist/storage/implementation/SyncRuleStateUpdate.js +36 -0
- package/dist/storage/implementation/SyncRuleStateUpdate.js.map +1 -0
- package/dist/storage/implementation/common/BucketDataDoc.d.ts +1 -1
- package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.d.ts +6 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.js +2 -0
- package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.js.map +1 -0
- package/dist/storage/implementation/common/PersistedBatch.d.ts +2 -3
- package/dist/storage/implementation/common/PersistedBatch.js +5 -0
- package/dist/storage/implementation/common/PersistedBatch.js.map +1 -1
- package/dist/storage/implementation/common/SourceRecordStore.d.ts +1 -2
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.d.ts +1 -1
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js +1 -1
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js.map +1 -1
- package/dist/storage/implementation/createMongoSyncBucketStorage.d.ts +2 -2
- package/dist/storage/implementation/createMongoSyncBucketStorage.js +4 -4
- package/dist/storage/implementation/createMongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/db.d.ts +10 -2
- package/dist/storage/implementation/db.js +9 -14
- package/dist/storage/implementation/db.js.map +1 -1
- package/dist/storage/implementation/models.d.ts +25 -50
- package/dist/storage/implementation/models.js.map +1 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +23 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js +403 -2
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -1
- package/dist/storage/implementation/v1/MongoChecksumsV1.js +114 -3
- package/dist/storage/implementation/v1/MongoChecksumsV1.js.map +1 -1
- package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +1 -1
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +22 -15
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +100 -27
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -1
- package/dist/storage/implementation/v1/PersistedBatchV1.d.ts +1 -2
- package/dist/storage/implementation/v1/PersistedBatchV1.js.map +1 -1
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.d.ts +2 -1
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js +3 -0
- package/dist/storage/implementation/v1/VersionedPowerSyncMongoV1.js.map +1 -1
- package/dist/storage/implementation/v1/models.d.ts +21 -1
- package/dist/storage/implementation/v1/models.js.map +1 -1
- package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +21 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js +459 -3
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoChecksumsV3.d.ts +27 -7
- package/dist/storage/implementation/v3/MongoChecksumsV3.js +202 -18
- package/dist/storage/implementation/v3/MongoChecksumsV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +20 -3
- package/dist/storage/implementation/v3/MongoCompactorV3.js +511 -22
- package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.d.ts +62 -0
- package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js +350 -0
- package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js.map +1 -0
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +50 -16
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +292 -75
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -1
- package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +2 -3
- package/dist/storage/implementation/v3/PersistedBatchV3.js +43 -20
- package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/SingleBucketStoreV3.js +52 -16
- package/dist/storage/implementation/v3/SingleBucketStoreV3.js.map +1 -1
- package/dist/storage/implementation/v3/SourceRecordStoreV3.d.ts +3 -3
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js +13 -11
- package/dist/storage/implementation/v3/SourceRecordStoreV3.js.map +1 -1
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +16 -14
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +32 -27
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -1
- package/dist/storage/implementation/v3/bucket-format.d.ts +4 -0
- package/dist/storage/implementation/v3/bucket-format.js +58 -0
- package/dist/storage/implementation/v3/bucket-format.js.map +1 -0
- package/dist/storage/implementation/v3/chunking.d.ts +10 -0
- package/dist/storage/implementation/v3/chunking.js +28 -0
- package/dist/storage/implementation/v3/chunking.js.map +1 -0
- package/dist/storage/implementation/v3/models.d.ts +111 -10
- package/dist/storage/implementation/v3/models.js +11 -28
- package/dist/storage/implementation/v3/models.js.map +1 -1
- package/dist/storage/implementation/v3/source-table-utils.d.ts +55 -0
- package/dist/storage/implementation/v3/source-table-utils.js +214 -0
- package/dist/storage/implementation/v3/source-table-utils.js.map +1 -0
- package/dist/storage/storage-index.d.ts +4 -4
- package/dist/storage/storage-index.js +4 -4
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/types/types.d.ts +2 -2
- package/dist/utils/test-utils.d.ts +1 -2
- package/dist/utils/test-utils.js +5 -1
- package/dist/utils/test-utils.js.map +1 -1
- package/dist/utils/util.d.ts +1 -1
- package/dist/utils/util.js +3 -2
- package/dist/utils/util.js.map +1 -1
- package/package.json +9 -19
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +1 -1
- package/src/storage/MongoBucketStorage.ts +464 -95
- package/src/storage/implementation/CheckpointState.ts +59 -0
- package/src/storage/implementation/MongoBucketBatch.ts +159 -405
- package/src/storage/implementation/MongoChecksums.ts +7 -138
- package/src/storage/implementation/MongoCompactor.ts +20 -10
- package/src/storage/implementation/MongoHydrationState.ts +61 -0
- package/src/storage/implementation/MongoParameterCompactor.ts +24 -5
- package/src/storage/implementation/MongoParsedSyncConfigSet.ts +88 -0
- package/src/storage/implementation/MongoPersistedReplicationStream.ts +118 -0
- package/src/storage/implementation/MongoPersistedSyncConfigContent.ts +152 -0
- package/src/storage/implementation/MongoStorageProvider.ts +5 -2
- package/src/storage/implementation/MongoSyncBucketStorage.ts +89 -250
- package/src/storage/implementation/MongoSyncRulesLock.ts +14 -5
- package/src/storage/implementation/SyncRuleStateUpdate.ts +38 -0
- package/src/storage/implementation/common/BucketDataDoc.ts +1 -1
- package/src/storage/implementation/common/MongoSyncBucketStorageCheckpoint.ts +7 -0
- package/src/storage/implementation/common/PersistedBatch.ts +8 -4
- package/src/storage/implementation/common/SourceRecordStore.ts +1 -2
- package/src/storage/implementation/common/VersionedPowerSyncMongoBase.ts +1 -1
- package/src/storage/implementation/createMongoSyncBucketStorage.ts +21 -7
- package/src/storage/implementation/db.ts +16 -21
- package/src/storage/implementation/models.ts +28 -61
- package/src/storage/implementation/v1/MongoBucketBatchV1.ts +503 -5
- package/src/storage/implementation/v1/MongoChecksumsV1.ts +137 -4
- package/src/storage/implementation/v1/MongoCompactorV1.ts +1 -1
- package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +150 -49
- package/src/storage/implementation/v1/PersistedBatchV1.ts +1 -2
- package/src/storage/implementation/v1/VersionedPowerSyncMongoV1.ts +11 -1
- package/src/storage/implementation/v1/models.ts +25 -0
- package/src/storage/implementation/v3/MongoBucketBatchV3.ts +611 -4
- package/src/storage/implementation/v3/MongoChecksumsV3.ts +262 -26
- package/src/storage/implementation/v3/MongoCompactorV3.ts +647 -30
- package/src/storage/implementation/v3/MongoStoppedSyncConfigCleanup.ts +480 -0
- package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +397 -111
- package/src/storage/implementation/v3/PersistedBatchV3.ts +63 -38
- package/src/storage/implementation/v3/SingleBucketStoreV3.ts +59 -19
- package/src/storage/implementation/v3/SourceRecordStoreV3.ts +12 -12
- package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +48 -38
- package/src/storage/implementation/v3/bucket-format.ts +70 -0
- package/src/storage/implementation/v3/chunking.ts +38 -0
- package/src/storage/implementation/v3/models.ts +150 -44
- package/src/storage/implementation/v3/source-table-utils.ts +392 -0
- package/src/storage/storage-index.ts +12 -4
- package/src/utils/test-utils.ts +6 -3
- package/src/utils/util.ts +3 -2
- package/test/src/__snapshots__/storage.test.ts.snap +0 -75
- package/test/src/__snapshots__/storage_sync.test.ts.snap +48 -48
- package/test/src/cleanup-stopped-sync-configs.test.ts +648 -0
- package/test/src/storage.test.ts +11 -11
- package/test/src/storage_compacting.test.ts +1956 -6
- package/test/src/storage_sync.test.ts +1429 -42
- package/test/src/storeCurrentData.test.ts +221 -0
- package/test/src/util.ts +2 -1
- package/tsconfig.json +1 -4
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/storage/implementation/BucketDefinitionMapping.d.ts +0 -17
- package/dist/storage/implementation/BucketDefinitionMapping.js +0 -58
- package/dist/storage/implementation/BucketDefinitionMapping.js.map +0 -1
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +0 -14
- package/dist/storage/implementation/MongoPersistedSyncRules.js +0 -64
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +0 -1
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +0 -15
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +0 -42
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +0 -1
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.d.ts +0 -13
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js +0 -2
- package/dist/storage/implementation/common/MongoSyncBucketStorageContext.js.map +0 -1
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +0 -9
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +0 -18
- package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +0 -1
- package/dist/storage/implementation/v3/MongoParameterLookupV3.d.ts +0 -5
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js +0 -9
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js.map +0 -1
- package/src/storage/implementation/BucketDefinitionMapping.ts +0 -72
- package/src/storage/implementation/MongoPersistedSyncRules.ts +0 -76
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +0 -56
- package/src/storage/implementation/common/MongoSyncBucketStorageContext.ts +0 -15
- package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +0 -24
- package/src/storage/implementation/v3/MongoParameterLookupV3.ts +0 -12
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
2
|
|
|
3
|
+
import { mongo } from '@powersync/lib-service-mongodb';
|
|
3
4
|
import { ErrorCode, Logger, ServiceError } from '@powersync/lib-services-framework';
|
|
4
5
|
import { storage } from '@powersync/service-core';
|
|
5
6
|
import { VersionedPowerSyncMongo } from './db.js';
|
|
@@ -11,13 +12,17 @@ import { VersionedPowerSyncMongo } from './db.js';
|
|
|
11
12
|
export class MongoSyncRulesLock implements storage.ReplicationLock {
|
|
12
13
|
private readonly refreshInterval: NodeJS.Timeout;
|
|
13
14
|
|
|
15
|
+
/**
|
|
16
|
+
* @param session optional session to create the lock within another transaction
|
|
17
|
+
*/
|
|
14
18
|
static async createLock(
|
|
15
19
|
db: VersionedPowerSyncMongo,
|
|
16
|
-
sync_rules: storage.
|
|
20
|
+
sync_rules: storage.PersistedReplicationStream,
|
|
21
|
+
session?: mongo.ClientSession
|
|
17
22
|
): Promise<MongoSyncRulesLock> {
|
|
18
23
|
const lockId = crypto.randomBytes(8).toString('hex');
|
|
19
24
|
const doc = await db.sync_rules.findOneAndUpdate(
|
|
20
|
-
{ _id: sync_rules.
|
|
25
|
+
{ _id: sync_rules.replicationStreamId, $or: [{ lock: null }, { 'lock.expires_at': { $lt: new Date() } }] },
|
|
21
26
|
{
|
|
22
27
|
$set: {
|
|
23
28
|
lock: {
|
|
@@ -28,13 +33,17 @@ export class MongoSyncRulesLock implements storage.ReplicationLock {
|
|
|
28
33
|
},
|
|
29
34
|
{
|
|
30
35
|
projection: { lock: 1 },
|
|
31
|
-
returnDocument: 'before'
|
|
36
|
+
returnDocument: 'before',
|
|
37
|
+
session
|
|
32
38
|
}
|
|
33
39
|
);
|
|
34
40
|
|
|
35
41
|
if (doc == null) {
|
|
36
42
|
// Query the existing lock to get the expiration time (best effort - it may have been released in the meantime).
|
|
37
|
-
const heldLock = await db.sync_rules.findOne(
|
|
43
|
+
const heldLock = await db.sync_rules.findOne(
|
|
44
|
+
{ _id: sync_rules.replicationStreamId },
|
|
45
|
+
{ projection: { lock: 1 }, session }
|
|
46
|
+
);
|
|
38
47
|
if (heldLock?.lock?.expires_at) {
|
|
39
48
|
throw new ServiceError(
|
|
40
49
|
ErrorCode.PSYNC_S1003,
|
|
@@ -45,7 +54,7 @@ export class MongoSyncRulesLock implements storage.ReplicationLock {
|
|
|
45
54
|
}
|
|
46
55
|
}
|
|
47
56
|
sync_rules.logger.info(`Locked replication stream for processing`);
|
|
48
|
-
return new MongoSyncRulesLock(db, sync_rules.
|
|
57
|
+
return new MongoSyncRulesLock(db, sync_rules.replicationStreamId, lockId, sync_rules.logger);
|
|
49
58
|
}
|
|
50
59
|
|
|
51
60
|
constructor(
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
|
+
import { storage } from '@powersync/service-core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Update pipeline to update replication stream status, covering all storage versions.
|
|
6
|
+
*
|
|
7
|
+
* Roughly equivalent to:
|
|
8
|
+
* $set: {
|
|
9
|
+
* state: state,
|
|
10
|
+
* 'sync_configs.$[].state': state
|
|
11
|
+
* }
|
|
12
|
+
*
|
|
13
|
+
* The difference is that this also handles v1 storage cases, where `sync_configs` is not present.
|
|
14
|
+
*/
|
|
15
|
+
export function syncRuleStateUpdatePipeline(state: storage.SyncRuleState): mongo.Document[] {
|
|
16
|
+
return [
|
|
17
|
+
{
|
|
18
|
+
$set: {
|
|
19
|
+
state,
|
|
20
|
+
sync_configs: {
|
|
21
|
+
$cond: [
|
|
22
|
+
{ $isArray: '$sync_configs' },
|
|
23
|
+
{
|
|
24
|
+
$map: {
|
|
25
|
+
input: '$sync_configs',
|
|
26
|
+
as: 'config',
|
|
27
|
+
in: {
|
|
28
|
+
$mergeObjects: ['$$config', { state }]
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
'$$REMOVE'
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
];
|
|
38
|
+
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
|
-
import { BucketDataSource, EvaluatedParameters, EvaluatedRow } from '@powersync/service-sync-rules';
|
|
2
|
+
import { BucketDataSource, BucketDefinitionId, EvaluatedParameters, EvaluatedRow } from '@powersync/service-sync-rules';
|
|
3
3
|
import * as bson from 'bson';
|
|
4
4
|
|
|
5
5
|
import { logger as defaultLogger, Logger } from '@powersync/lib-services-framework';
|
|
6
|
-
import { InternalOpId, storage, utils } from '@powersync/service-core';
|
|
6
|
+
import { BucketDefinitionMapping, InternalOpId, storage, utils } from '@powersync/service-core';
|
|
7
7
|
import { JSONBig } from '@powersync/service-jsonbig';
|
|
8
8
|
import { mongoTableId, replicaIdToSubkey } from '../../../utils/util.js';
|
|
9
|
-
import { BucketDefinitionId, BucketDefinitionMapping } from '../BucketDefinitionMapping.js';
|
|
10
9
|
import { currentBucketKey, MAX_ROW_SIZE } from '../MongoBucketBatchShared.js';
|
|
11
10
|
import { MongoIdSequence } from '../MongoIdSequence.js';
|
|
12
11
|
import type { VersionedPowerSyncMongo } from '../db.js';
|
|
@@ -97,7 +96,7 @@ export abstract class PersistedBatch {
|
|
|
97
96
|
}
|
|
98
97
|
|
|
99
98
|
saveBucketData(options: SaveBucketDataOptions) {
|
|
100
|
-
const remaining_buckets = new Map<string,
|
|
99
|
+
const remaining_buckets = new Map<string, SourceRecordBucketState>();
|
|
101
100
|
for (let bucket of options.before_buckets) {
|
|
102
101
|
const mapped: SourceRecordBucketState = {
|
|
103
102
|
bucket: bucket.bucket,
|
|
@@ -105,6 +104,11 @@ export abstract class PersistedBatch {
|
|
|
105
104
|
id: bucket.id,
|
|
106
105
|
table: bucket.table
|
|
107
106
|
};
|
|
107
|
+
if (options.table.bucketDataSourceIds != null && !options.table.bucketDataSourceIds.has(mapped.definitionId!)) {
|
|
108
|
+
// The bucket definition is not active anymore.
|
|
109
|
+
// We don't cleanup these references upfront, but do need to ignore them after the definition is removed.
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
108
112
|
remaining_buckets.set(currentBucketKey(mapped), mapped);
|
|
109
113
|
}
|
|
110
114
|
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { mongo } from '@powersync/lib-service-mongodb';
|
|
2
2
|
import { Logger } from '@powersync/lib-services-framework';
|
|
3
3
|
import { storage } from '@powersync/service-core';
|
|
4
|
-
import { EvaluatedParameters, EvaluatedRow } from '@powersync/service-sync-rules';
|
|
4
|
+
import { BucketDefinitionId, EvaluatedParameters, EvaluatedRow, ParameterIndexId } from '@powersync/service-sync-rules';
|
|
5
5
|
import * as bson from 'bson';
|
|
6
|
-
import { BucketDefinitionId, ParameterIndexId } from '../BucketDefinitionMapping.js';
|
|
7
6
|
|
|
8
7
|
export interface SourceRecordLookupEntry {
|
|
9
8
|
sourceTableId: bson.ObjectId;
|
|
@@ -67,7 +67,7 @@ export abstract class BaseVersionedPowerSyncMongo {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
protected async listCollectionsByPrefix<T extends mongo.Document>(prefix: string): Promise<mongo.Collection<T>[]> {
|
|
70
|
-
const collections = await this.db.listCollections({ name:
|
|
70
|
+
const collections = await this.db.listCollections({ name: { $regex: `^${prefix}` } }, { nameOnly: true }).toArray();
|
|
71
71
|
|
|
72
72
|
return collections
|
|
73
73
|
.filter((collection) => collection.name.startsWith(prefix))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { storage } from '@powersync/service-core';
|
|
2
2
|
import { MongoBucketStorage } from '../MongoBucketStorage.js';
|
|
3
|
-
import {
|
|
3
|
+
import { MongoPersistedReplicationStream } from './MongoPersistedReplicationStream.js';
|
|
4
4
|
import { MongoSyncBucketStorage, MongoSyncBucketStorageOptions } from './MongoSyncBucketStorage.js';
|
|
5
5
|
import { MongoSyncBucketStorageV1 } from './v1/MongoSyncBucketStorageV1.js';
|
|
6
6
|
import { MongoSyncBucketStorageV3 } from './v3/MongoSyncBucketStorageV3.js';
|
|
@@ -11,15 +11,29 @@ export type { MongoSyncBucketStorage };
|
|
|
11
11
|
|
|
12
12
|
export function createMongoSyncBucketStorage(
|
|
13
13
|
factory: MongoBucketStorage,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
replicationStreamId: number,
|
|
15
|
+
replicationStream: MongoPersistedReplicationStream,
|
|
16
|
+
replicationStreamName: string,
|
|
17
17
|
writeCheckpointMode: storage.WriteCheckpointMode | undefined,
|
|
18
18
|
options: MongoSyncBucketStorageOptions
|
|
19
19
|
): MongoSyncBucketStorage {
|
|
20
|
-
if (
|
|
21
|
-
return new MongoSyncBucketStorageV3(
|
|
20
|
+
if (replicationStream.getStorageConfig().incrementalReprocessing) {
|
|
21
|
+
return new MongoSyncBucketStorageV3(
|
|
22
|
+
factory,
|
|
23
|
+
replicationStreamId,
|
|
24
|
+
replicationStream,
|
|
25
|
+
replicationStreamName,
|
|
26
|
+
writeCheckpointMode,
|
|
27
|
+
options
|
|
28
|
+
);
|
|
22
29
|
}
|
|
23
30
|
|
|
24
|
-
return new MongoSyncBucketStorageV1(
|
|
31
|
+
return new MongoSyncBucketStorageV1(
|
|
32
|
+
factory,
|
|
33
|
+
replicationStreamId,
|
|
34
|
+
replicationStream,
|
|
35
|
+
replicationStreamName,
|
|
36
|
+
writeCheckpointMode,
|
|
37
|
+
options
|
|
38
|
+
);
|
|
25
39
|
}
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
InstanceDocument,
|
|
15
15
|
SourceTableDocument,
|
|
16
16
|
StorageConfig,
|
|
17
|
-
|
|
17
|
+
SyncRuleDocumentBase,
|
|
18
18
|
WriteCheckpointDocument
|
|
19
19
|
} from './models.js';
|
|
20
20
|
import {
|
|
@@ -41,7 +41,7 @@ export class PowerSyncMongo {
|
|
|
41
41
|
readonly bucket_data: mongo.Collection<BucketDataDocumentV1>;
|
|
42
42
|
readonly bucket_parameters: mongo.Collection<BucketParameterDocument>;
|
|
43
43
|
readonly op_id_sequence: mongo.Collection<IdSequenceDocument>;
|
|
44
|
-
readonly sync_rules: mongo.Collection<
|
|
44
|
+
readonly sync_rules: mongo.Collection<SyncRuleDocumentBase>;
|
|
45
45
|
readonly source_tables: mongo.Collection<SourceTableDocument>;
|
|
46
46
|
readonly custom_write_checkpoints: mongo.Collection<CustomWriteCheckpointDocument>;
|
|
47
47
|
readonly write_checkpoints: mongo.Collection<WriteCheckpointDocument>;
|
|
@@ -77,8 +77,11 @@ export class PowerSyncMongo {
|
|
|
77
77
|
this.connection_report_events = this.db.collection('connection_report_events');
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
versioned(storageConfig: StorageConfig & { incrementalReprocessing: true }): VersionedPowerSyncMongoV3;
|
|
81
|
+
versioned(storageConfig: StorageConfig & { incrementalReprocessing: false }): VersionedPowerSyncMongoV1;
|
|
82
|
+
versioned(storageConfig: StorageConfig): VersionedPowerSyncMongo;
|
|
80
83
|
versioned(storageConfig: StorageConfig): VersionedPowerSyncMongo {
|
|
81
|
-
if (storageConfig.
|
|
84
|
+
if (storageConfig.version >= storage.STORAGE_VERSION_3) {
|
|
82
85
|
return new VersionedPowerSyncMongoV3(this, storageConfig);
|
|
83
86
|
}
|
|
84
87
|
|
|
@@ -90,22 +93,18 @@ export class PowerSyncMongo {
|
|
|
90
93
|
*/
|
|
91
94
|
async listBucketDataCollectionsV3(groupId?: number): Promise<mongo.Collection<BucketDataDocumentV3>[]> {
|
|
92
95
|
const prefix = groupId == null ? 'bucket_data_' : `bucket_data_${groupId}_`;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return collections
|
|
96
|
-
.filter((collection) => collection.name.startsWith(prefix))
|
|
97
|
-
.map((collection) => this.db.collection<BucketDataDocumentV3>(collection.name));
|
|
96
|
+
return this.collectionsByPrefix<BucketDataDocumentV3>(prefix);
|
|
98
97
|
}
|
|
99
98
|
|
|
100
99
|
/**
|
|
101
100
|
* Not safe for user-provided prefix - only for hardcoded values.
|
|
102
101
|
*/
|
|
103
|
-
private async collectionsByPrefix(prefix: string): Promise<mongo.Collection<
|
|
104
|
-
const collections = await this.db.listCollections({ name:
|
|
102
|
+
private async collectionsByPrefix<T extends mongo.Document = never>(prefix: string): Promise<mongo.Collection<T>[]> {
|
|
103
|
+
const collections = await this.db.listCollections({ name: { $regex: `^${prefix}` } }, { nameOnly: true }).toArray();
|
|
105
104
|
|
|
106
105
|
return collections
|
|
107
106
|
.filter((collection) => collection.name.startsWith(prefix))
|
|
108
|
-
.map((collection) => this.db.collection<
|
|
107
|
+
.map((collection) => this.db.collection<T>(collection.name));
|
|
109
108
|
}
|
|
110
109
|
|
|
111
110
|
/**
|
|
@@ -141,16 +140,12 @@ export class PowerSyncMongo {
|
|
|
141
140
|
async listSourceTableCollections(
|
|
142
141
|
replicationStreamId?: number
|
|
143
142
|
): Promise<mongo.Collection<CommonSourceTableDocument>[]> {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
return collections
|
|
152
|
-
.filter((collection) => collection.name.startsWith(prefix))
|
|
153
|
-
.map((collection) => this.db.collection<CommonSourceTableDocument>(collection.name));
|
|
143
|
+
if (replicationStreamId == null) {
|
|
144
|
+
return this.collectionsByPrefix('source_table_');
|
|
145
|
+
}
|
|
146
|
+
const name = this.sourceTableCollectionName(replicationStreamId);
|
|
147
|
+
const collections = await this.db.listCollections({ name }, { nameOnly: true }).toArray();
|
|
148
|
+
return collections.map((c) => this.db.collection<CommonSourceTableDocument>(c.name));
|
|
154
149
|
}
|
|
155
150
|
|
|
156
151
|
/**
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { ErrorCode, ServiceError } from '@powersync/lib-services-framework';
|
|
2
|
-
import { InternalOpId,
|
|
3
|
-
import { SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
2
|
+
import { InternalOpId, storage } from '@powersync/service-core';
|
|
3
|
+
import { ParameterIndexId, SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
4
4
|
import { event_types } from '@powersync/service-types';
|
|
5
5
|
import * as bson from 'bson';
|
|
6
|
-
import { ParameterIndexId } from './BucketDefinitionMapping.js';
|
|
7
6
|
import type { CurrentDataDocument, SourceTableDocumentV1 } from './v1/models.js';
|
|
8
|
-
import type {
|
|
7
|
+
import type { CurrentDataDocumentV3, RecordedLookupV3 } from './v3/models.js';
|
|
9
8
|
|
|
10
9
|
/**
|
|
11
10
|
* Replica id uniquely identifying a row on the source database.
|
|
@@ -157,18 +156,14 @@ export interface IdSequenceDocument {
|
|
|
157
156
|
op_id: bigint;
|
|
158
157
|
}
|
|
159
158
|
|
|
160
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Base for sync_rules collection.
|
|
161
|
+
*/
|
|
162
|
+
export interface SyncRuleDocumentBase {
|
|
161
163
|
_id: number;
|
|
162
164
|
|
|
163
165
|
state: storage.SyncRuleState;
|
|
164
166
|
|
|
165
|
-
/**
|
|
166
|
-
* True if initial snapshot has been replicated.
|
|
167
|
-
*
|
|
168
|
-
* Can only be false if state == PROCESSING.
|
|
169
|
-
*/
|
|
170
|
-
snapshot_done: boolean;
|
|
171
|
-
|
|
172
167
|
/**
|
|
173
168
|
* This is now used for "resumeLsn".
|
|
174
169
|
*
|
|
@@ -182,31 +177,6 @@ export interface SyncRuleDocument {
|
|
|
182
177
|
*/
|
|
183
178
|
snapshot_lsn: string | undefined;
|
|
184
179
|
|
|
185
|
-
/**
|
|
186
|
-
* The last consistent checkpoint.
|
|
187
|
-
*
|
|
188
|
-
* There may be higher OpIds used in the database if we're in the middle of replicating a large transaction.
|
|
189
|
-
*/
|
|
190
|
-
last_checkpoint: bigint | null;
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* The LSN associated with the last consistent checkpoint.
|
|
194
|
-
*/
|
|
195
|
-
last_checkpoint_lsn: string | null;
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* If set, no new checkpoints may be created < this value.
|
|
199
|
-
*/
|
|
200
|
-
no_checkpoint_before: string | null;
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Goes together with no_checkpoint_before.
|
|
204
|
-
*
|
|
205
|
-
* If a keepalive is triggered that creates the checkpoint > no_checkpoint_before,
|
|
206
|
-
* then the checkpoint must be equal to this keepalive_op.
|
|
207
|
-
*/
|
|
208
|
-
keepalive_op: string | null;
|
|
209
|
-
|
|
210
180
|
slot_name: string | null;
|
|
211
181
|
|
|
212
182
|
/**
|
|
@@ -230,23 +200,6 @@ export interface SyncRuleDocument {
|
|
|
230
200
|
|
|
231
201
|
last_fatal_error_ts: Date | null;
|
|
232
202
|
|
|
233
|
-
content: string;
|
|
234
|
-
serialized_plan?: SerializedSyncPlan | null;
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Required for V3+ storage.
|
|
238
|
-
*/
|
|
239
|
-
rule_mapping?: {
|
|
240
|
-
/**
|
|
241
|
-
* Map of uniqueName -> id, unique per replication stream.
|
|
242
|
-
*/
|
|
243
|
-
definitions: Record<string, string>;
|
|
244
|
-
/**
|
|
245
|
-
* Map of (lookupName, queryId) -> id, unique per replication stream.
|
|
246
|
-
*/
|
|
247
|
-
parameter_indexes: Record<string, string>;
|
|
248
|
-
};
|
|
249
|
-
|
|
250
203
|
lock?: {
|
|
251
204
|
id: string;
|
|
252
205
|
expires_at: Date;
|
|
@@ -255,6 +208,25 @@ export interface SyncRuleDocument {
|
|
|
255
208
|
storage_version?: number;
|
|
256
209
|
}
|
|
257
210
|
|
|
211
|
+
export interface SyncRuleCheckpointFields {
|
|
212
|
+
/**
|
|
213
|
+
* The last consistent checkpoint.
|
|
214
|
+
*
|
|
215
|
+
* There may be higher OpIds used in the database if we're in the middle of replicating a large transaction.
|
|
216
|
+
*/
|
|
217
|
+
last_checkpoint: bigint | null;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The LSN associated with the last consistent checkpoint.
|
|
221
|
+
*/
|
|
222
|
+
last_checkpoint_lsn: string | null;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* If set, no new checkpoints may be created < this value.
|
|
226
|
+
*/
|
|
227
|
+
no_checkpoint_before: string | null;
|
|
228
|
+
}
|
|
229
|
+
|
|
258
230
|
export interface StorageConfig extends storage.StorageVersionConfig {
|
|
259
231
|
/**
|
|
260
232
|
* When true, bucket_data.checksum is guaranteed to be persisted as a Long.
|
|
@@ -289,11 +261,6 @@ export interface CheckpointEventDocument {
|
|
|
289
261
|
_id: bson.ObjectId;
|
|
290
262
|
}
|
|
291
263
|
|
|
292
|
-
export type SyncRuleCheckpointState = Pick<
|
|
293
|
-
SyncRuleDocument,
|
|
294
|
-
'last_checkpoint' | 'last_checkpoint_lsn' | '_id' | 'state'
|
|
295
|
-
>;
|
|
296
|
-
|
|
297
264
|
export interface CustomWriteCheckpointDocument {
|
|
298
265
|
_id: bson.ObjectId;
|
|
299
266
|
user_id: string;
|
|
@@ -329,6 +296,6 @@ export interface InstanceDocument {
|
|
|
329
296
|
export interface ClientConnectionDocument extends event_types.ClientConnection {}
|
|
330
297
|
|
|
331
298
|
export type CurrentDataDocumentId = CurrentDataDocument['_id'] | CurrentDataDocumentV3['_id'];
|
|
332
|
-
export type CommonCurrentBucket = CurrentBucket
|
|
299
|
+
export type CommonCurrentBucket = CurrentBucket;
|
|
333
300
|
export type CommonCurrentLookup = bson.Binary | RecordedLookupV3;
|
|
334
|
-
export type CommonSourceTableDocument = SourceTableDocumentV1 |
|
|
301
|
+
export type CommonSourceTableDocument = Pick<SourceTableDocumentV1, '_id' | 'snapshot_status'>;
|