mm_mysql 1.9.6 → 1.9.7
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/index.js +90 -115
- package/package.json +2 -2
- package/sql.js +1 -1
- package/test.js +26 -4
package/index.js
CHANGED
|
@@ -5,17 +5,14 @@
|
|
|
5
5
|
*/
|
|
6
6
|
const {
|
|
7
7
|
createPool
|
|
8
|
-
} = require('
|
|
8
|
+
} = require('mysql2/promise');
|
|
9
9
|
const {
|
|
10
10
|
DB
|
|
11
11
|
} = require('./db');
|
|
12
12
|
|
|
13
13
|
const Link_model = require('./link_model');
|
|
14
|
-
const {
|
|
15
|
-
table
|
|
16
|
-
} = require('console');
|
|
17
14
|
|
|
18
|
-
var
|
|
15
|
+
var pools = {};
|
|
19
16
|
|
|
20
17
|
/**
|
|
21
18
|
* @description 数据库封装
|
|
@@ -46,8 +43,8 @@ class Mysql {
|
|
|
46
43
|
* sql语句
|
|
47
44
|
*/
|
|
48
45
|
this.sql = "";
|
|
49
|
-
//
|
|
50
|
-
this.
|
|
46
|
+
// 连接池
|
|
47
|
+
this.pool;
|
|
51
48
|
// 连接态 0未连接,1已连接
|
|
52
49
|
this.state = 0;
|
|
53
50
|
|
|
@@ -65,10 +62,15 @@ class Mysql {
|
|
|
65
62
|
database: "mm",
|
|
66
63
|
// 是否支持多个sql语句同时操作
|
|
67
64
|
multipleStatements: false,
|
|
68
|
-
// 打印SQL
|
|
69
|
-
log:
|
|
70
|
-
// 排除打印
|
|
71
|
-
log_ignore: [1062]
|
|
65
|
+
// // 打印SQL
|
|
66
|
+
// log: true,
|
|
67
|
+
// // 排除打印
|
|
68
|
+
// log_ignore: [1062],
|
|
69
|
+
// debug: true,
|
|
70
|
+
// 启用keep-alive,保持连接活跃
|
|
71
|
+
enableKeepAlive: true,
|
|
72
|
+
waitForConnections: true,
|
|
73
|
+
compress: false // 启用压缩
|
|
72
74
|
};
|
|
73
75
|
|
|
74
76
|
// 唯一标识符
|
|
@@ -83,49 +85,29 @@ class Mysql {
|
|
|
83
85
|
* @param {Array} val 替换值
|
|
84
86
|
* @return {Promise|Array} 异步构造器, 当await时返回执行结果
|
|
85
87
|
*/
|
|
86
|
-
this.run = function(sql, val) {
|
|
87
|
-
var _this = this;
|
|
88
|
+
this.run = async function(sql, val) {
|
|
88
89
|
this.sql = sql;
|
|
89
90
|
if ($this.config.log) {
|
|
90
91
|
$.log.debug("SQL:", sql);
|
|
91
92
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
db.release();
|
|
111
|
-
if (err) {
|
|
112
|
-
_this.error = {
|
|
113
|
-
code: err.errno,
|
|
114
|
-
message: err.sqlMessage
|
|
115
|
-
};
|
|
116
|
-
if ($this.config.log_ignore.indexOf(err.errno) === -1) {
|
|
117
|
-
$.log.error("SQL error:", err);
|
|
118
|
-
}
|
|
119
|
-
resolve([]);
|
|
120
|
-
reject(err);
|
|
121
|
-
} else {
|
|
122
|
-
_this.error = undefined;
|
|
123
|
-
resolve(rows);
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
});
|
|
93
|
+
var conn;
|
|
94
|
+
var list = [];
|
|
95
|
+
try {
|
|
96
|
+
// 关于连接池初始化,请参阅上文
|
|
97
|
+
conn = await $this.pool.getConnection();
|
|
98
|
+
const [rows] = await conn.query(sql, val);
|
|
99
|
+
// 查询解析时,连接会自动释放
|
|
100
|
+
list = rows;
|
|
101
|
+
} catch (err) {
|
|
102
|
+
console.error("mysql查询错误", err);
|
|
103
|
+
this.error = {
|
|
104
|
+
code: err.errno,
|
|
105
|
+
message: err.sqlMessage
|
|
106
|
+
};
|
|
107
|
+
} finally {
|
|
108
|
+
if (conn) conn.release();
|
|
109
|
+
}
|
|
110
|
+
return list;
|
|
129
111
|
};
|
|
130
112
|
|
|
131
113
|
/**
|
|
@@ -134,8 +116,7 @@ class Mysql {
|
|
|
134
116
|
* @param {Array} val 替换值
|
|
135
117
|
* @return {Promise|Array} 异步构造器, 当await时返回执行结果
|
|
136
118
|
*/
|
|
137
|
-
this.exec = function(sql, val) {
|
|
138
|
-
var _this = this;
|
|
119
|
+
this.exec = async function(sql, val) {
|
|
139
120
|
if (this.task) {
|
|
140
121
|
this.task_sql += sql + "\r\n";
|
|
141
122
|
if (this.task === 1) {
|
|
@@ -154,64 +135,46 @@ class Mysql {
|
|
|
154
135
|
if ($this.config.log) {
|
|
155
136
|
$.log.debug("SQL:", sql);
|
|
156
137
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
$this.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
138
|
+
var conn;
|
|
139
|
+
var bl = -1;
|
|
140
|
+
try {
|
|
141
|
+
conn = await $this.pool.getConnection();
|
|
142
|
+
var [rows] = await conn.execute(sql, val);
|
|
143
|
+
console.log("执行结果", rows);
|
|
144
|
+
if (rows.constructor == Array) {
|
|
145
|
+
if (rows.length > 0) {
|
|
146
|
+
var num = 0;
|
|
147
|
+
rows.map(function(item) {
|
|
148
|
+
num += item['affectedRows'];
|
|
149
|
+
});
|
|
150
|
+
if (num === 0) {
|
|
151
|
+
bl = rows.length;
|
|
152
|
+
} else {
|
|
153
|
+
bl = num;
|
|
169
154
|
}
|
|
170
|
-
resolve(-1);
|
|
171
|
-
reject(err);
|
|
172
155
|
} else {
|
|
173
|
-
|
|
174
|
-
if (err) {
|
|
175
|
-
_this.error = {
|
|
176
|
-
code: err.errno,
|
|
177
|
-
message: err.sqlMessage
|
|
178
|
-
};
|
|
179
|
-
if ($this.config.log_ignore.indexOf(err.errno) === -1) {
|
|
180
|
-
$.log.error("SQL error:", err);
|
|
181
|
-
}
|
|
182
|
-
resolve(-1);
|
|
183
|
-
reject(err);
|
|
184
|
-
} else {
|
|
185
|
-
_this.error = undefined;
|
|
186
|
-
if (o.constructor == Array) {
|
|
187
|
-
if (o.length > 0) {
|
|
188
|
-
var num = 0;
|
|
189
|
-
o.map(function(item) {
|
|
190
|
-
num += item['affectedRows'];
|
|
191
|
-
});
|
|
192
|
-
if (num === 0) {
|
|
193
|
-
resolve(o.length);
|
|
194
|
-
} else {
|
|
195
|
-
resolve(num);
|
|
196
|
-
}
|
|
197
|
-
} else {
|
|
198
|
-
resolve(1);
|
|
199
|
-
}
|
|
200
|
-
} else {
|
|
201
|
-
var num = o['affectedRows'];
|
|
202
|
-
if (num === 0) {
|
|
203
|
-
resolve(1);
|
|
204
|
-
} else {
|
|
205
|
-
resolve(num);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
// 结束会话
|
|
210
|
-
db.release();
|
|
211
|
-
});
|
|
156
|
+
bl = 1;
|
|
212
157
|
}
|
|
213
|
-
}
|
|
214
|
-
|
|
158
|
+
} else {
|
|
159
|
+
var num = rows['affectedRows'];
|
|
160
|
+
if (num === 0) {
|
|
161
|
+
bl = 1;
|
|
162
|
+
} else {
|
|
163
|
+
bl = num;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// 查询解析时,连接会自动释放
|
|
167
|
+
} catch (err) {
|
|
168
|
+
console.error("mysql执行错误", err);
|
|
169
|
+
this.error = {
|
|
170
|
+
code: err.errno,
|
|
171
|
+
message: err.sqlMessage
|
|
172
|
+
};
|
|
173
|
+
} finally {
|
|
174
|
+
if (conn) conn.release();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return bl;
|
|
215
178
|
};
|
|
216
179
|
|
|
217
180
|
/**
|
|
@@ -223,6 +186,19 @@ class Mysql {
|
|
|
223
186
|
}
|
|
224
187
|
}
|
|
225
188
|
|
|
189
|
+
Mysql.prototype.getConn = async function() {
|
|
190
|
+
let conn;
|
|
191
|
+
try {
|
|
192
|
+
conn = await this.pool.getConnection();
|
|
193
|
+
} catch (err) {
|
|
194
|
+
console.error('Connection failed - retrying: ' + err.message);
|
|
195
|
+
// 重试逻辑,例如等待一段时间后再次尝试获取连接
|
|
196
|
+
await new Promise(resolve => setTimeout(resolve, 5000)); // 等待5秒后重试
|
|
197
|
+
conn = await this.getConn(); // 递归调用直到成功获取连接
|
|
198
|
+
}
|
|
199
|
+
return conn;
|
|
200
|
+
}
|
|
201
|
+
|
|
226
202
|
/**
|
|
227
203
|
* 导入数据库
|
|
228
204
|
* @param {Object} file sql文件
|
|
@@ -301,7 +277,6 @@ Mysql.prototype.save = async function(file, func, tables = []) {
|
|
|
301
277
|
}
|
|
302
278
|
|
|
303
279
|
tableCount = tables.length;
|
|
304
|
-
console.log("tables", tables);
|
|
305
280
|
for (var i = 0; i < tables.length; i++) {
|
|
306
281
|
var tableName = tables[i];
|
|
307
282
|
try {
|
|
@@ -412,21 +387,21 @@ Mysql.prototype.setConfig = function(cg) {
|
|
|
412
387
|
* @param {boolean} 是否重置
|
|
413
388
|
*/
|
|
414
389
|
Mysql.prototype.open = function(reset) {
|
|
415
|
-
if (reset || !this.state || !
|
|
390
|
+
if (reset || !this.state || !pools[this.identifier]) {
|
|
416
391
|
this.state = 1;
|
|
417
|
-
|
|
392
|
+
pools[this.identifier] = createPool(this.config);
|
|
418
393
|
}
|
|
419
|
-
this.
|
|
394
|
+
this.pool = pools[this.identifier];
|
|
420
395
|
};
|
|
421
396
|
|
|
422
397
|
/**
|
|
423
398
|
* @description 关闭连接
|
|
424
399
|
*/
|
|
425
400
|
Mysql.prototype.close = function() {
|
|
426
|
-
if (
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
delete
|
|
401
|
+
if (pools[this.identifier]) {
|
|
402
|
+
pools[this.identifier].end();
|
|
403
|
+
pools[this.identifier] = null;
|
|
404
|
+
delete pools[this.identifier];
|
|
430
405
|
}
|
|
431
406
|
};
|
|
432
407
|
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_mysql",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.7",
|
|
4
4
|
"description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"mm_expand": "^1.8.0",
|
|
8
8
|
"mm_logs": "^1.1.7",
|
|
9
|
-
"
|
|
9
|
+
"mysql2": "^3.14.0"
|
|
10
10
|
},
|
|
11
11
|
"scripts": {
|
|
12
12
|
"test": "node test.js",
|
package/sql.js
CHANGED
package/test.js
CHANGED
|
@@ -17,9 +17,31 @@ async function test() {
|
|
|
17
17
|
|
|
18
18
|
db = sql.db();
|
|
19
19
|
db.table = 'user_account';
|
|
20
|
-
|
|
21
|
-
var obj = await db.getObj(
|
|
22
|
-
|
|
20
|
+
db.key = "user_id";
|
|
21
|
+
var obj = await db.getObj({
|
|
22
|
+
"user_id": "1"
|
|
23
|
+
});
|
|
24
|
+
console.log("查询对象", obj);
|
|
25
|
+
|
|
26
|
+
var i = 0;
|
|
27
|
+
var timer = setInterval(async () => {
|
|
28
|
+
var now = new Date();
|
|
29
|
+
var start = now.getTime();
|
|
30
|
+
console.log(start);
|
|
31
|
+
var bl = await db.set({
|
|
32
|
+
"user_id": "1"
|
|
33
|
+
}, {
|
|
34
|
+
"nickname": "hao" + (i++)
|
|
35
|
+
});
|
|
36
|
+
now = new Date();
|
|
37
|
+
var end = now.getTime();
|
|
38
|
+
console.log(end);
|
|
39
|
+
console.log(end - start);
|
|
40
|
+
console.log("测试", bl);
|
|
41
|
+
}, 3000);
|
|
42
|
+
|
|
43
|
+
// obj.nickname = "hao";
|
|
44
|
+
// console.log("查询对象", obj, db.error, db.sql);
|
|
23
45
|
|
|
24
46
|
// // 导入
|
|
25
47
|
// var ret = await sql.load("./data.sql".fullname(__dirname), function(progress, index, error, sql_str) {
|
|
@@ -45,7 +67,7 @@ async function test() {
|
|
|
45
67
|
// });
|
|
46
68
|
// console.log("导入结果", ret);
|
|
47
69
|
|
|
48
|
-
sql.close();
|
|
70
|
+
// sql.close();
|
|
49
71
|
|
|
50
72
|
}
|
|
51
73
|
test();
|