mongo-db本机客户端中的数据库连接

时间:2013-07-02 08:14:56

标签: node.js mongodb express node-mongodb-native

我有一个express / nodeJs应用程序,它将使用mongo-db本机客户端来保持Mongo-db的持久性。现在我的问题是我见过的大多数示例都有一个集合,因此在该js文件中进行连接,例如tutorial(在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)
        });
      }
    });
};

还有其他一些方法可以保留它(请查看上面的url以获取完整的教程)。

我的问题是我有更多的集合,因此我担心如何与数据库建立单一连接并将其用于所有集合。我还想如果您可以指定如何连接副本集以及读取的主数据库。

或者我应该在我的每个collection.js文件中调用连接,就像上面提到的教程一样。 请帮我。

1 个答案:

答案 0 :(得分: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);
});

然后在任何其他文件中,您需要再次使用数据库require database.js:

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

...

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

...

最后,database.js文件遵循单例模式:

/**
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();