mm_mysql 1.8.6 → 1.8.9
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 +14 -3
- package/package.json +2 -2
- package/sql.js +2 -2
- package/test.js +7 -1
package/index.js
CHANGED
|
@@ -45,6 +45,8 @@ class Mysql {
|
|
|
45
45
|
this.sql = "";
|
|
46
46
|
// 连接器
|
|
47
47
|
this.conn;
|
|
48
|
+
// 连接态 0未连接,1已连接
|
|
49
|
+
this.state = 0;
|
|
48
50
|
|
|
49
51
|
// 数据库配置参数
|
|
50
52
|
this.config = {
|
|
@@ -94,6 +96,9 @@ class Mysql {
|
|
|
94
96
|
message: $.info(err).between('Error: ', '\n')
|
|
95
97
|
};
|
|
96
98
|
$.log.error("SQL error:", err);
|
|
99
|
+
if (err.message.indexOf("connect ECONNREFUSED") !== -1) {
|
|
100
|
+
$this.state = 0;
|
|
101
|
+
}
|
|
97
102
|
resolve([]);
|
|
98
103
|
reject(err);
|
|
99
104
|
} else {
|
|
@@ -146,7 +151,6 @@ class Mysql {
|
|
|
146
151
|
if ($this.config.log) {
|
|
147
152
|
$.log.debug("SQL:", sql);
|
|
148
153
|
}
|
|
149
|
-
// $this.conn.config.connectionConfig = false;
|
|
150
154
|
// 返回一个 Promise
|
|
151
155
|
return new Promise((resolve, reject) => {
|
|
152
156
|
$this.conn.getConnection(function(err, db) {
|
|
@@ -156,6 +160,9 @@ class Mysql {
|
|
|
156
160
|
message: $.info(err).between('Error: ', '\n')
|
|
157
161
|
};
|
|
158
162
|
$.log.error("SQL error:", err);
|
|
163
|
+
if (err.message.indexOf("connect ECONNREFUSED") !== -1) {
|
|
164
|
+
$this.state = 0;
|
|
165
|
+
}
|
|
159
166
|
resolve(-1);
|
|
160
167
|
reject(err);
|
|
161
168
|
} else {
|
|
@@ -250,9 +257,11 @@ Mysql.prototype.setConfig = function(cg) {
|
|
|
250
257
|
|
|
251
258
|
/**
|
|
252
259
|
* @description 打开数据库, 如果没有则建立数据库连接再打开
|
|
260
|
+
* @param {boolean} 是否重置
|
|
253
261
|
*/
|
|
254
|
-
Mysql.prototype.open = function() {
|
|
255
|
-
if (!pool[this.identifier]) {
|
|
262
|
+
Mysql.prototype.open = function(reset) {
|
|
263
|
+
if (reset || !this.state || !pool[this.identifier]) {
|
|
264
|
+
this.state = 1;
|
|
256
265
|
pool[this.identifier] = createPool(this.config);
|
|
257
266
|
}
|
|
258
267
|
this.conn = pool[this.identifier];
|
|
@@ -263,7 +272,9 @@ Mysql.prototype.open = function() {
|
|
|
263
272
|
*/
|
|
264
273
|
Mysql.prototype.close = function() {
|
|
265
274
|
if (pool[this.identifier]) {
|
|
275
|
+
pool[this.identifier].end();
|
|
266
276
|
pool[this.identifier] = null;
|
|
277
|
+
delete pool[this.identifier];
|
|
267
278
|
}
|
|
268
279
|
};
|
|
269
280
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_mysql",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.9",
|
|
4
4
|
"description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
@@ -34,4 +34,4 @@
|
|
|
34
34
|
"url": "https://github.com/qiuwenwu/mm_mysql/issues"
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://github.com/qiuwenwu/mm_mysql#readme"
|
|
37
|
-
}
|
|
37
|
+
}
|
package/sql.js
CHANGED
|
@@ -441,7 +441,7 @@ Sql.prototype.toWhere = function(obj, like) {
|
|
|
441
441
|
});
|
|
442
442
|
where += " and " + escapeId(k.replace('_has', "")) + " in (" + vals.join(',') + ")";
|
|
443
443
|
} else if (k.endWith('_like')) {
|
|
444
|
-
where += " and " + escapeId(k) + " LIKE '%" + val.trim("'") + "%'";
|
|
444
|
+
where += " and " + escapeId(k.replace('_like', "")) + " LIKE '%" + val.trim("'") + "%'";
|
|
445
445
|
} else if (typeof(val) === "string" && !/^[0-9]+$/.test(val)) {
|
|
446
446
|
where += " and " + escapeId(k) + " LIKE '%" + val.trim("'") + "%'";
|
|
447
447
|
} else {
|
|
@@ -467,7 +467,7 @@ Sql.prototype.toWhere = function(obj, like) {
|
|
|
467
467
|
});
|
|
468
468
|
where += " and " + escapeId(k.replace('_has', "")) + " in (" + vals.join(',') + ")";
|
|
469
469
|
} else if (k.endWith('_like')) {
|
|
470
|
-
where += " and " + escapeId(k) + " LIKE '%" + val.trim("'") + "%'";
|
|
470
|
+
where += " and " + escapeId(k.replace('_like', "")) + " LIKE '%" + val.trim("'") + "%'";
|
|
471
471
|
} else {
|
|
472
472
|
where += " and " + escapeId(k) + " = " + val;
|
|
473
473
|
}
|
package/test.js
CHANGED
|
@@ -77,6 +77,7 @@ var tpl = {
|
|
|
77
77
|
// test_tpl_set();
|
|
78
78
|
|
|
79
79
|
async function test() {
|
|
80
|
+
console.log('测试开始')
|
|
80
81
|
var sql = new Mysql();
|
|
81
82
|
var db = sql.db();
|
|
82
83
|
// 是否输出语法
|
|
@@ -89,6 +90,8 @@ async function test() {
|
|
|
89
90
|
"password": "Asd159357"
|
|
90
91
|
});
|
|
91
92
|
sql.open();
|
|
93
|
+
// sql.close();
|
|
94
|
+
// sql.open();
|
|
92
95
|
|
|
93
96
|
// --- 测试事件 ---
|
|
94
97
|
$.eventer.on("mysql_add_before:user_account", function(json) {
|
|
@@ -178,8 +181,11 @@ async function test() {
|
|
|
178
181
|
}, obj_new);
|
|
179
182
|
console.log("语法", db.sql, bl);
|
|
180
183
|
}
|
|
181
|
-
|
|
182
184
|
test();
|
|
185
|
+
// setInterval(() => {
|
|
186
|
+
// test();
|
|
187
|
+
// }, 3000)
|
|
188
|
+
|
|
183
189
|
|
|
184
190
|
// 测试模板查询
|
|
185
191
|
// async function test_tpl_get() {
|