@axiosleo/orm-mysql 0.9.14 → 0.9.16

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.14',
12
+ version: '0.9.16',
13
13
  commands_dir: path.join(__dirname, '../commands'),
14
14
  });
15
15
 
package/index.d.ts CHANGED
@@ -89,7 +89,7 @@ export declare class Query {
89
89
 
90
90
  constructor(operator?: OperatorType, alias?: string | null);
91
91
 
92
- table(table: string, alias: string | null): this;
92
+ table(table: string, alias?: string | null): this;
93
93
 
94
94
  tables(...tables: TableOption[]): this;
95
95
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiosleo/orm-mysql",
3
- "version": "0.9.14",
3
+ "version": "0.9.16",
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);
@@ -280,10 +282,12 @@ class Builder {
280
282
  }
281
283
 
282
284
  _buildConditionContain(condition, isNot = false) {
283
- if (condition.key.indexOf('$') !== -1) {
285
+ if (condition.key.indexOf('->') !== -1) {
286
+ let keys = condition.key.split('->');
287
+ let k = `${this._buildFieldKey(keys[0])}`;
284
288
  let res = this._buildConditionValues(condition.value);
285
- let sql = res ? `JSON_CONTAINS(${this._buildFieldKey(condition.key)}, JSON_ARRAY(${res}))` :
286
- `JSON_CONTAINS(${this._buildFieldKey(condition.key)}, JSON_ARRAY(?))`;
289
+ let sql = res ? `JSON_CONTAINS(${k}, JSON_ARRAY(${res}), '${keys[1]}')` :
290
+ `JSON_CONTAINS(${k}, JSON_ARRAY(?), '${keys[1]}')`;
287
291
  return isNot ? `${sql}=0` : sql;
288
292
  }
289
293
  let res = this._buildConditionValues(condition.value);
@@ -297,18 +301,20 @@ class Builder {
297
301
  }
298
302
  let sql = typeof prefix === 'undefined' ? 'WHERE ' : prefix;
299
303
  if (conditions.length) {
300
- sql += `${conditions.map((c) => {
301
- if (typeof c.key === 'undefined') {
302
- c.key = null;
303
- }
304
- if (typeof c.value === 'undefined') {
305
- c.value = null;
306
- }
307
- if (c.key === null && c.value === null) {
308
- return ` ${c.opt} `;
304
+ sql += `${conditions.map((c, index) => {
305
+ const opt = c.opt.toLowerCase();
306
+ if (opt === 'group' && Array.isArray(c.value)) {
307
+ let t = `(${this._buildCondition(c.value, '')})`;
308
+ return index === 0 ? t : ` AND ${t}`;
309
309
  }
310
- if (c.value === null) {
311
- return c.opt === '=' ? `ISNULL(${this._buildFieldKey(c.key)})` : `!ISNULL(${this._buildFieldKey(c.key)})`;
310
+ if (opt === 'in') {
311
+ return this._buildConditionIn(c);
312
+ } else if (opt === 'not in') {
313
+ return this._buildConditionIn(c, true);
314
+ } else if (opt === 'contain') {
315
+ return this._buildConditionContain(c);
316
+ } else if (opt === 'not contain') {
317
+ return this._buildConditionContain(c, true);
312
318
  }
313
319
  if (c.key && c.key.indexOf('->') !== -1) {
314
320
  const keys = c.key.split('->');
@@ -320,17 +326,17 @@ class Builder {
320
326
  }
321
327
  ], '');
322
328
  }
323
- const opt = c.opt.toLowerCase();
324
- if (opt === 'in') {
325
- return this._buildConditionIn(c);
326
- } else if (opt === 'not in') {
327
- return this._buildConditionIn(c, true);
328
- } else if (opt === 'group' && Array.isArray(c.value)) {
329
- return `(${this._buildCondition(c.value, '')})`;
330
- } else if (opt === 'contain') {
331
- return this._buildConditionContain(c);
332
- } else if (opt === 'not contain') {
333
- return this._buildConditionContain(c, true);
329
+ if (typeof c.key === 'undefined') {
330
+ c.key = null;
331
+ }
332
+ if (typeof c.value === 'undefined') {
333
+ c.value = null;
334
+ }
335
+ if (c.key === null && c.value === null) {
336
+ return ` ${c.opt} `;
337
+ }
338
+ if (c.value === null) {
339
+ return c.opt === '=' ? `ISNULL(${this._buildFieldKey(c.key)})` : `!ISNULL(${this._buildFieldKey(c.key)})`;
334
340
  }
335
341
  let res = this._buildConditionValues(c.value);
336
342
  if (!is.empty(res)) {