Skip to content

Commit ec5c1f2

Browse files
authored
Merge pull request #12 from share/mongodb-4
⬆️ Add support for `mongodb` v4
2 parents b5e69dc + 9087f96 commit ec5c1f2

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

lib/mongo-milestone-db.js

Lines changed: 5 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 mongodbRequire = require('./mongodb');
33

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

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

226229
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: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
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);
1011

1112
function create(options, callback) {
@@ -20,7 +21,13 @@ const MONGO_URL = process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test'
2021
mongo: (shareDbCallback) => {
2122
let mongo;
2223

23-
mongodb.connect(MONGO_URL)
24+
const connect = (url) => {
25+
if (typeof mongodb.connect === 'function') return mongodb.connect(url);
26+
const client = new mongodb.MongoClient(MONGO_URL);
27+
return client.connect();
28+
};
29+
30+
connect(MONGO_URL)
2431
.then((mongoConnection) => {
2532
mongo = mongoConnection;
2633
return MongoMilestoneDB._isLegacyMongoClient(mongo)
@@ -38,6 +45,10 @@ const MONGO_URL = process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test'
3845
}
3946

4047
describe(`[${ driver } driver]`, () => {
48+
beforeEach(() => {
49+
mongodbRequire.mongodb = mongodb;
50+
});
51+
4152
require('sharedb/test/milestone-db')({create: create});
4253

4354
describe('MongoMilestoneDB', () => {
@@ -249,7 +260,7 @@ const MONGO_URL = process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test'
249260
beforeEach(() => {
250261
const options = {interval: 100};
251262
db = new MongoMilestoneDB(MONGO_URL, options);
252-
return db._mongoPromise.then(mongo => mongo.db().dropDatabase());
263+
return db._db().then(mongo => mongo.dropDatabase());
253264
});
254265

255266
afterEach((done) => {

0 commit comments

Comments
 (0)