mm_mysql 2.0.0 → 2.0.2
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 +10 -10
- package/package.json +2 -3
- package/test.js +20 -18
- package/upgrade.sql +34 -0
package/index.js
CHANGED
|
@@ -118,7 +118,7 @@ class Mysql {
|
|
|
118
118
|
*/
|
|
119
119
|
this.exec = async function(sql, val) {
|
|
120
120
|
if (this.task) {
|
|
121
|
-
this.task_sql += sql + "\
|
|
121
|
+
this.task_sql += sql + "\n";
|
|
122
122
|
if (this.task === 1) {
|
|
123
123
|
return this.task_sql;
|
|
124
124
|
} else if (this.task === 2) {
|
|
@@ -215,7 +215,7 @@ Mysql.prototype.load = async function(file, func) {
|
|
|
215
215
|
try {
|
|
216
216
|
var data = file.loadText();
|
|
217
217
|
// 将SQL文件内容分割成单独的语句
|
|
218
|
-
const arr = data.split(';\
|
|
218
|
+
const arr = data.replace(/\r\n/g, '\n').split(';\n');
|
|
219
219
|
count = arr.length;
|
|
220
220
|
for (var i = 0; i < arr.length; i++) {
|
|
221
221
|
var sql_str = arr[i].trim();
|
|
@@ -267,7 +267,7 @@ Mysql.prototype.save = async function(file, func, tables = []) {
|
|
|
267
267
|
|
|
268
268
|
try {
|
|
269
269
|
// 开始导出数据库
|
|
270
|
-
stream.write('SET FOREIGN_KEY_CHECKS = 0;\
|
|
270
|
+
stream.write('SET FOREIGN_KEY_CHECKS = 0;\n\n');
|
|
271
271
|
count++;
|
|
272
272
|
|
|
273
273
|
if (!tables.length) {
|
|
@@ -284,15 +284,15 @@ Mysql.prototype.save = async function(file, func, tables = []) {
|
|
|
284
284
|
try {
|
|
285
285
|
// 导出表结构
|
|
286
286
|
const createTable = await this.run(`SHOW CREATE TABLE ${tableName}`);
|
|
287
|
-
stream.write(`-- 表结构: ${tableName}\
|
|
288
|
-
stream.write(`DROP TABLE IF EXISTS \`${tableName}\`;\
|
|
287
|
+
stream.write(`-- 表结构: ${tableName}\n`);
|
|
288
|
+
stream.write(`DROP TABLE IF EXISTS \`${tableName}\`;\n`);
|
|
289
289
|
count++;
|
|
290
|
-
stream.write(`${createTable[0]['Create Table']};\
|
|
290
|
+
stream.write(`${createTable[0]['Create Table']};\n\n`);
|
|
291
291
|
count++;
|
|
292
292
|
// 导出表数据
|
|
293
293
|
const rows = await this.run(`SELECT * FROM ${tableName}`);
|
|
294
294
|
if (rows.length > 0) {
|
|
295
|
-
stream.write(`-- 表数据: ${tableName}\
|
|
295
|
+
stream.write(`-- 表数据: ${tableName}\n`);
|
|
296
296
|
for (const row of rows) {
|
|
297
297
|
const rowValues = Object.values(row)
|
|
298
298
|
.map(value => {
|
|
@@ -311,10 +311,10 @@ Mysql.prototype.save = async function(file, func, tables = []) {
|
|
|
311
311
|
.replace(/\u0000/g, '\\0') + "'";
|
|
312
312
|
})
|
|
313
313
|
.join(',');
|
|
314
|
-
stream.write(`INSERT INTO ${tableName} VALUES (${rowValues});\
|
|
314
|
+
stream.write(`INSERT INTO ${tableName} VALUES (${rowValues});\n`);
|
|
315
315
|
count++;
|
|
316
316
|
}
|
|
317
|
-
stream.write('\
|
|
317
|
+
stream.write('\n');
|
|
318
318
|
}
|
|
319
319
|
} catch (err) {
|
|
320
320
|
errors.push({
|
|
@@ -331,7 +331,7 @@ Mysql.prototype.save = async function(file, func, tables = []) {
|
|
|
331
331
|
}
|
|
332
332
|
|
|
333
333
|
// 完成导出
|
|
334
|
-
stream.write('SET FOREIGN_KEY_CHECKS = 1;\
|
|
334
|
+
stream.write('SET FOREIGN_KEY_CHECKS = 1;\n');
|
|
335
335
|
count++;
|
|
336
336
|
stream.end();
|
|
337
337
|
} catch (err) {
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_mysql",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"
|
|
8
|
-
"mm_logs": "^1.1.9",
|
|
7
|
+
"mm_logs": "^1.2.0",
|
|
9
8
|
"mysql2": "^3.14.0"
|
|
10
9
|
},
|
|
11
10
|
"scripts": {
|
package/test.js
CHANGED
|
@@ -14,7 +14,9 @@ async function test() {
|
|
|
14
14
|
multipleStatements: true
|
|
15
15
|
});
|
|
16
16
|
// await sql.open();
|
|
17
|
-
|
|
17
|
+
|
|
18
|
+
// await sql.load("./upgrade.sql");
|
|
19
|
+
|
|
18
20
|
db = sql.db();
|
|
19
21
|
db.table = 'user_account';
|
|
20
22
|
db.key = "user_id";
|
|
@@ -23,22 +25,22 @@ async function test() {
|
|
|
23
25
|
});
|
|
24
26
|
console.log("查询对象", obj);
|
|
25
27
|
|
|
26
|
-
var i = 0;
|
|
27
|
-
var timer = setInterval(async () => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}, 3000);
|
|
28
|
+
// var i = 0;
|
|
29
|
+
// var timer = setInterval(async () => {
|
|
30
|
+
// var now = new Date();
|
|
31
|
+
// var start = now.getTime();
|
|
32
|
+
// console.log(start);
|
|
33
|
+
// var bl = await db.set({
|
|
34
|
+
// "user_id": "1"
|
|
35
|
+
// }, {
|
|
36
|
+
// "nickname": "hao" + (i++)
|
|
37
|
+
// });
|
|
38
|
+
// now = new Date();
|
|
39
|
+
// var end = now.getTime();
|
|
40
|
+
// console.log(end);
|
|
41
|
+
// console.log(end - start);
|
|
42
|
+
// console.log("测试", bl);
|
|
43
|
+
// }, 3000);
|
|
42
44
|
|
|
43
45
|
// obj.nickname = "hao";
|
|
44
46
|
// console.log("查询对象", obj, db.error, db.sql);
|
|
@@ -67,7 +69,7 @@ async function test() {
|
|
|
67
69
|
// });
|
|
68
70
|
// console.log("导入结果", ret);
|
|
69
71
|
|
|
70
|
-
|
|
72
|
+
sql.close();
|
|
71
73
|
|
|
72
74
|
}
|
|
73
75
|
test();
|
package/upgrade.sql
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Navicat Premium Data Transfer
|
|
3
|
+
|
|
4
|
+
Source Server : 本地
|
|
5
|
+
Source Server Type : MySQL
|
|
6
|
+
Source Server Version : 50726
|
|
7
|
+
Source Host : localhost:3306
|
|
8
|
+
Source Schema : card
|
|
9
|
+
|
|
10
|
+
Target Server Type : MySQL
|
|
11
|
+
Target Server Version : 50726
|
|
12
|
+
File Encoding : 65001
|
|
13
|
+
|
|
14
|
+
Date: 08/04/2025 09:01:37
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
SET NAMES utf8mb4;
|
|
18
|
+
SET FOREIGN_KEY_CHECKS = 0;
|
|
19
|
+
|
|
20
|
+
-- ----------------------------
|
|
21
|
+
-- Table structure for test
|
|
22
|
+
-- ----------------------------
|
|
23
|
+
DROP TABLE IF EXISTS `test`;
|
|
24
|
+
CREATE TABLE `test` (
|
|
25
|
+
`test_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
26
|
+
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
|
|
27
|
+
PRIMARY KEY (`test_id`) USING BTREE
|
|
28
|
+
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
|
29
|
+
|
|
30
|
+
-- ----------------------------
|
|
31
|
+
-- Records of test
|
|
32
|
+
-- ----------------------------
|
|
33
|
+
|
|
34
|
+
SET FOREIGN_KEY_CHECKS = 1;
|