Monday 15 April 2013

node.js - How to get a instance of db from node-mongo native driver? -


Note, the Mojo DDB connection has been opened in the main app.js file and the following Call it back to:

  mongodb.connect ('MongoDBUrlGoesHere', function (mistake, db) {app.listen (app.get ('port'), function AppListnCB () {console .log ("listening server on port" + app.get ('port'));});});   

All this application has been done for example only one DB.

Now, if we are in the second external.js file and need the same db object which is Elyady Connect. It can be done very easily if we are using or

Does anyone help me to know how it can be done with the original driver?

You can write a cover, a new module where you store the DB example, this way Some of:

  // db.js var HOSTNAME = ... var port = ... var db = module.exports = {}; Var example; Db.connect = function () {... instance = & lt; Db_instance & gt ;; }; Db.disconnect = function () {... instance = null; }; Db.instance = function () {Return Example; };   

Now, every time you need a DB example, do this:

  var db = require (". / Path / to / Db "); Db.instance ();    

No comments:

Post a Comment