@axiosleo/orm-mysql 0.9.8 → 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.8',
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,13 +108,13 @@ 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
 
102
115
  andWhere(key: string | null, opt: OptType, value: ConditionValueType | WhereOptions[]): this;
103
116
 
104
- attr(...attr: Attr[]): this;
117
+ attr(...attr: Attr[] | Attr[][]): this;
105
118
 
106
119
  orderBy(sortField: string, sortOrder: 'asc' | 'desc'): this;
107
120
 
@@ -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.8",
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
  }
@@ -371,9 +371,6 @@ class ManageSQLBuilder extends Builder {
371
371
  if (is.empty(options.columns)) {
372
372
  throw new Error('At least one column is required');
373
373
  }
374
- if (!Object.values(options.columns).find(c => c.primaryKey === true)) {
375
- throw new Error('At least one primary key column is required');
376
- }
377
374
  let columns = Object.keys(options.columns).map(name => {
378
375
  return { name, ...options.columns[name] };
379
376
  });
@@ -550,6 +547,8 @@ class ManageSQLBuilder extends Builder {
550
547
  str += '(11)';
551
548
  } else if (type === 'VARCHAR') {
552
549
  str += '(255)';
550
+ } else if (type === 'TINYINT') {
551
+ str += '(4)';
553
552
  }
554
553
  if (options.allowNull === false) {
555
554
  str += ' NOT NULL';
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
@@ -24,6 +24,7 @@ class Query {
24
24
  conditions: [],
25
25
  orders: [],
26
26
  tables: [],
27
+ attrs: [],
27
28
  operator,
28
29
  data: null,
29
30
  groupField: [],
@@ -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;
@@ -124,18 +150,12 @@ class Query {
124
150
  return this;
125
151
  }
126
152
 
127
- attr(...attr) {
128
- if (attr.length === 1 && is.array(attr)) {
129
- this.options.attrs = attr;
130
- return this;
131
- }
132
- if (!attr.length) {
133
- return this;
134
- }
135
- if (!this.options.attrs) {
153
+ attr(...attrs) {
154
+ if (!attrs.length) {
136
155
  this.options.attrs = [];
156
+ return this;
137
157
  }
138
- this.options.attrs.push(...attr);
158
+ this.options.attrs = attrs;
139
159
  return this;
140
160
  }
141
161