-
Notifications
You must be signed in to change notification settings - Fork 22
updated to support mongodb v3.2 and above #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rohantarai
wants to merge
1
commit into
robinfehr:master
Choose a base branch
from
rohantarai:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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 | ||
| // 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); | ||
| } | ||
|
|
||
|
|
@@ -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 + '@' | ||
| : ''; | ||
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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'); | ||
|
|
@@ -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) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you write it backwards compatible? |
||
| if (err && err.message && err.message.indexOf('duplicate key') > -1) { | ||
| return callback(new Error('ConcurrencyError: Session was updated by someone else!')); | ||
| } | ||
|
|
@@ -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) { | ||
|
|
@@ -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; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...