title: Node.js and global MySQL connection OBJ
body: <p>I'm a beginner with Node.js, is it ever ok to use MySQL as a global var?</p>
  
  <p>I have a db_helper.js with this code inside:</p>
  
  <pre><code>global.client = require('mysql').createConnection({
      user: '__mysqluser__',
      password: '__mysqlpass__',
      database: '__mysqldb__',
      timezone: '-03:00'
  });
  global.client.connect();
  </code></pre>
  
  <p>On my main.js I just do a:</p>
  
  <pre><code>require('db_helper');
  </code></pre>
  
  <p>Then on other js files, whenever I need to UPDATE or SELECT I just call:</p>
  
  <pre><code>global.client(query, data);
  </code></pre>
  
  <p>I haven't seen any code like this yet, but it works as expected, but I'm experiencing random crashes from time to time, when reloading pages.</p>
  
  <p>Is it OK to use it like this? Are my crashes related to the way I connect to the DB? </p>
  
  <p>I think it's related because when the crash happens, is because MySQL fails to return data, but the crash happens when parsing the result, like:</p>
  
  <pre><code>global.client.query(query, function(err, results, fields) {
     if (err) throw err;
     if (results &amp;&amp; Object.prototype.toString.call(results) === '[object Array]') {
        var j = result[0].data; 
     }
  }
  </code></pre>
  
  <p>Most of the time, <strong>var j</strong> has the value I expect it to have, but when node crashes, this var returns empty, despite the fact that it checks for (results) consistency before. Node crashes saying:</p>
  
  <pre><code>result[0].data is undefined
  </code></pre>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT:</strong> The crash happens whenever the client js file gets updated on server. (after a local edit of the .js file &amp; server upload via FTP), Why?</p>