Fix undefined handling
This commit is contained in:
parent
9f063c13c5
commit
4302a125ef
11
lib/mysql.js
11
lib/mysql.js
|
@ -135,7 +135,9 @@ MySQL.prototype.toFields = function (model, data) {
|
||||||
var props = this._models[model].properties;
|
var props = this._models[model].properties;
|
||||||
Object.keys(data).forEach(function (key) {
|
Object.keys(data).forEach(function (key) {
|
||||||
if (props[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));
|
}.bind(this));
|
||||||
return fields.join(',');
|
return fields.join(',');
|
||||||
|
@ -156,6 +158,7 @@ function dateToMysql(val) {
|
||||||
|
|
||||||
MySQL.prototype.toDatabase = function (prop, val) {
|
MySQL.prototype.toDatabase = function (prop, val) {
|
||||||
if (val === null) return 'NULL';
|
if (val === null) return 'NULL';
|
||||||
|
if (val === undefined) return;
|
||||||
if (val.constructor.name === 'Object') {
|
if (val.constructor.name === 'Object') {
|
||||||
var operator = Object.keys(val)[0]
|
var operator = Object.keys(val)[0]
|
||||||
val = val[operator];
|
val = val[operator];
|
||||||
|
@ -299,7 +302,11 @@ MySQL.prototype.all = function all(model, filter, callback) {
|
||||||
|
|
||||||
function buildOrderBy(order) {
|
function buildOrderBy(order) {
|
||||||
if (typeof order === 'string') order = [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) {
|
function buildLimit(limit, offset) {
|
||||||
|
|
Loading…
Reference in New Issue