Skip to content

Commit

Permalink
ignore empty array for embed, conditions and has.
Browse files Browse the repository at this point in the history
  • Loading branch information
jails committed Aug 16, 2021
1 parent 87abb42 commit a58349d
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/query.js
Expand Up @@ -3,6 +3,19 @@ var extend = require('extend-merge').extend;
var merge = require('extend-merge').merge;
var Model = require('chaos-orm').Model;

function isEmpty(value) {
if (!value) {
return true;
}
for (var i in value) {
return false;
}
if (Array.isArray(value) && value.length) {
return false;
}
return true;
}

/**
* The Query wrapper.
*/
Expand Down Expand Up @@ -308,8 +321,10 @@ class Query {
* @return Function Returns `this`.
*/
where(conditions, alias) {
var conditions = this.statement().dialect().prefix(conditions, alias ? alias : this.alias(), false);
this.statement().where(conditions);
if (!isEmpty(conditions)) {
var conditions = this.statement().dialect().prefix(conditions, alias ? alias : this.alias(), false);
this.statement().where(conditions);
}
return this;
}

Expand Down Expand Up @@ -432,14 +447,16 @@ class Query {
if (!arguments.length) {
return this._embed;
}
if (typeof embed === "string" && arguments.length === 2) {
var mix = {};
mix[embed] = conditions || [];
embed = [mix];
} else {
embed = Array.isArray(embed) ? embed : [embed];
if (!isEmpty(embed)) {
if (typeof embed === "string" && arguments.length === 2) {
var mix = {};
mix[embed] = conditions || [];
embed = [mix];
} else {
embed = Array.isArray(embed) ? embed : [embed];
}
this._embed = this._embed.concat(embed);
}
this._embed = this._embed.concat(embed);
return this;
}

Expand All @@ -452,12 +469,14 @@ class Query {
if (!arguments.length) {
return this._has;
}
if (typeof has === 'string') {
var mix = {};
mix[has] = conditions || [];
has = [mix];
if (!isEmpty(has)) {
if (typeof has === 'string') {
var mix = {};
mix[has] = conditions || [];
has = [mix];
}
this._has = this._has.concat(has);
}
this._has = this._has.concat(has)
return this;
}

Expand Down

0 comments on commit a58349d

Please sign in to comment.