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
54 changes: 29 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,8 @@ ShareDbMongo.prototype._connect = function(mongo, options) {
var mongoPoll = options.mongoPoll;
if (mongoPoll) {
var tasks = {
mongoClient: (typeof mongo === 'function') ? mongo : function(parallelCb) {
mongodb.connect(mongo, options.mongoOptions, parallelCb);
},
mongoPollClient: (typeof mongoPoll === 'function') ? mongoPoll : function(parallelCb) {
mongodb.connect(mongoPoll, options.mongoPollOptions, parallelCb);
}
mongoClient: connect(mongo, options.mongoOptions),
mongoPollClient: connect(mongoPoll, options.mongoPollOptions)
};
async.parallel(tasks, function(err, results) {
if (err) throw err;
Expand Down Expand Up @@ -190,9 +186,33 @@ ShareDbMongo.prototype._connect = function(mongo, options) {
// TODO: Don't pass options directly to mongodb.connect();
// only pass options.mongoOptions
var mongoOptions = options.mongoOptions || options;
mongodb.connect(mongo, mongoOptions, finish);
connect(mongo, mongoOptions)(finish);
};

function connect(mongo, options) {
if (typeof mongo === 'function') return mongo;
return function(callback) {
options = Object.assign({}, options);
delete options.mongo;
delete options.mongoPoll;
delete options.mongoPollOptions;
delete options.pollDelay;
delete options.disableIndexCreation;
delete options.allowAllQueries;
delete options.allowJSQueries;
delete options.allowAllQueries;
delete options.allowAggregateQueries;
delete options.getOpsWithoutStrictLinking;

if (typeof mongodb.connect === 'function') {
mongodb.connect(mongo, options, callback);
} else {
var client = new mongodb.MongoClient(mongo, options);
client.connect(callback);
}
};
}

ShareDbMongo.prototype.close = function(callback) {
if (!callback) {
callback = function(err) {
Expand Down Expand Up @@ -1449,24 +1469,8 @@ var collectionOperationsMap = {
collection.distinct(value.field, query, cb);
},
$aggregate: function(collection, query, value, cb) {
collection.aggregate(value, function(err, resultsOrCursor) {
if (err) {
return cb(err);
}
if (Array.isArray(resultsOrCursor)) {
// 2.x Mongo driver directly produces a results array:
// https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#aggregate
var resultsArray = resultsOrCursor;
cb(null, resultsArray);
} else {
// 3.x Mongo driver produces an AggregationCursor:
// https://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#aggregate
//
// ShareDB expects serializable result data, so use `Cursor#toArray` for that.
var cursor = resultsOrCursor;
cursor.toArray(cb);
}
});
var cursor = collection.aggregate(value);
cursor.toArray(cb);
},
$mapReduce: function(collection, query, value, cb) {
if (typeof value !== 'object') {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"dependencies": {
"async": "^2.6.3",
"mongodb": "^2.1.2 || ^3.0.0",
"mongodb": "^2.1.2 || ^3.0.0 || ^4.0.0",
"sharedb": "^1.0.0-beta"
},
"devDependencies": {
Expand All @@ -16,6 +16,7 @@
"mocha": "^6.2.2",
"mongodb2": "npm:mongodb@^2.1.2",
"mongodb3": "npm:mongodb@^3.0.0",
"mongodb4": "npm:mongodb@^4.0.0",
"nyc": "^14.1.1",
"ot-json1": "^1.0.1",
"sharedb-mingo-memory": "^1.1.1",
Expand All @@ -27,7 +28,8 @@
"test": "mocha",
"test:mongodb2": "_SHAREDB_MONGODB_DRIVER=mongodb2 npm test",
"test:mongodb3": "_SHAREDB_MONGODB_DRIVER=mongodb3 npm test",
"test:all": "npm run test:mongodb2 && npm run test:mongodb3",
"test:mongodb4": "_SHAREDB_MONGODB_DRIVER=mongodb4 npm test",
"test:all": "npm run test:mongodb2 && npm run test:mongodb3 && npm run test:mongodb4",
"test-cover": "nyc --temp-dir=coverage -r text -r lcov npm run test:all"
},
"repository": "git://github.com/share/sharedb-mongo.git",
Expand Down