Merge branch 'release/1.4.3' into production
This commit is contained in:
commit
c77b966949
25
lib/mysql.js
25
lib/mysql.js
|
@ -57,6 +57,9 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
|
|||
// Take other options for mysql driver
|
||||
// See https://github.com/strongloop/loopback-connector-mysql/issues/46
|
||||
for (var p in s) {
|
||||
if (p === 'database' && s.createDatabase) {
|
||||
continue;
|
||||
}
|
||||
if (options[p] === undefined) {
|
||||
options[p] = s[p];
|
||||
}
|
||||
|
@ -149,7 +152,7 @@ MySQL.prototype.query = function (sql, callback) {
|
|||
connection.query(sql, function (err, data) {
|
||||
if (debugEnabled) {
|
||||
if (err) {
|
||||
console.error('Error: ', err);
|
||||
console.error('Error: %j', err);
|
||||
}
|
||||
debug('Data: ', data);
|
||||
}
|
||||
|
@ -234,7 +237,7 @@ MySQL.prototype.updateOrCreate = MySQL.prototype.save = function (model, data, c
|
|||
if (props[key] || mysql.id(model, key)) {
|
||||
var k = mysql.columnEscaped(model, key);
|
||||
var v;
|
||||
v = mysql.toDatabase(props[key], data[key]);
|
||||
v = mysql.toDatabase(props[key], data[key], true);
|
||||
if (v !== undefined) {
|
||||
fieldsNames.push(k);
|
||||
fieldValues.push(v);
|
||||
|
@ -264,7 +267,7 @@ MySQL.prototype.toFields = function (model, data) {
|
|||
var props = this._models[model].properties;
|
||||
Object.keys(data).forEach(function (key) {
|
||||
if (props[key]) {
|
||||
var value = this.toDatabase(props[key], data[key]);
|
||||
var value = this.toDatabase(props[key], data[key], true);
|
||||
if (undefined === value) {
|
||||
return;
|
||||
}
|
||||
|
@ -293,11 +296,11 @@ function dateToMysql(val) {
|
|||
* @param val
|
||||
* @returns {*}
|
||||
*/
|
||||
MySQL.prototype.toDatabase = function (prop, val) {
|
||||
MySQL.prototype.toDatabase = function (prop, val, forCreate) {
|
||||
if (val === null || val === undefined) {
|
||||
return 'NULL';
|
||||
}
|
||||
if (val.constructor.name === 'Object') {
|
||||
if (!forCreate && val.constructor.name === 'Object') {
|
||||
var operator = Object.keys(val)[0]
|
||||
val = val[operator];
|
||||
if (operator === 'between') {
|
||||
|
@ -342,6 +345,10 @@ MySQL.prototype.toDatabase = function (prop, val) {
|
|||
return this.client.escape(val);
|
||||
}
|
||||
if (typeof prop.type === 'function') {
|
||||
if (prop.type.modelName) {
|
||||
// For embedded models
|
||||
return this.client.escape(JSON.stringify(val));
|
||||
}
|
||||
return this.client.escape(prop.type(val));
|
||||
}
|
||||
return this.client.escape(val.toString());
|
||||
|
@ -362,7 +369,11 @@ MySQL.prototype.fromDatabase = function (model, data) {
|
|||
for (var p in props) {
|
||||
var key = this.column(model, p);
|
||||
var val = data[key];
|
||||
if (typeof val === 'undefined' || val === null) {
|
||||
if (val === undefined) {
|
||||
continue;
|
||||
}
|
||||
if (val === null) {
|
||||
json[p] = null;
|
||||
continue;
|
||||
}
|
||||
if (props[p]) {
|
||||
|
@ -655,7 +666,7 @@ MySQL.prototype.autoupdate = function (models, cb) {
|
|||
|
||||
function done(err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
console.error('%j', err);
|
||||
}
|
||||
if (--wait === 0 && cb) {
|
||||
cb();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "loopback-connector-mysql",
|
||||
"version": "1.4.2",
|
||||
"version": "1.4.3",
|
||||
"description": "MySQL connector for loopback-datasource-juggler",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
|
Loading…
Reference in New Issue