@axiosleo/orm-mysql 0.9.13 → 0.9.15

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/bin/orm-mysql.js CHANGED
@@ -9,7 +9,7 @@ const app = new App({
9
9
  name: 'MySQL ORM CLI',
10
10
  desc: 'migrate, model, seed, etc.',
11
11
  bin: 'orm-mysql',
12
- version: '0.9.13',
12
+ version: '0.9.15',
13
13
  commands_dir: path.join(__dirname, '../commands'),
14
14
  });
15
15
 
package/index.d.ts CHANGED
@@ -22,8 +22,8 @@ export type Clients = {
22
22
  export type ConditionValueType = null | string | number | boolean | Date | Array<string | number | boolean | Date> | Query;
23
23
 
24
24
  export type OptType = '=' | '!=' | '>' | '<' | '>=' | '<=' |
25
- 'LIKE' | 'NOT LIKE' | 'IN' | 'NOT IN' | 'BETWEEN' | 'NOT BETWEEN' | 'IS' | 'IS NOT' | 'REGEXP' | 'NOT REGEXP' | 'AND' | 'OR' | 'GROUP' |
26
- 'like' | 'not like' | 'in' | 'not in' | 'between' | 'not between' | 'is' | 'is not' | 'regexp' | 'not regexp' | 'and' | 'or' | 'group';
25
+ 'LIKE' | 'NOT LIKE' | 'IN' | 'NOT IN' | 'BETWEEN' | 'NOT BETWEEN' | 'IS' | 'IS NOT' | 'REGEXP' | 'NOT REGEXP' | 'AND' | 'OR' | 'GROUP' | 'CONTAIN' | 'NOT contain' |
26
+ 'like' | 'not like' | 'in' | 'not in' | 'between' | 'not between' | 'is' | 'is not' | 'regexp' | 'not regexp' | 'and' | 'or' | 'group' | 'contain' | 'not contain';
27
27
 
28
28
  export interface WhereOptions {
29
29
  key: string | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiosleo/orm-mysql",
3
- "version": "0.9.13",
3
+ "version": "0.9.15",
4
4
  "description": "MySQL ORM tool",
5
5
  "keywords": [
6
6
  "mysql",
package/src/builder.js CHANGED
@@ -268,10 +268,12 @@ class Builder {
268
268
  } else if (!Array.isArray(condition.value) && !(condition.value instanceof Query)) {
269
269
  throw new Error('Value must be an array or sub-query for "IN" condition');
270
270
  }
271
- if (condition.key.indexOf('$') !== -1) {
271
+ if (condition.key.indexOf('->') !== -1) {
272
+ let keys = condition.key.split('->');
273
+ let k = `${this._buildFieldKey(keys[0])}`;
272
274
  let res = this._buildConditionValues(condition.value);
273
- let sql = res ? `JSON_CONTAINS(JSON_ARRAY(${res}), ${this._buildFieldKey(condition.key)})` :
274
- `JSON_CONTAINS(JSON_ARRAY(?), ${this._buildFieldKey(condition.key)})`;
275
+ let sql = res ? `JSON_CONTAINS(JSON_ARRAY(${res}), JSON_EXTRACT(${k}, '${keys[1]}'))` :
276
+ `JSON_CONTAINS(JSON_ARRAY(?), JSON_EXTRACT(${k}, '${keys[1]}'))`;
275
277
  return isNot ? `${sql}=0` : sql;
276
278
  }
277
279
  let res = this._buildConditionValues(condition.value);
@@ -279,6 +281,20 @@ class Builder {
279
281
  return res ? `${this._buildFieldKey(condition.key)} ${opt} (${res})` : `${this._buildFieldKey(condition.key)} ${opt} (?)`;
280
282
  }
281
283
 
284
+ _buildConditionContain(condition, isNot = false) {
285
+ if (condition.key.indexOf('->') !== -1) {
286
+ let keys = condition.key.split('->');
287
+ let k = `${this._buildFieldKey(keys[0])}`;
288
+ let res = this._buildConditionValues(condition.value);
289
+ let sql = res ? `JSON_CONTAINS(${k}, JSON_ARRAY(${res}), '${keys[1]}')` :
290
+ `JSON_CONTAINS(${k}, JSON_ARRAY(?), '${keys[1]}')`;
291
+ return isNot ? `${sql}=0` : sql;
292
+ }
293
+ let res = this._buildConditionValues(condition.value);
294
+ const opt = isNot ? 'NOT LIKE' : 'LIKE';
295
+ return res ? `${this._buildFieldKey(condition.key)} ${opt} CONCAT('%', ?, '%')` : `${this._buildFieldKey(condition.key)} ${opt} CONCAT('%', ?, '%')`;
296
+ }
297
+
282
298
  _buildCondition(conditions, prefix) {
283
299
  if (!conditions || !conditions.length) {
284
300
  return '';
@@ -286,17 +302,18 @@ class Builder {
286
302
  let sql = typeof prefix === 'undefined' ? 'WHERE ' : prefix;
287
303
  if (conditions.length) {
288
304
  sql += `${conditions.map((c) => {
289
- if (typeof c.key === 'undefined') {
290
- c.key = null;
291
- }
292
- if (typeof c.value === 'undefined') {
293
- c.value = null;
294
- }
295
- if (c.key === null && c.value === null) {
296
- return ` ${c.opt} `;
305
+ const opt = c.opt.toLowerCase();
306
+ if (opt === 'group' && Array.isArray(c.value)) {
307
+ return `(${this._buildCondition(c.value, '')})`;
297
308
  }
298
- if (c.value === null) {
299
- return c.opt === '=' ? `ISNULL(${this._buildFieldKey(c.key)})` : `!ISNULL(${this._buildFieldKey(c.key)})`;
309
+ if (opt === 'in') {
310
+ return this._buildConditionIn(c);
311
+ } else if (opt === 'not in') {
312
+ return this._buildConditionIn(c, true);
313
+ } else if (opt === 'contain') {
314
+ return this._buildConditionContain(c);
315
+ } else if (opt === 'not contain') {
316
+ return this._buildConditionContain(c, true);
300
317
  }
301
318
  if (c.key && c.key.indexOf('->') !== -1) {
302
319
  const keys = c.key.split('->');
@@ -308,13 +325,17 @@ class Builder {
308
325
  }
309
326
  ], '');
310
327
  }
311
- const opt = c.opt.toLowerCase();
312
- if (opt === 'in') {
313
- return this._buildConditionIn(c);
314
- } else if (opt === 'not in') {
315
- return this._buildConditionIn(c, true);
316
- } else if (opt === 'group' && Array.isArray(c.value)) {
317
- return `(${this._buildCondition(c.value, '')})`;
328
+ if (typeof c.key === 'undefined') {
329
+ c.key = null;
330
+ }
331
+ if (typeof c.value === 'undefined') {
332
+ c.value = null;
333
+ }
334
+ if (c.key === null && c.value === null) {
335
+ return ` ${c.opt} `;
336
+ }
337
+ if (c.value === null) {
338
+ return c.opt === '=' ? `ISNULL(${this._buildFieldKey(c.key)})` : `!ISNULL(${this._buildFieldKey(c.key)})`;
318
339
  }
319
340
  let res = this._buildConditionValues(c.value);
320
341
  if (!is.empty(res)) {