title: nodejs parse mysql row to object
body: <p>I'm a little newbie in node.js + mysql + object oriented.</p>
  
  <p>Following question <a href="https://stackoverflow.com/questions/16264162/return-rows-with-nodejs-and-node-mysql">here</a> I would like the 'Content' object to use the values returned by a mysql query. What I'm doing now I find it is really redundant and possibly stupid as rows[0] itself is the object I want to use. Any better way for doing this? Or different approach if this is wrong also appreciated.</p>
  
  <p>(I'm using binary uuid keys that must be hex-stringifyed again to send as resource response)</p>
  
  <p>content.js:</p>
  
  <pre><code>function Content() {
    this.id = '';
    this.name = '';
    this.domain = '';
  }
  
  Content.prototype.validate = function(path, queryParams) {
    ...
    return true;
  };
  
  Content.prototype.whatever = function(apiVersion, params, callback) {
    ...
    return callback(null, newParams);
  };
  </code></pre>
  
  <p>mysql.js:</p>
  
  <pre><code>MySQLDb.SELECT_CONTENT_ID = "SELECT id, name, domain FROM content WHERE id = UNHEX(?)";
  
  MySQLDb.prototype.findContentByID = function(id, callback) {
    this.dbConnection.query(MySQLDb.SELECT_CONTENT_ID, [ id ],
        function(err, rows, fields) {
          var content = new Content();
  
          if (rows.length &gt; 0) {
            var i = 0;
            for (var key in rows[0]) {
              if (rows[0].hasOwnProperty(key) &amp;&amp; content.hasOwnProperty(key)) {
                // BINARY(16) --&gt; HEX string
                if (fields[i].columnType === 254) {
                  content[key] = rows[0][key].toString('hex').toUpperCase();
                } else {
                  content[key] = rows[0][key];
                }
              } else {
                console.log('Column ' + key + ' out of sync on table "content"');
              }
  
              i += 1;
            }
          }
  
          callback(err, content);
        });
  };
  </code></pre>
  
  <p>contentRes.js:</p>
  
  <pre><code>contentRes.GETWhatever = function(req, res) {
    db.findContentByID(req.params.id, function onContent(err, content) {
      if (err || !content.validate(req.path, req.query)) {
        return res.send({});
      }
  
      content.whatever(req.query.apiVersion, req.query.d,
          function onWhateverdone(err, params) {
            if (err) {
              return res.send({});
            }
  
            return res.send(params);
          });
    });
  };
  </code></pre>