Reusing Instance of object via require funcationlity

58 views
Skip to first unread message

Manish Bansal

unread,
Sep 2, 2014, 8:05:34 AM9/2/14
to nod...@googlegroups.com
Hi developers, 
I am new to node.js and mongodb and trying to use native mongodb driver for node to read database. 
now my very basic example works good, but this is not i really want to achieve at the end of the day. 

//Testing installation

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://127.0.0.1:27017/testDB', function(err, db) {
 
var collection = db.collection('testTable');
    collection
.find().toArray(function(err, results) {
        console
.log(results);
        db
.close();
     
});
 
});

I want to serve my data via http through node server, if on every operation i would be closing the connection, then i would be ended up creating thousands of connections simultaneously, and Main objective to choose node+mongodb is performance and real-time (of-course i will be using ws to achieve real-time objectives) information distribution system goes for a toss.


// file dbconnection.js

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://127.0.0.1:27017/testDB', function(err, db) {
   
//Now here i dont want to close the db connection but want to reuse  
   exports
.getConnection = db;
 
});
});


// file TestConnection.js 
var dbconnection = require('./dbconnection.js')
var db = dbconnection.getConnection; 


//Error on next line  (basically db object is null)
var collection = db.collection('testTable');
collection
.find().toArray(function(err, results) {
 console
.dir(results);
});


I am very much confuse how i can use require funcationality effectively to pass object instance across project. one i can think of present scenario is creating db connection instance  on the main server.js page where i am creating http object and pass this object to function call.  

Please advice me what are the best possible ways to achieve such requirements. so that I will create main instance only once and keep on using it for my every request come via node HTTP server. 

Thanks in advance, 
Manish Bansal 


Oleg Verych

unread,
Sep 3, 2014, 3:06:44 AM9/3/14
to nod...@googlegroups.com
Hi, Manish
 
I am very much confuse how i can use require funcationality effectively to pass object instance across project. one i can think of present scenario is creating db connection instance  on the main server.js page where i am creating http object and pass this object to function call.  

Please advice me what are the best possible ways to achieve such requirements. so that I will create main instance only once and keep on using it for my every request come via node HTTP server. 

 
There are projects like this one to show how to start:



OTOH, my project is about real case of using single db with
connection via `mongodb-native` without any additional libs,
but with robust setup (reconnection, etc.) and simple interface:


In any general case access to the current db connection from
any node.js module is done via:

```
var db = require('./lib/mongodb.js').client
// but check manually if it is set up yet
```

In any application module(specific to that project), where `api` is passed,
db can be accessed like so [1]

```
function middleware(req, res, next){
var db, hst

    db = api.db
    hst = db.getCollection('hst')

    return hst.find({}).toArray(function(err, arr){
        if(err) return next(err)
        if(!arr.length) return next('apperror_zero')

        // now there is a valid array of data
        return res.json(arr)
    })
}
```

Manish Bansal

unread,
Sep 3, 2014, 3:42:29 PM9/3/14
to nod...@googlegroups.com
Hi Oleg, 
Thank alot for the reply, but as i have mentioned earlier i am very new to nodejs world and does not understand most of its features, i tried finding online resources but those are limited or very basic either very advance.
nevertheless, Oleg i haven't yet tried reading what is express.js and neither i am looking for any 3rd party adins/package to achieve this basic database connection functionality. moreover i am looking for best practice what real node developers follows to encounter such issues. curious to know if they create new connection on every user request or they do share connections. 

and also if you can help me with a very basic and minimal code example of sharing database connection object, thought-out modules will be a great help for me.
moreover not just db connection object but if you can put some light on common practice of  sharing instance of objects throughout projects using modules/requires would be much much appreciated. 

Thanks for writing this.
Manish Bansal  
Reply all
Reply to author
Forward
0 new messages