mm_mysql 1.9.2 → 1.9.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 +27 -16
- package/package.json +33 -33
- package/test.js +10 -3
package/index.js
CHANGED
|
@@ -11,7 +11,9 @@ const {
|
|
|
11
11
|
} = require('./db');
|
|
12
12
|
|
|
13
13
|
const Link_model = require('./link_model');
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
table
|
|
16
|
+
} = require('console');
|
|
15
17
|
|
|
16
18
|
var pool = {};
|
|
17
19
|
|
|
@@ -81,16 +83,20 @@ class Mysql {
|
|
|
81
83
|
* @param {Array} val 替换值
|
|
82
84
|
* @return {Promise|Array} 异步构造器, 当await时返回执行结果
|
|
83
85
|
*/
|
|
84
|
-
this.run = function
|
|
86
|
+
this.run = function(sql, val) {
|
|
85
87
|
var _this = this;
|
|
86
88
|
this.sql = sql;
|
|
87
89
|
if ($this.config.log) {
|
|
88
90
|
$.log.debug("SQL:", sql);
|
|
89
91
|
}
|
|
90
92
|
|
|
93
|
+
if (!$this.conn) {
|
|
94
|
+
$this.open();
|
|
95
|
+
}
|
|
96
|
+
|
|
91
97
|
// 返回一个 Promise
|
|
92
98
|
return new Promise((resolve, reject) => {
|
|
93
|
-
$this.conn.getConnection(function
|
|
99
|
+
$this.conn.getConnection(function(err, db) {
|
|
94
100
|
if (err) {
|
|
95
101
|
_this.error = {
|
|
96
102
|
code: 2003,
|
|
@@ -132,7 +138,7 @@ class Mysql {
|
|
|
132
138
|
* @param {Array} val 替换值
|
|
133
139
|
* @return {Promise|Array} 异步构造器, 当await时返回执行结果
|
|
134
140
|
*/
|
|
135
|
-
this.exec = function
|
|
141
|
+
this.exec = function(sql, val) {
|
|
136
142
|
var _this = this;
|
|
137
143
|
if (this.task) {
|
|
138
144
|
this.task_sql += sql + "\r\n";
|
|
@@ -152,9 +158,14 @@ class Mysql {
|
|
|
152
158
|
if ($this.config.log) {
|
|
153
159
|
$.log.debug("SQL:", sql);
|
|
154
160
|
}
|
|
161
|
+
|
|
162
|
+
if (!$this.conn) {
|
|
163
|
+
$this.open();
|
|
164
|
+
}
|
|
165
|
+
|
|
155
166
|
// 返回一个 Promise
|
|
156
167
|
return new Promise((resolve, reject) => {
|
|
157
|
-
$this.conn.getConnection(function
|
|
168
|
+
$this.conn.getConnection(function(err, db) {
|
|
158
169
|
if (err) {
|
|
159
170
|
_this.error = {
|
|
160
171
|
code: 41000,
|
|
@@ -183,7 +194,7 @@ class Mysql {
|
|
|
183
194
|
if (o.constructor == Array) {
|
|
184
195
|
if (o.length > 0) {
|
|
185
196
|
var num = 0;
|
|
186
|
-
o.map(function
|
|
197
|
+
o.map(function(item) {
|
|
187
198
|
num += item['affectedRows'];
|
|
188
199
|
});
|
|
189
200
|
if (num === 0) {
|
|
@@ -214,7 +225,7 @@ class Mysql {
|
|
|
214
225
|
/**
|
|
215
226
|
* @description 获取数据库管理器
|
|
216
227
|
*/
|
|
217
|
-
this.db = function
|
|
228
|
+
this.db = function() {
|
|
218
229
|
return new DB($this);
|
|
219
230
|
};
|
|
220
231
|
}
|
|
@@ -226,7 +237,7 @@ class Mysql {
|
|
|
226
237
|
* @param {Function} func 回调函数
|
|
227
238
|
* @return {Promise} 异步构造器, 当await时返回执行结果
|
|
228
239
|
*/
|
|
229
|
-
Mysql.prototype.load = async function
|
|
240
|
+
Mysql.prototype.load = async function(file, func) {
|
|
230
241
|
var count = 0;
|
|
231
242
|
var progress = 0;
|
|
232
243
|
var errors = [];
|
|
@@ -275,7 +286,7 @@ Mysql.prototype.load = async function (file, func) {
|
|
|
275
286
|
* @param {Function} func 回调函数
|
|
276
287
|
* @return {Promise} 异步构造器, 当await时返回执行结果
|
|
277
288
|
*/
|
|
278
|
-
Mysql.prototype.save = async function
|
|
289
|
+
Mysql.prototype.save = async function(file, func, tables = []) {
|
|
279
290
|
const fs = require('fs');
|
|
280
291
|
const stream = fs.createWriteStream(file);
|
|
281
292
|
let count = 0;
|
|
@@ -289,14 +300,14 @@ Mysql.prototype.save = async function (file, func, tables = []) {
|
|
|
289
300
|
stream.write('SET FOREIGN_KEY_CHECKS = 0;\r\n\r\n');
|
|
290
301
|
count++;
|
|
291
302
|
|
|
292
|
-
if(!tables.length) {
|
|
303
|
+
if (!tables.length) {
|
|
293
304
|
// 获取所有表
|
|
294
305
|
var tbs = await this.run('SHOW TABLES');
|
|
295
306
|
tables = tbs.map((item) => {
|
|
296
307
|
return item[Object.keys(item)[0]];
|
|
297
308
|
});
|
|
298
309
|
}
|
|
299
|
-
|
|
310
|
+
|
|
300
311
|
tableCount = tables.length;
|
|
301
312
|
console.log("tables", tables);
|
|
302
313
|
for (var i = 0; i < tables.length; i++) {
|
|
@@ -376,7 +387,7 @@ Mysql.prototype.save = async function (file, func, tables = []) {
|
|
|
376
387
|
* @param {Array} arr_table 关联的数据表
|
|
377
388
|
* @return {Object} 管理模型
|
|
378
389
|
*/
|
|
379
|
-
Mysql.prototype.dbs = async function
|
|
390
|
+
Mysql.prototype.dbs = async function(key, value, clear_prefix, ...arr_table) {
|
|
380
391
|
var lm = new Link_model({
|
|
381
392
|
key,
|
|
382
393
|
value,
|
|
@@ -393,9 +404,9 @@ Mysql.prototype.dbs = async function (key, value, clear_prefix, ...arr_table) {
|
|
|
393
404
|
* 设置配置参数
|
|
394
405
|
* @param {Object} cg 配置对象或配置路径
|
|
395
406
|
*/
|
|
396
|
-
Mysql.prototype.setConfig = function
|
|
407
|
+
Mysql.prototype.setConfig = function(cg) {
|
|
397
408
|
var obj;
|
|
398
|
-
if (typeof
|
|
409
|
+
if (typeof(cg) === "string") {
|
|
399
410
|
obj = cg.loadJson(this.dir);
|
|
400
411
|
} else {
|
|
401
412
|
obj = cg;
|
|
@@ -408,7 +419,7 @@ Mysql.prototype.setConfig = function (cg) {
|
|
|
408
419
|
* @description 打开数据库, 如果没有则建立数据库连接再打开
|
|
409
420
|
* @param {boolean} 是否重置
|
|
410
421
|
*/
|
|
411
|
-
Mysql.prototype.open = function
|
|
422
|
+
Mysql.prototype.open = function(reset) {
|
|
412
423
|
if (reset || !this.state || !pool[this.identifier]) {
|
|
413
424
|
this.state = 1;
|
|
414
425
|
pool[this.identifier] = createPool(this.config);
|
|
@@ -419,7 +430,7 @@ Mysql.prototype.open = function (reset) {
|
|
|
419
430
|
/**
|
|
420
431
|
* @description 关闭连接
|
|
421
432
|
*/
|
|
422
|
-
Mysql.prototype.close = function
|
|
433
|
+
Mysql.prototype.close = function() {
|
|
423
434
|
if (pool[this.identifier]) {
|
|
424
435
|
pool[this.identifier].end();
|
|
425
436
|
pool[this.identifier] = null;
|
package/package.json
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "mm_mysql",
|
|
3
|
+
"version": "1.9.3",
|
|
4
|
+
"description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"mm_expand": "^1.8.0",
|
|
8
|
+
"mm_logs": "^1.1.7"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node test.js",
|
|
12
|
+
"dev": "nodemon test.js"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://gitee.com/qiuwenwu91/mm_mysql.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"mysql",
|
|
20
|
+
"async",
|
|
21
|
+
"await",
|
|
22
|
+
"promise",
|
|
23
|
+
"add",
|
|
24
|
+
"del",
|
|
25
|
+
"set",
|
|
26
|
+
"get",
|
|
27
|
+
"query",
|
|
28
|
+
"run",
|
|
29
|
+
"exec"
|
|
30
|
+
],
|
|
31
|
+
"author": "qww",
|
|
32
|
+
"license": "ISC"
|
|
33
|
+
}
|
package/test.js
CHANGED
|
@@ -13,19 +13,26 @@ async function test() {
|
|
|
13
13
|
password: "Asd159357",
|
|
14
14
|
multipleStatements: true
|
|
15
15
|
});
|
|
16
|
-
await sql.open();
|
|
16
|
+
// await sql.open();
|
|
17
|
+
|
|
18
|
+
// db = sql.db();
|
|
19
|
+
// db.table = 'user_account';
|
|
20
|
+
|
|
21
|
+
// var obj = await db.getObj();
|
|
22
|
+
// console.log(obj);
|
|
23
|
+
|
|
17
24
|
// // 导入
|
|
18
25
|
// var ret = await sql.load("./data.sql".fullname(__dirname), function(progress, index, error, sql_str) {
|
|
19
26
|
// console.log("导入", progress, index, error, sql_str);
|
|
20
27
|
// });
|
|
21
28
|
// console.log("导入结果", ret);
|
|
22
|
-
|
|
29
|
+
|
|
23
30
|
// 导出指定表'user_account', 'user_info'
|
|
24
31
|
// var ret2 = await sql.save("./bat.sql".fullname(__dirname), function(progress, index, error, sql_str) {
|
|
25
32
|
// console.log("导出", progress, index, error, sql_str);
|
|
26
33
|
// }, ['user_account', 'user_info']);
|
|
27
34
|
// console.log("导出结果", ret2);
|
|
28
|
-
|
|
35
|
+
|
|
29
36
|
// // 导出
|
|
30
37
|
// var ret2 = await sql.save("./bat.sql".fullname(__dirname), function(progress, index, error, sql_str) {
|
|
31
38
|
// // console.log("导出", progress, index, error, sql_str);
|