mgenev/nodember — pull request #13 by aforty (2014-12-28 00:20:23+00:00)
Title: use config db url for connect-mongo

##### The Problem

When running `grunt` I received the following error:

```
/Users/aforty/Dropbox/Code/aca/node_modules/connect-mongo/lib/connect-mongo.js:125
throw new Error('Required MongoStore option `db` missing');
^
Error: Required MongoStore option `db` missing
at buildUrlFromOptions (/Users/aforty/Dropbox/Code/aca/node_modules/connect-mongo/lib/connect-mongo.js:125:15)
at new MongoStore (/Users/aforty/Dropbox/Code/aca/node_modules/connect-mongo/lib/connect-mongo.js:206:7)
at Function. (/Users/aforty/Dropbox/Code/aca/server/config/express.js:55:20)
at Function.app.configure (/Users/aforty/Dropbox/Code/aca/node_modules/express/lib/application.js:391:61)
at module.exports (/Users/aforty/Dropbox/Code/aca/server/config/express.js:44:9)
at Object. (/Users/aforty/Dropbox/Code/aca/server.js:46:35)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
```

After some investigation it turned out that the `connect-mongo` and `mongoose` packages both installed and used a different version of the `mongodb` package internally, mainly because `mongoose` has a very specific requirement for mongodb to be at "1.4.7", while `connect-mongo` is ">= 1.2.0 <2.0.0". I'm not sure why npm then decides to install a version that is not the latest for `connect-mongo` but for some reason it does.

```
$ npm list mongodb
nodember@0.1.0 /Users/aforty/Dropbox/Code/aca
├─┬ connect-mongo@0.5.0
│ └── mongodb@1.4.26
└─┬ mongoose@3.8.15
└── mongodb@1.4.7
```

The problem with this is exposed at node_modules/connect-mongo/lib/connect-mongo.js:201

```
} else if (options.db && options.db instanceof Db) {
debug('use strategy: `native_db`');
initWithNativeDb();
} else {
debug('use strategy: `legacy`');
buildUrlFromOptions();
initWithUrl();
}
```

`options.db instanceof Db` would return false because `options.db` was created with a differing versions of `mongodb`. `connect-mongo` would then attempt to the 'legacy' strategy and fail.

##### The Solution
Instead of passing the instantiated db connection in, we simply let `connect-mongo` instantiate its own connection with whatever version of `mongodb` it chooses. So instead of passing in the connection instance, we simply pass in the url as defined in the configuration.

Another possible solution is to include the specific version of mongodb that `mongoose` requires in package.json. If package.json included mongodb@1.4.7 then connect-mongo would end up using that and not install its own. But because this solution requires the maintainer to keep track of whatever version these two packages require, it seems like more of a headache going forward.