From 4302a125ef7e5aba4f2b53789b780e8e0363a112 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Wed, 27 Mar 2013 04:38:18 +0400 Subject: [PATCH] Fix undefined handling --- lib/mysql.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 0e5334e..f812703 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -135,7 +135,9 @@ MySQL.prototype.toFields = function (model, data) { var props = this._models[model].properties; Object.keys(data).forEach(function (key) { if (props[key]) { - fields.push('`' + key.replace(/\./g, '`.`') + '` = ' + this.toDatabase(props[key], data[key])); + var value = this.toDatabase(props[key], data[key]); + if ('undefined' === typeof value) return; + fields.push('`' + key.replace(/\./g, '`.`') + '` = ' + value); } }.bind(this)); return fields.join(','); @@ -156,6 +158,7 @@ function dateToMysql(val) { MySQL.prototype.toDatabase = function (prop, val) { if (val === null) return 'NULL'; + if (val === undefined) return; if (val.constructor.name === 'Object') { var operator = Object.keys(val)[0] val = val[operator]; @@ -299,7 +302,11 @@ MySQL.prototype.all = function all(model, filter, callback) { function buildOrderBy(order) { if (typeof order === 'string') order = [order]; - return 'ORDER BY ' + order.join(', '); + return 'ORDER BY ' + order.map(function(o) { + var t = o.split(/\s+/); + if (t.length === 1) return '`' + o + '`'; + return '`' + t[0] + '` ' + t[1]; + }).join(', '); } function buildLimit(limit, offset) {