loopback-connector-mysql
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -206,3 +206,3 @@ /*! | ||
*/ | ||
MySQL.prototype.updateOrCreate = function (model, data, callback) { | ||
MySQL.prototype.updateOrCreate = MySQL.prototype.save = function (model, data, callback) { | ||
var mysql = this; | ||
@@ -248,3 +248,5 @@ var fieldsNames = []; | ||
var value = this.toDatabase(props[key], data[key]); | ||
if ('undefined' === typeof value) return; | ||
if (undefined === value) { | ||
return; | ||
} | ||
fields.push(self.columnEscaped(model, key) + ' = ' + value); | ||
@@ -276,4 +278,5 @@ } | ||
MySQL.prototype.toDatabase = function (prop, val) { | ||
if (val === null) return 'NULL'; | ||
if (val === undefined) return 'NULL'; | ||
if (val === null || val === undefined) { | ||
return 'NULL'; | ||
} | ||
if (val.constructor.name === 'Object') { | ||
@@ -286,17 +289,24 @@ var operator = Object.keys(val)[0] | ||
this.toDatabase(prop, val[1]); | ||
} else if (operator == 'inq' || operator == 'nin') { | ||
if (!(val.propertyIsEnumerable('length')) && typeof val === 'object' && typeof val.length === 'number') { //if value is array | ||
} else if (operator === 'inq' || operator === 'nin') { | ||
if (Array.isArray(val)) { //if value is array | ||
for (var i = 0; i < val.length; i++) { | ||
val[i] = this.client.escape(val[i]); | ||
val[i] = this.toDatabase(prop, val[i]); | ||
} | ||
return val.join(','); | ||
} else { | ||
return val; | ||
return this.toDatabase(prop, val); | ||
} | ||
} | ||
} | ||
if (!prop) return val; | ||
if (prop.type.name === 'Number') return Number(val); | ||
if (prop.type.name === 'Date') { | ||
if (!val) return 'NULL'; | ||
if (!prop) { | ||
return this.client.escape(val); | ||
} | ||
if (prop.type === Number) { | ||
val = Number(val); | ||
return isNaN(val) ? 'NULL' : val; | ||
} | ||
if (prop.type === Date) { | ||
if (!val) { | ||
return 'NULL'; | ||
} | ||
if (!val.toUTCString) { | ||
@@ -307,3 +317,5 @@ val = new Date(val); | ||
} | ||
if (prop.type.name == "Boolean") return val ? 1 : 0; | ||
if (prop.type === Boolean) { | ||
return val ? 1 : 0; | ||
} | ||
if (prop.type.name === 'GeoPoint') { | ||
@@ -407,2 +419,5 @@ return val ? 'Point(' + val.lat + ',' + val.lng + ')' : 'NULL'; | ||
MySQL.prototype._buildWhere = function (model, conds) { | ||
if (conds === null || conds === undefined || (typeof conds !== 'object')) { | ||
return ''; | ||
} | ||
var self = this; | ||
@@ -492,3 +507,9 @@ var props = self._models[model].properties; | ||
function buildLimit(limit, offset) { | ||
return 'LIMIT ' + (offset ? (offset + ', ' + limit) : limit); | ||
if (isNaN(limit)) { | ||
limit = 0; | ||
} | ||
if (isNaN(offset)) { | ||
offset = 0; | ||
} | ||
return 'LIMIT ' + (offset ? (offset + ',' + limit) : limit); | ||
} | ||
@@ -527,3 +548,3 @@ | ||
if (filter.limit) { | ||
sql += ' ' + buildLimit(filter.limit, filter.skip || 0); | ||
sql += ' ' + buildLimit(filter.limit, filter.skip || filter.offset || 0); | ||
} | ||
@@ -552,2 +573,17 @@ | ||
MySQL.prototype.count = function count(model, callback, where) { | ||
this.query('SELECT count(*) as cnt FROM ' + | ||
this.tableEscaped(model) + ' ' + this.buildWhere(model, where), | ||
function (err, res) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var c = (res && res[0] && res[0].cnt) || 0; | ||
callback(err, c); | ||
}); | ||
}; | ||
/** | ||
@@ -566,3 +602,3 @@ * Delete instances for the given model | ||
} | ||
this.query('DELETE FROM ' | ||
this.query('DELETE FROM ' | ||
+ this.tableEscaped(model) + ' ' + this.buildWhere(model, where || {}), | ||
@@ -569,0 +605,0 @@ function (err, data) { |
{ | ||
"name": "loopback-connector-mysql", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "MySQL connector for loopback-datasource-juggler", | ||
@@ -18,3 +18,3 @@ "main": "index.js", | ||
"mocha": "~1.18.0", | ||
"rc": "~0.3.1" | ||
"rc": "~0.4.0" | ||
}, | ||
@@ -21,0 +21,0 @@ "repository": { |
133664
22
2707