title: Connection to Mongodb-Native-Driver in express.js
body: <p>I am using mongodb-native-driver in express.js app. I have around 6 collections in the database, so I have created 6 js files with each having a collection as a javascript object (e.g <code>function collection(){}</code>) and the prototypes functions handling all the manipulation on those collections. I thought this would be a good architecture.</p>
  
  <p>But the problem I am having is <strong>how to connect to the database? Should I create a connection in each of this files and use them?</strong> I think that would be an overkill as the connect in mongodb-native-driver creates a pool of connections and having several of them would not be justified. </p>
  
  <p><strong>So how do I create a single connection pool and use it in all the collections.js files?</strong> I want to have the connection like its implemented in mongoose. Let me know if any of my thought process in architecture of the app is wrong. </p>
  
  <p><em>Using Mongoose would solve these problems, but I have read in several places thats it slower than native-driver and also I would prefer a schema-less models.</em></p>
  
  <p><strong>Edit:</strong> I created a module out of models. Each collection was in a file and it took the database as an argument. Now in the index.js file I called the database connection and kept a variable db after I got the database from the connection. (<em>I used the auto-reconnect feature to make sure that the connection wasn't lost</em>). In the same index.js file I exported each of the collections like this</p>
  
  <pre><code>exports.model1 = require('./model1').(db)
  exprorts.model2 = require('./model2').(db)
  </code></pre>
  
  <p>This ensured that the database part was handled in just one module and the app would just call function that each model.js file exported like <code>save()</code>, <code>fincdbyid()</code> etc (<code>whatever you do in the function is upto you to implement</code>).</p>