From 7344088cc6b57082b0504325c5c97e41808edb2e Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 19 Aug 2014 16:36:33 -0700 Subject: [PATCH 1/3] Fix the createDatabase option --- lib/mysql.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index f72c72c..53b41a8 100644 --- a/lib/mysql.js +++ b/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); } @@ -655,7 +658,7 @@ MySQL.prototype.autoupdate = function (models, cb) { function done(err) { if (err) { - console.error(err); + console.error('%j', err); } if (--wait === 0 && cb) { cb(); From ea51841f5e62dabf57dbfe6a956991482a9bd41d Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 20 Aug 2014 14:25:53 -0700 Subject: [PATCH 2/3] Fix MySQL conversion for embedded model instance --- lib/mysql.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 53b41a8..319f3d7 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -237,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); @@ -267,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; } @@ -296,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') { @@ -345,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()); @@ -365,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]) { From 95442c85072bec622db3349b44604c03ac6110df Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 20 Aug 2014 14:39:59 -0700 Subject: [PATCH 3/3] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f0396c9..0692413 100644 --- a/package.json +++ b/package.json @@ -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": {