Skip to content
Open
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
44 changes: 26 additions & 18 deletions lib/databases/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var util = require('util'),
isNew = mongoVersion.indexOf('1.') !== 0,
ObjectID = isNew ? mongo.ObjectID : mongo.BSONPure.ObjectID,
_ = require('lodash');

function cleanSessionData(json) {
if (!json) {
return json;
Expand Down Expand Up @@ -41,18 +41,21 @@ var MongoSessionStore = function (options) {
_.defaults(options, defaults);

var defaultOpt = {
ssl: false
// ssl is by default false in node-mongodb-native driver v1.4.x / v2.2 and above
// ssl: false
};

options.options = options.options || {};

if (isNew) {
defaultOpt.autoReconnect = false;
// autoReconnect needs to be true to reconnect on failure, by default it is true in node-mongodb-native driver v2.2 and above
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is intentionally set to false,
cloud-native environment, if there is no connection to the db, the process is killed...

// defaultOpt.autoReconnect = false;
defaultOpt.useNewUrlParser = true;
defaultOpt.useUnifiedTopology = true;
_.defaults(options.options, defaultOpt);
} else {
defaultOpt.auto_reconnect = false;
// auto_reconnect not required as by default it is false in node-mongodb-native driver v1.4.x
// defaultOpt.auto_reconnect = false;
_.defaults(options.options, defaultOpt);
}

Expand All @@ -69,15 +72,19 @@ _.extend(MongoSessionStore.prototype, {
var options = this.options;

var connectionUrl;
var client;

// for mongodb Atlas uri / local mongodb v3.2 and above replicset uri
if (options.url) {
connectionUrl = options.url;
} else {
}
// for local mongodb v3.2 and above (not replicaset)
else {
var members = options.servers
? options.servers
: [{host: options.host, port: options.port}];
: [{ host: options.host, port: options.port }];

var memberString = _(members).map(function(m) { return m.host + ':' + m.port; });
var memberString = _(members).map(function (m) { return m.host + ':' + m.port; });
var authString = options.username && options.password
? options.username + ':' + options.password + '@'
: '';
Expand All @@ -88,11 +95,10 @@ _.extend(MongoSessionStore.prototype, {
connectionUrl = 'mongodb://' + authString + memberString + '/' + options.dbName + optionsString;
}

var client;

// for node-mongodb-native driver v2.2 and above
if (mongo.MongoClient.length === 2) {
client = new mongo.MongoClient(connectionUrl, options.options);
client.connect(function(err, cl) {
client.connect(function (err, cl) {
if (err) {
if (callback) callback(err);
return;
Expand All @@ -104,9 +110,11 @@ _.extend(MongoSessionStore.prototype, {
}
initDb();
});
} else {
}
// for node-mongodb-native driver v1.4.x
else {
client = new mongo.MongoClient();
client.connect(connectionUrl, options.options, function(err, db) {
client.connect(connectionUrl, options.options, function (err, db) {
if (err) {
if (callback) callback(err);
return;
Expand All @@ -126,7 +134,7 @@ _.extend(MongoSessionStore.prototype, {

var finish = function (err) {
self.sessions = self.db.collection(options.collectionName);
self.sessions.ensureIndex({ expires: 1 }, { expireAfterSeconds: 0 }, function() {});
self.sessions.createIndex({ expires: 1 }, { expireAfterSeconds: 0 }, function() {});
if (!err) {
self.isConnected = true;
self.emit('connect');
Expand Down Expand Up @@ -210,7 +218,7 @@ _.extend(MongoSessionStore.prototype, {

if (!this.options.ignoreConcurrency) sess._hash = new ObjectID().toString();

this.sessions.update(query, sess, { upsert: true, safe: true }, function(err, modifiedCount) {
this.sessions.updateOne(query, { $set: sess }, { upsert: true }, function(err, modifiedCount) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write it backwards compatible?
i.e. if typeof this.sessions.updateOne === 'function' ? updateOne : update

if (err && err.message && err.message.indexOf('duplicate key') > -1) {
return callback(new Error('ConcurrencyError: Session was updated by someone else!'));
}
Expand Down Expand Up @@ -247,11 +255,11 @@ _.extend(MongoSessionStore.prototype, {
},

destroy: function (sid, callback) {
this.sessions.remove({ _id: sid }, callback || function () {});
this.sessions.deleteOne({ _id: sid }, callback || function () {});
},

length: function (callback) {
this.sessions.count(callback || function () {});
this.sessions.estimatedDocumentCount(callback || function() {});
},

all: function (callback) {
Expand All @@ -268,9 +276,9 @@ _.extend(MongoSessionStore.prototype, {
},

clear: function (callback) {
this.sessions.remove(callback || function () {});
this.sessions.deleteMany(callback || function() {});
}

});

module.exports = MongoSessionStore;
module.exports = MongoSessionStore;