How can I connect to multiple MongoDB databases using monk?

712 Views Asked by At

I have a Node.js application with 2 modules, each of which have their own database. I'm using monk to connect to these databases, can they each connect to their own database, or am I limited to only one DB connection per app?

This code is used in both modules:

var mongo = require('mongodb');
var monk = require('monk');
...

module.exports = function(modules) {

    ...

    var StorageClass = function() {
        var myDb;

        this.init = function() {
            console.log('Connecting to Mongo DB on %s', config.database.URL);
            myDb = monk(config.database.URL);
        }

        ...
    }

    var storage = new CatchupStorageClass(); 
    storage.init();
    return storage;
}

Looks like when this code is executed in the second module, it wipes out the configuration for the first module and replaces it with its own. These 2 modules does not even use a shared storage class, they each have their own (duplicated) copy with a different name.

How is it possible to have more than one connection in a Node.js app? Does monk support it?

1

There are 1 best solutions below

0
On BEST ANSWER

Yes, monk does support connections to different database in the same app. My problem was coming from incorrect use of multiple base strategies in passportjs. I simply needed to name my different base strategies separately: Use multiple local strategies in PassportJS