@axiosleo/orm-mysql 0.5.1 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiosleo/orm-mysql",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "MySQL ORM tool",
5
5
  "keywords": [
6
6
  "mysql",
package/src/builder.js CHANGED
@@ -192,6 +192,21 @@ class Builder {
192
192
  return null;
193
193
  }
194
194
 
195
+ _buildContidionIn(condition, isNot = false) {
196
+ if (!Array.isArray(condition.value) && !(condition.value instanceof Query)) {
197
+ throw new Error('Value must be an array for "IN" condition');
198
+ }
199
+ if (condition.key.indexOf('$') !== -1) {
200
+ let res = this._buildConditionValues(condition.value);
201
+ let sql = res ? `JSON_CONTAINS(JSON_ARRAY(${res}), ${this._buildFieldKey(condition.key)})` :
202
+ `JSON_CONTAINS(JSON_ARRAY(?), ${this._buildFieldKey(condition.key)})`;
203
+ return isNot ? `${sql}=0` : sql;
204
+ }
205
+ let res = this._buildConditionValues(condition.value);
206
+ const opt = isNot ? 'NOT IN' : 'IN';
207
+ return res ? `${this._buildFieldKey(condition.key)} ${opt} (${res})` : `${this._buildFieldKey(condition.key)} ${opt} (?)`;
208
+ }
209
+
195
210
  _buildContidion(conditions, prefix) {
196
211
  if (!conditions || !conditions.length) {
197
212
  return '';
@@ -216,9 +231,10 @@ class Builder {
216
231
  ], '');
217
232
  }
218
233
  const opt = c.opt.toLowerCase();
219
- if (opt === 'in' && Array.isArray(c.value)) {
220
- let res = this._buildConditionValues(c.value);
221
- return res ? `${this._buildFieldKey(c.key)} IN (${res})` : `${this._buildFieldKey(c.key)} IN (?)`;
234
+ if (opt === 'in') {
235
+ return this._buildContidionIn(c);
236
+ } else if (opt === 'not in') {
237
+ return this._buildContidionIn(c, true);
222
238
  } else if (opt === 'group' && Array.isArray(c.value)) {
223
239
  return `(${this._buildContidion(c.value, '')})`;
224
240
  }
package/src/query.js CHANGED
@@ -57,6 +57,9 @@ class Query {
57
57
  }
58
58
 
59
59
  whereConditions(...condition) {
60
+ if (!condition.length) {
61
+ return this;
62
+ }
60
63
  if (this.options.conditions.length) {
61
64
  this.options.conditions.push({ key: null, opt: 'AND', value: null });
62
65
  }