@axiosleo/orm-mysql 0.9.2 → 0.9.4

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.2',
12
+ version: '0.9.4',
13
13
  commands_dir: path.join(__dirname, '../commands'),
14
14
  });
15
15
 
package/index.d.ts CHANGED
@@ -53,9 +53,12 @@ export type QueryOperatorBaseOptions = {
53
53
  queryHandler?: QueryHandler;
54
54
  };
55
55
 
56
+ export type AttrSubQuery = () => Query;
57
+ export type Attr = string | AttrSubQuery;
58
+
56
59
  export type QueryOperatorOptions = QueryOperatorBaseOptions & {
57
60
  conditions: WhereOptions[];
58
- attrs?: string[] | null;
61
+ attrs?: Attr[] | null;
59
62
  orders: OrderByOptions[];
60
63
  pageLimit?: number;
61
64
  pageOffset?: number;
@@ -70,7 +73,9 @@ export type QueryOperatorOptions = QueryOperatorBaseOptions & {
70
73
  }
71
74
 
72
75
  export declare class Query {
73
- constructor(operator?: OperatorType);
76
+ options: QueryOperatorOptions;
77
+
78
+ constructor(operator?: OperatorType, alias?: string | null);
74
79
 
75
80
  table(tableName: string, alias: string | null): this;
76
81
 
@@ -90,7 +95,7 @@ export declare class Query {
90
95
 
91
96
  andWhere(key: string | null, opt: OptType, value: ConditionValueType | WhereOptions[]): this;
92
97
 
93
- attr(...attr: string[]): this;
98
+ attr(...attr: Attr[]): this;
94
99
 
95
100
  orderBy(sortField: string, sortOrder: 'asc' | 'desc'): this;
96
101
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiosleo/orm-mysql",
3
- "version": "0.9.2",
3
+ "version": "0.9.4",
4
4
  "description": "MySQL ORM tool",
5
5
  "keywords": [
6
6
  "mysql",
package/src/builder.js CHANGED
@@ -32,7 +32,21 @@ class Builder {
32
32
  }
33
33
  // eslint-disable-next-line no-fallthrough
34
34
  case 'select': {
35
- emit(tmp, `SELECT ${options.attrs ? options.attrs.map((a) => this._buildFieldKey(a)).join(',') : '*'} FROM ${this._buildTables(options.tables)}`);
35
+ options.attrs = options.attrs || [];
36
+ const attrs = options.attrs.map((attr) => {
37
+ if (attr instanceof Function) {
38
+ const query = attr();
39
+ const builder = new Builder(query.options);
40
+ let s = `(${builder.sql})`;
41
+ if (query.alias) {
42
+ return query.alias.indexOf(' ') > -1 ? s + ' ' + this._buildFieldKey(query.alias)
43
+ : s + ' AS ' + this._buildFieldKey(query.alias);
44
+ }
45
+ return s;
46
+ }
47
+ return attr;
48
+ });
49
+ emit(tmp, `SELECT ${attrs.length ? attrs.map((a) => this._buildFieldKey(a)).join(',') : '*'} FROM ${this._buildTables(options.tables)}`);
36
50
  emit(tmp, this._buildJoins(options.joins));
37
51
  emit(tmp, this._buildContidion(options.conditions));
38
52
  emit(tmp, this._buildOrders(options.orders));
@@ -199,6 +213,11 @@ class Builder {
199
213
  }
200
214
 
201
215
  _buildConditionValues(val) {
216
+ if (is.string(val)) {
217
+ if (val.startsWith('`') && val.endsWith('`')) {
218
+ return val;
219
+ }
220
+ }
202
221
  if (val instanceof Query) {
203
222
  const builder = new Builder(val.options);
204
223
  this.values = this.values.concat(builder.values);
@@ -263,7 +282,13 @@ class Builder {
263
282
  return `(${this._buildContidion(c.value, '')})`;
264
283
  }
265
284
  let res = this._buildConditionValues(c.value);
266
- return res ? `${this._buildFieldKey(c.key)} ${c.opt} (${res})` : `${this._buildFieldKey(c.key)} ${c.opt} ?`;
285
+ if (!is.empty(res)) {
286
+ if (res.startsWith('`') && res.endsWith('`')) {
287
+ return `${this._buildFieldKey(c.key)} ${c.opt} ${res}`;
288
+ }
289
+ return `${this._buildFieldKey(c.key)} ${c.opt} (${res})`;
290
+ }
291
+ return `${this._buildFieldKey(c.key)} ${c.opt} ?`;
267
292
  }).join('')}`;
268
293
  }
269
294
  return sql;
package/src/query.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  class Query {
4
- constructor(operator = 'select') {
4
+ constructor(operator = 'select', alias = null) {
5
5
  this.options = {
6
6
  driver: 'mysql',
7
7
  queryHandler: null,
@@ -16,6 +16,7 @@ class Query {
16
16
  suffix: null,
17
17
  transaction: false
18
18
  };
19
+ this.alias = alias || null;
19
20
  }
20
21
 
21
22
  table(tableName, alias) {