title: Use mysql with promises in node.js
body: <p>I just followed the <a href="https://scotch.io/tutorials/easy-node-authentication-setup-and-local" rel="nofollow noreferrer">tutorial for authentication on node from scotch.io</a>.
  They used mongodb as a backend, and I ported this to mysql. This is my first node app connecting to a database.</p>
  
  <p>I used bluebird to <code>promisify</code> the <code>mysqljs/mysql</code> package in <code>config/database.js</code></p>
  
  <pre><code>const mysql = require('mysql');
  const Promise = require("bluebird");
  Promise.promisifyAll(mysql);
  Promise.promisifyAll(require("mysql/lib/Connection").prototype);
  Promise.promisifyAll(require("mysql/lib/Pool").prototype);
  </code></pre>
  
  <p>Here is the code of my <code>model/user.js</code>: </p>
  
  <pre><code>const mysqlPool = require('../../config/database'),
      bcrypt = require('bcrypt-nodejs'),
      Promise = require("bluebird");
      getDb = function(){
          return mysqlPool.getConnectionAsync();
      }
  let user = {
      create(user, done) {
          getDb().then((db) =&gt; {
              return db.queryAsync(`insert into bot_users (name, token, type, external_id) values (?, ?, ?, ?)`,
                  [user.name, user.token, user.type, user.external_id])
          }).then((results, fields) =&gt; {
              console.log(results, fields)
              user.id = results.insertId
              return done(null, user)
          })
      },
  </code></pre>
  
  <p>There is something that does not look good to me: </p>
  
  <pre><code>getDb().then((db) =&gt; {
      return db.queryAsync(...)
  }).then(...)
  </code></pre>
  
  <p>looks somehow convoluted. I would like to have a variable <code>db</code> that I could use directly as <code>db.queryAsync(...).then(...)</code>, but I'm not able to find out how to assign such a promise to the variable <code>db</code>.</p>
  
  <p>Also I would like an advice on my approach. Is this a good approach to use promises here or is there a better one ?</p>
  
  <p>Is it necessary to <code>return done(null, user)</code> inside the <code>then</code> callback in order to stick to the general node convention, or is this no longer required when using promises (bluebird) ?</p>