Skip to content

Commit 0610786

Browse files
authored
Merge pull request #3 from share/mongo-3
Add support for `mongodb` v3
2 parents 3b5a40b + 369eaaa commit 0610786

File tree

6 files changed

+355
-218
lines changed

6 files changed

+355
-218
lines changed

.travis.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
language: node_js
22

33
node_js:
4-
- "6"
5-
- "8"
4+
- "12"
65
- "10"
6+
- "8"
77

88
services:
99
- docker
1010

1111
env:
12-
- MONGODB_VERSION="2.6"
13-
- MONGODB_VERSION="3.6"
12+
- MONGODB_VERSION="4.2"
1413
- MONGODB_VERSION="4.0"
14+
- MONGODB_VERSION="3.6"
15+
- MONGODB_VERSION="3.4"
1516

1617
before_install:
1718
- docker run -d -p 127.0.0.1:27017:27017 mongo:$MONGODB_VERSION

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const shareDb = new ShareDB({ milestoneDb: milestoneDb });
2525

2626
### Mongo
2727

28-
The underlying Mongo database can be configured in a number of ways. This library wraps v2 of the [`mongodb`][2]
28+
The underlying Mongo database can be configured in a number of ways. This library uses the [`mongodb`][2]
2929
library, so any configuration that can be used there can be used in this library.
3030

3131
Mongo can be configured simply using a connection string and any desired [options][3]:
@@ -34,7 +34,7 @@ Mongo can be configured simply using a connection string and any desired [option
3434
const milestoneDb = new MongoMilestoneDB('mongodb://localhost:27017/test', { loggerLevel: 'debug' });
3535
```
3636

37-
It can also be configured with a callback that provides an instance of a Mongo [`Db` object][4]:
37+
It can also be configured with a callback that provides an instance of a [`MongoClient`][4]:
3838

3939
```javascript
4040
const mongodb = require('mongodb');
@@ -59,8 +59,7 @@ const milestoneDb = new MongoMilestoneDB({
5959
#### Intervals
6060

6161
By default, ShareDB will save a milestone snapshot with a given frequency. This library defaults to an interval of
62-
1,000, saving milestones when the 1,000th, 2,000th, etc. versions are committed. That default interval can be
63-
configured:
62+
1,000, saving milestones when the 1,000th, 2,000th, etc. versions are committed. That interval can be configured:
6463

6564
```javascript
6665
const milestoneDb = new MongoMilestoneDB({
@@ -133,5 +132,5 @@ const milestoneDb = new MongoMilestoneDB({
133132

134133
[1]: https://github.com/share/sharedb
135134
[2]: https://mongodb.github.io/node-mongodb-native/
136-
[3]: http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect
137-
[4]: http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html
135+
[3]: https://mongodb.github.io/node-mongodb-native/3.6/api/MongoClient.html#.connect
136+
[4]: https://mongodb.github.io/node-mongodb-native/3.6/api/MongoClient.html

lib/mongo-milestone-db.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class MongoMilestoneDB extends MilestoneDB {
8383
const updatedSnapshot = MongoMilestoneDB._snapshotToDbRepresentation(snapshot);
8484
const options = {upsert: true};
8585

86-
return collection.updateOne(query, updatedSnapshot, options);
86+
return collection.updateOne(query, {$set: updatedSnapshot}, options);
8787
});
8888
}
8989

@@ -144,21 +144,27 @@ class MongoMilestoneDB extends MilestoneDB {
144144
}
145145

146146
_close() {
147-
return this._db()
148-
.then((db) => {
147+
return this._client()
148+
.then((client) => {
149149
this._mongoPromise = null;
150-
return db.close();
150+
return client.close();
151151
});
152152
}
153153

154-
_db() {
154+
_client() {
155155
if (!this._mongoPromise) {
156156
return Promise.reject(new MongoClosedError());
157157
}
158158

159159
return this._mongoPromise;
160160
}
161161

162+
_db() {
163+
return this._client().then(client => (MongoMilestoneDB._isLegacyMongoClient(client)
164+
? client
165+
: client.db()));
166+
}
167+
162168
_collection(collectionName) {
163169
let name;
164170
let collection;
@@ -244,6 +250,14 @@ class MongoMilestoneDB extends MilestoneDB {
244250
if (typeof object !== 'object') return object;
245251
return Object.assign({}, object);
246252
}
253+
254+
static _isLegacyMongoClient(client) {
255+
// mongodb 2.x connect returns a DB object that also implements the
256+
// functionality of a client, such as `close()`. mongodb 3.x connect
257+
// returns a Client without the `collection()` method
258+
return typeof client.collection === 'function'
259+
&& typeof client.close === 'function';
260+
}
247261
}
248262

249263
class InvalidCollectionNameError extends Error {

package-lock.json

Lines changed: 109 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
{
22
"name": "sharedb-milestone-mongo",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "MongoDB milestone snapshot database adapter for ShareDB",
55
"main": "lib/index.js",
66
"dependencies": {
7-
"mongodb": "^2.2.36",
7+
"mongodb": "^2.2.36 || ^3.0.0",
88
"sharedb": "1.0.0-beta.18"
99
},
1010
"devDependencies": {
1111
"coveralls": "^2.11.8",
1212
"eslint": "^5.13.0",
1313
"expect.js": "^0.3.1",
1414
"istanbul": "^0.4.5",
15-
"mocha": "^2.3.3"
15+
"mocha": "^2.3.3",
16+
"mongodb2": "npm:mongodb@^2.2.36",
17+
"mongodb3": "npm:mongodb@^3.0.0"
1618
},
1719
"scripts": {
1820
"lint": "node_modules/.bin/eslint --ignore-path .gitignore '**/*.js'",

0 commit comments

Comments
 (0)