title: node.js + mysql connection pooling
body: <p>I'm trying to figure out how to structure my application to use MySQL most efficent way. I'm using node-mysql module. Other threads here suggested to use connection pooling so i set up a little module mysql.js</p>
  
  <pre><code>var mysql = require('mysql');
  
  var pool  = mysql.createPool({
      host     : 'localhost',
      user     : 'root',
      password : 'root',
      database : 'guess'
  });
  
  exports.pool = pool;
  </code></pre>
  
  <p>Now whenever I want to query mysql I require this module and then query the databse</p>
  
  <pre><code>var mysql = require('../db/mysql').pool;
  
  var test = function(req, res) {
       mysql.getConnection(function(err, conn){
           conn.query("select * from users", function(err, rows) {
                res.json(rows);
           })
       })
  }
  </code></pre>
  
  <p>Is this good approach? I couldn't really find too much examples of using mysql connections besides very simple one where everything is done in main app.js script so I don't really know what the convention / best practices are.</p>
  
  <p>Should I always use connection.end() after each query? What if I forget about it somewhere?</p>
  
  <p>How to rewrite the exports part of my mysql module to return just a connection so I don't have to write getConnection() every time?</p>