mm_sql 1.4.4 → 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/index.js +46 -28
- package/package.json +5 -5
- package/test.js +15 -5
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;
|