1

I have an express/nodeJs app which will use mongo-db native client for persistance in Mongo-db. Now my problem is that most of the example I have seen have one collection and therefore do the connection in that js file, like this tutorial(which is mentioned in mongo-db native client docs). There the connection code is like this:

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)
        });
      }
    });
};

There are other methods also which I kept out to keep it small(check in the above url for full tutorial).

My problem is that I have few more collections and therefore I am worried as to how to make a single connection to the database and use it for all the collections. I would also like if you can specify how to make connections to replica-sets also for reads and the main database for writes.

Or should I make calls to connections in each of my collection.js files like the above mentioned tutorial has done in one. Please help me.

4

1 回答 1

2

尝试使用单例模式

在您的主要 express 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(); 
于 2013-10-21T00:45:56.807 回答