mm_mysql 2.5.1 → 2.5.3
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 -14
- package/package.json +5 -5
- package/sql.js +28 -0
package/index.js
CHANGED
|
@@ -41,7 +41,7 @@ class Mysql extends Base {
|
|
|
41
41
|
this._pool = null;
|
|
42
42
|
// 移除单连接支持,统一使用连接池
|
|
43
43
|
|
|
44
|
-
//
|
|
44
|
+
// 错误信息(注意:同 scope 下为单例共享,并发时以最后一次写入为准)
|
|
45
45
|
this.error = null;
|
|
46
46
|
// 最近执行的SQL语句
|
|
47
47
|
this.sql = '';
|
|
@@ -136,13 +136,12 @@ Mysql.prototype.open = async function () {
|
|
|
136
136
|
* @returns {Promise<boolean>}
|
|
137
137
|
*/
|
|
138
138
|
Mysql.prototype.close = async function () {
|
|
139
|
+
if (!this._pool) {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
139
142
|
try {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
await this._pool.end();
|
|
143
|
-
this._pool = null;
|
|
144
|
-
}
|
|
145
|
-
|
|
143
|
+
await this._pool.end();
|
|
144
|
+
this._pool = null;
|
|
146
145
|
this.log('info', '数据库连接池已关闭');
|
|
147
146
|
return true;
|
|
148
147
|
} catch (error) {
|
|
@@ -157,12 +156,15 @@ Mysql.prototype.close = async function () {
|
|
|
157
156
|
* @throws {TypeError} 当连接池不存在时
|
|
158
157
|
*/
|
|
159
158
|
Mysql.prototype.getConn = async function () {
|
|
160
|
-
//
|
|
159
|
+
// 连接池不存在则创建
|
|
161
160
|
if (!this._pool) {
|
|
162
161
|
await this.open();
|
|
163
162
|
}
|
|
163
|
+
// 连接池可能已被外部关闭,检测并重建
|
|
164
|
+
if (this._pool._closed) {
|
|
165
|
+
await this.open();
|
|
166
|
+
}
|
|
164
167
|
|
|
165
|
-
// 统一从连接池获取连接
|
|
166
168
|
return await this._pool.getConnection();
|
|
167
169
|
};
|
|
168
170
|
|
|
@@ -187,14 +189,13 @@ Mysql.prototype.run = async function (sql, params = []) {
|
|
|
187
189
|
let conn = null;
|
|
188
190
|
let ret = [];
|
|
189
191
|
try {
|
|
190
|
-
// 获取连接
|
|
191
192
|
conn = await this.getConn();
|
|
192
193
|
this.sql = sql;
|
|
193
194
|
let [rows] = await conn.query(sql, params);
|
|
194
195
|
ret = rows;
|
|
195
196
|
} catch (err) {
|
|
196
197
|
console.error('SQL执行失败', err);
|
|
197
|
-
this.sql = err.sql;
|
|
198
|
+
this.sql = err.sql || sql;
|
|
198
199
|
this.error = {
|
|
199
200
|
code: err.errno,
|
|
200
201
|
message: err.sqlMessage
|
|
@@ -226,14 +227,13 @@ Mysql.prototype.exec = async function (sql, params = []) {
|
|
|
226
227
|
let conn = null;
|
|
227
228
|
let ret = 0;
|
|
228
229
|
try {
|
|
229
|
-
// 获取连接
|
|
230
230
|
conn = await this.getConn();
|
|
231
231
|
this.sql = sql;
|
|
232
232
|
let [result] = await conn.execute(sql, params);
|
|
233
233
|
ret = result.insertId || result.affectedRows || result.changedRows || 1;
|
|
234
234
|
} catch (error) {
|
|
235
235
|
console.error('SQL执行失败', error);
|
|
236
|
-
this.sql = error.sql;
|
|
236
|
+
this.sql = error.sql || sql;
|
|
237
237
|
this.error = {
|
|
238
238
|
code: error.errno,
|
|
239
239
|
message: error.sqlMessage
|
|
@@ -394,7 +394,7 @@ Mysql.prototype.getPoolStats = function () {
|
|
|
394
394
|
*/
|
|
395
395
|
Mysql.prototype.healthCheck = async function () {
|
|
396
396
|
try {
|
|
397
|
-
if (!this._pool
|
|
397
|
+
if (!this._pool) {
|
|
398
398
|
return {
|
|
399
399
|
status: 'error',
|
|
400
400
|
message: '数据库连接未初始化'
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_mysql",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.3",
|
|
4
4
|
"description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"mm_expand": "^2.5.
|
|
8
|
-
"mysql2": "^3.23.
|
|
7
|
+
"mm_expand": "^2.5.3",
|
|
8
|
+
"mysql2": "^3.23.3",
|
|
9
9
|
"sqlstring": "^2.3.3"
|
|
10
10
|
},
|
|
11
11
|
"scripts": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"eslint": "^10.8.1",
|
|
40
|
-
"eslint-plugin-jsdoc": "^64.0
|
|
41
|
-
"mm_eslint": "^1.7.
|
|
40
|
+
"eslint-plugin-jsdoc": "^64.2.0",
|
|
41
|
+
"mm_eslint": "^1.7.6"
|
|
42
42
|
}
|
|
43
43
|
}
|
package/sql.js
CHANGED
|
@@ -441,6 +441,10 @@ Sql.prototype._buildLikeWhere = function (obj) {
|
|
|
441
441
|
var where = '';
|
|
442
442
|
for (var k in obj) {
|
|
443
443
|
var val = obj[k];
|
|
444
|
+
// 防御:如果 String.prototype 被污染导致 .slice 丢失,强制重建原始字符串
|
|
445
|
+
if (typeof val === 'string') {
|
|
446
|
+
val = String(val);
|
|
447
|
+
}
|
|
444
448
|
// 先转义再处理字符串,避免转义字符被错误处理
|
|
445
449
|
val = escape(val);
|
|
446
450
|
if (k.endsWith('_min')) {
|
|
@@ -469,6 +473,10 @@ Sql.prototype._buildExactWhere = function (obj) {
|
|
|
469
473
|
var where = '';
|
|
470
474
|
for (var k in obj) {
|
|
471
475
|
var val = obj[k];
|
|
476
|
+
// 防御:如果 String.prototype 被污染导致 .slice 丢失,强制重建原始字符串
|
|
477
|
+
if (typeof val === 'string') {
|
|
478
|
+
val = String(val);
|
|
479
|
+
}
|
|
472
480
|
// 先转义再处理字符串,避免转义字符被错误处理
|
|
473
481
|
val = escape(val);
|
|
474
482
|
if (k.endsWith('_min')) {
|
|
@@ -767,6 +775,26 @@ Sql.prototype.getObj = async function (query, sort, view, like, timeout = 20000)
|
|
|
767
775
|
return obj;
|
|
768
776
|
};
|
|
769
777
|
|
|
778
|
+
/**
|
|
779
|
+
* 查询数据
|
|
780
|
+
* @param {object} query 查询条件
|
|
781
|
+
* @param {string} sort 排序
|
|
782
|
+
* @param {string} view 返回的字段
|
|
783
|
+
* @param {boolean} like 是否使用like匹配, 为空使用默认方式
|
|
784
|
+
* @param {number} timeout 超时时间(毫秒),默认20000ms
|
|
785
|
+
* @returns {Promise|Array} 查询结果
|
|
786
|
+
*/
|
|
787
|
+
Sql.prototype.list = async function (query, sort, view, like, timeout = 20000) {
|
|
788
|
+
if (!this.table) {
|
|
789
|
+
throw new Error('表名未设置');
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
let sql = this.toGetSql(query, sort, view, like);
|
|
793
|
+
this.sql = sql;
|
|
794
|
+
let list = await this.run(sql);
|
|
795
|
+
return list;
|
|
796
|
+
};
|
|
797
|
+
|
|
770
798
|
/**
|
|
771
799
|
* 统计记录数
|
|
772
800
|
* @param {object} query 查询条件
|