mm_sql 1.3.7 → 1.3.8

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Admin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,2 +1,244 @@
1
- # mm_sql
2
- sql语句帮助类,用于封装继承到mysql、sqlite、sql server的帮助类
1
+ # mm_sql
2
+
3
+ 一个通用的SQL帮助类,支持通过切换db_type实现对不同数据库的操作,包括MySQL和SQLite等。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install mm_sql
9
+ ```
10
+
11
+ ## 依赖
12
+
13
+ - mm_mysql: ^2.0.3
14
+ - mm_sqlite: ^1.0.0
15
+
16
+ ## 使用方法
17
+
18
+ ### 1. 基本使用
19
+
20
+ ```javascript
21
+ // 引入模块
22
+ const Sql = require('mm_sql');
23
+
24
+ // 创建实例(默认使用MySQL)
25
+ const sql = new Sql({
26
+ host: 'localhost',
27
+ port: 3306,
28
+ user: 'root',
29
+ password: 'password',
30
+ database: 'test_db',
31
+ table: 'users'
32
+ });
33
+
34
+ // 初始化连接
35
+ await sql.init();
36
+
37
+ // 查询数据
38
+ const users = await sql.get({ status: 1 }, 'created_at DESC');
39
+
40
+ // 销毁连接
41
+ await sql.destroy();
42
+ ```
43
+
44
+ ### 2. 切换数据库类型
45
+
46
+ ```javascript
47
+ // 创建MySQL实例
48
+ const mysqlSql = new Sql({
49
+ db_type: 'mysql',
50
+ host: 'localhost',
51
+ user: 'root',
52
+ password: 'password',
53
+ database: 'mysql_db',
54
+ table: 'users'
55
+ });
56
+
57
+ // 切换到SQLite
58
+ const sqliteSql = mysqlSql.use('sqlite', {
59
+ database: 'sqlite.db',
60
+ table: 'users'
61
+ });
62
+
63
+ // 使用SQLite进行操作
64
+ await sqliteSql.init();
65
+ const data = await sqliteSql.get({});
66
+ ```
67
+
68
+ ### 3. 主要方法
69
+
70
+ #### 查询方法
71
+
72
+ ```javascript
73
+ // 查询多条数据
74
+ const list = await sql.get({ status: 1 }, 'id DESC');
75
+
76
+ // 查询单条数据
77
+ const user = await sql.getObj({ id: 1 });
78
+
79
+ // 分页查询
80
+ const result = await sql.getCount(
81
+ { status: 1 },
82
+ 'created_at DESC',
83
+ 'id, name, email'
84
+ );
85
+ // result结构: { list: [...], total: 100, page: 1, size: 20, pages: 5 }
86
+
87
+ // 统计
88
+ const count = await sql.count({ status: 1 });
89
+ ```
90
+
91
+ #### 增删改方法
92
+
93
+ ```javascript
94
+ // 添加数据
95
+ const addResult = await sql.add({
96
+ name: '张三',
97
+ age: 25,
98
+ status: 1
99
+ });
100
+
101
+ // 修改数据
102
+ const updateResult = await sql.set(
103
+ { id: 1 }, // 查询条件
104
+ { name: '李四', age: 26 } // 更新字段
105
+ );
106
+
107
+ // 删除数据
108
+ const deleteResult = await sql.del({ id: 1 });
109
+
110
+ // 添加或修改(如果存在则修改,不存在则添加)
111
+ const addOrSetResult = await sql.addOrSet(
112
+ { id: 1 }, // 查询条件
113
+ { name: '王五', age: 27 } // 要设置的字段
114
+ );
115
+ ```
116
+
117
+ #### 批量操作
118
+
119
+ ```javascript
120
+ // 批量添加
121
+ const addListResult = await sql.addList([
122
+ { name: '用户1', status: 1 },
123
+ { name: '用户2', status: 1 }
124
+ ]);
125
+
126
+ // 批量修改
127
+ const setListResult = await sql.setList([
128
+ { query: { id: 1 }, item: { status: 0 } },
129
+ { query: { id: 2 }, item: { status: 0 } }
130
+ ]);
131
+
132
+ // 批量删除
133
+ const delListResult = await sql.delList([
134
+ { query: { id: 1 } },
135
+ { query: { id: 2 } }
136
+ ]);
137
+ ```
138
+
139
+ #### 模型操作
140
+
141
+ ```javascript
142
+ // 获取模型对象
143
+ const model = await sql.getObj({ id: 1 });
144
+
145
+ // 直接修改模型属性会自动更新到数据库
146
+ model.name = '新名称'; // 自动执行UPDATE操作
147
+ model.age += 1; // 自动执行UPDATE操作,增加年龄
148
+ ```
149
+
150
+ ### 4. 配置选项
151
+
152
+ ```javascript
153
+ const sql = new Sql({
154
+ // 数据库类型
155
+ db_type: 'mysql', // 可选值: 'mysql', 'sqlite'
156
+
157
+ // 表相关配置
158
+ table: 'users', // 表名
159
+ key: 'id', // 主键
160
+
161
+ // 分页配置
162
+ page: 1, // 默认页码
163
+ size: 20, // 默认每页数量
164
+ limit: 1000, // 最大查询数量
165
+
166
+ // MySQL配置
167
+ host: 'localhost',
168
+ port: 3306,
169
+ user: 'root',
170
+ password: '',
171
+ database: '',
172
+ charset: 'utf8mb4',
173
+
174
+ // SQLite配置
175
+ database: 'db.sqlite', // SQLite数据库文件路径
176
+
177
+ // 其他配置
178
+ filter: {}, // 过滤参数
179
+ separator: '|' // 分隔符,用于多条件查询
180
+ });
181
+ ```
182
+
183
+ ## 事务操作
184
+
185
+ ```javascript
186
+ // MySQL事务示例
187
+ const mysqlTransactionResult = await sql.exec(`
188
+ START TRANSACTION;
189
+ INSERT INTO users (name, age) VALUES ('测试用户', 30);
190
+ UPDATE users SET status = 1 WHERE id = LAST_INSERT_ID();
191
+ COMMIT;
192
+ `);
193
+
194
+ // SQLite事务示例
195
+ const sqliteTransactionResult = await sql.exec(`
196
+ BEGIN TRANSACTION;
197
+ INSERT INTO users (name, age) VALUES ('测试用户', 30);
198
+ UPDATE users SET status = 1 WHERE id = last_insert_rowid();
199
+ COMMIT;
200
+ `);
201
+
202
+ // 注意:不同数据库的事务语法和函数可能有所不同,请根据实际使用的数据库类型调整SQL语句
203
+
204
+ ## 高级用法
205
+
206
+ ### 1. SQL模板
207
+
208
+ ```javascript
209
+ // 定义SQL模板
210
+ const sqlTemplate = {
211
+ name: '`name` LIKE "{0}%"',
212
+ age: '`age` > {0}'
213
+ };
214
+
215
+ // 使用模板构建查询条件
216
+ const query = sql.tpl_query({ name: '张', age: 20 }, sqlTemplate);
217
+ // 生成的查询条件: "`name` LIKE \"张%\" && `age` > 20"
218
+ ```
219
+
220
+ ### 2. 自定义SQL执行
221
+
222
+ ```javascript
223
+ // 执行自定义查询SQL
224
+ const result = await sql.run('SELECT * FROM users WHERE status = 1');
225
+
226
+ // 执行自定义更新SQL
227
+ const updateResult = await sql.exec('UPDATE users SET status = 0 WHERE id = 1');
228
+ ```
229
+
230
+ ## 注意事项
231
+
232
+ 1. 使用前必须先调用`init()`方法初始化连接
233
+ 2. 使用完成后应调用`destroy()`方法销毁连接,避免资源泄漏
234
+ 3. 切换数据库类型时会重新创建适配器,原来的连接会被关闭
235
+ 4. 为提高性能,系统会缓存数据库适配器,相同配置的实例会复用适配器
236
+
237
+ ## 支持的数据库类型
238
+
239
+ - MySQL
240
+ - SQLite
241
+
242
+ ## 许可证
243
+
244
+ MIT
package/db/mm.db ADDED
Binary file