Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
language: node_js

node_js:
- "6"
- "8"
- "12"
- "10"
- "8"

services:
- docker

env:
- MONGODB_VERSION="2.6"
- MONGODB_VERSION="3.6"
- MONGODB_VERSION="4.2"
- MONGODB_VERSION="4.0"
- MONGODB_VERSION="3.6"
- MONGODB_VERSION="3.4"

before_install:
- docker run -d -p 127.0.0.1:27017:27017 mongo:$MONGODB_VERSION
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const shareDb = new ShareDB({ milestoneDb: milestoneDb });

### Mongo

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

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

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

```javascript
const mongodb = require('mongodb');
Expand All @@ -59,8 +59,7 @@ const milestoneDb = new MongoMilestoneDB({
#### Intervals

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

```javascript
const milestoneDb = new MongoMilestoneDB({
Expand Down Expand Up @@ -133,5 +132,5 @@ const milestoneDb = new MongoMilestoneDB({

[1]: https://github.com/share/sharedb
[2]: https://mongodb.github.io/node-mongodb-native/
[3]: http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect
[4]: http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html
[3]: https://mongodb.github.io/node-mongodb-native/3.6/api/MongoClient.html#.connect
[4]: https://mongodb.github.io/node-mongodb-native/3.6/api/MongoClient.html
24 changes: 19 additions & 5 deletions lib/mongo-milestone-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class MongoMilestoneDB extends MilestoneDB {
const updatedSnapshot = MongoMilestoneDB._snapshotToDbRepresentation(snapshot);
const options = {upsert: true};

return collection.updateOne(query, updatedSnapshot, options);
return collection.updateOne(query, {$set: updatedSnapshot}, options);
});
}

Expand Down Expand Up @@ -144,21 +144,27 @@ class MongoMilestoneDB extends MilestoneDB {
}

_close() {
return this._db()
.then((db) => {
return this._client()
.then((client) => {
this._mongoPromise = null;
return db.close();
return client.close();
});
}

_db() {
_client() {
if (!this._mongoPromise) {
return Promise.reject(new MongoClosedError());
}

return this._mongoPromise;
}

_db() {
return this._client().then(client => (MongoMilestoneDB._isLegacyMongoClient(client)
? client
: client.db()));
}

_collection(collectionName) {
let name;
let collection;
Expand Down Expand Up @@ -244,6 +250,14 @@ class MongoMilestoneDB extends MilestoneDB {
if (typeof object !== 'object') return object;
return Object.assign({}, object);
}

static _isLegacyMongoClient(client) {
// mongodb 2.x connect returns a DB object that also implements the
// functionality of a client, such as `close()`. mongodb 3.x connect
// returns a Client without the `collection()` method
return typeof client.collection === 'function'
&& typeof client.close === 'function';
}
}

class InvalidCollectionNameError extends Error {
Expand Down
110 changes: 109 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{
"name": "sharedb-milestone-mongo",
"version": "0.2.0",
"version": "0.3.0",
"description": "MongoDB milestone snapshot database adapter for ShareDB",
"main": "lib/index.js",
"dependencies": {
"mongodb": "^2.2.36",
"mongodb": "^2.2.36 || ^3.0.0",
"sharedb": "1.0.0-beta.18"
},
"devDependencies": {
"coveralls": "^2.11.8",
"eslint": "^5.13.0",
"expect.js": "^0.3.1",
"istanbul": "^0.4.5",
"mocha": "^2.3.3"
"mocha": "^2.3.3",
"mongodb2": "npm:mongodb@^2.2.36",
"mongodb3": "npm:mongodb@^3.0.0"
},
"scripts": {
"lint": "node_modules/.bin/eslint --ignore-path .gitignore '**/*.js'",
Expand Down
Loading