mm_mysql 2.5.6 → 2.6.0
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/example/index.js +40 -0
- package/index.js +24 -574
- package/lib/mysql.js +536 -0
- package/lib/query_error.js +22 -0
- package/{sql.js → lib/sql.js} +14 -4
- package/lib/tools/index.js +16 -0
- package/lib/tools/mysql/index.js +208 -0
- package/lib/tools/mysql_rw/index.js +228 -0
- package/lib/tools/sql_dump/index.js +181 -0
- package/package.json +51 -43
- package/test.js +12 -6
- package/tool/mysql/index.js +7 -0
- package/tool/mysql/tool.json +118 -0
- package/tool/mysql_rw/index.js +7 -0
- package/tool/mysql_rw/tool.json +105 -0
- package/tool/sql_dump/index.js +7 -0
- package/tool/sql_dump/tool.json +83 -0
- /package/{db.js → lib/db.js} +0 -0
package/package.json
CHANGED
|
@@ -1,43 +1,51 @@
|
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "mm_mysql",
|
|
3
|
+
"version": "2.6.0",
|
|
4
|
+
"description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"mm_admin": "^1.1.0",
|
|
8
|
+
"mm_eslint": "^1.0.0",
|
|
9
|
+
"mm_helper": "^1.0.0",
|
|
10
|
+
"mm_mod": "^1.0.1",
|
|
11
|
+
"mysql2": "^3.24.2",
|
|
12
|
+
"sqlstring": "^2.3.3"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"start": "node index.js",
|
|
16
|
+
"test": "node test.js",
|
|
17
|
+
"lint": "eslint . --config eslint.config.js",
|
|
18
|
+
"lint:fix": "eslint . --config eslint.config.js --fix"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://gitee.com/qiuwenwu91/mm_mysql.git"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"mm_mod",
|
|
26
|
+
"mm_adapter",
|
|
27
|
+
"mm_persist",
|
|
28
|
+
"mysql",
|
|
29
|
+
"async",
|
|
30
|
+
"await",
|
|
31
|
+
"promise",
|
|
32
|
+
"mysql2",
|
|
33
|
+
"database",
|
|
34
|
+
"query",
|
|
35
|
+
"transaction",
|
|
36
|
+
"sql",
|
|
37
|
+
"db"
|
|
38
|
+
],
|
|
39
|
+
"layer": "infrastructure",
|
|
40
|
+
"group": "persistence",
|
|
41
|
+
"author": "qww",
|
|
42
|
+
"license": "ISC",
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=12.0.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"eslint": "^10.9.1",
|
|
48
|
+
"eslint-plugin-jsdoc": "^64.2.1",
|
|
49
|
+
"mm_eslint": "^1.7.7"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/test.js
CHANGED
|
@@ -40,24 +40,30 @@ class TestMysql {
|
|
|
40
40
|
TestMysql.prototype._init = async function() {
|
|
41
41
|
try {
|
|
42
42
|
console.log('=== 初始化测试环境(优化版) ===');
|
|
43
|
-
|
|
43
|
+
|
|
44
44
|
// 直接使用指定数据库,避免重复连接
|
|
45
45
|
if (!this.config.database) {
|
|
46
46
|
this.config.database = 'test_db';
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
|
|
49
|
+
// 先以空 database 连接并创建测试数据库,避免 Unknown database 报错
|
|
50
|
+
const bootstrap = new Mysql({ ...this.config, database: '' });
|
|
51
|
+
await bootstrap.open();
|
|
52
|
+
await bootstrap.exec(`CREATE DATABASE IF NOT EXISTS ${this.config.database}`);
|
|
53
|
+
await bootstrap.close();
|
|
54
|
+
|
|
49
55
|
// 创建Mysql实例
|
|
50
56
|
this._mysql = new Mysql(this.config);
|
|
51
|
-
|
|
57
|
+
|
|
52
58
|
// 连接数据库
|
|
53
59
|
await this._mysql.open();
|
|
54
|
-
|
|
60
|
+
|
|
55
61
|
// 创建测试数据库(如果不存在)
|
|
56
62
|
await this._createTestDatabase();
|
|
57
|
-
|
|
63
|
+
|
|
58
64
|
// 创建测试表
|
|
59
65
|
await this._createTestTable();
|
|
60
|
-
|
|
66
|
+
|
|
61
67
|
console.log('测试环境初始化完成');
|
|
62
68
|
return true;
|
|
63
69
|
} catch (error) {
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mysql",
|
|
3
|
+
"handler": "manager",
|
|
4
|
+
"title": "MySQL管理器",
|
|
5
|
+
"description": "操作 MySQL 数据库,支持 SQL 与 ORM 两种方式。适用场景:需要持久化数据、增删改查、统计计数时。工作流提示:action 必填(query/execute/runSql 配 sql,add/del/set/list/getObj/count/upsert 配 table/query/body),读结果在 list/count、写结果在 affected/id。",
|
|
6
|
+
"type": "data",
|
|
7
|
+
"params": {
|
|
8
|
+
"type": "object",
|
|
9
|
+
"properties": {
|
|
10
|
+
"action": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"description": "操作类型: SQL方法(query/execute/runSql) + ORM方法(add/del/set/list/getObj/count) + upsert + batch",
|
|
13
|
+
"enum": [
|
|
14
|
+
"query",
|
|
15
|
+
"execute",
|
|
16
|
+
"runSql",
|
|
17
|
+
"add",
|
|
18
|
+
"del",
|
|
19
|
+
"set",
|
|
20
|
+
"list",
|
|
21
|
+
"getObj",
|
|
22
|
+
"count",
|
|
23
|
+
"upsert",
|
|
24
|
+
"batch"
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
"sql": {
|
|
28
|
+
"type": "string",
|
|
29
|
+
"description": "SQL语句(SQL方法需要)"
|
|
30
|
+
},
|
|
31
|
+
"params": {
|
|
32
|
+
"type": "array",
|
|
33
|
+
"description": "SQL参数(可选)"
|
|
34
|
+
},
|
|
35
|
+
"table": {
|
|
36
|
+
"type": "string",
|
|
37
|
+
"description": "表名(ORM方法需要)"
|
|
38
|
+
},
|
|
39
|
+
"query": {
|
|
40
|
+
"type": "object",
|
|
41
|
+
"description": "查询条件(ORM方法需要)"
|
|
42
|
+
},
|
|
43
|
+
"body": {
|
|
44
|
+
"type": "object",
|
|
45
|
+
"description": "数据对象(add/set/upsert 操作需要)"
|
|
46
|
+
},
|
|
47
|
+
"unique": {
|
|
48
|
+
"type": "array",
|
|
49
|
+
"description": "唯一约束字段列表(upsert 操作,不更新的字段)"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"required": [
|
|
53
|
+
"action"
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
"returns": {
|
|
57
|
+
"description": "管理操作结果,读操作返回 list+count,写操作返回 affected,异常时返回 error",
|
|
58
|
+
"type": "object",
|
|
59
|
+
"properties": {
|
|
60
|
+
"list": {
|
|
61
|
+
"type": "array",
|
|
62
|
+
"description": "查询结果列表(query/run 操作)"
|
|
63
|
+
},
|
|
64
|
+
"count": {
|
|
65
|
+
"type": "number",
|
|
66
|
+
"description": "列表条目数/统计数"
|
|
67
|
+
},
|
|
68
|
+
"affected": {
|
|
69
|
+
"type": "number",
|
|
70
|
+
"description": "受影响的行数(写操作)"
|
|
71
|
+
},
|
|
72
|
+
"id": {
|
|
73
|
+
"type": "number",
|
|
74
|
+
"description": "插入的自增ID(upsert 操作)"
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"options": {
|
|
79
|
+
"host": {
|
|
80
|
+
"type": "string",
|
|
81
|
+
"description": "数据库主机地址",
|
|
82
|
+
"default": "localhost"
|
|
83
|
+
},
|
|
84
|
+
"port": {
|
|
85
|
+
"type": "number",
|
|
86
|
+
"description": "数据库端口",
|
|
87
|
+
"default": 3306
|
|
88
|
+
},
|
|
89
|
+
"user": {
|
|
90
|
+
"type": "string",
|
|
91
|
+
"description": "数据库用户名",
|
|
92
|
+
"default": "root"
|
|
93
|
+
},
|
|
94
|
+
"password": {
|
|
95
|
+
"type": "string",
|
|
96
|
+
"description": "数据库密码",
|
|
97
|
+
"default": "Asd159357"
|
|
98
|
+
},
|
|
99
|
+
"database": {
|
|
100
|
+
"type": "string",
|
|
101
|
+
"description": "数据库名",
|
|
102
|
+
"default": "mm"
|
|
103
|
+
},
|
|
104
|
+
"charset": {
|
|
105
|
+
"type": "string",
|
|
106
|
+
"description": "字符集",
|
|
107
|
+
"default": "utf8mb4"
|
|
108
|
+
},
|
|
109
|
+
"pool_size": {
|
|
110
|
+
"type": "number",
|
|
111
|
+
"description": "连接池大小",
|
|
112
|
+
"default": 10,
|
|
113
|
+
"min": 1,
|
|
114
|
+
"max": 100
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
"group": "storage"
|
|
118
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mysql_rw",
|
|
3
|
+
"handler": "manager",
|
|
4
|
+
"title": "MySQL读写分离",
|
|
5
|
+
"description": "MySQL 读写分离,SELECT 走读库、写操作走主库,多读库负载均衡。适用场景:高并发读、读写分离部署时。工作流提示:action 必填(SQL 配 sql/values,ORM 配 table/query/body),读在 list/count、写后 affected、路由在 route。",
|
|
6
|
+
"type": "data",
|
|
7
|
+
"params": {
|
|
8
|
+
"type": "object",
|
|
9
|
+
"properties": {
|
|
10
|
+
"action": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"description": "操作类型: SQL(query/insert/update/delete/execute/runSql) + ORM(add/del/set/getObj/list/count),写操作走主库,读操作走读库",
|
|
13
|
+
"enum": [
|
|
14
|
+
"query",
|
|
15
|
+
"insert",
|
|
16
|
+
"update",
|
|
17
|
+
"delete",
|
|
18
|
+
"execute",
|
|
19
|
+
"runSql",
|
|
20
|
+
"add",
|
|
21
|
+
"del",
|
|
22
|
+
"set",
|
|
23
|
+
"getObj",
|
|
24
|
+
"list",
|
|
25
|
+
"count"
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
"sql": {
|
|
29
|
+
"type": "string",
|
|
30
|
+
"description": "SQL 语句(runSql 可传多条以分号分隔的建表/插入语句,强制走主库)"
|
|
31
|
+
},
|
|
32
|
+
"values": {
|
|
33
|
+
"type": "array",
|
|
34
|
+
"description": "SQL 参数值(使用参数化查询防注入)"
|
|
35
|
+
},
|
|
36
|
+
"table": {
|
|
37
|
+
"type": "string",
|
|
38
|
+
"description": "表名(ORM 方法需要)"
|
|
39
|
+
},
|
|
40
|
+
"query": {
|
|
41
|
+
"type": "object",
|
|
42
|
+
"description": "查询条件(ORM 方法需要),如 {username: 'demo'} 或 {id: 1}"
|
|
43
|
+
},
|
|
44
|
+
"body": {
|
|
45
|
+
"type": "object",
|
|
46
|
+
"description": "数据对象(add/set 操作需要)"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"required": [
|
|
50
|
+
"action"
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
"returns": {
|
|
54
|
+
"description": "操作结果,返回 route 字段标识实际路由到的库",
|
|
55
|
+
"type": "object",
|
|
56
|
+
"properties": {
|
|
57
|
+
"list": {
|
|
58
|
+
"type": "array",
|
|
59
|
+
"description": "查询结果列表(query操作)"
|
|
60
|
+
},
|
|
61
|
+
"count": {
|
|
62
|
+
"type": "number",
|
|
63
|
+
"description": "查询结果数量(query操作)"
|
|
64
|
+
},
|
|
65
|
+
"affected": {
|
|
66
|
+
"type": "number",
|
|
67
|
+
"description": "影响行数或插入ID(insert/update/delete操作)"
|
|
68
|
+
},
|
|
69
|
+
"route": {
|
|
70
|
+
"type": "string",
|
|
71
|
+
"description": "实际路由: master/replica"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
"options": {
|
|
76
|
+
"master": {
|
|
77
|
+
"type": "object",
|
|
78
|
+
"description": "写库(主库)配置",
|
|
79
|
+
"properties": {
|
|
80
|
+
"host": { "type": "string", "description": "主机", "default": "localhost" },
|
|
81
|
+
"port": { "type": "number", "description": "端口", "default": 3306 },
|
|
82
|
+
"user": { "type": "string", "description": "用户名", "default": "root" },
|
|
83
|
+
"password": { "type": "string", "description": "密码", "default": "Asd159357" },
|
|
84
|
+
"database": { "type": "string", "description": "数据库名", "default": "mm" },
|
|
85
|
+
"connectionLimit": { "type": "number", "description": "连接池大小", "default": 10 }
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"replicas": {
|
|
89
|
+
"type": "array",
|
|
90
|
+
"description": "读库(从库)配置列表,多台随机负载均衡",
|
|
91
|
+
"items": {
|
|
92
|
+
"type": "object",
|
|
93
|
+
"properties": {
|
|
94
|
+
"host": { "type": "string", "description": "主机" },
|
|
95
|
+
"port": { "type": "number", "description": "端口", "default": 3306 },
|
|
96
|
+
"user": { "type": "string", "description": "用户名" },
|
|
97
|
+
"password": { "type": "string", "description": "密码" },
|
|
98
|
+
"database": { "type": "string", "description": "数据库名" },
|
|
99
|
+
"connectionLimit": { "type": "number", "description": "连接池大小", "default": 10 }
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
"group": "system"
|
|
105
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sql_dump",
|
|
3
|
+
"handler": "manager",
|
|
4
|
+
"title": "SQL导出管理器",
|
|
5
|
+
"description": "将 MySQL 表导出为 SQL 文件(建表语句+数据)。适用场景:备份表结构数据、迁移或重建环境时。工作流提示:table 必填导出表名,output_file 指定路径(默认 ./backup/{table}.sql),路径在 file_path、条数与行数在 statements/rows。",
|
|
6
|
+
"type": "data",
|
|
7
|
+
"params": {
|
|
8
|
+
"type": "object",
|
|
9
|
+
"properties": {
|
|
10
|
+
"table": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"description": "要导出的表名"
|
|
13
|
+
},
|
|
14
|
+
"output_file": {
|
|
15
|
+
"type": "string",
|
|
16
|
+
"description": "输出文件路径(可选,默认 ./backup/{table}.sql)"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"required": ["table"]
|
|
20
|
+
},
|
|
21
|
+
"returns": {
|
|
22
|
+
"description": "导出结果",
|
|
23
|
+
"type": "object",
|
|
24
|
+
"properties": {
|
|
25
|
+
"file_path": {
|
|
26
|
+
"type": "string",
|
|
27
|
+
"description": "导出文件绝对路径"
|
|
28
|
+
},
|
|
29
|
+
"table": {
|
|
30
|
+
"type": "string",
|
|
31
|
+
"description": "导出的表名"
|
|
32
|
+
},
|
|
33
|
+
"statements": {
|
|
34
|
+
"type": "number",
|
|
35
|
+
"description": "生成的 SQL 语句条数"
|
|
36
|
+
},
|
|
37
|
+
"rows": {
|
|
38
|
+
"type": "number",
|
|
39
|
+
"description": "导出的数据行数"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"options": {
|
|
44
|
+
"host": {
|
|
45
|
+
"type": "string",
|
|
46
|
+
"description": "数据库主机地址",
|
|
47
|
+
"default": "localhost"
|
|
48
|
+
},
|
|
49
|
+
"port": {
|
|
50
|
+
"type": "number",
|
|
51
|
+
"description": "数据库端口",
|
|
52
|
+
"default": 3306
|
|
53
|
+
},
|
|
54
|
+
"user": {
|
|
55
|
+
"type": "string",
|
|
56
|
+
"description": "数据库用户名",
|
|
57
|
+
"default": "root"
|
|
58
|
+
},
|
|
59
|
+
"password": {
|
|
60
|
+
"type": "string",
|
|
61
|
+
"description": "数据库密码",
|
|
62
|
+
"default": "Asd159357"
|
|
63
|
+
},
|
|
64
|
+
"database": {
|
|
65
|
+
"type": "string",
|
|
66
|
+
"description": "数据库名",
|
|
67
|
+
"default": "mm"
|
|
68
|
+
},
|
|
69
|
+
"charset": {
|
|
70
|
+
"type": "string",
|
|
71
|
+
"description": "字符集",
|
|
72
|
+
"default": "utf8mb4"
|
|
73
|
+
},
|
|
74
|
+
"pool_size": {
|
|
75
|
+
"type": "number",
|
|
76
|
+
"description": "连接池大小",
|
|
77
|
+
"default": 10,
|
|
78
|
+
"min": 1,
|
|
79
|
+
"max": 100
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
"group": "storage"
|
|
83
|
+
}
|
/package/{db.js → lib/db.js}
RENAMED
|
File without changes
|