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) {