Skip to content

Commit cdf29ca

Browse files
added support for sharded mongodb connections
1 parent b4fe4ae commit cdf29ca

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

.changeset/weak-lions-obey.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@powersync/service-module-mongodb': minor
3+
'@powersync/service-core': minor
4+
'@powersync/service-image': minor
5+
---
6+
7+
Added support for sharded MongoDB replication connections.

modules/module-mongodb/src/replication/ChangeStream.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,23 @@ export class ChangeStream {
179179

180180
// We need to get the snapshot time before taking the initial snapshot.
181181
const hello = await this.defaultDb.command({ hello: 1 });
182-
const snapshotTime = hello.lastWrite?.majorityOpTime?.ts as mongo.Timestamp;
183-
if (hello.msg == 'isdbgrid') {
184-
throw new ServiceError(
185-
ErrorCode.PSYNC_S1341,
186-
'Sharded MongoDB Clusters are not supported yet (including MongoDB Serverless instances).'
187-
);
188-
} else if (hello.setName == null) {
182+
// Use the clusterTime for sharded clusters
183+
const snapshotTime: mongo.Timestamp = hello.lastWrite?.majorityOpTime?.ts ?? hello.$clusterTime?.clusterTime;
184+
185+
// Sharded cluster don't provide a setName, but we do support them.
186+
// We don't support standalone instances
187+
if (hello.msg != 'isdbgrid' && hello.setName == null) {
189188
throw new ServiceError(
190189
ErrorCode.PSYNC_S1342,
191190
'Standalone MongoDB instances are not supported - use a replicaset.'
192191
);
193-
} else if (snapshotTime == null) {
192+
}
193+
194+
if (snapshotTime == null) {
194195
// Not known where this would happen apart from the above cases
195196
throw new ReplicationAssertionError('MongoDB lastWrite timestamp not found.');
196197
}
198+
197199
// We previously used {snapshot: true} for the snapshot session.
198200
// While it gives nice consistency guarantees, it fails when the
199201
// snapshot takes longer than 5 minutes, due to minSnapshotHistoryWindowInSeconds

modules/module-mongodb/src/replication/replication-utils.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ErrorCode, ServiceError } from '@powersync/lib-services-framework';
2-
import { MongoManager } from './MongoManager.js';
32
import { PostImagesOption } from '../types/types.js';
3+
import { MongoManager } from './MongoManager.js';
44

55
export const CHECKPOINTS_COLLECTION = '_powersync_checkpoints';
66

@@ -10,12 +10,9 @@ export async function checkSourceConfiguration(connectionManager: MongoManager):
1010
const db = connectionManager.db;
1111

1212
const hello = await db.command({ hello: 1 });
13-
if (hello.msg == 'isdbgrid') {
14-
throw new ServiceError(
15-
ErrorCode.PSYNC_S1341,
16-
'Sharded MongoDB Clusters are not supported yet (including MongoDB Serverless instances).'
17-
);
18-
} else if (hello.setName == null) {
13+
// Sharded cluster don't provide a setName, but we do support them.
14+
// We don't support standalone instances
15+
if (hello.msg != 'isdbgrid' && hello.setName == null) {
1916
throw new ServiceError(ErrorCode.PSYNC_S1342, 'Standalone MongoDB instances are not supported - use a replicaset.');
2017
}
2118

0 commit comments

Comments
 (0)