@powersync/service-module-mongodb-storage 0.16.0 → 0.17.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 +34 -0
- package/dist/storage/MongoBucketStorage.d.ts +6 -4
- package/dist/storage/MongoBucketStorage.js +110 -36
- package/dist/storage/MongoBucketStorage.js.map +1 -1
- package/dist/storage/implementation/BucketDefinitionMapping.d.ts +4 -6
- package/dist/storage/implementation/BucketDefinitionMapping.js +3 -3
- package/dist/storage/implementation/BucketDefinitionMapping.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 +33 -22
- package/dist/storage/implementation/MongoBucketBatch.js +45 -271
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
- package/dist/storage/implementation/MongoChecksums.d.ts +2 -1
- package/dist/storage/implementation/MongoChecksums.js.map +1 -1
- package/dist/storage/implementation/MongoCompactor.d.ts +1 -1
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +4 -4
- package/dist/storage/implementation/MongoPersistedSyncRules.js +11 -8
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +1 -1
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +19 -5
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +53 -19
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +1 -1
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +21 -10
- package/dist/storage/implementation/MongoSyncBucketStorage.js +18 -163
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +5 -1
- package/dist/storage/implementation/MongoSyncRulesLock.js +7 -3
- 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/PersistedBatch.d.ts +2 -2
- package/dist/storage/implementation/common/SourceRecordStore.d.ts +1 -2
- package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.d.ts +1 -1
- package/dist/storage/implementation/createMongoSyncBucketStorage.d.ts +2 -2
- package/dist/storage/implementation/createMongoSyncBucketStorage.js.map +1 -1
- package/dist/storage/implementation/db.d.ts +10 -2
- package/dist/storage/implementation/db.js.map +1 -1
- package/dist/storage/implementation/models.d.ts +31 -47
- package/dist/storage/implementation/models.js.map +1 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +15 -1
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js +385 -0
- package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -1
- package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +1 -1
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +16 -7
- package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +77 -6
- 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/models.d.ts +12 -1
- package/dist/storage/implementation/v1/models.js.map +1 -1
- package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +17 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js +429 -0
- package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +1 -1
- package/dist/storage/implementation/v3/MongoParameterLookupV3.d.ts +1 -2
- package/dist/storage/implementation/v3/MongoParameterLookupV3.js.map +1 -1
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +29 -7
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +117 -16
- package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -1
- package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +1 -2
- package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +3 -2
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +3 -0
- package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -1
- package/dist/storage/implementation/v3/models.d.ts +61 -3
- package/dist/storage/implementation/v3/models.js.map +1 -1
- package/package.json +6 -6
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +1 -1
- package/src/storage/MongoBucketStorage.ts +166 -44
- package/src/storage/implementation/BucketDefinitionMapping.ts +12 -9
- package/src/storage/implementation/CheckpointState.ts +59 -0
- package/src/storage/implementation/MongoBucketBatch.ts +81 -355
- package/src/storage/implementation/MongoChecksums.ts +2 -1
- package/src/storage/implementation/MongoCompactor.ts +1 -1
- package/src/storage/implementation/MongoPersistedSyncRules.ts +13 -7
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +69 -24
- package/src/storage/implementation/MongoSyncBucketStorage.ts +40 -215
- package/src/storage/implementation/MongoSyncRulesLock.ts +9 -3
- package/src/storage/implementation/SyncRuleStateUpdate.ts +38 -0
- package/src/storage/implementation/common/BucketDataDoc.ts +1 -1
- package/src/storage/implementation/common/PersistedBatch.ts +2 -2
- package/src/storage/implementation/common/SourceRecordStore.ts +1 -2
- package/src/storage/implementation/createMongoSyncBucketStorage.ts +2 -2
- package/src/storage/implementation/db.ts +5 -2
- package/src/storage/implementation/models.ts +35 -58
- package/src/storage/implementation/v1/MongoBucketBatchV1.ts +478 -1
- package/src/storage/implementation/v1/MongoCompactorV1.ts +1 -1
- package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +111 -16
- package/src/storage/implementation/v1/PersistedBatchV1.ts +1 -2
- package/src/storage/implementation/v1/models.ts +15 -0
- package/src/storage/implementation/v3/MongoBucketBatchV3.ts +564 -1
- package/src/storage/implementation/v3/MongoCompactorV3.ts +1 -1
- package/src/storage/implementation/v3/MongoParameterLookupV3.ts +1 -2
- package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +150 -22
- package/src/storage/implementation/v3/PersistedBatchV3.ts +1 -2
- package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +7 -2
- package/src/storage/implementation/v3/models.ts +70 -2
- package/test/src/storage_sync.test.ts +422 -6
- package/test/src/storeCurrentData.test.ts +211 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import * as lib_mongo from '@powersync/lib-service-mongodb';
|
|
2
2
|
import { mongo } from '@powersync/lib-service-mongodb';
|
|
3
|
+
import { ServiceAssertionError } from '@powersync/lib-services-framework';
|
|
3
4
|
import {
|
|
4
5
|
CheckpointChanges,
|
|
5
6
|
deserializeParameterLookup,
|
|
6
7
|
GetCheckpointChangesOptions,
|
|
7
8
|
InternalOpId,
|
|
8
9
|
internalToExternalOpId,
|
|
10
|
+
maxLsn,
|
|
9
11
|
ParameterSetLimitExceededError,
|
|
10
12
|
ProtocolOpId,
|
|
11
13
|
storage,
|
|
@@ -20,14 +22,20 @@ import {
|
|
|
20
22
|
MongoSyncBucketStorageCheckpoint,
|
|
21
23
|
MongoSyncBucketStorageContext
|
|
22
24
|
} from '../common/MongoSyncBucketStorageContext.js';
|
|
23
|
-
import {
|
|
25
|
+
import { SourceKey } from '../models.js';
|
|
24
26
|
import { MongoBucketBatchOptions } from '../MongoBucketBatch.js';
|
|
25
27
|
import { MongoChecksums } from '../MongoChecksums.js';
|
|
26
28
|
import { MongoCompactOptions, MongoCompactor } from '../MongoCompactor.js';
|
|
27
29
|
import { MongoParameterCompactor } from '../MongoParameterCompactor.js';
|
|
28
|
-
import {
|
|
30
|
+
import { MongoPersistedSyncRulesContentV1 } from '../MongoPersistedSyncRulesContent.js';
|
|
29
31
|
import { MongoSyncBucketStorage, MongoSyncBucketStorageOptions } from '../MongoSyncBucketStorage.js';
|
|
30
|
-
import {
|
|
32
|
+
import {
|
|
33
|
+
BucketDataDocumentV1,
|
|
34
|
+
BucketDataKeyV1,
|
|
35
|
+
BucketStateDocument,
|
|
36
|
+
loadBucketDataDocumentV1,
|
|
37
|
+
SyncRuleDocumentV1
|
|
38
|
+
} from './models.js';
|
|
31
39
|
import { MongoBucketBatchV1 } from './MongoBucketBatchV1.js';
|
|
32
40
|
import { MongoChecksumsV1 } from './MongoChecksumsV1.js';
|
|
33
41
|
import { MongoCompactorV1 } from './MongoCompactorV1.js';
|
|
@@ -42,7 +50,7 @@ export class MongoSyncBucketStorageV1 extends MongoSyncBucketStorage {
|
|
|
42
50
|
constructor(
|
|
43
51
|
factory: MongoBucketStorage,
|
|
44
52
|
group_id: number,
|
|
45
|
-
sync_rules:
|
|
53
|
+
sync_rules: MongoPersistedSyncRulesContentV1,
|
|
46
54
|
slot_name: string,
|
|
47
55
|
writeCheckpointMode: storage.WriteCheckpointMode | undefined,
|
|
48
56
|
options: MongoSyncBucketStorageOptions
|
|
@@ -56,6 +64,105 @@ export class MongoSyncBucketStorageV1 extends MongoSyncBucketStorage {
|
|
|
56
64
|
return new MongoBucketBatchV1(batchOptions);
|
|
57
65
|
}
|
|
58
66
|
|
|
67
|
+
protected async fetchCheckpointState(
|
|
68
|
+
session: mongo.ClientSession
|
|
69
|
+
): Promise<{ checkpoint: bigint; lsn: string | null } | null> {
|
|
70
|
+
const doc = (await this.db.sync_rules.findOne(
|
|
71
|
+
{ _id: this.group_id },
|
|
72
|
+
{
|
|
73
|
+
session,
|
|
74
|
+
projection: { _id: 1, state: 1, last_checkpoint: 1, last_checkpoint_lsn: 1, snapshot_done: 1 }
|
|
75
|
+
}
|
|
76
|
+
)) as SyncRuleDocumentV1;
|
|
77
|
+
if (!doc?.snapshot_done || ![storage.SyncRuleState.ACTIVE, storage.SyncRuleState.ERRORED].includes(doc.state)) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
checkpoint: doc.last_checkpoint ?? 0n,
|
|
82
|
+
lsn: doc.last_checkpoint_lsn ?? null
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected async getWriterSyncState() {
|
|
87
|
+
const doc = (await this.db.sync_rules.findOne(
|
|
88
|
+
{
|
|
89
|
+
_id: this.group_id
|
|
90
|
+
},
|
|
91
|
+
{ projection: { last_checkpoint_lsn: 1, keepalive_op: 1, snapshot_lsn: 1 } }
|
|
92
|
+
)) as SyncRuleDocumentV1;
|
|
93
|
+
const checkpointLsn = doc?.last_checkpoint_lsn ?? null;
|
|
94
|
+
return {
|
|
95
|
+
lastCheckpointLsn: checkpointLsn,
|
|
96
|
+
resumeFromLsn: maxLsn(checkpointLsn, doc?.snapshot_lsn),
|
|
97
|
+
keepaliveOp: doc?.keepalive_op ? BigInt(doc.keepalive_op) : null,
|
|
98
|
+
syncConfigId: null
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
protected async terminateSyncRuleState(): Promise<void> {
|
|
103
|
+
await this.db.sync_rules.updateOne(
|
|
104
|
+
{
|
|
105
|
+
_id: this.group_id
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
$set: {
|
|
109
|
+
state: storage.SyncRuleState.TERMINATED,
|
|
110
|
+
persisted_lsn: null,
|
|
111
|
+
snapshot_done: false
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
protected async getStatusImpl(): Promise<storage.SyncRuleStatus> {
|
|
118
|
+
const doc = (await this.db.sync_rules.findOne(
|
|
119
|
+
{
|
|
120
|
+
_id: this.group_id
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
projection: {
|
|
124
|
+
snapshot_done: 1,
|
|
125
|
+
last_checkpoint_lsn: 1,
|
|
126
|
+
state: 1,
|
|
127
|
+
snapshot_lsn: 1,
|
|
128
|
+
keepalive_op: 1
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
)) as SyncRuleDocumentV1;
|
|
132
|
+
if (doc == null) {
|
|
133
|
+
throw new ServiceAssertionError('Cannot find replication stream status');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
snapshot_done: doc.snapshot_done,
|
|
138
|
+
snapshot_lsn: doc.snapshot_lsn ?? null,
|
|
139
|
+
active: doc.state == storage.SyncRuleState.ACTIVE,
|
|
140
|
+
checkpoint_lsn: doc.last_checkpoint_lsn,
|
|
141
|
+
keepalive_op: doc.keepalive_op == null ? null : BigInt(doc.keepalive_op)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
protected async clearSyncRuleState(): Promise<void> {
|
|
146
|
+
await this.db.sync_rules.updateOne(
|
|
147
|
+
{
|
|
148
|
+
_id: this.group_id
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
$set: {
|
|
152
|
+
snapshot_done: false,
|
|
153
|
+
persisted_lsn: null,
|
|
154
|
+
last_checkpoint_lsn: null,
|
|
155
|
+
last_checkpoint: null,
|
|
156
|
+
no_checkpoint_before: null
|
|
157
|
+
},
|
|
158
|
+
$unset: {
|
|
159
|
+
snapshot_lsn: 1
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
{ maxTimeMS: lib_mongo.db.MONGO_CLEAR_OPERATION_TIMEOUT_MS }
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
59
166
|
protected createMongoChecksums(options: MongoSyncBucketStorageOptions): MongoChecksums {
|
|
60
167
|
return new MongoChecksumsV1(this.db, this.group_id, {
|
|
61
168
|
...options.checksumOptions,
|
|
@@ -75,18 +182,6 @@ export class MongoSyncBucketStorageV1 extends MongoSyncBucketStorage {
|
|
|
75
182
|
return new MongoParameterCompactorV1(this.db, this.group_id, checkpoint, options);
|
|
76
183
|
}
|
|
77
184
|
|
|
78
|
-
protected sourceTableBaseId(): Partial<CommonSourceTableDocument> {
|
|
79
|
-
return { group_id: this.group_id };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
protected augmentCreatedSourceTableDocument(
|
|
83
|
-
_createDoc: CommonSourceTableDocument,
|
|
84
|
-
_options: storage.ResolveTableOptions,
|
|
85
|
-
_candidateSourceTable: storage.SourceTable
|
|
86
|
-
): void {}
|
|
87
|
-
|
|
88
|
-
protected async initializeResolvedSourceRecords(_sourceTableId: bson.ObjectId): Promise<void> {}
|
|
89
|
-
|
|
90
185
|
protected override get versionContext(): MongoSyncBucketStorageContext<VersionedPowerSyncMongoV1> {
|
|
91
186
|
return {
|
|
92
187
|
db: this.db,
|
|
@@ -3,9 +3,8 @@ import { ReplicationAssertionError } from '@powersync/lib-services-framework';
|
|
|
3
3
|
import { storage } from '@powersync/service-core';
|
|
4
4
|
import * as bson from 'bson';
|
|
5
5
|
|
|
6
|
-
import { BucketDataSource } from '@powersync/service-sync-rules';
|
|
6
|
+
import { BucketDataSource, BucketDefinitionId } from '@powersync/service-sync-rules';
|
|
7
7
|
import { mongoTableId } from '../../../utils/util.js';
|
|
8
|
-
import { BucketDefinitionId } from '../BucketDefinitionMapping.js';
|
|
9
8
|
import { EMPTY_DATA } from '../MongoBucketBatchShared.js';
|
|
10
9
|
import {
|
|
11
10
|
BucketStateUpdate,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SerializedSyncPlan } from '@powersync/service-core';
|
|
1
2
|
import * as bson from 'bson';
|
|
2
3
|
import { BucketDataDoc } from '../common/BucketDataDoc.js';
|
|
3
4
|
import {
|
|
@@ -8,6 +9,8 @@ import {
|
|
|
8
9
|
LEGACY_BUCKET_DATA_DEFINITION_ID,
|
|
9
10
|
SourceKey,
|
|
10
11
|
SourceTableDocument,
|
|
12
|
+
SyncRuleCheckpointFields,
|
|
13
|
+
SyncRuleDocumentBase,
|
|
11
14
|
TaggedBucketParameterDocument
|
|
12
15
|
} from '../models.js';
|
|
13
16
|
|
|
@@ -75,6 +78,18 @@ export interface SourceTableDocumentV1 extends SourceTableDocument {
|
|
|
75
78
|
group_id: number;
|
|
76
79
|
}
|
|
77
80
|
|
|
81
|
+
export interface SyncRuleDocumentV1 extends SyncRuleDocumentBase, SyncRuleCheckpointFields<string | null> {
|
|
82
|
+
content: string;
|
|
83
|
+
serialized_plan?: SerializedSyncPlan | null;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* True if initial snapshot has been replicated.
|
|
87
|
+
*
|
|
88
|
+
* Can only be false if state == PROCESSING.
|
|
89
|
+
*/
|
|
90
|
+
snapshot_done: boolean;
|
|
91
|
+
}
|
|
92
|
+
|
|
78
93
|
export interface BucketStateDocumentV1 extends BucketStateDocumentBase {
|
|
79
94
|
_id: BucketStateDocumentBase['_id'] & {
|
|
80
95
|
g: number;
|