mm_sqlite 1.1.3 → 1.1.5
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 +21 -201
- package/README.md +192 -95
- package/db.js +128 -112
- package/index.js +751 -542
- package/package.json +2 -2
- package/sql.js +245 -372
- package/config.json +0 -8
- package/db/db/test_db_methods.db.db +0 -0
- package/db/mm.db +0 -0
- package/sql_builder.js +0 -375
- package/test.js +0 -81
- package/test_all_methods.js +0 -115
- package/test_db_methods.js +0 -262
package/LICENSE
CHANGED
|
@@ -1,201 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 邱文武
|
|
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
|
@@ -2,28 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
高性能SQLite数据库操作模块,提供与mm_mysql完全兼容的API接口。适用于Node.js应用的轻量级数据存储解决方案。
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
- 📊 **内存优化**:优化的内存使用,减少GC压力
|
|
10
|
-
- 🔧 **链式调用**:流畅的SQL构建器链式调用
|
|
11
|
-
- 🛡️ **错误处理**:完善的错误处理机制
|
|
12
|
-
- 📈 **性能监控**:内置性能测试和内存监控
|
|
5
|
+
[](https://www.npmjs.com/package/mm_sqlite)
|
|
6
|
+
[](https://www.npmjs.com/package/mm_sqlite)
|
|
7
|
+
[](https://nodejs.org/en/)
|
|
8
|
+
[](LICENSE)
|
|
13
9
|
|
|
14
10
|
## 安装
|
|
15
11
|
|
|
16
|
-
通过npm安装模块:
|
|
17
|
-
|
|
18
12
|
```bash
|
|
19
13
|
npm install mm_sqlite --save
|
|
20
14
|
```
|
|
21
15
|
|
|
22
|
-
|
|
16
|
+
## 特性
|
|
23
17
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
- 🚀 **高性能优化**:经过深度优化的SQL构建器和连接管理
|
|
19
|
+
- 🔄 **完全兼容**:与mm_mysql模块API完全兼容
|
|
20
|
+
- 📊 **内存优化**:优化的内存使用,减少GC压力
|
|
21
|
+
- 🔧 **链式调用**:流畅的SQL构建器链式调用
|
|
22
|
+
- 🛡️ **错误处理**:完善的错误处理机制
|
|
23
|
+
- 📈 **连接池支持**:支持连接池模式,提高并发性能
|
|
24
|
+
- 🔒 **事务支持**:完整的事务处理机制
|
|
27
25
|
|
|
28
26
|
## 依赖要求
|
|
29
27
|
|
|
@@ -41,11 +39,25 @@ const { Sqlite } = require('mm_sqlite');
|
|
|
41
39
|
|
|
42
40
|
// 创建数据库实例
|
|
43
41
|
const sqlite = new Sqlite({
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
dir: './db/', // 数据库文件存储目录
|
|
43
|
+
database: 'test', // 数据库文件名(不包含扩展名)
|
|
44
|
+
charset: 'utf8mb4', // 字符集
|
|
45
|
+
timezone: '+08:00', // 时区
|
|
46
|
+
connect_timeout: 20000, // 连接超时时间(毫秒)
|
|
47
|
+
acquire_timeout: 20000, // 获取连接超时时间(毫秒)
|
|
48
|
+
query_timeout: 20000, // 查询超时时间(毫秒)
|
|
49
|
+
connection_limit: 1, // 连接限制(>1启用连接池)
|
|
50
|
+
enable_keep_alive: true, // 启用保活
|
|
51
|
+
keep_alive_initial_delay: 10000, // 保活初始延迟
|
|
52
|
+
enable_reconnect: true, // 启用重连
|
|
53
|
+
reconnect_interval: 1000, // 重连间隔
|
|
54
|
+
max_reconnect_attempts: 5, // 最大重连尝试次数
|
|
55
|
+
wait_for_connections: true, // 等待连接
|
|
56
|
+
pool_min: 1, // 连接池最小连接数
|
|
57
|
+
pool_max: 5, // 连接池最大连接数
|
|
58
|
+
pool_acquire_timeout: 30000, // 连接池获取超时
|
|
59
|
+
pool_idle_timeout: 60000, // 连接池空闲超时
|
|
60
|
+
pool_reap_interval: 1000 // 连接池清理间隔
|
|
49
61
|
});
|
|
50
62
|
|
|
51
63
|
// 打开数据库连接
|
|
@@ -161,34 +173,38 @@ await sqlite.close();
|
|
|
161
173
|
|
|
162
174
|
```javascript
|
|
163
175
|
// 开始事务
|
|
164
|
-
await sqlite.beginTransaction();
|
|
176
|
+
const transaction = await sqlite.beginTransaction();
|
|
165
177
|
|
|
166
178
|
try {
|
|
167
|
-
//
|
|
168
|
-
await
|
|
169
|
-
|
|
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
|
+
await transaction.exec('INSERT INTO users (username, email) VALUES (?, ?)', ['张三', 'zhangsan@example.com']);
|
|
181
|
+
await transaction.exec('INSERT INTO profiles (user_id, bio) VALUES (?, ?)', [1, '这是一个测试用户']);
|
|
179
182
|
|
|
180
183
|
// 提交事务
|
|
181
|
-
await
|
|
184
|
+
await transaction.commit();
|
|
182
185
|
} catch (error) {
|
|
183
186
|
// 回滚事务
|
|
184
|
-
await
|
|
187
|
+
await transaction.rollback();
|
|
185
188
|
console.error('事务执行失败:', error);
|
|
186
189
|
}
|
|
187
190
|
```
|
|
188
191
|
|
|
189
|
-
####
|
|
192
|
+
#### 使用事务包装器
|
|
190
193
|
|
|
191
194
|
```javascript
|
|
195
|
+
// 使用事务包装器简化事务处理
|
|
196
|
+
const result = await sqlite.transaction(async (transaction) => {
|
|
197
|
+
// 在事务中执行操作
|
|
198
|
+
await transaction.exec('INSERT INTO users (username, email) VALUES (?, ?)', ['李四', 'lisi@example.com']);
|
|
199
|
+
await transaction.exec('INSERT INTO profiles (user_id, bio) VALUES (?, ?)', [2, '这是另一个测试用户']);
|
|
200
|
+
|
|
201
|
+
return { success: true };
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
console.log('事务执行结果:', result);
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
#### 复杂查询
|
|
192
208
|
// 多表连接查询
|
|
193
209
|
const results = await sqlite
|
|
194
210
|
.table('users')
|
|
@@ -242,102 +258,173 @@ const users = await sqlite
|
|
|
242
258
|
new Sqlite(config)
|
|
243
259
|
```
|
|
244
260
|
|
|
245
|
-
|
|
246
|
-
- `config.
|
|
247
|
-
- `config.
|
|
248
|
-
- `config.
|
|
249
|
-
- `config.
|
|
250
|
-
- `config.
|
|
261
|
+
**配置参数:**
|
|
262
|
+
- `config.dir` - 数据库文件存储目录,默认为`'./db/'`
|
|
263
|
+
- `config.database` - 数据库文件名,默认为`'mm'`
|
|
264
|
+
- `config.charset` - 字符集,默认为`'utf8mb4'`
|
|
265
|
+
- `config.timezone` - 时区,默认为`'+08:00'`
|
|
266
|
+
- `config.connect_timeout` - 连接超时时间,默认`20000ms`
|
|
267
|
+
- `config.acquire_timeout` - 获取连接超时时间,默认`20000ms`
|
|
268
|
+
- `config.query_timeout` - 查询超时时间,默认`20000ms`
|
|
269
|
+
- `config.connection_limit` - 连接限制(>1启用连接池),默认`1`
|
|
270
|
+
- `config.enable_keep_alive` - 启用保活,默认`true`
|
|
271
|
+
- `config.keep_alive_initial_delay` - 保活初始延迟,默认`10000ms`
|
|
272
|
+
- `config.enable_reconnect` - 启用重连,默认`true`
|
|
273
|
+
- `config.reconnect_interval` - 重连间隔,默认`1000ms`
|
|
274
|
+
- `config.max_reconnect_attempts` - 最大重连尝试次数,默认`5`
|
|
275
|
+
- `config.wait_for_connections` - 等待连接,默认`true`
|
|
276
|
+
- `config.pool_min` - 连接池最小连接数,默认`1`
|
|
277
|
+
- `config.pool_max` - 连接池最大连接数,默认`5`
|
|
278
|
+
- `config.pool_acquire_timeout` - 连接池获取超时,默认`30000ms`
|
|
279
|
+
- `config.pool_idle_timeout` - 连接池空闲超时,默认`60000ms`
|
|
280
|
+
- `config.pool_reap_interval` - 连接池清理间隔,默认`1000ms`
|
|
251
281
|
|
|
252
282
|
#### 主要方法
|
|
253
283
|
|
|
254
|
-
- `
|
|
255
|
-
- `
|
|
256
|
-
- `
|
|
257
|
-
- `
|
|
258
|
-
- `
|
|
259
|
-
- `table
|
|
284
|
+
- `open(timeout)` - 打开数据库连接,返回`Promise<Boolean>`
|
|
285
|
+
- `close()` - 关闭数据库连接,返回`Promise<Boolean>`
|
|
286
|
+
- `getConn(timeout)` - 获取数据库连接,返回`Promise<Connection>`
|
|
287
|
+
- `run(sql, params, timeout)` - 执行SQL查询语句,返回`Promise<Array>`
|
|
288
|
+
- `exec(sql, params, timeout)` - 执行SQL语句(非查询操作),返回`Promise<Object>`
|
|
289
|
+
- `read(table, condition, options)` - 读取整张表的数据,返回`Promise<Array>`
|
|
290
|
+
- `db(database)` - 获取数据库操作实例,返回`DB`实例
|
|
291
|
+
- `beginTransaction()` - 开始事务,返回`Promise<Object>`(事务对象)
|
|
292
|
+
- `transaction(callback)` - 在事务中执行多个操作,返回`Promise<*>`
|
|
260
293
|
|
|
261
|
-
|
|
294
|
+
#### 事务对象方法
|
|
262
295
|
|
|
263
|
-
|
|
296
|
+
- `commit()` - 提交事务
|
|
297
|
+
- `rollback()` - 回滚事务
|
|
298
|
+
- `exec(sql, params)` - 在事务中执行SQL语句
|
|
264
299
|
|
|
265
|
-
|
|
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>
|
|
300
|
+
### DB类
|
|
270
301
|
|
|
271
|
-
|
|
302
|
+
DB类继承自Sql类,提供SQL构建器功能。
|
|
272
303
|
|
|
273
|
-
|
|
274
|
-
- `delList(list, like, batchSize, timeout)` - 批量删除数据,返回Promise<Object>
|
|
275
|
-
- `setList(list, like, batchSize, timeout)` - 批量更新数据,返回Promise<Object>
|
|
276
|
-
|
|
277
|
-
#### 统计方法
|
|
304
|
+
#### 主要方法
|
|
278
305
|
|
|
279
|
-
- `
|
|
280
|
-
- `
|
|
281
|
-
- `
|
|
282
|
-
- `
|
|
283
|
-
- `
|
|
306
|
+
- `table(tableName)` - 设置表名,返回当前实例
|
|
307
|
+
- `add(body)` - 添加单条数据,返回`Promise<Object>`
|
|
308
|
+
- `del(query, like)` - 删除数据,返回`Promise<Object>`
|
|
309
|
+
- `set(query, body, like)` - 更新数据,返回`Promise<Object>`
|
|
310
|
+
- `get(query, sort, view, like, timeout)` - 查询多条数据,返回`Promise<Array>`
|
|
311
|
+
- `getObj(query, sort, view, like, timeout)` - 获取单条记录,返回`Promise<Object|null>`
|
|
312
|
+
- `addList(list, lock, batchSize, timeout)` - 批量添加数据,返回`Promise<Object>`
|
|
313
|
+
- `delList(list, like, batchSize, timeout)` - 批量删除数据,返回`Promise<Object>`
|
|
314
|
+
- `count(query, like, timeout)` - 统计记录数,返回`Promise<Number>`
|
|
315
|
+
- `groupCount(query, groupby, view, sort)` - 分组统计,返回`Promise<Array>`
|
|
316
|
+
- `groupSum(query, groupby, view, sort)` - 分组求和,返回`Promise<Array>`
|
|
317
|
+
- `groupAvg(query, groupby, view, sort)` - 分组平均值,返回`Promise<Array>`
|
|
318
|
+
|
|
319
|
+
#### SQL构建器方法
|
|
320
|
+
|
|
321
|
+
- `select(fields)` - 设置查询字段
|
|
322
|
+
- `where(condition)` - 设置查询条件
|
|
323
|
+
- `orWhere(condition)` - 设置OR查询条件
|
|
324
|
+
- `like(field, value)` - 设置LIKE查询条件
|
|
325
|
+
- `orderBy(field, direction)` - 设置排序
|
|
326
|
+
- `groupBy(field)` - 设置分组
|
|
327
|
+
- `having(condition)` - 设置HAVING条件
|
|
328
|
+
- `limit(count)` - 设置限制数量
|
|
329
|
+
- `offset(count)` - 设置偏移量
|
|
330
|
+
- `join(table, condition)` - 设置表连接
|
|
284
331
|
|
|
285
332
|
## 错误处理
|
|
286
333
|
|
|
287
334
|
模块提供完善的错误处理机制:
|
|
288
335
|
|
|
336
|
+
1. **参数验证**:所有公开方法都会验证输入参数,参数不符合要求时会抛出`TypeError`
|
|
337
|
+
2. **SQL错误捕获**:SQL执行错误会被捕获并记录到日志中
|
|
338
|
+
3. **连接错误处理**:连接失败时会自动重连,超过最大重试次数后抛出错误
|
|
339
|
+
4. **事务错误处理**:事务执行失败时会自动回滚,确保数据一致性
|
|
340
|
+
|
|
341
|
+
错误处理示例:
|
|
342
|
+
|
|
289
343
|
```javascript
|
|
344
|
+
// 参数验证错误
|
|
290
345
|
try {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
346
|
+
await db.add(null); // 抛出TypeError
|
|
347
|
+
} catch (err) {
|
|
348
|
+
console.error('参数错误:', err.message);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// SQL执行错误
|
|
352
|
+
try {
|
|
353
|
+
await db.exec('SELECT * FROM non_existent_table');
|
|
354
|
+
} catch (err) {
|
|
355
|
+
console.error('SQL错误:', err.message);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// 事务错误处理
|
|
359
|
+
try {
|
|
360
|
+
await sqlite.transaction(async (tx) => {
|
|
361
|
+
await tx.exec('INSERT INTO users (name) VALUES (?)', ['Alice']);
|
|
362
|
+
await tx.exec('INSERT INTO users (name) VALUES (?)', [null]); // 触发错误
|
|
363
|
+
});
|
|
364
|
+
} catch (err) {
|
|
365
|
+
console.error('事务执行失败:', err.message);
|
|
300
366
|
}
|
|
301
367
|
```
|
|
302
368
|
|
|
303
369
|
## 性能监控
|
|
304
370
|
|
|
305
|
-
|
|
371
|
+
模块内置连接池和性能优化功能:
|
|
372
|
+
|
|
373
|
+
1. **连接池管理**:自动管理数据库连接,提高并发性能
|
|
374
|
+
2. **连接复用**:连接使用完毕后自动归还到连接池
|
|
375
|
+
3. **连接保活**:定期检查连接状态,自动重连失效连接
|
|
376
|
+
4. **超时控制**:支持连接超时、查询超时等配置
|
|
377
|
+
|
|
378
|
+
连接池配置示例:
|
|
306
379
|
|
|
307
380
|
```javascript
|
|
308
|
-
// 启用性能日志
|
|
309
381
|
const sqlite = new Sqlite({
|
|
310
|
-
|
|
311
|
-
|
|
382
|
+
connection_limit: 5, // 启用连接池,最大5个连接
|
|
383
|
+
pool_min: 1, // 最小连接数
|
|
384
|
+
pool_max: 5, // 最大连接数
|
|
385
|
+
pool_idle_timeout: 60000, // 空闲超时60秒
|
|
386
|
+
enable_keep_alive: true, // 启用保活
|
|
387
|
+
enable_reconnect: true // 启用自动重连
|
|
312
388
|
});
|
|
313
|
-
|
|
314
|
-
// 测试特定操作的性能
|
|
315
|
-
console.time('query_test');
|
|
316
|
-
const results = await sqlite.table('users').get();
|
|
317
|
-
console.timeEnd('query_test');
|
|
318
389
|
```
|
|
319
390
|
|
|
320
391
|
对于大规模应用,建议在生产环境前进行压力测试,确保数据库操作满足性能要求。
|
|
321
392
|
|
|
322
393
|
## 兼容性
|
|
323
394
|
|
|
324
|
-
- Node.js
|
|
325
|
-
-
|
|
326
|
-
-
|
|
395
|
+
- **Node.js版本**:支持 Node.js 12.0 及以上版本
|
|
396
|
+
- **SQLite版本**:支持 SQLite 3.0 及以上版本
|
|
397
|
+
- **操作系统**:支持 Windows、Linux、macOS
|
|
398
|
+
|
|
399
|
+
## FAQ
|
|
400
|
+
|
|
401
|
+
### Q: 如何处理数据库文件不存在的情况?
|
|
402
|
+
A: 模块会自动创建数据库文件,无需手动创建。
|
|
403
|
+
|
|
404
|
+
### Q: 如何优化查询性能?
|
|
405
|
+
A: 建议使用索引、合理设计表结构、避免全表扫描。
|
|
327
406
|
|
|
328
|
-
|
|
407
|
+
### Q: 如何处理并发访问?
|
|
408
|
+
A: 模块内置连接池机制,支持并发访问。当连接数超过1时自动启用连接池。
|
|
329
409
|
|
|
330
|
-
|
|
331
|
-
A:
|
|
410
|
+
### Q: 如何备份数据库?
|
|
411
|
+
A: 可以直接复制数据库文件进行备份。
|
|
332
412
|
|
|
333
|
-
|
|
334
|
-
A:
|
|
413
|
+
### Q: 如何查看SQL执行日志?
|
|
414
|
+
A: 启用调试模式即可查看详细的SQL执行日志。
|
|
335
415
|
|
|
336
|
-
|
|
337
|
-
A:
|
|
416
|
+
### Q: 连接池如何工作?
|
|
417
|
+
A: 当`connection_limit`大于1时启用连接池,连接池会自动管理连接的获取和释放。
|
|
338
418
|
|
|
339
|
-
|
|
340
|
-
A:
|
|
419
|
+
### Q: 事务处理是否安全?
|
|
420
|
+
A: 是的,事务处理包含完整的错误处理机制,失败时会自动回滚。
|
|
421
|
+
|
|
422
|
+
### Q: 支持哪些SQL操作?
|
|
423
|
+
A: 支持标准的CRUD操作、复杂查询、事务处理、批量操作等。
|
|
424
|
+
|
|
425
|
+
## 贡献
|
|
426
|
+
|
|
427
|
+
欢迎提交 Issue 和 Pull Request 来改进这个模块。
|
|
341
428
|
|
|
342
429
|
## 许可证
|
|
343
430
|
|
|
@@ -350,3 +437,13 @@ MIT License
|
|
|
350
437
|
1. 运行测试确保功能正常
|
|
351
438
|
2. 添加适当的注释和文档
|
|
352
439
|
3. 遵循项目的代码风格
|
|
440
|
+
4. 确保代码符合ES6+标准
|
|
441
|
+
5. 提交前请先拉取最新代码,解决冲突
|
|
442
|
+
|
|
443
|
+
### 开发流程
|
|
444
|
+
|
|
445
|
+
1. Fork 仓库
|
|
446
|
+
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
|
|
447
|
+
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
|
|
448
|
+
4. 推送到分支 (`git push origin feature/AmazingFeature`)
|
|
449
|
+
5. 打开 Pull Request
|