需要我的模块的多个文件覆盖它的variables

我正在尝试创build一个mongo连接池工厂,检查连接到mongo是否已经存在并返回一个连接。 如果它不创build连接池并返回一个连接。

我希望能够从要查询mongo的多个文件中要求这个。 每个文件都需要像这样的mongo:

var fooMongoFactory = require('../../lib/mongoFactory').init(mongodb://localhost:27017/foo); 

然后你在你的文件中使用它像这样:

 fooMongoFactory.getConnection().then(function(db) { // do stuff }); 

我遇到的问题是我想能够在一个文件中指定多个不同的mongo实例,但是当我这样做的时候,我的第二个init覆盖了第一个。 例:

 var fooMongoFactory = require('../../lib/mongoFactory').init(mongodb://localhost:27017/foo); var barMongoFactory = require('../../lib/mongoFactory').init(mongodb://localhost:27017/bar); fooMongoFactory.getConnection().then(function(db) { // querying in here is actually pointing at the mongo db "bar" }); 

我如何调整我的工厂,以便我可以连接到多个不同的mongo实例,以及跨多个文件使用相同的工厂,而不必每次都实例化它? 我想过使用构造函数,但是这会在每个使用mongoFactory的文件中创build一个新的连接池。

 /** * Creates and manages the Mongo connection pool * * @type {exports} */ var Q = require('q'); var MongoClient = require('mongodb').MongoClient; var dbPromise = null; var db = null; module.exports = function() { return { init: function init(connectionString) { db = connectionString; return module.exports; }, /** * Gets a connection to Mongo from the pool. If the pool has not been instantiated it, * instantiates it and returns a connection. Else it just returns a connection from the pool * * @returns {*} - A promise object that will resolve to a mongo db object */ getConnection: function getConnection() { // get a connection to mongo using the db string and return dbPromise } } }(); 

mongodb节点模块已经具有内置的连接池function,当你调用connect()时会自动使用它。 默认的最大连接池大小是5 ,但是您可以在连接url中更改此值(例如'mongodb://localhost:27017/foo?maxPoolSize=15' )。

您还需要将通过在服务器configuration选项 poolSize设置为某个小于或等于maxPoolSize值来更改创build的实际连接数。 您可能还想要将auto_reconnect设置为true。

现在你可以做的就是保持一个对象在host:port上,该对象包含该服务器的数据库对象。 如果有人传入包含主机端口的连接string,则返回该池。 否则,创build,caching并返回新的数据库对象。

我最终创build了模块,以便您必须传入要连接到的mongodb的连接string。 这个模块最终跟踪所有与mongo连接的连接,以及是否有连接到MongoDB的当前连接。

 /** * Creates and manages the Mongo connection pool * * @type {exports} */ var Q = require('q'); var MongoClient = require('mongodb').MongoClient; var _ = require('underscore'); var connections = []; var dbPromise = null; module.exports = function() { return { /** * Gets a connection to Mongo from the pool. If the pool has not been instantiated it, * instantiates it and returns a connection. Else it just returns a connection from the pool * * @returns {*} - A promise object that will resolve to a mongo db object */ getConnection: function getConnection(connectionString) { var def = Q.defer(); // If connectionString is null or undefined, return an error if(_.isEmpty(connectionString)) { def.reject('getConnection must contain a first parameter'); return dbPromise = def.promise; } // Check if connections contains an object with connectionString equal to the connectionString passed in and set the var to it var pool = _.findWhere(connections, {connectionString: connectionString}); // If no conneciton pool has been instantiated, instantiate it, else return a connection from the pool if(_.isUndefined(pool)) { // Initialize connection once MongoClient.connect(connectionString, function(err, database) { if (err) { def.reject(err); } // Add the connection to the array connections.push({connectionString: connectionString, db: database}); def.resolve(database); }); } else { // Else we have not instantiated the pool yet and we need to def.resolve(pool.db); } return dbPromise = def.promise; } }; }(); 

https://github.com/toymachiner62/mongo-factory