mm_mysql 2.1.0 → 2.2.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/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # mm_mysql
2
- 这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数
2
+ 一个简洁、高效的Node.js MySQL操作库,支持async/await语法,提供直观的API和强大的功能扩展。
3
+
4
+ [![npm version](https://badge.fury.io/js/mm_mysql.svg)](https://www.npmjs.com/package/mm_mysql)
5
+ [![GitHub issues](https://img.shields.io/github/issues/qiuwenwu91/mm_mysql.svg)](https://gitee.com/qiuwenwu91/mm_mysql/issues)
6
+ [![GitHub license](https://img.shields.io/github/license/qiuwenwu91/mm_mysql.svg)](https://github.com/qiuwenwu91/mm_mysql/blob/main/LICENSE)
3
7
 
4
8
  ## 安装
5
9
 
@@ -21,20 +25,28 @@ const mysql = new Mysql({
21
25
  database: "your_database"
22
26
  });
23
27
 
24
- // 初始化并打开连接
25
- await mysql.init();
28
+ // 打开连接(不需要调用init()方法)
26
29
  await mysql.open();
27
30
 
28
- // 获取数据库管理器
29
- const db = mysql.db();
31
+ // 获取数据库管理器并设置表名
32
+ const db = mysql.db().new('users', 'id');
30
33
 
31
34
  // 执行操作...
32
35
 
33
36
  // 使用完后关闭连接
34
37
  await mysql.close();
35
- await mysql.destroy();
36
38
  ```
37
39
 
40
+ ## 主要特性
41
+
42
+ - 支持async/await语法,避免回调地狱
43
+ - 提供简洁直观的API接口
44
+ - 内置事务支持
45
+ - 自动类型推断的表创建功能(支持int、float、varchar、text、datetime、date、time、boolean等多种数据类型)
46
+ - 灵活的查询条件支持
47
+ - 支持连接池管理
48
+ - 调试模式支持
49
+
38
50
  ## API文档
39
51
 
40
52
  ### Mysql类
@@ -72,95 +84,104 @@ const config = {
72
84
  - `config` {Object} 配置对象
73
85
  - 返回:{Mysql} 实例本身
74
86
 
75
- ##### init()
76
- 初始化服务
77
- - 返回:{Promise} 初始化结果
78
-
79
- ##### open(reset)
87
+ ##### open()
80
88
  打开数据库连接
81
- - `reset` {Boolean} 是否重置连接
82
- - 返回:{Promise} 连接结果
89
+ - 返回:{Promise<boolean>} 连接结果
83
90
 
84
91
  ##### close()
85
92
  关闭数据库连接
86
- - 返回:{Promise} 关闭结果
87
-
88
- ##### destroy()
89
- 销毁服务,释放资源
90
- - 返回:{Promise} 销毁结果
93
+ - 返回:{Promise<boolean>} 关闭结果
91
94
 
92
- ##### run(sql, params)
95
+ ##### run(sql, params, timeout)
93
96
  执行查询SQL语句
94
97
  - `sql` {String} SQL语句
95
- - `params` {Array} 参数数组
96
- - 返回:{Promise|Array} 查询结果数组
98
+ - `params` {Array} 参数数组(可选)
99
+ - `timeout` {Number} 超时时间(毫秒,可选)
100
+ - 返回:{Promise<Array>} 查询结果数组
97
101
 
98
- ##### exec(sql, params)
102
+ ##### exec(sql, params, timeout)
99
103
  执行非查询SQL语句(增删改)
100
104
  - `sql` {String} SQL语句
101
- - `params` {Array} 参数数组
102
- - 返回:{Promise|Number} 影响的行数
105
+ - `params` {Array} 参数数组(可选)
106
+ - `timeout` {Number} 超时时间(毫秒,可选)
107
+ - 返回:{Promise<Object>} 执行结果对象,包含affectedRows和insertId等信息
103
108
 
104
109
  ##### read(table, where, options)
105
110
  读取表数据
106
111
  - `table` {String} 表名
107
112
  - `where` {Object} 查询条件
108
113
  - `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} 加载结果
114
+ - 返回:{Promise<Array>} 查询结果
120
115
 
121
116
  ##### db()
122
117
  获取数据库管理器
123
118
  - 返回:{DB} 数据库管理器实例
124
119
 
120
+ ##### beginTransaction()
121
+ 开始事务
122
+ - 返回:{Promise<Object>} 事务连接对象
123
+
124
+ ##### transaction(callback)
125
+ 执行事务
126
+ - `callback` {Function} 事务回调函数,接收事务连接作为参数
127
+ - 返回:{Promise<any>} 回调函数的返回结果
128
+
125
129
  ### DB类方法
126
130
 
127
- #### add(table, data)
128
- 添加数据
131
+ #### createTable(model, options)
132
+ 自动创建数据库表,支持多种数据类型推断
133
+ - `model` {Object} 模型对象,包含表名和字段定义
134
+ - `table` {String} 表名
135
+ - `fields` {Object} 字段定义,支持以下数据类型:
136
+ - 数字类型(自动推断为INT/FLOAT)
137
+ - 字符串类型(自动推断为VARCHAR/TEXT/DATE/DATETIME/TIME)
138
+ - 布尔类型(自动推断为TINYINT(1))
139
+ - Date对象(自动推断为DATETIME)
140
+ - 时间戳(自动推断为DATETIME)
141
+ - `options` {Object} 选项配置(可选)
142
+ - `primary_key` {String} 主键字段名(可选)
143
+ - `if_not_exists` {Boolean} 是否仅当表不存在时创建(可选,默认为true)
144
+ - 返回:{Promise<Object>} 执行结果
145
+
146
+ #### new(table, key)
147
+ 创建数据库表管理器
129
148
  - `table` {String} 表名
130
- - `data` {Object} 数据对象
131
- - 返回:{Promise|Number} 新增的ID或影响行数
149
+ - `key` {String} 主键名(可选,默认为{table}_id)
150
+ - 返回:{DB} 数据库表管理器实例
132
151
 
133
- #### set(table, where, data)
134
- 修改数据
135
- - `table` {String} 表名
136
- - `where` {Object} 条件对象
152
+ #### add(data)
153
+ 添加数据
137
154
  - `data` {Object} 数据对象
138
- - 返回:{Promise|Number} 影响行数
155
+ - 返回:{Promise<Number>} 插入的ID
139
156
 
140
- #### get(table, where, options)
141
- 查询数据
142
- - `table` {String} 表名
143
- - `where` {Object} 条件对象
144
- - `options` {Object} 选项(fields, order, group, limit等)
145
- - 返回:{Promise|Array} 查询结果数组
157
+ #### get(where, options)
158
+ 获取数据
159
+ - `where` {Object} 条件对象(可选)
160
+ - `options` {Object} 选项配置(可选)
161
+ - 返回:{Promise<Array>} 查询结果
162
+
163
+ #### set(data, where)
164
+ 更新数据
165
+ - `data` {Object} 数据对象
166
+ - `where` {Object} 条件对象(可选)
167
+ - 返回:{Promise<Number>} 影响行数
146
168
 
147
- #### del(table, where)
169
+ #### del(where)
148
170
  删除数据
149
- - `table` {String} 表名
150
- - `where` {Object} 条件对象
151
- - 返回:{Promise|Number} 影响行数
171
+ - `where` {Object} 条件对象(可选)
172
+ - 返回:{Promise<Number>} 影响行数
152
173
 
153
174
  #### exec(sql, params)
154
175
  执行SQL语句
155
176
  - `sql` {String} SQL语句
156
177
  - `params` {Array} 参数数组
157
- - 返回:{Promise|Object} 执行结果
178
+ - 返回:{Promise<Object>} 执行结果
158
179
 
159
180
  #### run(sql, params)
160
181
  执行查询SQL语句
161
182
  - `sql` {String} SQL语句
162
183
  - `params` {Array} 参数数组
163
- - 返回:{Promise|Array} 查询结果
184
+ - 返回:{Promise<Array>} 查询结果
164
185
 
165
186
  ### 事务操作
166
187
 
@@ -176,13 +197,68 @@ const config = {
176
197
 
177
198
  ### 使用示例
178
199
 
200
+ #### 表创建示例
201
+
202
+ ```javascript
203
+ const { Mysql } = require('mm_mysql');
204
+
205
+ async function createTableExample() {
206
+ const mysql = new Mysql({
207
+ host: "localhost",
208
+ user: "root",
209
+ password: "123456",
210
+ database: "test"
211
+ });
212
+
213
+ await mysql.open();
214
+
215
+ try {
216
+ // 创建数据库表管理器
217
+ const db = mysql.db();
218
+
219
+ // 定义表模型
220
+ const userModel = {
221
+ table: 'users',
222
+ fields: {
223
+ id: 1, // 整数类型 -> INT
224
+ username: 'test', // 字符串类型 -> VARCHAR(255)
225
+ email: 'test@example.com', // 字符串类型 -> VARCHAR(255)
226
+ age: 25, // 整数类型 -> INT
227
+ salary: 5000.50, // 浮点数类型 -> FLOAT
228
+ active: true, // 布尔类型 -> TINYINT(1)
229
+ birth_date: '2000-01-01', // 日期字符串 -> DATE
230
+ bio: '这是一个很长的个人简介...', // 长字符串 -> TEXT
231
+ created_at: '2023-01-01 12:00:00', // 日期时间字符串 -> DATETIME
232
+ login_time: new Date(), // Date对象 -> DATETIME
233
+ last_active: Date.now() // 时间戳 -> DATETIME
234
+ }
235
+ };
236
+
237
+ // 创建表
238
+ const result = await db.createTable(userModel, {
239
+ primary_key: 'id',
240
+ if_not_exists: true
241
+ });
242
+
243
+ console.log('表创建成功:', result);
244
+
245
+ } catch (error) {
246
+ console.error('创建表失败:', error);
247
+ } finally {
248
+ await mysql.close();
249
+ }
250
+ }
251
+
252
+ createTableExample().catch(console.error);
253
+ ```
254
+
179
255
  #### 基本查询操作
180
256
 
181
257
  ```javascript
182
258
  const { Mysql } = require('mm_mysql');
183
259
 
184
260
  async function main() {
185
- const db = new Mysql({
261
+ const mysql = new Mysql({
186
262
  host: "localhost",
187
263
  user: "root",
188
264
  password: "123456",
@@ -190,20 +266,21 @@ async function main() {
190
266
  debug: true
191
267
  });
192
268
 
193
- await db.init();
194
- await db.open();
269
+ await mysql.open();
195
270
 
196
271
  // 执行查询
197
- const users = await db.run('SELECT * FROM users WHERE age > ?', [20]);
272
+ const users = await mysql.run('SELECT * FROM users WHERE age > ?', [20]);
198
273
  console.log(users);
199
274
 
200
275
  // 添加数据
201
- const insertId = await db.exec('INSERT INTO users (name, age) VALUES (?, ?)', ['张三', 25]);
276
+ const result = await mysql.exec('INSERT INTO users (name, age) VALUES (?, ?)', ['张三', 25]);
277
+ console.log('Inserted ID:', result.insertId);
278
+ console.log('Affected rows:', result.affectedRows);
202
279
 
203
280
  // 使用db管理器
204
- const dbManager = db.db();
205
- dbManager.table = 'users';
206
- const result = await dbManager.add({name: '李四', age: 26});
281
+ const db = mysql.db().new('users', 'id');
282
+ const newId = await db.add({name: '李四', age: 26});
283
+ console.log('New user ID:', newId);
207
284
 
208
285
  await db.close();
209
286
  await db.destroy();
@@ -212,47 +289,92 @@ async function main() {
212
289
  main().catch(console.error);
213
290
  ```
214
291
 
215
- #### 数据库结构导入导出
292
+ #### 使用DB类的完整示例
293
+
294
+ ```javascript
295
+ const { Mysql } = require('mm_mysql');
296
+
297
+ async function dbClassExample() {
298
+ const mysql = new Mysql({
299
+ host: "localhost",
300
+ user: "root",
301
+ password: "123456",
302
+ database: "test"
303
+ });
304
+
305
+ await mysql.open();
306
+
307
+ try {
308
+ // 创建DB实例
309
+ const db = mysql.db().new('users', 'id');
310
+
311
+ // 添加数据
312
+ const newId = await db.add({name: '王五', age: 30, email: 'wangwu@example.com'});
313
+ console.log('New user ID:', newId);
314
+
315
+ // 获取数据
316
+ const user = await db.get({id: newId});
317
+ console.log('User:', user);
318
+
319
+ // 更新数据
320
+ const updatedRows = await db.set({age: 31, email: 'wangwu_updated@example.com'}, {id: newId});
321
+ console.log('Updated rows:', updatedRows);
322
+
323
+ // 删除数据
324
+ const deletedRows = await db.del({id: newId});
325
+ console.log('Deleted rows:', deletedRows);
326
+
327
+ } finally {
328
+ await mysql.close();
329
+ }
330
+ }
331
+
332
+ dbClassExample().catch(console.error);
333
+ ```
334
+
335
+ #### 事务操作示例
216
336
 
217
337
  ```javascript
218
338
  const { Mysql } = require('mm_mysql');
219
339
 
220
- async function exportAndImport() {
221
- const db = new Mysql({
340
+ async function transactionExample() {
341
+ const mysql = new Mysql({
222
342
  host: "localhost",
223
343
  user: "root",
224
344
  password: "123456",
225
345
  database: "test"
226
346
  });
227
347
 
228
- await db.init();
229
- await db.open();
348
+ await mysql.open();
230
349
 
231
350
  try {
232
- // 创建文件对象(需实现相应接口)
233
- const file = { /* 文件对象实现 */ };
351
+ // 使用transaction方法执行事务
352
+ const result = await mysql.transaction(async (conn) => {
353
+ // 在事务中执行多个操作
354
+ const insertResult1 = await conn.exec('INSERT INTO users (name, age) VALUES (?, ?)', ['赵六', 35]);
355
+ const insertResult2 = await conn.exec('INSERT INTO users (name, age) VALUES (?, ?)', ['孙七', 40]);
356
+
357
+ // 如果都成功,返回结果
358
+ return { user1: insertResult1.insertId, user2: insertResult2.insertId };
359
+ });
234
360
 
235
- // 导出数据库结构到文件
236
- await db.save(file);
237
- console.log('数据库结构已导出');
361
+ console.log('Transaction succeeded:', result);
238
362
 
239
- // 从文件导入数据库结构
240
- await db.load(file);
241
- console.log('数据库结构已导入');
363
+ } catch (error) {
364
+ console.error('Transaction failed:', error);
242
365
  } finally {
243
- await db.close();
244
- await db.destroy();
366
+ await mysql.close();
245
367
  }
246
368
  }
247
369
 
248
- exportAndImport().catch(console.error);
370
+ transactionExample().catch(console.error);
249
371
  ```
250
372
 
251
373
  ## 注意事项
252
374
 
253
375
  1. 使用前请确保已正确配置数据库连接信息
254
376
  2. 建议使用try-catch处理可能出现的错误
255
- 3. 在程序结束时调用close()和destroy()关闭数据库连接并释放资源
377
+ 3. 在程序结束时调用close()关闭数据库连接
256
378
  4. 使用事务时,记得正确处理事务的提交和回滚
257
379
  5. 查询条件支持多种格式:
258
380
  - `_min`: 大于等于
@@ -260,7 +382,28 @@ exportAndImport().catch(console.error);
260
382
  - `_not`: 不等于
261
383
  - `_has`: IN查询
262
384
  - `_like`: LIKE查询
385
+ 6. createTable方法会根据字段值自动推断数据类型,如需更精确的类型控制,建议直接使用SQL语句
386
+
387
+ ## 更新日志
388
+
389
+ ### v2.2.0
390
+ - 新增createTable方法,支持多种数据类型自动推断
391
+ - 支持boolean类型自动转换为TINYINT(1)
392
+ - 支持date字符串自动识别为DATE类型
393
+ - 支持Date对象自动识别为DATETIME类型
394
+ - 支持时间戳自动识别为DATETIME类型
263
395
 
264
396
  ## 许可证
265
397
 
266
398
  ISC License
399
+
400
+ ## 贡献
401
+
402
+ 欢迎提交Issue和Pull Request!
403
+
404
+ ## 联系方式
405
+
406
+ 如有问题或建议,请通过以下方式联系:
407
+
408
+ - 项目地址:https://gitee.com/qiuwenwu91/mm_mysql
409
+ - 邮箱:qiuwenwu91@163.com