@axiosleo/orm-mysql 0.9.9 → 0.9.10

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.9',
12
+ version: '0.9.10',
13
13
  commands_dir: path.join(__dirname, '../commands'),
14
14
  });
15
15
 
package/index.d.ts CHANGED
@@ -49,8 +49,8 @@ export interface JoinOption {
49
49
  on?: string;
50
50
  }
51
51
 
52
- export interface TableOption {
53
- tableName: string;
52
+ interface TableOption {
53
+ table: string;
54
54
  alias: string | null;
55
55
  }
56
56
 
@@ -78,12 +78,25 @@ export type QueryOperatorOptions = QueryOperatorBaseOptions & {
78
78
  transaction: boolean;
79
79
  }
80
80
 
81
+ type GroupWhereOptionsArr = [string | null, OptType, ConditionValueType | WhereOptions[] | null]
82
+ | [string | null, ConditionValueType | WhereOptions[]];
83
+
84
+ type GroupWhereOptions = {
85
+ key?: string | null;
86
+ opt?: OptType;
87
+ value?: ConditionValueType | WhereOptions[] | null;
88
+ }
89
+
90
+ type GroupWhereItem = GroupWhereOptions | string | GroupWhereOptionsArr;
91
+
81
92
  export declare class Query {
82
93
  options: QueryOperatorOptions;
83
94
 
84
95
  constructor(operator?: OperatorType, alias?: string | null);
85
96
 
86
- table(tableName: string, alias: string | null): this;
97
+ table(table: string, alias: string | null): this;
98
+
99
+ tables(...tables: TableOption[]): this;
87
100
 
88
101
  limit(limit: number): this;
89
102
 
@@ -95,7 +108,7 @@ export declare class Query {
95
108
 
96
109
  whereConditions(...condition: WhereOptions[]): this;
97
110
 
98
- groupWhere(...condition: WhereOptions[]): this;
111
+ groupWhere(...condition: GroupWhereItem[]): this;
99
112
 
100
113
  orWhere(key: string | null, opt: OptType, value: ConditionValueType | WhereOptions[]): this;
101
114
 
@@ -165,6 +178,12 @@ export declare class QueryHandler {
165
178
  */
166
179
  table(table: string, alias?: string | null): QueryOperator;
167
180
 
181
+ /**
182
+ * select tables
183
+ * @param tables
184
+ */
185
+ tables(...tables: TableOption[]): QueryOperator;
186
+
168
187
  /**
169
188
  * execute sql
170
189
  * @param options
@@ -240,7 +259,7 @@ export declare class Hook {
240
259
  */
241
260
  static pre: (
242
261
  callback: (options: QueryOperatorOptions) => void,
243
- option: { table?: string, opt?: OperatorType }
262
+ option?: { table?: string, opt?: OperatorType }
244
263
  ) => string;
245
264
 
246
265
  /**
@@ -248,7 +267,7 @@ export declare class Hook {
248
267
  */
249
268
  static post: (
250
269
  callback: (options: QueryOperatorOptions, result: QueryResult | Error) => void,
251
- option: { table?: string, opt?: OperatorType }
270
+ option?: { table?: string, opt?: OperatorType }
252
271
  ) => string;
253
272
 
254
273
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiosleo/orm-mysql",
3
- "version": "0.9.9",
3
+ "version": "0.9.10",
4
4
  "description": "MySQL ORM tool",
5
5
  "keywords": [
6
6
  "mysql",
package/src/builder.js CHANGED
@@ -186,7 +186,7 @@ class Builder {
186
186
  throw new Error('At least one table is required');
187
187
  }
188
188
  return tables.map((t) => {
189
- let name = t.tableName.split('.').map((n) => {
189
+ let name = t.table.split('.').map((n) => {
190
190
  if (n[0] === '`' && n[n.length - 1] === '`') {
191
191
  return n;
192
192
  }
package/src/operator.js CHANGED
@@ -124,6 +124,11 @@ class QueryHandler {
124
124
  return (new QueryOperator(this.conn, this.options)).table(table, alias);
125
125
  }
126
126
 
127
+ tables(...tables) {
128
+ const operator = new QueryOperator(this.conn, this.options);
129
+ return operator.tables(...tables);
130
+ }
131
+
127
132
  async upsert(tableName, data, condition = {}) {
128
133
  const count = await this.table(tableName).whereObject(condition).count();
129
134
  if (count) {
package/src/query.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const { _assign } = require('@axiosleo/cli-tool/src/helper/obj');
4
4
  const { _validate } = require('./utils');
5
+ const is = require('@axiosleo/cli-tool/src/helper/is');
5
6
 
6
7
  function joinOn(table, on, options = {}) {
7
8
  let o = _assign({ alias: null, join_type: 'INNER', table, on }, options);
@@ -35,8 +36,13 @@ class Query {
35
36
  this.alias = alias || null;
36
37
  }
37
38
 
38
- table(tableName, alias) {
39
- this.options.tables.push({ tableName, alias });
39
+ table(table, alias) {
40
+ this.options.tables.push({ table, alias });
41
+ return this;
42
+ }
43
+
44
+ tables(...tables) {
45
+ this.options.tables = tables;
40
46
  return this;
41
47
  }
42
48
 
@@ -94,7 +100,27 @@ class Query {
94
100
  }
95
101
  const condition = { key: null, opt: 'group', value: [] };
96
102
  conditions.forEach((c) => {
97
- condition.value.push(c);
103
+ if (is.string(c)) {
104
+ condition.value.push({ key: null, opt: c, value: null });
105
+ } else if (is.object(c)) {
106
+ condition.value.push({
107
+ key: null,
108
+ opt: '=',
109
+ value: null,
110
+ ...c
111
+ });
112
+ } else if (is.array(c)) {
113
+ const [k, o, v] = c;
114
+ if (c.length === 2) {
115
+ condition.value.push({ key: k, opt: '=', value: o });
116
+ } else if (c.length === 3) {
117
+ condition.value.push({ key: k, opt: o, value: v });
118
+ } else {
119
+ throw new Error('Invalid condition: ' + c);
120
+ }
121
+ } else {
122
+ condition.value.push(c);
123
+ }
98
124
  });
99
125
  this.options.conditions.push(condition);
100
126
  return this;