@axiosleo/orm-mysql 0.9.11-alpha.3 → 0.9.11
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 +1 -1
- package/index.d.ts +16 -12
- package/package.json +1 -1
- package/src/builder.js +19 -4
- package/src/operator.js +36 -4
- package/src/query.js +10 -4
package/bin/orm-mysql.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -92,6 +92,8 @@ export declare class Query {
|
|
|
92
92
|
|
|
93
93
|
tables(...tables: TableOption[]): this;
|
|
94
94
|
|
|
95
|
+
keys(...keys: string[]): this;
|
|
96
|
+
|
|
95
97
|
limit(limit: number): this;
|
|
96
98
|
|
|
97
99
|
offset(offset: number): this;
|
|
@@ -145,24 +147,25 @@ export declare class QueryOperator extends Query {
|
|
|
145
147
|
|
|
146
148
|
find<T>(): Promise<T>;
|
|
147
149
|
|
|
148
|
-
|
|
150
|
+
count(): Promise<number>;
|
|
149
151
|
|
|
150
|
-
|
|
152
|
+
delete(id?: number, index_field_name?: string): Promise<MySQLQueryResult>;
|
|
151
153
|
|
|
152
|
-
|
|
154
|
+
update(row?: any): Promise<MySQLQueryResult>;
|
|
153
155
|
|
|
154
|
-
|
|
156
|
+
update<T extends Object>(row?: T): Promise<MySQLQueryResult>;
|
|
155
157
|
|
|
156
|
-
|
|
158
|
+
insert(row?: any): Promise<MySQLQueryResult>;
|
|
157
159
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
160
|
+
insert<T extends Object>(row?: T): Promise<MySQLQueryResult>;
|
|
161
|
+
|
|
162
|
+
insertAll(rows: any[]): Promise<MySQLQueryResult[]>;
|
|
163
|
+
|
|
164
|
+
insertAll<T extends Object>(rows: T[]): Promise<MySQLQueryResult[]>;
|
|
165
|
+
|
|
166
|
+
upsertRow(row: any, ...conditions: WhereItem[]): Promise<MySQLQueryResult>;
|
|
164
167
|
|
|
165
|
-
upsertRow<T extends Object>(
|
|
168
|
+
upsertRow<T extends Object>(row: T, ...conditions: WhereItem[]): Promise<MySQLQueryResult>;
|
|
166
169
|
}
|
|
167
170
|
|
|
168
171
|
export declare class QueryHandler {
|
|
@@ -192,6 +195,7 @@ export declare class QueryHandler {
|
|
|
192
195
|
|
|
193
196
|
/**
|
|
194
197
|
* insert or update
|
|
198
|
+
* @deprecated
|
|
195
199
|
* @param tableName
|
|
196
200
|
* @param data
|
|
197
201
|
* @param condition
|
package/package.json
CHANGED
package/src/builder.js
CHANGED
|
@@ -68,9 +68,12 @@ class Builder {
|
|
|
68
68
|
break;
|
|
69
69
|
}
|
|
70
70
|
case 'insert': {
|
|
71
|
-
const fields = this._buildValues(options.data);
|
|
71
|
+
const { fields, sqlStr } = this._buildValues(options.data);
|
|
72
72
|
emit(tmp, `INSERT INTO ${this._buildTables(options.tables)}(${fields.map((f) => `\`${f}\``).join(',')})`);
|
|
73
|
-
emit(tmp, `VALUES
|
|
73
|
+
emit(tmp, `VALUES ${sqlStr}`);
|
|
74
|
+
if (options.keys) {
|
|
75
|
+
emit(tmp, `ON DUPLICATE KEY UPDATE ${options.keys.map((f) => `\`${f}\` = VALUES(\`${f}\`)`).join(',')}`);
|
|
76
|
+
}
|
|
74
77
|
sql = tmp.join(' ');
|
|
75
78
|
break;
|
|
76
79
|
}
|
|
@@ -78,7 +81,7 @@ class Builder {
|
|
|
78
81
|
if (is.invalid(options.data)) {
|
|
79
82
|
throw new Error('Data is required for update operation');
|
|
80
83
|
}
|
|
81
|
-
const fields = this.
|
|
84
|
+
const fields = this._buildValue(options.data);
|
|
82
85
|
emit(tmp, `UPDATE ${this._buildTables(options.tables)}`);
|
|
83
86
|
emit(tmp, `SET ${fields.map((f) => `\`${f}\` = ?`).join(',')}`);
|
|
84
87
|
if (!options.conditions.length) {
|
|
@@ -210,7 +213,7 @@ class Builder {
|
|
|
210
213
|
return sql;
|
|
211
214
|
}
|
|
212
215
|
|
|
213
|
-
|
|
216
|
+
_buildValue(obj) {
|
|
214
217
|
const fields = [];
|
|
215
218
|
Object.keys(obj).forEach((key) => {
|
|
216
219
|
fields.push(`${key}`);
|
|
@@ -225,6 +228,18 @@ class Builder {
|
|
|
225
228
|
return fields;
|
|
226
229
|
}
|
|
227
230
|
|
|
231
|
+
_buildValues(value) {
|
|
232
|
+
let fields = [];
|
|
233
|
+
if (is.array(value)) {
|
|
234
|
+
[fields] = value.map((v) => this._buildValue(v));
|
|
235
|
+
let item = '(' + fields.map(f => '?').join(',') + ')';
|
|
236
|
+
return { fields, sqlStr: new Array(value.length).fill(item).join(',') };
|
|
237
|
+
}
|
|
238
|
+
fields = this._buildValue(value);
|
|
239
|
+
|
|
240
|
+
return { fields, sqlStr: '(' + fields.map(f => '?').join(',') + ')' };
|
|
241
|
+
}
|
|
242
|
+
|
|
228
243
|
_buildConditionValues(val) {
|
|
229
244
|
if (is.string(val)) {
|
|
230
245
|
if (val.startsWith('`') && val.endsWith('`')) {
|
package/src/operator.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const os = require('os');
|
|
3
4
|
const { Builder } = require('./builder');
|
|
4
5
|
const Query = require('./query');
|
|
5
6
|
const Hook = require('./hook');
|
|
6
7
|
const { _query } = require('./core');
|
|
8
|
+
const { printer } = require('@axiosleo/cli-tool');
|
|
9
|
+
const is = require('@axiosleo/cli-tool/src/helper/is');
|
|
7
10
|
|
|
8
11
|
class QueryOperator extends Query {
|
|
9
12
|
/**
|
|
@@ -56,6 +59,18 @@ class QueryOperator extends Query {
|
|
|
56
59
|
}
|
|
57
60
|
Hook.listen({ label: 'post', table: from, opt: this.options.operator }, this.options, res);
|
|
58
61
|
} catch (err) {
|
|
62
|
+
const e = new Error();
|
|
63
|
+
let f = e.stack.split(os.EOL).find(s =>
|
|
64
|
+
!s.startsWith('Error') &&
|
|
65
|
+
s.indexOf('QueryOperator') < 0 &&
|
|
66
|
+
s.indexOf('node:internal') < 0
|
|
67
|
+
);
|
|
68
|
+
if (f) {
|
|
69
|
+
printer.println();
|
|
70
|
+
printer.print('[MySQL] error : '.data).print(f.trim().warning).println();
|
|
71
|
+
printer.print('[MySQL] message: '.data).print(err.message.error).println();
|
|
72
|
+
printer.print('[MySQL] query : '.data).print(err.sql).println().println();
|
|
73
|
+
}
|
|
59
74
|
Hook.listen({ label: 'post', table: from, opt: this.options.operator }, this.options, err);
|
|
60
75
|
throw err;
|
|
61
76
|
}
|
|
@@ -83,11 +98,28 @@ class QueryOperator extends Query {
|
|
|
83
98
|
return await this.exec();
|
|
84
99
|
}
|
|
85
100
|
|
|
86
|
-
async insert(data) {
|
|
101
|
+
async insert(data, keys = null) {
|
|
102
|
+
if (keys) {
|
|
103
|
+
this.options.onDuplicateKeys = keys;
|
|
104
|
+
}
|
|
87
105
|
this.options.operator = 'insert';
|
|
88
|
-
if (
|
|
106
|
+
if (!is.empty(data)) {
|
|
89
107
|
this.set(data);
|
|
90
108
|
}
|
|
109
|
+
if (!is.object(this.options.data)) {
|
|
110
|
+
throw new Error('data must be an object');
|
|
111
|
+
}
|
|
112
|
+
return await this.exec();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async insertAll(rows = []) {
|
|
116
|
+
this.options.operator = 'insert';
|
|
117
|
+
if (rows.length) {
|
|
118
|
+
this.options.data = rows;
|
|
119
|
+
}
|
|
120
|
+
if (!is.array(this.options.data)) {
|
|
121
|
+
throw new Error('data must be an array');
|
|
122
|
+
}
|
|
91
123
|
return await this.exec();
|
|
92
124
|
}
|
|
93
125
|
|
|
@@ -118,11 +150,11 @@ class QueryOperator extends Query {
|
|
|
118
150
|
.whereConditions(...conditions)
|
|
119
151
|
.count();
|
|
120
152
|
if (count) {
|
|
121
|
-
return await query
|
|
153
|
+
return await query
|
|
122
154
|
.whereConditions(...conditions)
|
|
123
155
|
.update(data);
|
|
124
156
|
}
|
|
125
|
-
return await query.
|
|
157
|
+
return await query.insert(data);
|
|
126
158
|
}
|
|
127
159
|
}
|
|
128
160
|
|
package/src/query.js
CHANGED
|
@@ -31,7 +31,8 @@ class Query {
|
|
|
31
31
|
having: [],
|
|
32
32
|
joins: [],
|
|
33
33
|
suffix: null,
|
|
34
|
-
transaction: false
|
|
34
|
+
transaction: false,
|
|
35
|
+
keys: null,
|
|
35
36
|
};
|
|
36
37
|
this.alias = alias || null;
|
|
37
38
|
}
|
|
@@ -46,6 +47,11 @@ class Query {
|
|
|
46
47
|
return this;
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
keys(...keys) {
|
|
51
|
+
this.options.keys = keys;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
|
|
49
55
|
limit(limit) {
|
|
50
56
|
this.options.pageLimit = limit;
|
|
51
57
|
return this;
|
|
@@ -212,10 +218,10 @@ class Query {
|
|
|
212
218
|
}
|
|
213
219
|
|
|
214
220
|
set(data) {
|
|
215
|
-
if (
|
|
216
|
-
|
|
221
|
+
if (is.invalid(data)) {
|
|
222
|
+
throw new Error('data is required');
|
|
217
223
|
}
|
|
218
|
-
|
|
224
|
+
this.options.data = data;
|
|
219
225
|
return this;
|
|
220
226
|
}
|
|
221
227
|
|