Skip to content

Commit da7ecc6

Browse files
authored
Upgrade MongoDB driver (#374)
* Upgrade mongodb and bson packages. * Changset. * Add comments. * Be explicit about batchSize = limit + 1.
1 parent 704553e commit da7ecc6

File tree

17 files changed

+80
-52
lines changed

17 files changed

+80
-52
lines changed

.changeset/perfect-kiwis-prove.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@powersync/service-module-mongodb-storage': patch
3+
'@powersync/service-rsocket-router': patch
4+
'@powersync/service-module-mongodb': patch
5+
'@powersync/service-core': patch
6+
'@powersync/lib-services-framework': patch
7+
'@powersync/lib-service-mongodb': patch
8+
---
9+
10+
Upgrade mongodb driver to improve stability.

libs/lib-mongodb/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
},
3030
"dependencies": {
3131
"@powersync/lib-services-framework": "workspace:*",
32-
"bson": "^6.10.3",
33-
"mongodb": "^6.14.1",
32+
"bson": "^6.10.4",
33+
"mongodb": "^6.20.0",
3434
"mongodb-connection-string-url": "^3.0.2",
3535
"ts-codec": "^1.3.0"
3636
},
3737
"devDependencies": {}
38-
}
38+
}

libs/lib-mongodb/src/db/mongo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export function createMongoClient(config: BaseMongoConfigDecoded, options?: Mong
6161

6262
// Identify the client
6363
appName: options?.powersyncVersion ? `powersync-storage ${options.powersyncVersion}` : 'powersync-storage',
64+
// Deprecated in the driver - in a future release we may have to rely on appName only.
6465
driverInfo: {
6566
// This is merged with the node driver info.
6667
name: 'powersync-storage',

libs/lib-services/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@powersync/service-sync-rules": "workspace:*",
2525
"ajv": "^8.12.0",
2626
"better-ajv-errors": "^1.2.0",
27-
"bson": "^6.10.3",
27+
"bson": "^6.10.4",
2828
"dotenv": "^16.4.5",
2929
"ipaddr.js": "^2.1.0",
3030
"lodash": "^4.17.21",
@@ -37,4 +37,4 @@
3737
"@types/lodash": "^4.17.5",
3838
"vitest": "^3.2.4"
3939
}
40-
}
40+
}

modules/module-mongodb-storage/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@powersync/service-jsonbig": "workspace:*",
3535
"@powersync/service-sync-rules": "workspace:*",
3636
"@powersync/service-types": "workspace:*",
37-
"bson": "^6.10.3",
37+
"bson": "^6.10.4",
3838
"ix": "^5.0.0",
3939
"lru-cache": "^10.2.2",
4040
"ts-codec": "^1.3.0",
@@ -43,4 +43,4 @@
4343
"devDependencies": {
4444
"@powersync/service-core-tests": "workspace:*"
4545
}
46-
}
46+
}

modules/module-mongodb-storage/src/storage/implementation/MongoCompactor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ export class MongoCompactor {
184184
}
185185
}
186186
],
187-
{ batchSize: this.moveBatchQueryLimit }
187+
{
188+
// batchSize is 1 more than limit to auto-close the cursor.
189+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
190+
batchSize: this.moveBatchQueryLimit + 1
191+
}
188192
);
189193
// We don't limit to a single batch here, since that often causes MongoDB to scan through more than it returns.
190194
// Instead, we load up to the limit.

modules/module-mongodb-storage/src/storage/implementation/MongoSyncBucketStorage.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ export class MongoSyncBucketStorage
403403
limit: batchLimit,
404404
// Increase batch size above the default 101, so that we can fill an entire batch in
405405
// one go.
406-
batchSize: batchLimit,
406+
// batchSize is 1 more than limit to auto-close the cursor.
407+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
408+
batchSize: batchLimit + 1,
407409
// Raw mode is returns an array of Buffer instead of parsed documents.
408410
// We use it so that:
409411
// 1. We can calculate the document size accurately without serializing again.
@@ -905,7 +907,9 @@ export class MongoSyncBucketStorage
905907
'_id.b': 1
906908
},
907909
limit: limit + 1,
908-
batchSize: limit + 1,
910+
// batchSize is 1 more than limit to auto-close the cursor.
911+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
912+
batchSize: limit + 2,
909913
singleBatch: true
910914
}
911915
)
@@ -935,7 +939,9 @@ export class MongoSyncBucketStorage
935939
lookup: 1
936940
},
937941
limit: limit + 1,
938-
batchSize: limit + 1,
942+
// batchSize is 1 more than limit to auto-close the cursor.
943+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
944+
batchSize: limit + 2,
939945
singleBatch: true
940946
}
941947
)

modules/module-mongodb-storage/src/storage/implementation/MongoWriteCheckpointAPI.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ export class MongoWriteCheckpointAPI implements storage.WriteCheckpointAPI {
111111
},
112112
{
113113
limit: limit + 1,
114-
batchSize: limit + 1,
114+
// batchSize is 1 more than limit to auto-close the cursor.
115+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
116+
batchSize: limit + 2,
115117
singleBatch: true
116118
}
117119
)
@@ -140,7 +142,9 @@ export class MongoWriteCheckpointAPI implements storage.WriteCheckpointAPI {
140142
},
141143
{
142144
limit: limit + 1,
143-
batchSize: limit + 1,
145+
// batchSize is 1 more than limit to auto-close the cursor.
146+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
147+
batchSize: limit + 2,
144148
singleBatch: true
145149
}
146150
)

modules/module-mongodb-storage/src/storage/implementation/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function generateSlotName(prefix: string, sync_rules_id: number) {
4141
* However, that makes `has_more` detection very difficult, since the cursor is always closed
4242
* after the first batch. Instead, we do a workaround to only fetch a single batch below.
4343
*
44-
* For this to be effective, set batchSize = limit in the find command.
44+
* For this to be effective, set batchSize = limit + 1 in the find command.
4545
*/
4646
export async function readSingleBatch<T>(cursor: mongo.AbstractCursor<T>): Promise<{ data: T[]; hasMore: boolean }> {
4747
try {

modules/module-mongodb/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@powersync/service-jsonbig": "workspace:*",
3535
"@powersync/service-sync-rules": "workspace:*",
3636
"@powersync/service-types": "workspace:*",
37-
"bson": "^6.10.3",
37+
"bson": "^6.10.4",
3838
"ts-codec": "^1.3.0",
3939
"uuid": "^11.1.0"
4040
},
@@ -43,4 +43,4 @@
4343
"@powersync/service-module-mongodb-storage": "workspace:*",
4444
"@powersync/service-module-postgres-storage": "workspace:*"
4545
}
46-
}
46+
}

0 commit comments

Comments
 (0)