@axiosleo/orm-mysql 0.9.9 → 0.9.11-alpha.1
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/.eslintrc +10 -32
- package/bin/orm-mysql.js +1 -1
- package/index.d.ts +33 -8
- package/package.json +1 -1
- package/src/builder.js +1 -1
- package/src/operator.js +19 -2
- package/src/query.js +30 -4
package/.eslintrc
CHANGED
|
@@ -7,22 +7,10 @@
|
|
|
7
7
|
"SwitchCase": 1
|
|
8
8
|
}
|
|
9
9
|
],
|
|
10
|
-
"quotes": [
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
],
|
|
14
|
-
"linebreak-style": [
|
|
15
|
-
0,
|
|
16
|
-
"unix"
|
|
17
|
-
],
|
|
18
|
-
"semi": [
|
|
19
|
-
2,
|
|
20
|
-
"always"
|
|
21
|
-
],
|
|
22
|
-
"strict": [
|
|
23
|
-
2,
|
|
24
|
-
"global"
|
|
25
|
-
],
|
|
10
|
+
"quotes": [2, "single"],
|
|
11
|
+
"linebreak-style": [0, "unix"],
|
|
12
|
+
"semi": [2, "always"],
|
|
13
|
+
"strict": [2, "global"],
|
|
26
14
|
"curly": 2,
|
|
27
15
|
"eqeqeq": 2,
|
|
28
16
|
"no-eval": 2,
|
|
@@ -65,16 +53,9 @@
|
|
|
65
53
|
"afterColon": true
|
|
66
54
|
}
|
|
67
55
|
],
|
|
68
|
-
"eol-last": [
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
],
|
|
72
|
-
"no-inner-declarations": [
|
|
73
|
-
1
|
|
74
|
-
],
|
|
75
|
-
"no-case-declarations": [
|
|
76
|
-
1
|
|
77
|
-
],
|
|
56
|
+
"eol-last": [2, "always"],
|
|
57
|
+
"no-inner-declarations": [1],
|
|
58
|
+
"no-case-declarations": [1],
|
|
78
59
|
"no-multiple-empty-lines": [
|
|
79
60
|
2,
|
|
80
61
|
{
|
|
@@ -82,10 +63,7 @@
|
|
|
82
63
|
"maxBOF": 1
|
|
83
64
|
}
|
|
84
65
|
],
|
|
85
|
-
"space-in-parens": [
|
|
86
|
-
2,
|
|
87
|
-
"never"
|
|
88
|
-
],
|
|
66
|
+
"space-in-parens": [2, "never"],
|
|
89
67
|
"no-multi-spaces": [
|
|
90
68
|
2,
|
|
91
69
|
{
|
|
@@ -107,9 +85,9 @@
|
|
|
107
85
|
"afterEach": true
|
|
108
86
|
},
|
|
109
87
|
"parserOptions": {
|
|
110
|
-
"ecmaVersion":
|
|
88
|
+
"ecmaVersion": 2020,
|
|
111
89
|
"sourceType": "script",
|
|
112
90
|
"ecmaFeatures": {}
|
|
113
91
|
},
|
|
114
92
|
"extends": "eslint:recommended"
|
|
115
|
-
}
|
|
93
|
+
}
|
package/bin/orm-mysql.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -49,8 +49,8 @@ export interface JoinOption {
|
|
|
49
49
|
on?: string;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
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(
|
|
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:
|
|
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[]
|
|
117
|
+
attr(...attr: Attr[]): this;
|
|
105
118
|
|
|
106
119
|
orderBy(sortField: string, sortOrder: 'asc' | 'desc'): this;
|
|
107
120
|
|
|
@@ -134,14 +147,18 @@ export declare class QueryOperator extends Query {
|
|
|
134
147
|
|
|
135
148
|
exec(): Promise<QueryResult>;
|
|
136
149
|
|
|
137
|
-
select<T>(): Promise<T[]>;
|
|
150
|
+
select<T>(...attrs: string[]): Promise<T[]>;
|
|
138
151
|
|
|
139
152
|
find<T>(): Promise<T>;
|
|
140
153
|
|
|
141
154
|
update(data?: any): Promise<MySQLQueryResult>;
|
|
142
155
|
|
|
156
|
+
update<T extends Object>(data?: T): Promise<MySQLQueryResult>;
|
|
157
|
+
|
|
143
158
|
insert(data?: any): Promise<MySQLQueryResult>;
|
|
144
159
|
|
|
160
|
+
insert<T extends Object>(data?: T): Promise<MySQLQueryResult>;
|
|
161
|
+
|
|
145
162
|
count(): Promise<number>;
|
|
146
163
|
|
|
147
164
|
/**
|
|
@@ -150,6 +167,8 @@ export declare class QueryOperator extends Query {
|
|
|
150
167
|
* @param index_field_name default is 'id'
|
|
151
168
|
*/
|
|
152
169
|
delete(id?: number, index_field_name?: string): Promise<MySQLQueryResult>;
|
|
170
|
+
|
|
171
|
+
upsertRow<T extends Object>(data: T, ...conditions: WhereOptions[]): Promise<MySQLQueryResult>;
|
|
153
172
|
}
|
|
154
173
|
|
|
155
174
|
export declare class QueryHandler {
|
|
@@ -165,6 +184,12 @@ export declare class QueryHandler {
|
|
|
165
184
|
*/
|
|
166
185
|
table(table: string, alias?: string | null): QueryOperator;
|
|
167
186
|
|
|
187
|
+
/**
|
|
188
|
+
* select tables
|
|
189
|
+
* @param tables
|
|
190
|
+
*/
|
|
191
|
+
tables(...tables: TableOption[]): QueryOperator;
|
|
192
|
+
|
|
168
193
|
/**
|
|
169
194
|
* execute sql
|
|
170
195
|
* @param options
|
|
@@ -240,7 +265,7 @@ export declare class Hook {
|
|
|
240
265
|
*/
|
|
241
266
|
static pre: (
|
|
242
267
|
callback: (options: QueryOperatorOptions) => void,
|
|
243
|
-
option
|
|
268
|
+
option?: { table?: string, opt?: OperatorType }
|
|
244
269
|
) => string;
|
|
245
270
|
|
|
246
271
|
/**
|
|
@@ -248,7 +273,7 @@ export declare class Hook {
|
|
|
248
273
|
*/
|
|
249
274
|
static post: (
|
|
250
275
|
callback: (options: QueryOperatorOptions, result: QueryResult | Error) => void,
|
|
251
|
-
option
|
|
276
|
+
option?: { table?: string, opt?: OperatorType }
|
|
252
277
|
) => string;
|
|
253
278
|
|
|
254
279
|
/**
|
package/package.json
CHANGED
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.
|
|
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
|
@@ -62,7 +62,10 @@ class QueryOperator extends Query {
|
|
|
62
62
|
return res;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
async select() {
|
|
65
|
+
async select(...attrs) {
|
|
66
|
+
if (attrs.length > 0) {
|
|
67
|
+
this.options.attrs = attrs;
|
|
68
|
+
}
|
|
66
69
|
this.options.operator = 'select';
|
|
67
70
|
return await this.exec();
|
|
68
71
|
}
|
|
@@ -82,7 +85,7 @@ class QueryOperator extends Query {
|
|
|
82
85
|
|
|
83
86
|
async insert(data) {
|
|
84
87
|
this.options.operator = 'insert';
|
|
85
|
-
if (data) {
|
|
88
|
+
if (typeof data !== 'undefined') {
|
|
86
89
|
this.set(data);
|
|
87
90
|
}
|
|
88
91
|
return await this.exec();
|
|
@@ -100,6 +103,15 @@ class QueryOperator extends Query {
|
|
|
100
103
|
this.options.operator = 'delete';
|
|
101
104
|
return await this.exec();
|
|
102
105
|
}
|
|
106
|
+
|
|
107
|
+
async upsertRow(data, condition = []) {
|
|
108
|
+
const query = new QueryOperator(this.conn, this.options);
|
|
109
|
+
const count = await query.whereConditions(...condition).count();
|
|
110
|
+
if (count) {
|
|
111
|
+
return await query.whereConditions(...condition).update(data);
|
|
112
|
+
}
|
|
113
|
+
return await query.insert(data);
|
|
114
|
+
}
|
|
103
115
|
}
|
|
104
116
|
|
|
105
117
|
class QueryHandler {
|
|
@@ -124,6 +136,11 @@ class QueryHandler {
|
|
|
124
136
|
return (new QueryOperator(this.conn, this.options)).table(table, alias);
|
|
125
137
|
}
|
|
126
138
|
|
|
139
|
+
tables(...tables) {
|
|
140
|
+
const operator = new QueryOperator(this.conn, this.options);
|
|
141
|
+
return operator.tables(...tables);
|
|
142
|
+
}
|
|
143
|
+
|
|
127
144
|
async upsert(tableName, data, condition = {}) {
|
|
128
145
|
const count = await this.table(tableName).whereObject(condition).count();
|
|
129
146
|
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(
|
|
39
|
-
this.options.tables.push({
|
|
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
|
-
|
|
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;
|
|
@@ -139,7 +165,7 @@ class Query {
|
|
|
139
165
|
}
|
|
140
166
|
|
|
141
167
|
groupBy(...groupField) {
|
|
142
|
-
this.options.groupField
|
|
168
|
+
this.options.groupField = groupField;
|
|
143
169
|
return this;
|
|
144
170
|
}
|
|
145
171
|
|