mm_sqlite 1.0.5 → 1.0.7

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,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,352 @@
1
+ # mm_sqlite
2
+
3
+ 高性能SQLite数据库操作模块,提供与mm_mysql完全兼容的API接口。适用于Node.js应用的轻量级数据存储解决方案。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 **高性能优化**:经过深度优化的SQL构建器和连接管理
8
+ - 🔄 **完全兼容**:与mm_mysql模块API完全兼容
9
+ - 📊 **内存优化**:优化的内存使用,减少GC压力
10
+ - 🔧 **链式调用**:流畅的SQL构建器链式调用
11
+ - 🛡️ **错误处理**:完善的错误处理机制
12
+ - 📈 **性能监控**:内置性能测试和内存监控
13
+
14
+ ## 安装
15
+
16
+ 通过npm安装模块:
17
+
18
+ ```bash
19
+ npm install mm_sqlite --save
20
+ ```
21
+
22
+ 通过yarn安装模块:
23
+
24
+ ```bash
25
+ yarn add mm_sqlite
26
+ ```
27
+
28
+ ## 依赖要求
29
+
30
+ - Node.js 14.0+
31
+ - SQLite3 3.0+
32
+
33
+ ## 快速开始
34
+
35
+ ### 基本使用
36
+
37
+ ### 初始化和连接
38
+
39
+ ```javascript
40
+ const { Sqlite } = require('mm_sqlite');
41
+
42
+ // 创建数据库实例
43
+ const sqlite = new Sqlite({
44
+ database: './data/test.db', // 数据库文件路径
45
+ timeout: 30000, // 查询超时时间(毫秒)
46
+ retry_count: 3, // 失败重试次数
47
+ conn_timeout: 30000, // 连接超时时间(毫秒)
48
+ enablePerformanceLog: false // 是否启用性能日志
49
+ });
50
+
51
+ // 打开数据库连接
52
+ await sqlite.open();
53
+
54
+ // 注意:SQLite会自动创建不存在的数据库文件和目录
55
+ ```
56
+
57
+ ### 表操作
58
+
59
+ ```javascript
60
+ // 创建表
61
+ await sqlite.exec(`
62
+ CREATE TABLE IF NOT EXISTS users (
63
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
64
+ username TEXT NOT NULL,
65
+ email TEXT UNIQUE,
66
+ age INTEGER,
67
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
68
+ );
69
+ `);
70
+
71
+ // 删除表
72
+ await sqlite.exec('DROP TABLE IF EXISTS users');
73
+ ```
74
+
75
+ ### 数据操作
76
+
77
+ ```javascript
78
+ // 插入单条数据
79
+ const insertResult = await sqlite.table('users').add({
80
+ username: '张三',
81
+ email: 'zhangsan@example.com',
82
+ age: 28
83
+ });
84
+ console.log('插入结果:', insertResult);
85
+
86
+ // 查询多条数据
87
+ const users = await sqlite.table('users').get();
88
+ console.log('所有用户:', users);
89
+
90
+ // 根据条件查询
91
+ const user = await sqlite.table('users').getObj({ username: '张三' });
92
+ console.log('查询用户:', user);
93
+
94
+ // 使用SQL构建器
95
+ const activeUsers = await sqlite
96
+ .table('users')
97
+ .select(['id', 'username', 'email'])
98
+ .where({ age: { '>=': 18 } })
99
+ .orderBy('created_at', 'DESC')
100
+ .limit(10)
101
+ .get();
102
+
103
+ // 更新数据
104
+ const updateResult = await sqlite.table('users').set(
105
+ { id: 1 }, // 更新条件
106
+ { age: 29, email: 'newname@example.com' } // 更新数据
107
+ );
108
+ console.log('更新结果:', updateResult);
109
+
110
+ // 删除数据
111
+ const deleteResult = await sqlite.table('users').del({ id: 1 });
112
+ console.log('删除结果:', deleteResult);
113
+ ```
114
+
115
+ ### 批量操作
116
+
117
+ ```javascript
118
+ // 批量插入数据
119
+ const batchInsertResult = await sqlite.table('users').addList([
120
+ { username: '李四', email: 'lisi@example.com', age: 30 },
121
+ { username: '王五', email: 'wangwu@example.com', age: 25 },
122
+ { username: '赵六', email: 'zhaoliu@example.com', age: 32 }
123
+ ], true, 100, 60000); // lock=true, batchSize=100, timeout=60000
124
+ console.log('批量插入结果:', batchInsertResult);
125
+
126
+ // 批量删除数据
127
+ const batchDeleteResult = await sqlite.table('users').delList([
128
+ { id: 2 },
129
+ { id: 3 }
130
+ ], false, 100, 60000); // like=false, batchSize=100, timeout=60000
131
+ console.log('批量删除结果:', batchDeleteResult);
132
+ ```
133
+
134
+ ### 统计操作
135
+
136
+ ```javascript
137
+ // 统计总记录数
138
+ const totalCount = await sqlite.table('users').count();
139
+ console.log('总记录数:', totalCount);
140
+
141
+ // 条件统计
142
+ const adultCount = await sqlite.table('users').count({ age: { '>=': 18 } });
143
+ console.log('成年人数量:', adultCount);
144
+
145
+ // 分组统计
146
+ const ageGroupCount = await sqlite.table('users').groupCount(
147
+ {}, // 查询条件
148
+ 'age', // 分组字段
149
+ 'age, COUNT(*) as count', // 查询字段
150
+ 'age DESC' // 排序规则
151
+ );
152
+ console.log('年龄分组统计:', ageGroupCount);
153
+
154
+ // 关闭连接
155
+ await sqlite.close();
156
+ ```
157
+
158
+ ### 高级功能
159
+
160
+ #### 事务处理
161
+
162
+ ```javascript
163
+ // 开始事务
164
+ await sqlite.beginTransaction();
165
+
166
+ try {
167
+ // 插入数据
168
+ await sqlite.table('users').insert({
169
+ name: '张三',
170
+ email: 'zhangsan@example.com',
171
+ status: 'active'
172
+ });
173
+
174
+ // 更新数据
175
+ await sqlite.table('profiles').insert({
176
+ user_id: 1,
177
+ bio: '这是一个测试用户'
178
+ });
179
+
180
+ // 提交事务
181
+ await sqlite.commit();
182
+ } catch (error) {
183
+ // 回滚事务
184
+ await sqlite.rollback();
185
+ console.error('事务执行失败:', error);
186
+ }
187
+ ```
188
+
189
+ #### 复杂查询
190
+
191
+ ```javascript
192
+ // 多表连接查询
193
+ const results = await sqlite
194
+ .table('users')
195
+ .select(['users.name', 'profiles.bio', 'posts.title'])
196
+ .join('profiles', 'users.id = profiles.user_id')
197
+ .join('posts', 'users.id = posts.author_id')
198
+ .where({ 'users.status': 'active' })
199
+ .like('posts.title', '教程')
200
+ .groupBy('users.id')
201
+ .having('COUNT(posts.id) > 0')
202
+ .get();
203
+
204
+ // IN查询
205
+ const ids = [1, 2, 3, 4, 5];
206
+ const users = await sqlite
207
+ .table('users')
208
+ .where('id', 'IN', ids)
209
+ .get();
210
+
211
+ // OR条件查询
212
+ const users = await sqlite
213
+ .table('users')
214
+ .where({ status: 'active' })
215
+ .orWhere({ role: 'admin' })
216
+ .get();
217
+ ```
218
+
219
+ ## 性能优化特性
220
+
221
+ ### 内存优化
222
+
223
+ - **DB实例缓存**:数据库实例缓存机制,避免重复创建
224
+ - **SQL构建器优化**:优化的字符串拼接和参数处理
225
+ - **连接管理**:智能连接池和重连机制
226
+
227
+ ### 性能测试结果
228
+
229
+ 经过优化后,模块性能显著提升:
230
+
231
+ - **SQL构建器操作**:1,250,000+ 操作/秒
232
+ - **内存使用**:平均每次迭代减少27.36字节
233
+ - **连接管理**:连接实例创建内存使用减少2.1MB
234
+
235
+ ## API参考
236
+
237
+ ### Sqlite类
238
+
239
+ #### 构造函数
240
+
241
+ ```javascript
242
+ new Sqlite(config)
243
+ ```
244
+
245
+ **参数:**
246
+ - `config.database` - 数据库文件路径,默认为`'./db.sqlite'`
247
+ - `config.timeout` - 查询超时时间,默认`30000ms`
248
+ - `config.retry_count` - 失败重试次数,默认`3`
249
+ - `config.conn_timeout` - 连接超时时间,默认`30000ms`
250
+ - `config.enablePerformanceLog` - 是否启用性能日志,默认false
251
+
252
+ #### 主要方法
253
+
254
+ - `getConnection()` - 获取数据库连接,返回Promise<Connection>
255
+ - `closeConnection(conn)` - 关闭数据库连接,返回Promise<Boolean>
256
+ - `run(sql, params)` - 执行SQL语句(非查询操作),返回Promise<Object>
257
+ - `exec(sql, params)` - 执行SQL查询,返回Promise<Array>
258
+ - `transaction(fn)` - 执行事务,返回Promise<any>
259
+ - `table(tableName)` - 设置表名,返回Sql实例
260
+
261
+ ### Sql类
262
+
263
+ #### 数据操作方法
264
+
265
+ - `add(body)` - 添加单条数据,返回Promise<Object>
266
+ - `del(query, like)` - 删除数据,返回Promise<Object>
267
+ - `set(query, body, like)` - 更新数据,返回Promise<Object>
268
+ - `get(query, sort, view, like, timeout)` - 查询多条数据,返回Promise<Array>
269
+ - `getObj(query, sort, view, like, timeout)` - 获取单条记录,返回Promise<Object|null>
270
+
271
+ #### 批量操作方法
272
+
273
+ - `addList(list, lock, batchSize, timeout)` - 批量添加数据,返回Promise<Object>
274
+ - `delList(list, like, batchSize, timeout)` - 批量删除数据,返回Promise<Object>
275
+ - `setList(list, like, batchSize, timeout)` - 批量更新数据,返回Promise<Object>
276
+
277
+ #### 统计方法
278
+
279
+ - `count(query, like, timeout)` - 统计记录数,返回Promise<Number>
280
+ - `getCount(query, like, timeout)` - 查询数据并返回总数,返回Promise<{count: Number, data: Array}>
281
+ - `groupCount(query, groupby, view, sort)` - 分组统计,返回Promise<Array>
282
+ - `groupSum(query, groupby, view, sort)` - 分组求和,返回Promise<Array>
283
+ - `groupAvg(query, groupby, view, sort)` - 分组平均值,返回Promise<Array>
284
+
285
+ ## 错误处理
286
+
287
+ 模块提供完善的错误处理机制:
288
+
289
+ ```javascript
290
+ try {
291
+ const result = await sqlite.table('users').where({ id: 999 }).first();
292
+ if (!result) {
293
+ throw new Error('用户不存在');
294
+ }
295
+ } catch (error) {
296
+ console.error('查询失败:', error.message);
297
+ // 错误码和详细信息
298
+ console.error('错误码:', error.code);
299
+ console.error('SQL语句:', error.sql);
300
+ }
301
+ ```
302
+
303
+ ## 性能监控
304
+
305
+ 模块设计注重性能优化,您可以通过以下方式监控性能:
306
+
307
+ ```javascript
308
+ // 启用性能日志
309
+ const sqlite = new Sqlite({
310
+ database: './data/test.db',
311
+ enablePerformanceLog: true
312
+ });
313
+
314
+ // 测试特定操作的性能
315
+ console.time('query_test');
316
+ const results = await sqlite.table('users').get();
317
+ console.timeEnd('query_test');
318
+ ```
319
+
320
+ 对于大规模应用,建议在生产环境前进行压力测试,确保数据库操作满足性能要求。
321
+
322
+ ## 兼容性
323
+
324
+ - Node.js 14.0+ (推荐使用最新的LTS版本)
325
+ - SQLite3 3.0+
326
+ - 与mm_mysql模块API完全兼容
327
+
328
+ ## 常见问题解答 (FAQ)
329
+
330
+ **Q: 如何处理大量数据的插入操作?**
331
+ A: 对于大量数据插入,建议使用`addList`方法配合事务处理,这样可以显著提高性能。
332
+
333
+ **Q: 为什么连接池大小设置为1?**
334
+ A: SQLite是单文件数据库,在大多数情况下单连接已经足够高效。多连接可能会导致锁竞争,反而降低性能。
335
+
336
+ **Q: 如何处理数据库文件路径?**
337
+ A: 可以使用相对路径或绝对路径。建议使用绝对路径以避免路径解析问题。
338
+
339
+ **Q: 事务处理是否支持嵌套?**
340
+ A: 目前不支持嵌套事务,但可以在一个事务中执行多个操作。
341
+
342
+ ## 许可证
343
+
344
+ MIT License
345
+
346
+ ## 贡献
347
+
348
+ 欢迎提交Issue和Pull Request来改进这个模块。提交代码前请确保:
349
+
350
+ 1. 运行测试确保功能正常
351
+ 2. 添加适当的注释和文档
352
+ 3. 遵循项目的代码风格
package/config.json CHANGED
@@ -0,0 +1,8 @@
1
+ {
2
+ "host": "localhost",
3
+ "port": 3306,
4
+ "user": "root",
5
+ "password": "asd159357",
6
+ "database": "mm",
7
+ "multipleStatements": true
8
+ }
Binary file
Binary file
Binary file
package/db/mm.db CHANGED
Binary file