Merge branch 'release/1.2.1' into production
This commit is contained in:
commit
1c8bfa5af6
33
lib/mysql.js
33
lib/mysql.js
|
@ -213,11 +213,7 @@ MySQL.prototype.updateOrCreate = function (model, data, callback) {
|
||||||
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;
|
||||||
if (!mysql.id(model, key)) {
|
v = mysql.toDatabase(props[key], data[key]);
|
||||||
v = mysql.toDatabase(props[key], data[key]);
|
|
||||||
} else {
|
|
||||||
v = data[key];
|
|
||||||
}
|
|
||||||
if (v !== undefined) {
|
if (v !== undefined) {
|
||||||
fieldsNames.push(k);
|
fieldsNames.push(k);
|
||||||
fieldValues.push(v);
|
fieldValues.push(v);
|
||||||
|
@ -392,11 +388,27 @@ MySQL.prototype.getColumns = function (model, props) {
|
||||||
return names.join(', ');
|
return names.join(', ');
|
||||||
};
|
};
|
||||||
|
|
||||||
function buildWhere(self, model, conds) {
|
MySQL.prototype.buildWhere = function (model, conds) {
|
||||||
|
var where = this._buildWhere(model, conds);
|
||||||
|
return where? 'WHERE ' + where : '';
|
||||||
|
};
|
||||||
|
|
||||||
|
MySQL.prototype._buildWhere = function (model, conds) {
|
||||||
|
var self = this;
|
||||||
var props = self._models[model].properties;
|
var props = self._models[model].properties;
|
||||||
|
|
||||||
var cs = [];
|
var cs = [];
|
||||||
Object.keys(conds).forEach(function (key) {
|
Object.keys(conds).forEach(function (key) {
|
||||||
|
if (key === 'and' || key === 'or') {
|
||||||
|
var clauses = conds[key];
|
||||||
|
if (Array.isArray(clauses)) {
|
||||||
|
clauses = clauses.map(function (c) {
|
||||||
|
return '(' + self._buildWhere(model, c) + ')';
|
||||||
|
});
|
||||||
|
return cs.push(clauses.join(' ' + key.toUpperCase() + ' '));
|
||||||
|
}
|
||||||
|
// The value is not an array, fall back to regular fields
|
||||||
|
}
|
||||||
var keyEscaped = self.columnEscaped(model, key);
|
var keyEscaped = self.columnEscaped(model, key);
|
||||||
var val = self.toDatabase(props[key], conds[key]);
|
var val = self.toDatabase(props[key], conds[key]);
|
||||||
if (conds[key] === null || conds[key] === undefined) {
|
if (conds[key] === null || conds[key] === undefined) {
|
||||||
|
@ -443,7 +455,7 @@ function buildWhere(self, model, conds) {
|
||||||
if (cs.length === 0) {
|
if (cs.length === 0) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
return 'WHERE ' + cs.join(' AND ');
|
return cs.join(' AND ');
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildOrderBy(self, model, order) {
|
function buildOrderBy(self, model, order) {
|
||||||
|
@ -486,7 +498,7 @@ MySQL.prototype.all = function all(model, filter, callback) {
|
||||||
if (filter) {
|
if (filter) {
|
||||||
|
|
||||||
if (filter.where) {
|
if (filter.where) {
|
||||||
sql += ' ' + buildWhere(self, model, filter.where);
|
sql += ' ' + self.buildWhere(model, filter.where);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filter.order) {
|
if (filter.order) {
|
||||||
|
@ -532,8 +544,9 @@ MySQL.prototype.destroyAll = function destroyAll(model, where, callback) {
|
||||||
where = undefined;
|
where = undefined;
|
||||||
}
|
}
|
||||||
this.query('DELETE FROM '
|
this.query('DELETE FROM '
|
||||||
+ this.tableEscaped(model) + ' ' + buildWhere(this, model, where || {}), function (err, data) {
|
+ this.tableEscaped(model) + ' ' + this.buildWhere(model, where || {}),
|
||||||
callback && callback(err, data);
|
function (err, data) {
|
||||||
|
callback && callback(err, data);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
{
|
{
|
||||||
"name": "loopback-connector-mysql",
|
"name": "loopback-connector-mysql",
|
||||||
"version": "1.2.0",
|
"version": "1.2.1",
|
||||||
"description": "MySQL connector for loopback-datasource-juggler",
|
"description": "MySQL connector for loopback-datasource-juggler",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha"
|
"test": "mocha"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"mysql": "~2.1.1",
|
"mysql": "~2.3.0",
|
||||||
"async": "~0.7.0",
|
"async": "~0.9.0",
|
||||||
"debug": "~0.8.0"
|
"debug": "~0.8.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
Loading…
Reference in New Issue