@powersync/service-module-mongodb 0.0.0-dev-20241128134723 → 0.0.0-dev-20241219110735
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 +69 -4
- package/dist/db/db-index.d.ts +1 -0
- package/dist/db/db-index.js +2 -0
- package/dist/db/db-index.js.map +1 -0
- package/dist/db/mongo.d.ts +35 -0
- package/dist/db/mongo.js +73 -0
- package/dist/db/mongo.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/locks/MonogLocks.d.ts +36 -0
- package/dist/locks/MonogLocks.js +83 -0
- package/dist/locks/MonogLocks.js.map +1 -0
- package/dist/migrations/MonogMigrationAgent.d.ts +12 -0
- package/dist/migrations/MonogMigrationAgent.js +25 -0
- package/dist/migrations/MonogMigrationAgent.js.map +1 -0
- package/dist/migrations/db/migrations/1684951997326-init.d.ts +3 -0
- package/dist/migrations/db/migrations/1684951997326-init.js +30 -0
- package/dist/migrations/db/migrations/1684951997326-init.js.map +1 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +2 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +5 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +3 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +54 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +3 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +26 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +1 -0
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +3 -0
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +28 -0
- package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +1 -0
- package/dist/migrations/mongo-migration-store.d.ts +7 -0
- package/dist/migrations/mongo-migration-store.js +49 -0
- package/dist/migrations/mongo-migration-store.js.map +1 -0
- package/dist/module/MongoModule.js +15 -4
- package/dist/module/MongoModule.js.map +1 -1
- package/dist/replication/MongoManager.d.ts +1 -1
- package/dist/replication/MongoManager.js +3 -2
- package/dist/replication/MongoManager.js.map +1 -1
- package/dist/storage/MongoBucketStorage.d.ts +48 -0
- package/dist/storage/MongoBucketStorage.js +425 -0
- package/dist/storage/MongoBucketStorage.js.map +1 -0
- package/dist/storage/implementation/MongoBucketBatch.d.ts +72 -0
- package/dist/storage/implementation/MongoBucketBatch.js +681 -0
- package/dist/storage/implementation/MongoBucketBatch.js.map +1 -0
- package/dist/storage/implementation/MongoCompactor.d.ts +40 -0
- package/dist/storage/implementation/MongoCompactor.js +310 -0
- package/dist/storage/implementation/MongoCompactor.js.map +1 -0
- package/dist/storage/implementation/MongoIdSequence.d.ts +12 -0
- package/dist/storage/implementation/MongoIdSequence.js +21 -0
- package/dist/storage/implementation/MongoIdSequence.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.d.ts +9 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.js +9 -0
- package/dist/storage/implementation/MongoPersistedSyncRules.js.map +1 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.d.ts +20 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js +26 -0
- package/dist/storage/implementation/MongoPersistedSyncRulesContent.js.map +1 -0
- package/dist/storage/implementation/MongoStorageProvider.d.ts +6 -0
- package/dist/storage/implementation/MongoStorageProvider.js +34 -0
- package/dist/storage/implementation/MongoStorageProvider.js.map +1 -0
- package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +36 -0
- package/dist/storage/implementation/MongoSyncBucketStorage.js +529 -0
- package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -0
- package/dist/storage/implementation/MongoSyncRulesLock.d.ts +16 -0
- package/dist/storage/implementation/MongoSyncRulesLock.js +65 -0
- package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -0
- package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +20 -0
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js +104 -0
- package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -0
- package/dist/storage/implementation/OperationBatch.d.ts +34 -0
- package/dist/storage/implementation/OperationBatch.js +119 -0
- package/dist/storage/implementation/OperationBatch.js.map +1 -0
- package/dist/storage/implementation/PersistedBatch.d.ts +46 -0
- package/dist/storage/implementation/PersistedBatch.js +223 -0
- package/dist/storage/implementation/PersistedBatch.js.map +1 -0
- package/dist/storage/implementation/config.d.ts +19 -0
- package/dist/storage/implementation/config.js +26 -0
- package/dist/storage/implementation/config.js.map +1 -0
- package/dist/storage/implementation/db.d.ts +36 -0
- package/dist/storage/implementation/db.js +47 -0
- package/dist/storage/implementation/db.js.map +1 -0
- package/dist/storage/implementation/models.d.ts +139 -0
- package/dist/storage/implementation/models.js +2 -0
- package/dist/storage/implementation/models.js.map +1 -0
- package/dist/storage/implementation/util.d.ts +58 -0
- package/dist/storage/implementation/util.js +196 -0
- package/dist/storage/implementation/util.js.map +1 -0
- package/dist/storage/storage-index.d.ts +14 -0
- package/dist/storage/storage-index.js +15 -0
- package/dist/storage/storage-index.js.map +1 -0
- package/dist/types/types.d.ts +3 -0
- package/dist/types/types.js +4 -1
- package/dist/types/types.js.map +1 -1
- package/package.json +11 -8
- package/src/db/db-index.ts +1 -0
- package/src/db/mongo.ts +81 -0
- package/src/index.ts +4 -0
- package/src/locks/MonogLocks.ts +147 -0
- package/src/migrations/MonogMigrationAgent.ts +39 -0
- package/src/migrations/db/migrations/1684951997326-init.ts +39 -0
- package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +5 -0
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +105 -0
- package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +38 -0
- package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +40 -0
- package/src/migrations/mongo-migration-store.ts +62 -0
- package/src/module/MongoModule.ts +18 -4
- package/src/replication/MongoManager.ts +6 -2
- package/src/storage/MongoBucketStorage.ts +530 -0
- package/src/storage/implementation/MongoBucketBatch.ts +893 -0
- package/src/storage/implementation/MongoCompactor.ts +392 -0
- package/src/storage/implementation/MongoIdSequence.ts +24 -0
- package/src/storage/implementation/MongoPersistedSyncRules.ts +16 -0
- package/src/storage/implementation/MongoPersistedSyncRulesContent.ts +49 -0
- package/src/storage/implementation/MongoStorageProvider.ts +42 -0
- package/src/storage/implementation/MongoSyncBucketStorage.ts +612 -0
- package/src/storage/implementation/MongoSyncRulesLock.ts +88 -0
- package/src/storage/implementation/MongoWriteCheckpointAPI.ts +146 -0
- package/src/storage/implementation/OperationBatch.ts +130 -0
- package/src/storage/implementation/PersistedBatch.ts +283 -0
- package/src/storage/implementation/config.ts +40 -0
- package/src/storage/implementation/db.ts +88 -0
- package/src/storage/implementation/models.ts +160 -0
- package/src/storage/implementation/util.ts +209 -0
- package/src/storage/storage-index.ts +14 -0
- package/src/types/types.ts +8 -1
- package/test/src/__snapshots__/storage_sync.test.ts.snap +332 -0
- package/test/src/change_stream.test.ts +34 -33
- package/test/src/change_stream_utils.ts +6 -6
- package/test/src/env.ts +1 -0
- package/test/src/slow_tests.test.ts +4 -4
- package/test/src/storage.test.ts +7 -0
- package/test/src/storage_compacting.test.ts +6 -0
- package/test/src/storage_sync.test.ts +113 -0
- package/test/src/util.ts +20 -7
- package/test/tsconfig.json +4 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +1 -1
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
53
|
+
import * as bson from 'bson';
|
|
54
|
+
import * as mongo from 'mongodb';
|
|
55
|
+
import { DisposableObserver, logger } from '@powersync/lib-services-framework';
|
|
56
|
+
import { storage, utils } from '@powersync/service-core';
|
|
57
|
+
import * as timers from 'timers/promises';
|
|
58
|
+
import * as db from '../../db/db-index.js';
|
|
59
|
+
import { MongoBucketBatch } from './MongoBucketBatch.js';
|
|
60
|
+
import { MongoCompactor } from './MongoCompactor.js';
|
|
61
|
+
import { MongoWriteCheckpointAPI } from './MongoWriteCheckpointAPI.js';
|
|
62
|
+
import { BSON_DESERIALIZE_OPTIONS, idPrefixFilter, mapOpEntry, readSingleBatch, serializeLookup } from './util.js';
|
|
63
|
+
export class MongoSyncBucketStorage extends DisposableObserver {
|
|
64
|
+
constructor(factory, group_id, sync_rules, slot_name, writeCheckpointMode = storage.WriteCheckpointMode.MANAGED) {
|
|
65
|
+
super();
|
|
66
|
+
this.factory = factory;
|
|
67
|
+
this.group_id = group_id;
|
|
68
|
+
this.sync_rules = sync_rules;
|
|
69
|
+
this.slot_name = slot_name;
|
|
70
|
+
this.checksumCache = new storage.ChecksumCache({
|
|
71
|
+
fetchChecksums: (batch) => {
|
|
72
|
+
return this.getChecksumsInternal(batch);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
this.db = factory.db;
|
|
76
|
+
this.writeCheckpointAPI = new MongoWriteCheckpointAPI({
|
|
77
|
+
db: this.db,
|
|
78
|
+
mode: writeCheckpointMode
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
get writeCheckpointMode() {
|
|
82
|
+
return this.writeCheckpointAPI.writeCheckpointMode;
|
|
83
|
+
}
|
|
84
|
+
setWriteCheckpointMode(mode) {
|
|
85
|
+
this.writeCheckpointAPI.setWriteCheckpointMode(mode);
|
|
86
|
+
}
|
|
87
|
+
batchCreateCustomWriteCheckpoints(checkpoints) {
|
|
88
|
+
return this.writeCheckpointAPI.batchCreateCustomWriteCheckpoints(checkpoints.map((checkpoint) => ({ ...checkpoint, sync_rules_id: this.group_id })));
|
|
89
|
+
}
|
|
90
|
+
createCustomWriteCheckpoint(checkpoint) {
|
|
91
|
+
return this.writeCheckpointAPI.createCustomWriteCheckpoint({
|
|
92
|
+
...checkpoint,
|
|
93
|
+
sync_rules_id: this.group_id
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
createManagedWriteCheckpoint(checkpoint) {
|
|
97
|
+
return this.writeCheckpointAPI.createManagedWriteCheckpoint(checkpoint);
|
|
98
|
+
}
|
|
99
|
+
lastWriteCheckpoint(filters) {
|
|
100
|
+
return this.writeCheckpointAPI.lastWriteCheckpoint({
|
|
101
|
+
...filters,
|
|
102
|
+
sync_rules_id: this.group_id
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
getParsedSyncRules(options) {
|
|
106
|
+
const { parsed, options: cachedOptions } = this.parsedSyncRulesCache ?? {};
|
|
107
|
+
/**
|
|
108
|
+
* Check if the cached sync rules, if present, had the same options.
|
|
109
|
+
* Parse sync rules if the options are different or if there is no cached value.
|
|
110
|
+
*/
|
|
111
|
+
if (!parsed || options.defaultSchema != cachedOptions?.defaultSchema) {
|
|
112
|
+
this.parsedSyncRulesCache = { parsed: this.sync_rules.parsed(options).sync_rules, options };
|
|
113
|
+
}
|
|
114
|
+
return this.parsedSyncRulesCache.parsed;
|
|
115
|
+
}
|
|
116
|
+
async getCheckpoint() {
|
|
117
|
+
const doc = await this.db.sync_rules.findOne({ _id: this.group_id }, {
|
|
118
|
+
projection: { last_checkpoint: 1, last_checkpoint_lsn: 1 }
|
|
119
|
+
});
|
|
120
|
+
return {
|
|
121
|
+
checkpoint: utils.timestampToOpId(doc?.last_checkpoint ?? 0n),
|
|
122
|
+
lsn: doc?.last_checkpoint_lsn ?? null
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
async startBatch(options, callback) {
|
|
126
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
127
|
+
try {
|
|
128
|
+
const doc = await this.db.sync_rules.findOne({
|
|
129
|
+
_id: this.group_id
|
|
130
|
+
}, { projection: { last_checkpoint_lsn: 1, no_checkpoint_before: 1, keepalive_op: 1 } });
|
|
131
|
+
const checkpoint_lsn = doc?.last_checkpoint_lsn ?? null;
|
|
132
|
+
const batch = __addDisposableResource(env_1, new MongoBucketBatch({
|
|
133
|
+
db: this.db,
|
|
134
|
+
syncRules: this.sync_rules.parsed(options).sync_rules,
|
|
135
|
+
groupId: this.group_id,
|
|
136
|
+
slotName: this.slot_name,
|
|
137
|
+
lastCheckpointLsn: checkpoint_lsn,
|
|
138
|
+
noCheckpointBeforeLsn: doc?.no_checkpoint_before ?? options.zeroLSN,
|
|
139
|
+
keepaliveOp: doc?.keepalive_op ?? null,
|
|
140
|
+
storeCurrentData: options.storeCurrentData,
|
|
141
|
+
skipExistingRows: options.skipExistingRows ?? false
|
|
142
|
+
}), true);
|
|
143
|
+
this.iterateListeners((cb) => cb.batchStarted?.(batch));
|
|
144
|
+
await callback(batch);
|
|
145
|
+
await batch.flush();
|
|
146
|
+
if (batch.last_flushed_op) {
|
|
147
|
+
return { flushed_op: String(batch.last_flushed_op) };
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
catch (e_1) {
|
|
154
|
+
env_1.error = e_1;
|
|
155
|
+
env_1.hasError = true;
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
const result_1 = __disposeResources(env_1);
|
|
159
|
+
if (result_1)
|
|
160
|
+
await result_1;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async resolveTable(options) {
|
|
164
|
+
const { group_id, connection_id, connection_tag, entity_descriptor } = options;
|
|
165
|
+
const { schema, name: table, objectId, replicationColumns } = entity_descriptor;
|
|
166
|
+
const columns = replicationColumns.map((column) => ({
|
|
167
|
+
name: column.name,
|
|
168
|
+
type: column.type,
|
|
169
|
+
type_oid: column.typeId
|
|
170
|
+
}));
|
|
171
|
+
let result = null;
|
|
172
|
+
await this.db.client.withSession(async (session) => {
|
|
173
|
+
const col = this.db.source_tables;
|
|
174
|
+
let doc = await col.findOne({
|
|
175
|
+
group_id: group_id,
|
|
176
|
+
connection_id: connection_id,
|
|
177
|
+
relation_id: objectId,
|
|
178
|
+
schema_name: schema,
|
|
179
|
+
table_name: table,
|
|
180
|
+
replica_id_columns2: columns
|
|
181
|
+
}, { session });
|
|
182
|
+
if (doc == null) {
|
|
183
|
+
doc = {
|
|
184
|
+
_id: new bson.ObjectId(),
|
|
185
|
+
group_id: group_id,
|
|
186
|
+
connection_id: connection_id,
|
|
187
|
+
relation_id: objectId,
|
|
188
|
+
schema_name: schema,
|
|
189
|
+
table_name: table,
|
|
190
|
+
replica_id_columns: null,
|
|
191
|
+
replica_id_columns2: columns,
|
|
192
|
+
snapshot_done: false
|
|
193
|
+
};
|
|
194
|
+
await col.insertOne(doc, { session });
|
|
195
|
+
}
|
|
196
|
+
const sourceTable = new storage.SourceTable(doc._id, connection_tag, objectId, schema, table, replicationColumns, doc.snapshot_done ?? true);
|
|
197
|
+
sourceTable.syncEvent = options.sync_rules.tableTriggersEvent(sourceTable);
|
|
198
|
+
sourceTable.syncData = options.sync_rules.tableSyncsData(sourceTable);
|
|
199
|
+
sourceTable.syncParameters = options.sync_rules.tableSyncsParameters(sourceTable);
|
|
200
|
+
const truncate = await col
|
|
201
|
+
.find({
|
|
202
|
+
group_id: group_id,
|
|
203
|
+
connection_id: connection_id,
|
|
204
|
+
_id: { $ne: doc._id },
|
|
205
|
+
$or: [{ relation_id: objectId }, { schema_name: schema, table_name: table }]
|
|
206
|
+
}, { session })
|
|
207
|
+
.toArray();
|
|
208
|
+
result = {
|
|
209
|
+
table: sourceTable,
|
|
210
|
+
dropTables: truncate.map((doc) => new storage.SourceTable(doc._id, connection_tag, doc.relation_id ?? 0, doc.schema_name, doc.table_name, doc.replica_id_columns2?.map((c) => ({ name: c.name, typeOid: c.type_oid, type: c.type })) ?? [], doc.snapshot_done ?? true))
|
|
211
|
+
};
|
|
212
|
+
});
|
|
213
|
+
return result;
|
|
214
|
+
}
|
|
215
|
+
async getParameterSets(checkpoint, lookups) {
|
|
216
|
+
const lookupFilter = lookups.map((lookup) => {
|
|
217
|
+
return serializeLookup(lookup);
|
|
218
|
+
});
|
|
219
|
+
const rows = await this.db.bucket_parameters
|
|
220
|
+
.aggregate([
|
|
221
|
+
{
|
|
222
|
+
$match: {
|
|
223
|
+
'key.g': this.group_id,
|
|
224
|
+
lookup: { $in: lookupFilter },
|
|
225
|
+
_id: { $lte: BigInt(checkpoint) }
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
$sort: {
|
|
230
|
+
_id: -1
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
$group: {
|
|
235
|
+
_id: { key: '$key', lookup: '$lookup' },
|
|
236
|
+
bucket_parameters: {
|
|
237
|
+
$first: '$bucket_parameters'
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
])
|
|
242
|
+
.toArray();
|
|
243
|
+
const groupedParameters = rows.map((row) => {
|
|
244
|
+
return row.bucket_parameters;
|
|
245
|
+
});
|
|
246
|
+
return groupedParameters.flat();
|
|
247
|
+
}
|
|
248
|
+
async *getBucketDataBatch(checkpoint, dataBuckets, options) {
|
|
249
|
+
if (dataBuckets.size == 0) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
let filters = [];
|
|
253
|
+
const end = checkpoint ? BigInt(checkpoint) : new bson.MaxKey();
|
|
254
|
+
for (let [name, start] of dataBuckets.entries()) {
|
|
255
|
+
filters.push({
|
|
256
|
+
_id: {
|
|
257
|
+
$gt: {
|
|
258
|
+
g: this.group_id,
|
|
259
|
+
b: name,
|
|
260
|
+
o: BigInt(start)
|
|
261
|
+
},
|
|
262
|
+
$lte: {
|
|
263
|
+
g: this.group_id,
|
|
264
|
+
b: name,
|
|
265
|
+
o: end
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
const limit = options?.limit ?? storage.DEFAULT_DOCUMENT_BATCH_LIMIT;
|
|
271
|
+
const sizeLimit = options?.chunkLimitBytes ?? storage.DEFAULT_DOCUMENT_CHUNK_LIMIT_BYTES;
|
|
272
|
+
const cursor = this.db.bucket_data.find({
|
|
273
|
+
$or: filters
|
|
274
|
+
}, {
|
|
275
|
+
session: undefined,
|
|
276
|
+
sort: { _id: 1 },
|
|
277
|
+
limit: limit,
|
|
278
|
+
// Increase batch size above the default 101, so that we can fill an entire batch in
|
|
279
|
+
// one go.
|
|
280
|
+
batchSize: limit,
|
|
281
|
+
// Raw mode is returns an array of Buffer instead of parsed documents.
|
|
282
|
+
// We use it so that:
|
|
283
|
+
// 1. We can calculate the document size accurately without serializing again.
|
|
284
|
+
// 2. We can delay parsing the results until it's needed.
|
|
285
|
+
// We manually use bson.deserialize below
|
|
286
|
+
raw: true,
|
|
287
|
+
// Since we're using raw: true and parsing ourselves later, we don't need bigint
|
|
288
|
+
// support here.
|
|
289
|
+
// Disabling due to https://jira.mongodb.org/browse/NODE-6165, and the fact that this
|
|
290
|
+
// is one of our most common queries.
|
|
291
|
+
useBigInt64: false
|
|
292
|
+
});
|
|
293
|
+
// We want to limit results to a single batch to avoid high memory usage.
|
|
294
|
+
// This approach uses MongoDB's batch limits to limit the data here, which limits
|
|
295
|
+
// to the lower of the batch count and size limits.
|
|
296
|
+
// This is similar to using `singleBatch: true` in the find options, but allows
|
|
297
|
+
// detecting "hasMore".
|
|
298
|
+
let { data, hasMore } = await readSingleBatch(cursor);
|
|
299
|
+
if (data.length == limit) {
|
|
300
|
+
// Limit reached - could have more data, despite the cursor being drained.
|
|
301
|
+
hasMore = true;
|
|
302
|
+
}
|
|
303
|
+
let batchSize = 0;
|
|
304
|
+
let currentBatch = null;
|
|
305
|
+
let targetOp = null;
|
|
306
|
+
// Ordered by _id, meaning buckets are grouped together
|
|
307
|
+
for (let rawData of data) {
|
|
308
|
+
const row = bson.deserialize(rawData, BSON_DESERIALIZE_OPTIONS);
|
|
309
|
+
const bucket = row._id.b;
|
|
310
|
+
if (currentBatch == null || currentBatch.bucket != bucket || batchSize >= sizeLimit) {
|
|
311
|
+
let start = undefined;
|
|
312
|
+
if (currentBatch != null) {
|
|
313
|
+
if (currentBatch.bucket == bucket) {
|
|
314
|
+
currentBatch.has_more = true;
|
|
315
|
+
}
|
|
316
|
+
const yieldBatch = currentBatch;
|
|
317
|
+
start = currentBatch.after;
|
|
318
|
+
currentBatch = null;
|
|
319
|
+
batchSize = 0;
|
|
320
|
+
yield { batch: yieldBatch, targetOp: targetOp };
|
|
321
|
+
targetOp = null;
|
|
322
|
+
}
|
|
323
|
+
start ?? (start = dataBuckets.get(bucket));
|
|
324
|
+
if (start == null) {
|
|
325
|
+
throw new Error(`data for unexpected bucket: ${bucket}`);
|
|
326
|
+
}
|
|
327
|
+
currentBatch = {
|
|
328
|
+
bucket,
|
|
329
|
+
after: start,
|
|
330
|
+
has_more: hasMore,
|
|
331
|
+
data: [],
|
|
332
|
+
next_after: start
|
|
333
|
+
};
|
|
334
|
+
targetOp = null;
|
|
335
|
+
}
|
|
336
|
+
const entry = mapOpEntry(row);
|
|
337
|
+
if (row.target_op != null) {
|
|
338
|
+
// MOVE, CLEAR
|
|
339
|
+
if (targetOp == null || row.target_op > targetOp) {
|
|
340
|
+
targetOp = row.target_op;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
currentBatch.data.push(entry);
|
|
344
|
+
currentBatch.next_after = entry.op_id;
|
|
345
|
+
batchSize += rawData.byteLength;
|
|
346
|
+
}
|
|
347
|
+
if (currentBatch != null) {
|
|
348
|
+
const yieldBatch = currentBatch;
|
|
349
|
+
currentBatch = null;
|
|
350
|
+
yield { batch: yieldBatch, targetOp: targetOp };
|
|
351
|
+
targetOp = null;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
async getChecksums(checkpoint, buckets) {
|
|
355
|
+
return this.checksumCache.getChecksumMap(checkpoint, buckets);
|
|
356
|
+
}
|
|
357
|
+
async getChecksumsInternal(batch) {
|
|
358
|
+
if (batch.length == 0) {
|
|
359
|
+
return new Map();
|
|
360
|
+
}
|
|
361
|
+
const filters = [];
|
|
362
|
+
for (let request of batch) {
|
|
363
|
+
filters.push({
|
|
364
|
+
_id: {
|
|
365
|
+
$gt: {
|
|
366
|
+
g: this.group_id,
|
|
367
|
+
b: request.bucket,
|
|
368
|
+
o: request.start ? BigInt(request.start) : new bson.MinKey()
|
|
369
|
+
},
|
|
370
|
+
$lte: {
|
|
371
|
+
g: this.group_id,
|
|
372
|
+
b: request.bucket,
|
|
373
|
+
o: BigInt(request.end)
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
const aggregate = await this.db.bucket_data
|
|
379
|
+
.aggregate([
|
|
380
|
+
{
|
|
381
|
+
$match: {
|
|
382
|
+
$or: filters
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
$group: {
|
|
387
|
+
_id: '$_id.b',
|
|
388
|
+
checksum_total: { $sum: '$checksum' },
|
|
389
|
+
count: { $sum: 1 },
|
|
390
|
+
has_clear_op: {
|
|
391
|
+
$max: {
|
|
392
|
+
$cond: [{ $eq: ['$op', 'CLEAR'] }, 1, 0]
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
], { session: undefined, readConcern: 'snapshot' })
|
|
398
|
+
.toArray();
|
|
399
|
+
return new Map(aggregate.map((doc) => {
|
|
400
|
+
return [
|
|
401
|
+
doc._id,
|
|
402
|
+
{
|
|
403
|
+
bucket: doc._id,
|
|
404
|
+
partialCount: doc.count,
|
|
405
|
+
partialChecksum: Number(BigInt(doc.checksum_total) & 0xffffffffn) & 0xffffffff,
|
|
406
|
+
isFullChecksum: doc.has_clear_op == 1
|
|
407
|
+
}
|
|
408
|
+
];
|
|
409
|
+
}));
|
|
410
|
+
}
|
|
411
|
+
async terminate(options) {
|
|
412
|
+
// Default is to clear the storage except when explicitly requested not to.
|
|
413
|
+
if (!options || options?.clearStorage) {
|
|
414
|
+
await this.clear();
|
|
415
|
+
}
|
|
416
|
+
await this.db.sync_rules.updateOne({
|
|
417
|
+
_id: this.group_id
|
|
418
|
+
}, {
|
|
419
|
+
$set: {
|
|
420
|
+
state: storage.SyncRuleState.TERMINATED,
|
|
421
|
+
persisted_lsn: null,
|
|
422
|
+
snapshot_done: false
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
async getStatus() {
|
|
427
|
+
const doc = await this.db.sync_rules.findOne({
|
|
428
|
+
_id: this.group_id
|
|
429
|
+
}, {
|
|
430
|
+
projection: {
|
|
431
|
+
snapshot_done: 1,
|
|
432
|
+
last_checkpoint_lsn: 1,
|
|
433
|
+
state: 1
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
if (doc == null) {
|
|
437
|
+
throw new Error('Cannot find sync rules status');
|
|
438
|
+
}
|
|
439
|
+
return {
|
|
440
|
+
snapshot_done: doc.snapshot_done,
|
|
441
|
+
active: doc.state == 'ACTIVE',
|
|
442
|
+
checkpoint_lsn: doc.last_checkpoint_lsn
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
async clear() {
|
|
446
|
+
while (true) {
|
|
447
|
+
try {
|
|
448
|
+
await this.clearIteration();
|
|
449
|
+
logger.info(`${this.slot_name} Done clearing data`);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
catch (e) {
|
|
453
|
+
if (e instanceof mongo.MongoServerError && e.codeName == 'MaxTimeMSExpired') {
|
|
454
|
+
logger.info(`${this.slot_name} Cleared batch of data in ${db.mongo.MONGO_CLEAR_OPERATION_TIMEOUT_MS}ms, continuing...`);
|
|
455
|
+
await timers.setTimeout(db.mongo.MONGO_CLEAR_OPERATION_TIMEOUT_MS / 5);
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
throw e;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
async clearIteration() {
|
|
465
|
+
// Individual operations here may time out with the maxTimeMS option.
|
|
466
|
+
// It is expected to still make progress, and continue on the next try.
|
|
467
|
+
await this.db.sync_rules.updateOne({
|
|
468
|
+
_id: this.group_id
|
|
469
|
+
}, {
|
|
470
|
+
$set: {
|
|
471
|
+
snapshot_done: false,
|
|
472
|
+
persisted_lsn: null,
|
|
473
|
+
last_checkpoint_lsn: null,
|
|
474
|
+
last_checkpoint: null,
|
|
475
|
+
no_checkpoint_before: null
|
|
476
|
+
}
|
|
477
|
+
}, { maxTimeMS: db.mongo.MONGO_CLEAR_OPERATION_TIMEOUT_MS });
|
|
478
|
+
await this.db.bucket_data.deleteMany({
|
|
479
|
+
_id: idPrefixFilter({ g: this.group_id }, ['b', 'o'])
|
|
480
|
+
}, { maxTimeMS: db.mongo.MONGO_CLEAR_OPERATION_TIMEOUT_MS });
|
|
481
|
+
await this.db.bucket_parameters.deleteMany({
|
|
482
|
+
key: idPrefixFilter({ g: this.group_id }, ['t', 'k'])
|
|
483
|
+
}, { maxTimeMS: db.mongo.MONGO_CLEAR_OPERATION_TIMEOUT_MS });
|
|
484
|
+
await this.db.current_data.deleteMany({
|
|
485
|
+
_id: idPrefixFilter({ g: this.group_id }, ['t', 'k'])
|
|
486
|
+
}, { maxTimeMS: db.mongo.MONGO_CLEAR_OPERATION_TIMEOUT_MS });
|
|
487
|
+
await this.db.source_tables.deleteMany({
|
|
488
|
+
group_id: this.group_id
|
|
489
|
+
}, { maxTimeMS: db.mongo.MONGO_CLEAR_OPERATION_TIMEOUT_MS });
|
|
490
|
+
}
|
|
491
|
+
async autoActivate() {
|
|
492
|
+
await this.db.client.withSession(async (session) => {
|
|
493
|
+
await session.withTransaction(async () => {
|
|
494
|
+
const doc = await this.db.sync_rules.findOne({ _id: this.group_id }, { session });
|
|
495
|
+
if (doc && doc.state == 'PROCESSING') {
|
|
496
|
+
await this.db.sync_rules.updateOne({
|
|
497
|
+
_id: this.group_id
|
|
498
|
+
}, {
|
|
499
|
+
$set: {
|
|
500
|
+
state: storage.SyncRuleState.ACTIVE
|
|
501
|
+
}
|
|
502
|
+
}, { session });
|
|
503
|
+
await this.db.sync_rules.updateMany({
|
|
504
|
+
_id: { $ne: this.group_id },
|
|
505
|
+
state: storage.SyncRuleState.ACTIVE
|
|
506
|
+
}, {
|
|
507
|
+
$set: {
|
|
508
|
+
state: storage.SyncRuleState.STOP
|
|
509
|
+
}
|
|
510
|
+
}, { session });
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
async reportError(e) {
|
|
516
|
+
const message = String(e.message ?? 'Replication failure');
|
|
517
|
+
await this.db.sync_rules.updateOne({
|
|
518
|
+
_id: this.group_id
|
|
519
|
+
}, {
|
|
520
|
+
$set: {
|
|
521
|
+
last_fatal_error: message
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
async compact(options) {
|
|
526
|
+
return new MongoCompactor(this.db, this.group_id, options).compact();
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
//# sourceMappingURL=MongoSyncBucketStorage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongoSyncBucketStorage.js","sourceRoot":"","sources":["../../../src/storage/implementation/MongoSyncBucketStorage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,OAAO,KAAK,IAAI,MAAM,MAAM,CA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,IAAI,CAAC,QAAQ;SACnB,EACD;YACE,IAAI,EAAE;gBACJ,gBAAgB,EAAE,OAAO;aAC1B;SACF,CACF,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,OAAgC;QAC5C,OAAO,IAAI,cAAc,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC;IACvE,CAAC;CACF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { storage } from '@powersync/service-core';
|
|
2
|
+
import { PowerSyncMongo } from './db.js';
|
|
3
|
+
/**
|
|
4
|
+
* Manages a lock on a sync rules document, so that only one process
|
|
5
|
+
* replicates those sync rules at a time.
|
|
6
|
+
*/
|
|
7
|
+
export declare class MongoSyncRulesLock implements storage.ReplicationLock {
|
|
8
|
+
private db;
|
|
9
|
+
sync_rules_id: number;
|
|
10
|
+
private lock_id;
|
|
11
|
+
private readonly refreshInterval;
|
|
12
|
+
static createLock(db: PowerSyncMongo, sync_rules: storage.PersistedSyncRulesContent): Promise<MongoSyncRulesLock>;
|
|
13
|
+
constructor(db: PowerSyncMongo, sync_rules_id: number, lock_id: string);
|
|
14
|
+
release(): Promise<void>;
|
|
15
|
+
private refresh;
|
|
16
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import { logger } from '@powersync/lib-services-framework';
|
|
3
|
+
/**
|
|
4
|
+
* Manages a lock on a sync rules document, so that only one process
|
|
5
|
+
* replicates those sync rules at a time.
|
|
6
|
+
*/
|
|
7
|
+
export class MongoSyncRulesLock {
|
|
8
|
+
static async createLock(db, sync_rules) {
|
|
9
|
+
const lockId = crypto.randomBytes(8).toString('hex');
|
|
10
|
+
const doc = await db.sync_rules.findOneAndUpdate({ _id: sync_rules.id, $or: [{ lock: null }, { 'lock.expires_at': { $lt: new Date() } }] }, {
|
|
11
|
+
$set: {
|
|
12
|
+
lock: {
|
|
13
|
+
id: lockId,
|
|
14
|
+
expires_at: new Date(Date.now() + 60 * 1000)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}, {
|
|
18
|
+
projection: { lock: 1 },
|
|
19
|
+
returnDocument: 'before'
|
|
20
|
+
});
|
|
21
|
+
if (doc == null) {
|
|
22
|
+
throw new Error(`Sync rules: ${sync_rules.id} have been locked by another process for replication.`);
|
|
23
|
+
}
|
|
24
|
+
return new MongoSyncRulesLock(db, sync_rules.id, lockId);
|
|
25
|
+
}
|
|
26
|
+
constructor(db, sync_rules_id, lock_id) {
|
|
27
|
+
this.db = db;
|
|
28
|
+
this.sync_rules_id = sync_rules_id;
|
|
29
|
+
this.lock_id = lock_id;
|
|
30
|
+
this.refreshInterval = setInterval(async () => {
|
|
31
|
+
try {
|
|
32
|
+
await this.refresh();
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
logger.error('Failed to refresh lock', e);
|
|
36
|
+
clearInterval(this.refreshInterval);
|
|
37
|
+
}
|
|
38
|
+
}, 30130);
|
|
39
|
+
}
|
|
40
|
+
async release() {
|
|
41
|
+
clearInterval(this.refreshInterval);
|
|
42
|
+
const result = await this.db.sync_rules.updateOne({
|
|
43
|
+
_id: this.sync_rules_id,
|
|
44
|
+
'lock.id': this.lock_id
|
|
45
|
+
}, {
|
|
46
|
+
$unset: { lock: 1 }
|
|
47
|
+
});
|
|
48
|
+
if (result.modifiedCount == 0) {
|
|
49
|
+
// Log and ignore
|
|
50
|
+
logger.warn(`Lock already released: ${this.sync_rules_id}/${this.lock_id}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async refresh() {
|
|
54
|
+
const result = await this.db.sync_rules.findOneAndUpdate({
|
|
55
|
+
_id: this.sync_rules_id,
|
|
56
|
+
'lock.id': this.lock_id
|
|
57
|
+
}, {
|
|
58
|
+
$set: { 'lock.expires_at': new Date(Date.now() + 60 * 1000) }
|
|
59
|
+
}, { returnDocument: 'after' });
|
|
60
|
+
if (result == null) {
|
|
61
|
+
throw new Error(`Lock not held anymore: ${this.sync_rules_id}/${this.lock_id}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=MongoSyncRulesLock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongoSyncRulesLock.js","sourceRoot":"","sources":["../../../src/storage/implementation/MongoSyncRulesLock.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAI3D;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IAG7B,MAAM,CAAC,KAAK,CAAC,UAAU,CACrB,EAAkB,EAClB,UAA6C;QAE7C,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACrD,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC,UAAU,CAAC,gBAAgB,CAC9C,EAAE,GAAG,EAAE,UAAU,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,iBAAiB,EAAE,EAAE,GAAG,EAAE,IAAI,IAAI,EAAE,EAAE,EAAE,CAAC,EAAE,EACzF;YACE,IAAI,EAAE;gBACJ,IAAI,EAAE;oBACJ,EAAE,EAAE,MAAM;oBACV,UAAU,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;iBAC7C;aACF;SACF,EACD;YACE,UAAU,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE;YACvB,cAAc,EAAE,QAAQ;SACzB,CACF,CAAC;QAEF,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,eAAe,UAAU,CAAC,EAAE,uDAAuD,CAAC,CAAC;QACvG,CAAC;QACD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xB,EACD;YACE,IAAI,EAAE,EAAE,iBAAiB,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,EAAE;SAC9D,EACD,EAAE,cAAc,EAAE,OAAO,EAAE,CAC5B,CAAC;QACF,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAClF,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { storage } from '@powersync/service-core';
|
|
2
|
+
import { PowerSyncMongo } from './db.js';
|
|
3
|
+
export type MongoCheckpointAPIOptions = {
|
|
4
|
+
db: PowerSyncMongo;
|
|
5
|
+
mode: storage.WriteCheckpointMode;
|
|
6
|
+
};
|
|
7
|
+
export declare class MongoWriteCheckpointAPI implements storage.WriteCheckpointAPI {
|
|
8
|
+
readonly db: PowerSyncMongo;
|
|
9
|
+
private _mode;
|
|
10
|
+
constructor(options: MongoCheckpointAPIOptions);
|
|
11
|
+
get writeCheckpointMode(): storage.WriteCheckpointMode;
|
|
12
|
+
setWriteCheckpointMode(mode: storage.WriteCheckpointMode): void;
|
|
13
|
+
batchCreateCustomWriteCheckpoints(checkpoints: storage.CustomWriteCheckpointOptions[]): Promise<void>;
|
|
14
|
+
createCustomWriteCheckpoint(options: storage.CustomWriteCheckpointOptions): Promise<bigint>;
|
|
15
|
+
createManagedWriteCheckpoint(checkpoint: storage.ManagedWriteCheckpointOptions): Promise<bigint>;
|
|
16
|
+
lastWriteCheckpoint(filters: storage.LastWriteCheckpointFilters): Promise<bigint | null>;
|
|
17
|
+
protected lastCustomWriteCheckpoint(filters: storage.CustomWriteCheckpointFilters): Promise<bigint | null>;
|
|
18
|
+
protected lastManagedWriteCheckpoint(filters: storage.ManagedWriteCheckpointFilters): Promise<bigint | null>;
|
|
19
|
+
}
|
|
20
|
+
export declare function batchCreateCustomWriteCheckpoints(db: PowerSyncMongo, checkpoints: storage.CustomWriteCheckpointOptions[]): Promise<void>;
|