2013-07-02 83 views
1

我有一個express/nodeJs應用程序,它將在Mongo-db中使用mongo-db本地客戶端進行持久保存。現在我的問題是,我看到的大多數示例都有一個集合,因此在該js文件中執行連接,如tutorial(在mongo-db本地客戶端文檔中提到)。有連接的代碼是這樣的:mongo-db本地客戶端的數據庫連接

var Db = require('mongodb').Db; 

var Connection = require('mongodb').Connection; 
var Server = require('mongodb').Server; 
var BSON = require('mongodb').BSON; 
var ObjectID = require('mongodb').ObjectID; 

ArticleProvider = function(host, port) { 
    this.db= new Db('node-mongo-blog', new Server(host, port, {auto_reconnect: true}, {})); 
    this.db.open(function(){}); 
}; 


ArticleProvider.prototype.getCollection= function(callback) { 
    this.db.collection('articles', function(error, article_collection) { 
    if(error) callback(error); 
    else callback(null, article_collection); 
    }); 
}; 

ArticleProvider.prototype.findAll = function(callback) { 
    this.getCollection(function(error, article_collection) { 
     if(error) callback(error) 
     else { 
     article_collection.find().toArray(function(error, results) { 
      if(error) callback(error) 
      else callback(null, results) 
     }); 
     } 
    }); 
}; 

還有其他的方法還我拒之門外,以保持它的小(辦理登機手續完全教程上述網址)。

我的問題是我有更多的集合,因此我擔心如何建立到數據庫的單一連接並將其用於所有集合。我也希望你能指定如何建立連接到副本集也讀取和主要數據庫寫入。

或者我應該調用每個collection.js文件中的連接,就像上面提到的教程已經完成一樣。 請幫幫我。

回答

2

嘗試使用singleton pattern

在你的主快遞app.js連接到數據庫和創建數據庫實例:

var Database = require('database.js'); 

... 

mongodb.connect(config.dbAddress, function (err, db) { 
    if(err) { 
    return console.log('Error connection to DB'); 
    } 

    Database.setDB(db); 

    app.listen(config.appPort); 
}); 

然後在任何其他文件,你需要使用的數據庫需要數據庫.js文件再次:

var Database = require('database.js'); 

... 

ArticleProvider = function() { 
    this.db = Database.getDB(); 
}; 

... 

最後database.js文件遵循Singleton模式:

/** 
Database Singleton Object 

database.js is used throughout the app to access the db object. Using mongodb 
native drivers the db object contains a pool of connections that are used to 
make requests to the db. To use this singleton object simply require it and 
either call getDB() or setDB(). The idea is to use setDB in app.js just 
after we connect to the db and receive the db object, then in any other file we 
need the db require and call getDB 
**/ 

var mongodb = require('mongodb'); 

var singleton = (function() { 

    var instance; //Singleton Instance 

    function init() { 

    var _db; //Instance db object (private) 

    //Other private variables and function can be declared here 

    return { 

     //Gets the instance's db object 
     getDB: function() { 
     return _db; 
     }, 

     //Sets the instance's db object 
     setDB: function(db) { 
     _db = db; 
     } 

     //Other public variables and methods can be declared here 
    }; 

    } 

    return { 
    //getInstance returns the singleton instance or creates a new one if 
    //not present 
    getInstance: function() { 
     if (!instance) { 
     instance = init(); 
     } 

     return instance; 
    } 
    }; 

})(); 

module.exports = singleton.getInstance(); 
相關問題