mm_mysql 2.0.2 → 2.0.4
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/README.md +266 -234
- package/db.js +516 -286
- package/index.js +1022 -394
- package/package.json +13 -12
- package/sql.js +993 -396
- package/config.json +0 -8
- package/link_model.js +0 -132
- package/sql.json +0 -56
- package/test.js +0 -846
- package/upgrade.sql +0 -34
package/README.md
CHANGED
|
@@ -1,234 +1,266 @@
|
|
|
1
|
-
# mm_mysql
|
|
2
|
-
这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数
|
|
3
|
-
|
|
4
|
-
## 安装
|
|
5
|
-
|
|
6
|
-
```bash
|
|
7
|
-
npm install mm_mysql
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
## 基本使用
|
|
11
|
-
|
|
12
|
-
```javascript
|
|
13
|
-
const {
|
|
14
|
-
|
|
15
|
-
// 创建mysql实例
|
|
16
|
-
const mysql =
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
};
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
#### 主要方法
|
|
69
|
-
|
|
70
|
-
##### setConfig(config)
|
|
71
|
-
设置数据库配置参数
|
|
72
|
-
- `config` {Object
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
- `
|
|
101
|
-
- `
|
|
102
|
-
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
1
|
+
# mm_mysql
|
|
2
|
+
这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数
|
|
3
|
+
|
|
4
|
+
## 安装
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm install mm_mysql
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## 基本使用
|
|
11
|
+
|
|
12
|
+
```javascript
|
|
13
|
+
const { Mysql } = require('mm_mysql');
|
|
14
|
+
|
|
15
|
+
// 创建mysql实例
|
|
16
|
+
const mysql = new Mysql({
|
|
17
|
+
host: "localhost",
|
|
18
|
+
port: 3306,
|
|
19
|
+
user: "root",
|
|
20
|
+
password: "your_password",
|
|
21
|
+
database: "your_database"
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// 初始化并打开连接
|
|
25
|
+
await mysql.init();
|
|
26
|
+
await mysql.open();
|
|
27
|
+
|
|
28
|
+
// 获取数据库管理器
|
|
29
|
+
const db = mysql.db();
|
|
30
|
+
|
|
31
|
+
// 执行操作...
|
|
32
|
+
|
|
33
|
+
// 使用完后关闭连接
|
|
34
|
+
await mysql.close();
|
|
35
|
+
await mysql.destroy();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## API文档
|
|
39
|
+
|
|
40
|
+
### Mysql类
|
|
41
|
+
|
|
42
|
+
#### 构造函数
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
new Mysql(config)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
参数说明:
|
|
49
|
+
- `config` {Object} 数据库配置对象
|
|
50
|
+
|
|
51
|
+
#### 配置参数
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
const config = {
|
|
55
|
+
host: "127.0.0.1", // 服务器地址
|
|
56
|
+
port: 3306, // 端口号
|
|
57
|
+
user: "root", // 连接用户名
|
|
58
|
+
password: "password", // 连接密码
|
|
59
|
+
database: "dbname", // 数据库名
|
|
60
|
+
multipleStatements: false, // 是否支持多个sql语句同时操作
|
|
61
|
+
debug: false, // 是否打印调试日志
|
|
62
|
+
charset: "utf8mb4", // 字符集
|
|
63
|
+
timezone: "+08:00", // 时区
|
|
64
|
+
connectionLimit: 10 // 连接池大小
|
|
65
|
+
};
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### 主要方法
|
|
69
|
+
|
|
70
|
+
##### setConfig(config)
|
|
71
|
+
设置数据库配置参数
|
|
72
|
+
- `config` {Object} 配置对象
|
|
73
|
+
- 返回:{Mysql} 实例本身
|
|
74
|
+
|
|
75
|
+
##### init()
|
|
76
|
+
初始化服务
|
|
77
|
+
- 返回:{Promise} 初始化结果
|
|
78
|
+
|
|
79
|
+
##### open(reset)
|
|
80
|
+
打开数据库连接
|
|
81
|
+
- `reset` {Boolean} 是否重置连接
|
|
82
|
+
- 返回:{Promise} 连接结果
|
|
83
|
+
|
|
84
|
+
##### close()
|
|
85
|
+
关闭数据库连接
|
|
86
|
+
- 返回:{Promise} 关闭结果
|
|
87
|
+
|
|
88
|
+
##### destroy()
|
|
89
|
+
销毁服务,释放资源
|
|
90
|
+
- 返回:{Promise} 销毁结果
|
|
91
|
+
|
|
92
|
+
##### run(sql, params)
|
|
93
|
+
执行查询SQL语句
|
|
94
|
+
- `sql` {String} SQL语句
|
|
95
|
+
- `params` {Array} 参数数组
|
|
96
|
+
- 返回:{Promise|Array} 查询结果数组
|
|
97
|
+
|
|
98
|
+
##### exec(sql, params)
|
|
99
|
+
执行非查询SQL语句(增删改)
|
|
100
|
+
- `sql` {String} SQL语句
|
|
101
|
+
- `params` {Array} 参数数组
|
|
102
|
+
- 返回:{Promise|Number} 影响的行数
|
|
103
|
+
|
|
104
|
+
##### read(table, where, options)
|
|
105
|
+
读取表数据
|
|
106
|
+
- `table` {String} 表名
|
|
107
|
+
- `where` {Object} 查询条件
|
|
108
|
+
- `options` {Object} 选项配置
|
|
109
|
+
- 返回:{Promise|Array} 查询结果
|
|
110
|
+
|
|
111
|
+
##### save(file)
|
|
112
|
+
保存数据库结构到SQL文件
|
|
113
|
+
- `file` {Object} 文件对象(需实现hasFile、readText等方法)
|
|
114
|
+
- 返回:{Promise} 保存结果
|
|
115
|
+
|
|
116
|
+
##### load(file)
|
|
117
|
+
从SQL文件加载数据库结构和数据
|
|
118
|
+
- `file` {Object} 文件对象(需实现hasFile、readText等方法)
|
|
119
|
+
- 返回:{Promise} 加载结果
|
|
120
|
+
|
|
121
|
+
##### db()
|
|
122
|
+
获取数据库管理器
|
|
123
|
+
- 返回:{DB} 数据库管理器实例
|
|
124
|
+
|
|
125
|
+
### DB类方法
|
|
126
|
+
|
|
127
|
+
#### add(table, data)
|
|
128
|
+
添加数据
|
|
129
|
+
- `table` {String} 表名
|
|
130
|
+
- `data` {Object} 数据对象
|
|
131
|
+
- 返回:{Promise|Number} 新增的ID或影响行数
|
|
132
|
+
|
|
133
|
+
#### set(table, where, data)
|
|
134
|
+
修改数据
|
|
135
|
+
- `table` {String} 表名
|
|
136
|
+
- `where` {Object} 条件对象
|
|
137
|
+
- `data` {Object} 数据对象
|
|
138
|
+
- 返回:{Promise|Number} 影响行数
|
|
139
|
+
|
|
140
|
+
#### get(table, where, options)
|
|
141
|
+
查询数据
|
|
142
|
+
- `table` {String} 表名
|
|
143
|
+
- `where` {Object} 条件对象
|
|
144
|
+
- `options` {Object} 选项(fields, order, group, limit等)
|
|
145
|
+
- 返回:{Promise|Array} 查询结果数组
|
|
146
|
+
|
|
147
|
+
#### del(table, where)
|
|
148
|
+
删除数据
|
|
149
|
+
- `table` {String} 表名
|
|
150
|
+
- `where` {Object} 条件对象
|
|
151
|
+
- 返回:{Promise|Number} 影响行数
|
|
152
|
+
|
|
153
|
+
#### exec(sql, params)
|
|
154
|
+
执行SQL语句
|
|
155
|
+
- `sql` {String} SQL语句
|
|
156
|
+
- `params` {Array} 参数数组
|
|
157
|
+
- 返回:{Promise|Object} 执行结果
|
|
158
|
+
|
|
159
|
+
#### run(sql, params)
|
|
160
|
+
执行查询SQL语句
|
|
161
|
+
- `sql` {String} SQL语句
|
|
162
|
+
- `params` {Array} 参数数组
|
|
163
|
+
- 返回:{Promise|Array} 查询结果
|
|
164
|
+
|
|
165
|
+
### 事务操作
|
|
166
|
+
|
|
167
|
+
#### beginTransaction()
|
|
168
|
+
开始事务
|
|
169
|
+
- 返回:{Promise} 事务结果
|
|
170
|
+
|
|
171
|
+
#### transaction(sql, params)
|
|
172
|
+
在事务中执行SQL
|
|
173
|
+
- `sql` {String|Array} SQL语句或语句数组
|
|
174
|
+
- `params` {Array} 参数数组
|
|
175
|
+
- 返回:{Promise} 执行结果
|
|
176
|
+
|
|
177
|
+
### 使用示例
|
|
178
|
+
|
|
179
|
+
#### 基本查询操作
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
const { Mysql } = require('mm_mysql');
|
|
183
|
+
|
|
184
|
+
async function main() {
|
|
185
|
+
const db = new Mysql({
|
|
186
|
+
host: "localhost",
|
|
187
|
+
user: "root",
|
|
188
|
+
password: "123456",
|
|
189
|
+
database: "test",
|
|
190
|
+
debug: true
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
await db.init();
|
|
194
|
+
await db.open();
|
|
195
|
+
|
|
196
|
+
// 执行查询
|
|
197
|
+
const users = await db.run('SELECT * FROM users WHERE age > ?', [20]);
|
|
198
|
+
console.log(users);
|
|
199
|
+
|
|
200
|
+
// 添加数据
|
|
201
|
+
const insertId = await db.exec('INSERT INTO users (name, age) VALUES (?, ?)', ['张三', 25]);
|
|
202
|
+
|
|
203
|
+
// 使用db管理器
|
|
204
|
+
const dbManager = db.db();
|
|
205
|
+
dbManager.table = 'users';
|
|
206
|
+
const result = await dbManager.add({name: '李四', age: 26});
|
|
207
|
+
|
|
208
|
+
await db.close();
|
|
209
|
+
await db.destroy();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
main().catch(console.error);
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
#### 数据库结构导入导出
|
|
216
|
+
|
|
217
|
+
```javascript
|
|
218
|
+
const { Mysql } = require('mm_mysql');
|
|
219
|
+
|
|
220
|
+
async function exportAndImport() {
|
|
221
|
+
const db = new Mysql({
|
|
222
|
+
host: "localhost",
|
|
223
|
+
user: "root",
|
|
224
|
+
password: "123456",
|
|
225
|
+
database: "test"
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
await db.init();
|
|
229
|
+
await db.open();
|
|
230
|
+
|
|
231
|
+
try {
|
|
232
|
+
// 创建文件对象(需实现相应接口)
|
|
233
|
+
const file = { /* 文件对象实现 */ };
|
|
234
|
+
|
|
235
|
+
// 导出数据库结构到文件
|
|
236
|
+
await db.save(file);
|
|
237
|
+
console.log('数据库结构已导出');
|
|
238
|
+
|
|
239
|
+
// 从文件导入数据库结构
|
|
240
|
+
await db.load(file);
|
|
241
|
+
console.log('数据库结构已导入');
|
|
242
|
+
} finally {
|
|
243
|
+
await db.close();
|
|
244
|
+
await db.destroy();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
exportAndImport().catch(console.error);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## 注意事项
|
|
252
|
+
|
|
253
|
+
1. 使用前请确保已正确配置数据库连接信息
|
|
254
|
+
2. 建议使用try-catch处理可能出现的错误
|
|
255
|
+
3. 在程序结束时调用close()和destroy()关闭数据库连接并释放资源
|
|
256
|
+
4. 使用事务时,记得正确处理事务的提交和回滚
|
|
257
|
+
5. 查询条件支持多种格式:
|
|
258
|
+
- `_min`: 大于等于
|
|
259
|
+
- `_max`: 小于等于
|
|
260
|
+
- `_not`: 不等于
|
|
261
|
+
- `_has`: IN查询
|
|
262
|
+
- `_like`: LIKE查询
|
|
263
|
+
|
|
264
|
+
## 许可证
|
|
265
|
+
|
|
266
|
+
ISC License
|