Fix undefined handling

This commit is contained in:
Anatoliy Chakkaev 2013-03-27 04:38:18 +04:00
parent 9f063c13c5
commit 4302a125ef
1 changed files with 9 additions and 2 deletions

View File

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