mm_sql 1.4.3 → 1.4.5
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/admin_vs_direct_test.js +14 -14
- package/debug_error_test.js +15 -15
- package/index.js +46 -28
- package/package.json +5 -5
- package/test.js +15 -5
package/admin_vs_direct_test.js
CHANGED
|
@@ -2,10 +2,10 @@ const { mysqlAdmin } = require('../mm_mysql');
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* 测试run方法对不存在的表的处理
|
|
5
|
-
* @param {
|
|
6
|
-
* @param {
|
|
5
|
+
* @param {object} admin_mysql - mysqlAdmin实例
|
|
6
|
+
* @param {object} direct_mysql - 直接Mysql实例
|
|
7
7
|
*/
|
|
8
|
-
async function
|
|
8
|
+
async function runMethod(admin_mysql, direct_mysql) {
|
|
9
9
|
console.log('\n测试1: 比较对不存在的表的处理...');
|
|
10
10
|
|
|
11
11
|
// mysqlAdmin实例测试
|
|
@@ -29,10 +29,10 @@ async function testRunMethod(admin_mysql, direct_mysql) {
|
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* 比较实例类型和属性
|
|
32
|
-
* @param {
|
|
33
|
-
* @param {
|
|
32
|
+
* @param {object} admin_mysql - mysqlAdmin实例
|
|
33
|
+
* @param {object} direct_mysql - 直接Mysql实例
|
|
34
34
|
*/
|
|
35
|
-
function
|
|
35
|
+
function instanceType(admin_mysql, direct_mysql) {
|
|
36
36
|
console.log('\n测试2: 比较实例类型和属性...');
|
|
37
37
|
console.log('mysqlAdmin实例类型:', admin_mysql.constructor.name);
|
|
38
38
|
console.log('直接Mysql实例类型:', direct_mysql.constructor.name);
|
|
@@ -42,10 +42,10 @@ function compareInstanceType(admin_mysql, direct_mysql) {
|
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
44
|
* 检查缓存问题
|
|
45
|
-
* @param {
|
|
46
|
-
* @param {
|
|
45
|
+
* @param {object} config - 数据库配置
|
|
46
|
+
* @param {object} admin_mysql - mysqlAdmin实例
|
|
47
47
|
*/
|
|
48
|
-
async function
|
|
48
|
+
async function cache(config, admin_mysql) {
|
|
49
49
|
console.log('\n测试3: 检查缓存问题...');
|
|
50
50
|
const admin_mysql2 = mysqlAdmin('compare_test', config); // 相同scope
|
|
51
51
|
const admin_mysql3 = mysqlAdmin('compare_test2', config); // 不同scope
|
|
@@ -87,9 +87,9 @@ async function compareAdminDirect() {
|
|
|
87
87
|
const direct_mysql = new Mysql(config);
|
|
88
88
|
console.log('✅ 直接Mysql实例创建成功');
|
|
89
89
|
|
|
90
|
-
await
|
|
91
|
-
|
|
92
|
-
await
|
|
90
|
+
await runMethod(admin_mysql, direct_mysql);
|
|
91
|
+
instanceType(admin_mysql, direct_mysql);
|
|
92
|
+
await cache(config, admin_mysql);
|
|
93
93
|
|
|
94
94
|
} catch (error) {
|
|
95
95
|
console.log('❌ 实例创建失败:', error.message);
|
|
@@ -99,7 +99,7 @@ async function compareAdminDirect() {
|
|
|
99
99
|
/**
|
|
100
100
|
* 测试mm_sql模块中的适配器行为
|
|
101
101
|
*/
|
|
102
|
-
async function
|
|
102
|
+
async function sqlAdapter() {
|
|
103
103
|
console.log('\n=== 测试mm_sql模块中的适配器行为 ===');
|
|
104
104
|
|
|
105
105
|
try {
|
|
@@ -151,7 +151,7 @@ async function main() {
|
|
|
151
151
|
console.log('开始比较mysqlAdmin和直接Mysql实例的行为...\n');
|
|
152
152
|
|
|
153
153
|
await compareAdminDirect();
|
|
154
|
-
await
|
|
154
|
+
await sqlAdapter();
|
|
155
155
|
|
|
156
156
|
console.log('\n比较测试完成');
|
|
157
157
|
}
|
package/debug_error_test.js
CHANGED
|
@@ -2,9 +2,9 @@ const { Sql } = require('./index.js');
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* 测试适配器直接调用
|
|
5
|
-
* @param {
|
|
5
|
+
* @param {object} mysql_sql - MySQL实例
|
|
6
6
|
*/
|
|
7
|
-
async function
|
|
7
|
+
async function adapterDirect(mysql_sql) {
|
|
8
8
|
console.log('\n测试1: 直接调用适配器run方法...');
|
|
9
9
|
try {
|
|
10
10
|
await mysql_sql._adapter.run('SELECT * FROM non_existent_table_xyz');
|
|
@@ -16,9 +16,9 @@ async function testAdapterDirect(mysql_sql) {
|
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* 测试mm_sql的run方法调用
|
|
19
|
-
* @param {
|
|
19
|
+
* @param {object} mysql_sql - MySQL实例
|
|
20
20
|
*/
|
|
21
|
-
async function
|
|
21
|
+
async function sqlRun(mysql_sql) {
|
|
22
22
|
console.log('\n测试2: 通过mm_sql的run方法调用...');
|
|
23
23
|
try {
|
|
24
24
|
await mysql_sql.run('SELECT * FROM non_existent_table_xyz');
|
|
@@ -30,9 +30,9 @@ async function testSqlRun(mysql_sql) {
|
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* 测试参数验证
|
|
33
|
-
* @param {
|
|
33
|
+
* @param {object} mysql_sql - MySQL实例
|
|
34
34
|
*/
|
|
35
|
-
async function
|
|
35
|
+
async function paramValid(mysql_sql) {
|
|
36
36
|
console.log('\n测试3: 测试参数验证...');
|
|
37
37
|
try {
|
|
38
38
|
await mysql_sql.run(123); // 无效参数类型
|
|
@@ -66,9 +66,9 @@ async function debugMysqlError() {
|
|
|
66
66
|
console.log('✅ mm_sql MySQL实例创建成功');
|
|
67
67
|
console.log('适配器类型:', mysql_sql._adapter?.constructor?.name);
|
|
68
68
|
|
|
69
|
-
await
|
|
70
|
-
await
|
|
71
|
-
await
|
|
69
|
+
await adapterDirect(mysql_sql);
|
|
70
|
+
await sqlRun(mysql_sql);
|
|
71
|
+
await paramValid(mysql_sql);
|
|
72
72
|
|
|
73
73
|
} catch (error) {
|
|
74
74
|
console.log('❌ mm_sql实例创建失败:', error.message);
|
|
@@ -77,9 +77,9 @@ async function debugMysqlError() {
|
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
* 测试SQLite适配器直接调用
|
|
80
|
-
* @param {
|
|
80
|
+
* @param {object} sqlite_sql - SQLite实例
|
|
81
81
|
*/
|
|
82
|
-
async function
|
|
82
|
+
async function sqliteAdapter(sqlite_sql) {
|
|
83
83
|
console.log('\n测试1: 直接调用适配器run方法...');
|
|
84
84
|
try {
|
|
85
85
|
await sqlite_sql._adapter.run('SELECT * FROM non_existent_table_xyz');
|
|
@@ -91,9 +91,9 @@ async function testSqliteAdapter(sqlite_sql) {
|
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
93
|
* 测试SQLite的run方法调用
|
|
94
|
-
* @param {
|
|
94
|
+
* @param {object} sqlite_sql - SQLite实例
|
|
95
95
|
*/
|
|
96
|
-
async function
|
|
96
|
+
async function sqliteRun(sqlite_sql) {
|
|
97
97
|
console.log('\n测试2: 通过mm_sql的run方法调用...');
|
|
98
98
|
try {
|
|
99
99
|
await sqlite_sql.run('SELECT * FROM non_existent_table_xyz');
|
|
@@ -105,9 +105,9 @@ async function testSqliteRun(sqlite_sql) {
|
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
107
|
* 测试SQLite参数验证
|
|
108
|
-
* @param {
|
|
108
|
+
* @param {object} sqlite_sql - SQLite实例
|
|
109
109
|
*/
|
|
110
|
-
async function
|
|
110
|
+
async function sqliteParam(sqlite_sql) {
|
|
111
111
|
console.log('\n测试3: 测试参数验证...');
|
|
112
112
|
try {
|
|
113
113
|
await sqlite_sql.run(123); // 无效参数类型
|
package/index.js
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* SQL通用类
|
|
3
|
-
* @author <a href="http://qww.elins.cn">qww</a>
|
|
4
|
-
* @version 1.0
|
|
5
|
-
*/
|
|
6
|
-
|
|
1
|
+
require('mm_expand');
|
|
7
2
|
/**
|
|
8
3
|
* 数据库SQL类
|
|
9
4
|
* @class
|
|
@@ -19,12 +14,35 @@ class Sql {
|
|
|
19
14
|
/**
|
|
20
15
|
* 配置信息
|
|
21
16
|
*/
|
|
22
|
-
this.config = {
|
|
23
|
-
|
|
17
|
+
this.config = {
|
|
18
|
+
scope: 'sys',
|
|
19
|
+
way: 'mysql',
|
|
20
|
+
sqlite: {
|
|
21
|
+
dir: './db/'.fullname(),
|
|
22
|
+
user: 'root',
|
|
23
|
+
password: '',
|
|
24
|
+
database: 'mm',
|
|
25
|
+
charset: 'utf8mb4',
|
|
26
|
+
connect_timeout: 20000,
|
|
27
|
+
connection_limit: 500
|
|
28
|
+
},
|
|
29
|
+
mysql: {
|
|
30
|
+
host: '127.0.0.1',
|
|
31
|
+
port: 3306,
|
|
32
|
+
user: 'root',
|
|
33
|
+
password: 'Asd159357',
|
|
34
|
+
database: '',
|
|
35
|
+
charset: 'utf8mb4',
|
|
36
|
+
timezone: '+08:00',
|
|
37
|
+
connect_timeout: 20000,
|
|
38
|
+
connection_limit: 500
|
|
39
|
+
},
|
|
40
|
+
|
|
24
41
|
dir: '/db/',
|
|
25
42
|
host: 'localhost',
|
|
26
43
|
port: 3306,
|
|
27
|
-
database: '', ...config || {}
|
|
44
|
+
database: '', ...config || {}
|
|
45
|
+
};
|
|
28
46
|
|
|
29
47
|
/**
|
|
30
48
|
* SQL适配器
|
|
@@ -49,25 +67,25 @@ Sql.prototype._init = function () {
|
|
|
49
67
|
* @private
|
|
50
68
|
*/
|
|
51
69
|
Sql.prototype._createSqlAdapter = function () {
|
|
52
|
-
const adapter_mod = `mm_${this.config.
|
|
70
|
+
const adapter_mod = `mm_${this.config.way}`;
|
|
53
71
|
const adapter = require(adapter_mod);
|
|
54
72
|
|
|
55
73
|
// 根据数据库类型选择不同的初始化方式
|
|
56
|
-
if (this.config.
|
|
74
|
+
if (this.config.way === 'mysql') {
|
|
57
75
|
// 创建MySQL实例
|
|
58
76
|
const admin = adapter.mysqlAdmin;
|
|
59
|
-
this._adapter = admin(this.config.scope, this.config);
|
|
60
|
-
} else if (this.config.
|
|
77
|
+
this._adapter = admin(this.config.scope, this.config[this.config.way]);
|
|
78
|
+
} else if (this.config.way === 'sqlite') {
|
|
61
79
|
// SQLite应该有类似的结构
|
|
62
80
|
const admin = adapter.sqliteAdmin;
|
|
63
|
-
this._adapter = admin(this.config.scope, this.config);
|
|
81
|
+
this._adapter = admin(this.config.scope, this.config[this.config.way]);
|
|
64
82
|
} else {
|
|
65
83
|
// 其他数据库类型直接使用Sql类(如果存在)
|
|
66
84
|
const adapter_cls = adapter.Sql;
|
|
67
85
|
if (adapter_cls) {
|
|
68
|
-
this._adapter = new adapter_cls(this.config);
|
|
86
|
+
this._adapter = new adapter_cls(this.config[this.config.way]);
|
|
69
87
|
} else {
|
|
70
|
-
throw new Error(`不支持的数据库类型: ${this.config.
|
|
88
|
+
throw new Error(`不支持的数据库类型: ${this.config.way}`);
|
|
71
89
|
}
|
|
72
90
|
}
|
|
73
91
|
};
|
|
@@ -101,7 +119,7 @@ Sql.prototype._validateSqlParams = function (sql, param, timeout) {
|
|
|
101
119
|
*/
|
|
102
120
|
Sql.prototype.run = function (sql, param = [], timeout = null) {
|
|
103
121
|
this._validateSqlParams(sql, param, timeout);
|
|
104
|
-
|
|
122
|
+
|
|
105
123
|
try {
|
|
106
124
|
// 支持两种调用方式:run(sql, param, timeout) 或 run({sql: '', param: []})
|
|
107
125
|
let final_sql = sql;
|
|
@@ -129,7 +147,7 @@ Sql.prototype.run = function (sql, param = [], timeout = null) {
|
|
|
129
147
|
*/
|
|
130
148
|
Sql.prototype.exec = function (sql, param = [], timeout = null) {
|
|
131
149
|
this._validateSqlParams(sql, param, timeout);
|
|
132
|
-
|
|
150
|
+
|
|
133
151
|
try {
|
|
134
152
|
// 支持两种调用方式:exec(sql, param, timeout) 或 exec({sql: '', param: []})
|
|
135
153
|
let final_sql = sql;
|
|
@@ -182,7 +200,7 @@ Sql.prototype.setConfig = function (config) {
|
|
|
182
200
|
if (!config || typeof config !== 'object') {
|
|
183
201
|
throw new TypeError('config must be object');
|
|
184
202
|
}
|
|
185
|
-
|
|
203
|
+
|
|
186
204
|
try {
|
|
187
205
|
this.config = Object.assign(this.config, config);
|
|
188
206
|
this._init();
|
|
@@ -203,13 +221,13 @@ Sql.prototype.tplQuery = function (param, sql_tpl) {
|
|
|
203
221
|
if (!param || typeof param !== 'object') {
|
|
204
222
|
throw new TypeError('param must be object');
|
|
205
223
|
}
|
|
206
|
-
|
|
224
|
+
|
|
207
225
|
// 如果sql_tpl为null或undefined,使用空对象
|
|
208
226
|
const template = sql_tpl || {};
|
|
209
|
-
|
|
227
|
+
|
|
210
228
|
try {
|
|
211
229
|
const segments = [];
|
|
212
|
-
|
|
230
|
+
|
|
213
231
|
for (const key in param) {
|
|
214
232
|
if (template[key]) {
|
|
215
233
|
// 替换占位符{0}为参数值
|
|
@@ -220,7 +238,7 @@ Sql.prototype.tplQuery = function (param, sql_tpl) {
|
|
|
220
238
|
segments.push('`' + key + "` = '" + param[key] + "'");
|
|
221
239
|
}
|
|
222
240
|
}
|
|
223
|
-
|
|
241
|
+
|
|
224
242
|
return segments.join(' AND ');
|
|
225
243
|
} catch (error) {
|
|
226
244
|
this.log('error', '模板查询生成失败', error);
|
|
@@ -240,13 +258,13 @@ Sql.prototype.tplBody = function (param, sql_tpl) {
|
|
|
240
258
|
if (!param || typeof param !== 'object') {
|
|
241
259
|
throw new TypeError('param must be object');
|
|
242
260
|
}
|
|
243
|
-
|
|
261
|
+
|
|
244
262
|
// 如果sql_tpl为null或undefined,使用空对象
|
|
245
263
|
const template = sql_tpl || {};
|
|
246
|
-
|
|
264
|
+
|
|
247
265
|
try {
|
|
248
266
|
const segments = [];
|
|
249
|
-
|
|
267
|
+
|
|
250
268
|
for (const key in param) {
|
|
251
269
|
if (template[key]) {
|
|
252
270
|
// 替换占位符{0}为参数值
|
|
@@ -257,7 +275,7 @@ Sql.prototype.tplBody = function (param, sql_tpl) {
|
|
|
257
275
|
segments.push('`' + key + "` = '" + param[key] + "'");
|
|
258
276
|
}
|
|
259
277
|
}
|
|
260
|
-
|
|
278
|
+
|
|
261
279
|
return segments.join(' , ');
|
|
262
280
|
} catch (error) {
|
|
263
281
|
this.log('error', '模板数据生成失败', error);
|
|
@@ -280,7 +298,7 @@ Sql.prototype.filter = function (param, arr) {
|
|
|
280
298
|
if (!Array.isArray(arr)) {
|
|
281
299
|
throw new TypeError('arr must be array');
|
|
282
300
|
}
|
|
283
|
-
|
|
301
|
+
|
|
284
302
|
try {
|
|
285
303
|
const result = {};
|
|
286
304
|
for (const key of arr) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_sql",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.5",
|
|
4
4
|
"description": "一个通用的SQL帮助类,支持通过切换db_type实现对不同数据库的操作,包括MySQL和SQLite等",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -44,12 +44,12 @@
|
|
|
44
44
|
"node": ">=12.0.0"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"mm_mysql": "^2.2.
|
|
48
|
-
"mm_sqlite": "^1.2.
|
|
47
|
+
"mm_mysql": "^2.2.9",
|
|
48
|
+
"mm_sqlite": "^1.2.9"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"eslint": "^9.39.2",
|
|
52
|
-
"eslint-plugin-jsdoc": "^
|
|
53
|
-
"mm_eslint": "^1.
|
|
52
|
+
"eslint-plugin-jsdoc": "^62.5.0",
|
|
53
|
+
"mm_eslint": "^1.6.8"
|
|
54
54
|
}
|
|
55
55
|
}
|
package/test.js
CHANGED
|
@@ -21,11 +21,21 @@ class SqlTest {
|
|
|
21
21
|
this.sql = new Sql({
|
|
22
22
|
// db_type: 'mysql',
|
|
23
23
|
db_type: 'sqlite',
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
sqlite: {
|
|
25
|
+
dir: './db',
|
|
26
|
+
user: 'root',
|
|
27
|
+
password: '',
|
|
28
|
+
database: 'mm',
|
|
29
|
+
charset: 'utf8mb4',
|
|
30
|
+
connect_timeout: 20000,
|
|
31
|
+
connection_limit: 500
|
|
32
|
+
},
|
|
33
|
+
mysql: {
|
|
34
|
+
host: 'localhost',
|
|
35
|
+
user: 'root',
|
|
36
|
+
password: 'Asd159357',
|
|
37
|
+
database: 'mm'
|
|
38
|
+
}
|
|
29
39
|
});
|
|
30
40
|
console.log('初始化测试成功');
|
|
31
41
|
return true;
|