Merge branch 'release/1.4.3' into production

This commit is contained in:
Raymond Feng 2014-08-20 15:07:42 -07:00
commit c77b966949
2 changed files with 19 additions and 8 deletions

View File

@ -57,6 +57,9 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
// Take other options for mysql driver // Take other options for mysql driver
// See https://github.com/strongloop/loopback-connector-mysql/issues/46 // See https://github.com/strongloop/loopback-connector-mysql/issues/46
for (var p in s) { for (var p in s) {
if (p === 'database' && s.createDatabase) {
continue;
}
if (options[p] === undefined) { if (options[p] === undefined) {
options[p] = s[p]; options[p] = s[p];
} }
@ -149,7 +152,7 @@ MySQL.prototype.query = function (sql, callback) {
connection.query(sql, function (err, data) { connection.query(sql, function (err, data) {
if (debugEnabled) { if (debugEnabled) {
if (err) { if (err) {
console.error('Error: ', err); console.error('Error: %j', err);
} }
debug('Data: ', data); debug('Data: ', data);
} }
@ -234,7 +237,7 @@ MySQL.prototype.updateOrCreate = MySQL.prototype.save = function (model, data, c
if (props[key] || mysql.id(model, key)) { if (props[key] || mysql.id(model, key)) {
var k = mysql.columnEscaped(model, key); var k = mysql.columnEscaped(model, key);
var v; var v;
v = mysql.toDatabase(props[key], data[key]); v = mysql.toDatabase(props[key], data[key], true);
if (v !== undefined) { if (v !== undefined) {
fieldsNames.push(k); fieldsNames.push(k);
fieldValues.push(v); fieldValues.push(v);
@ -264,7 +267,7 @@ 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]) {
var value = this.toDatabase(props[key], data[key]); var value = this.toDatabase(props[key], data[key], true);
if (undefined === value) { if (undefined === value) {
return; return;
} }
@ -293,11 +296,11 @@ function dateToMysql(val) {
* @param val * @param val
* @returns {*} * @returns {*}
*/ */
MySQL.prototype.toDatabase = function (prop, val) { MySQL.prototype.toDatabase = function (prop, val, forCreate) {
if (val === null || val === undefined) { if (val === null || val === undefined) {
return 'NULL'; return 'NULL';
} }
if (val.constructor.name === 'Object') { if (!forCreate && val.constructor.name === 'Object') {
var operator = Object.keys(val)[0] var operator = Object.keys(val)[0]
val = val[operator]; val = val[operator];
if (operator === 'between') { if (operator === 'between') {
@ -342,6 +345,10 @@ MySQL.prototype.toDatabase = function (prop, val) {
return this.client.escape(val); return this.client.escape(val);
} }
if (typeof prop.type === 'function') { 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(prop.type(val));
} }
return this.client.escape(val.toString()); return this.client.escape(val.toString());
@ -362,7 +369,11 @@ MySQL.prototype.fromDatabase = function (model, data) {
for (var p in props) { for (var p in props) {
var key = this.column(model, p); var key = this.column(model, p);
var val = data[key]; var val = data[key];
if (typeof val === 'undefined' || val === null) { if (val === undefined) {
continue;
}
if (val === null) {
json[p] = null;
continue; continue;
} }
if (props[p]) { if (props[p]) {
@ -655,7 +666,7 @@ MySQL.prototype.autoupdate = function (models, cb) {
function done(err) { function done(err) {
if (err) { if (err) {
console.error(err); console.error('%j', err);
} }
if (--wait === 0 && cb) { if (--wait === 0 && cb) {
cb(); cb();

View File

@ -1,6 +1,6 @@
{ {
"name": "loopback-connector-mysql", "name": "loopback-connector-mysql",
"version": "1.4.2", "version": "1.4.3",
"description": "MySQL connector for loopback-datasource-juggler", "description": "MySQL connector for loopback-datasource-juggler",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {