Skip to content

Commit 6f56203

Browse files
committed
⬆️ Add support for mongodb v4
This change adds support for [`mongodb` v4][1]. The only [change][2] that seems to affect us is: - removal of the top-level `connect()` function [1]: https://github.com/mongodb/node-mongodb-native/releases/tag/v4.0.0 [2]: https://github.com/mongodb/node-mongodb-native/blob/4.0/docs/CHANGES_4.0.0.md
1 parent b5e69dc commit 6f56203

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

lib/mongo-milestone-db.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const MilestoneDB = require('sharedb').MilestoneDB;
2-
const mongodb = require('mongodb');
2+
const mongodb = require('./mongodb').mongodb;
33

44
class MongoMilestoneDB extends MilestoneDB {
55
constructor(mongo, options) {
@@ -220,7 +220,9 @@ class MongoMilestoneDB extends MilestoneDB {
220220
});
221221
}
222222

223-
return mongodb.connect(mongo, options);
223+
if (typeof mongodb.connect === 'function') return mongodb.connect(mongo, options);
224+
const client = new mongodb.MongoClient(mongo, options);
225+
return client.connect();
224226
}
225227

226228
static _snapshotToDbRepresentation(snapshot) {

lib/mongodb.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const mongodb = require('mongodb');
2+
3+
module.exports = {
4+
mongodb: mongodb,
5+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "MongoDB milestone snapshot database adapter for ShareDB",
55
"main": "lib/index.js",
66
"dependencies": {
7-
"mongodb": "^2.2.36 || ^3.0.0",
7+
"mongodb": "^2.2.36 || ^3.0.0 || ^4.0.0",
88
"sharedb": "^1.0.0"
99
},
1010
"devDependencies": {
@@ -14,6 +14,7 @@
1414
"mocha": "^8.2.1",
1515
"mongodb2": "npm:mongodb@^2.2.36",
1616
"mongodb3": "npm:mongodb@^3.0.0",
17+
"mongodb4": "npm:mongodb@^4.0.0",
1718
"nyc": "^15.1.0"
1819
},
1920
"scripts": {

test/mongo-milestone-db.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
const expect = require('chai').expect;
33
const MongoMilestoneDB = require('../lib/mongo-milestone-db');
44
const SnapshotFactory = require('./factories/snapshot-factory');
5+
const mongodbRequire = require('../lib/mongodb');
56

67
const MONGO_URL = process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test';
78

8-
['mongodb2', 'mongodb3'].forEach((driver) => {
9+
['mongodb2', 'mongodb3', 'mongodb4'].forEach((driver) => {
910
const mongodb = require(driver);
11+
mongodbRequire.mongodb = mongodb;
1012

1113
function create(options, callback) {
1214
if (typeof options === 'function') {
@@ -20,7 +22,13 @@ const MONGO_URL = process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test'
2022
mongo: (shareDbCallback) => {
2123
let mongo;
2224

23-
mongodb.connect(MONGO_URL)
25+
const connect = (url) => {
26+
if (typeof mongodb.connect === 'function') return mongodb.connect(url);
27+
const client = new mongodb.MongoClient(MONGO_URL);
28+
return client.connect();
29+
};
30+
31+
connect(MONGO_URL)
2432
.then((mongoConnection) => {
2533
mongo = mongoConnection;
2634
return MongoMilestoneDB._isLegacyMongoClient(mongo)
@@ -249,7 +257,7 @@ const MONGO_URL = process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test'
249257
beforeEach(() => {
250258
const options = {interval: 100};
251259
db = new MongoMilestoneDB(MONGO_URL, options);
252-
return db._mongoPromise.then(mongo => mongo.db().dropDatabase());
260+
return db._db().then(mongo => mongo.dropDatabase());
253261
});
254262

255263
afterEach((done) => {

0 commit comments

Comments
 (0)