mm_sqlite 1.3.2 → 1.3.3

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/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
package/LICENSE ADDED
@@ -0,0 +1,21 @@
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 ADDED
@@ -0,0 +1,199 @@
1
+ # mm_sqlite
2
+ [中文](./README.md) | [English](./README_EN.md)
3
+
4
+ 高性能SQLite数据库操作模块,提供与mm_mysql完全兼容的API接口。适用于Node.js应用的轻量级数据存储解决方案。
5
+
6
+ ## 安装
7
+
8
+ ```bash
9
+ npm install mm_sqlite --save
10
+ ```
11
+
12
+ ## 特性
13
+
14
+ - 🚀 **高性能优化**:经过深度优化的SQL构建器和连接管理
15
+ - 🔄 **完全兼容**:与mm_mysql模块API完全兼容,可无缝切换
16
+ - 📊 **内存优化**:优化的内存使用,减少GC压力
17
+ - 🔧 **链式调用**:流畅的SQL构建器链式调用
18
+ - 🛡️ **错误处理**:完善的错误处理机制
19
+ - 📈 **连接池支持**:支持连接池模式,提高并发性能
20
+ - 🔒 **事务支持**:完整的事务处理机制
21
+ - 🔍 **类型安全**:完善的JSDoc类型注释
22
+
23
+ ## 依赖要求
24
+
25
+ - Node.js 14.0+
26
+ - SQLite3 3.0+
27
+
28
+ ## 快速开始
29
+
30
+ ### 基本使用
31
+
32
+ #### 初始化和连接
33
+
34
+ ```javascript
35
+ const { Sqlite } = require('mm_sqlite');
36
+
37
+ // 创建数据库实例
38
+ const sqlite = new Sqlite({
39
+ dir: './db/', // 数据库文件存储目录
40
+ database: 'test', // 数据库文件名(不包含扩展名)
41
+ charset: 'utf8mb4', // 字符集
42
+ timezone: '+08:00', // 时区
43
+ connect_timeout: 20000, // 连接超时时间(毫秒)
44
+ acquire_timeout: 20000, // 获取连接超时时间(毫秒)
45
+ query_timeout: 20000, // 查询超时时间(毫秒)
46
+ connection_limit: 1, // 连接限制(>1启用连接池)
47
+ enable_keep_alive: true, // 启用保活
48
+ keep_alive_initial_delay: 10000, // 保活初始延迟
49
+ enable_reconnect: true, // 启用重连
50
+ reconnect_interval: 1000, // 重连间隔
51
+ max_reconnect_attempts: 5, // 最大重连尝试次数
52
+ wait_for_connections: true, // 等待连接
53
+ pool_min: 1, // 连接池最小连接数
54
+ pool_max: 5, // 连接池最大连接数
55
+ pool_acquire_timeout: 30000, // 连接池获取超时
56
+ pool_idle_timeout: 60000, // 连接池空闲超时
57
+ pool_reap_interval: 1000 // 连接池清理间隔
58
+ });
59
+
60
+ // 打开数据库连接
61
+ await sqlite.open();
62
+ ```
63
+
64
+ #### 基础操作
65
+
66
+ ```javascript
67
+ // 获取数据库管理器
68
+ const db = sqlite.db();
69
+
70
+ // 设置表名
71
+ const userDb = db.new('users', 'id');
72
+
73
+ // 插入数据
74
+ const result = await userDb.add({
75
+ username: '张三',
76
+ email: 'zhangsan@example.com',
77
+ age: 28,
78
+ created_at: new Date()
79
+ });
80
+
81
+ // 查询数据
82
+ const users = await userDb.get();
83
+
84
+ // 更新数据
85
+ await userDb.set({ id: 1 }, { age: 29 });
86
+
87
+ // 删除数据
88
+ await userDb.del({ id: 1 });
89
+ ```
90
+
91
+ #### 高级查询
92
+
93
+ ```javascript
94
+ // 条件查询
95
+ const adultUsers = await userDb.get({
96
+ age: { _gte: 18 }
97
+ });
98
+
99
+ // 分页查询
100
+ const pageUsers = await userDb.get({
101
+ _page: 1,
102
+ _size: 10,
103
+ _sort: 'created_at desc'
104
+ });
105
+
106
+ // 聚合查询
107
+ const stats = await userDb.aggr({
108
+ _count: 'id',
109
+ _avg: 'age',
110
+ _max: 'age',
111
+ _min: 'age'
112
+ });
113
+ ```
114
+
115
+ ## API参考
116
+
117
+ ### Sqlite类
118
+
119
+ #### 构造函数
120
+ ```javascript
121
+ new Sqlite(config)
122
+ ```
123
+
124
+ #### 主要方法
125
+
126
+ - `open()` - 打开数据库连接
127
+ - `close()` - 关闭数据库连接
128
+ - `db()` - 获取数据库管理器实例
129
+ - `run(sql, params)` - 执行查询SQL
130
+ - `exec(sql, params)` - 执行非查询SQL
131
+
132
+ ### DB类
133
+
134
+ #### 表操作
135
+ - `addTable(table, field, type, auto, commit, timeout)` - 创建表
136
+ - `dropTable(table, timeout)` - 删除表
137
+ - `renameTable(table, new_table, timeout)` - 重命名表
138
+ - `emptyTable(table, timeout)` - 清空表
139
+ - `hasTable(table, timeout)` - 检查表是否存在
140
+
141
+ #### 字段操作
142
+ - `addField(field, type, value, not_null, auto, comment, timeout)` - 添加字段
143
+ - `delField(table, field, timeout)` - 删除字段
144
+ - `renameField(table, field, new_field, type, timeout)` - 重命名字段
145
+ - `editField(table, field, type, timeout)` - 修改字段
146
+ - `fields(table, field_name, timeout)` - 获取字段信息
147
+
148
+ #### 数据操作
149
+ - `add(data, timeout)` - 插入数据
150
+ - `set(where, data, timeout)` - 更新数据
151
+ - `del(where, timeout)` - 删除数据
152
+ - `get(where, timeout)` - 查询数据
153
+ - `getTableData(table, batchSize, timeout)` - 获取表数据
154
+
155
+ #### 事务操作
156
+ - `start()` - 开始事务
157
+ - `commit(transaction)` - 提交事务
158
+ - `back(transaction)` - 回滚事务
159
+ - `transaction(callback)` - 事务中执行操作
160
+
161
+ ## 配置参数
162
+
163
+ | 参数 | 类型 | 默认值 | 描述 |
164
+ |------|------|--------|------|
165
+ | dir | string | './db/' | 数据库文件存储目录 |
166
+ | database | string | 'mm' | 数据库文件名 |
167
+ | charset | string | 'utf8mb4' | 字符集 |
168
+ | timezone | string | '+08:00' | 时区 |
169
+ | connect_timeout | number | 20000 | 连接超时时间(ms) |
170
+ | acquire_timeout | number | 20000 | 获取连接超时时间(ms) |
171
+ | query_timeout | number | 20000 | 查询超时时间(ms) |
172
+ | connection_limit | number | 1 | 连接限制 |
173
+ | enable_keep_alive | boolean | true | 启用保活 |
174
+ | keep_alive_initial_delay | number | 10000 | 保活初始延迟(ms) |
175
+ | enable_reconnect | boolean | true | 启用重连 |
176
+ | reconnect_interval | number | 1000 | 重连间隔(ms) |
177
+ | max_reconnect_attempts | number | 5 | 最大重连尝试次数 |
178
+ | wait_for_connections | boolean | true | 等待连接 |
179
+ | pool_min | number | 1 | 连接池最小连接数 |
180
+ | pool_max | number | 5 | 连接池最大连接数 |
181
+ | pool_acquire_timeout | number | 30000 | 连接池获取超时(ms) |
182
+ | pool_idle_timeout | number | 60000 | 连接池空闲超时(ms) |
183
+ | pool_reap_interval | number | 1000 | 连接池清理间隔(ms) |
184
+
185
+ ## 开发规范
186
+
187
+ - 使用JavaScript开发,禁止使用TypeScript
188
+ - 使用JSDoc注释进行类型定义
189
+ - 遵循统一的命名规范和方法签名
190
+ - 完善的错误处理机制
191
+ - 支持异步/等待语法
192
+
193
+ ## 许可证
194
+
195
+ MIT License
196
+
197
+ ## 贡献
198
+
199
+ 欢迎提交Issue和Pull Request来改进这个项目。
package/README_EN.md ADDED
@@ -0,0 +1,293 @@
1
+ # mm_sqlite
2
+
3
+ [中文](./README.md) | [English](./README_EN.md)
4
+
5
+ High-performance SQLite database operation module with API fully compatible with mm_mysql. A lightweight data storage solution for Node.js applications.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install mm_sqlite --save
11
+ ```
12
+
13
+ ## Features
14
+
15
+ - 🚀 **High Performance**: Deeply optimized SQL builder and connection management
16
+ - 🔄 **Fully Compatible**: API fully compatible with mm_mysql module, seamless switching
17
+ - 📊 **Memory Optimized**: Optimized memory usage, reduced GC pressure
18
+ - 🔧 **Chainable API**: Fluent SQL builder with chainable calls
19
+ - 🛡️ **Error Handling**: Comprehensive error handling mechanism
20
+ - 📈 **Connection Pool Support**: Connection pool mode for better concurrency
21
+ - 🔒 **Transaction Support**: Complete transaction handling mechanism
22
+ - 🔍 **Type Safety**: Comprehensive JSDoc type annotations
23
+
24
+ ## Requirements
25
+
26
+ - Node.js 14.0+
27
+ - SQLite3 3.0+
28
+
29
+ ## Quick Start
30
+
31
+ ### Basic Usage
32
+
33
+ #### Initialization and Connection
34
+
35
+ ```javascript
36
+ const { Sqlite } = require('mm_sqlite');
37
+
38
+ // Create database instance
39
+ const sqlite = new Sqlite({
40
+ dir: './db/', // Database file storage directory
41
+ database: 'test', // Database filename (without extension)
42
+ charset: 'utf8mb4', // Character set
43
+ timezone: '+08:00', // Timezone
44
+ connect_timeout: 20000, // Connection timeout (ms)
45
+ acquire_timeout: 20000, // Connection acquisition timeout (ms)
46
+ query_timeout: 20000, // Query timeout (ms)
47
+ connection_limit: 1, // Connection limit (>1 enables connection pool)
48
+ enable_keep_alive: true, // Enable keep-alive
49
+ keep_alive_initial_delay: 10000, // Keep-alive initial delay
50
+ enable_reconnect: true, // Enable reconnection
51
+ reconnect_interval: 1000, // Reconnection interval
52
+ max_reconnect_attempts: 5, // Maximum reconnection attempts
53
+ wait_for_connections: true, // Wait for connections
54
+ pool_min: 1, // Connection pool minimum connections
55
+ pool_max: 5, // Connection pool maximum connections
56
+ pool_acquire_timeout: 30000, // Connection pool acquisition timeout
57
+ pool_idle_timeout: 60000, // Connection pool idle timeout
58
+ pool_reap_interval: 1000 // Connection pool cleanup interval
59
+ });
60
+
61
+ // Open database connection
62
+ await sqlite.open();
63
+ ```
64
+
65
+ #### Basic Operations
66
+
67
+ ```javascript
68
+ // Get database manager
69
+ const db = sqlite.db();
70
+
71
+ // Set table name
72
+ const userDb = db.new('users', 'id');
73
+
74
+ // Insert data
75
+ const result = await userDb.add({
76
+ username: 'John',
77
+ email: 'john@example.com',
78
+ age: 28,
79
+ created_at: new Date()
80
+ });
81
+
82
+ // Query data
83
+ const users = await userDb.get();
84
+
85
+ // Update data
86
+ await userDb.set({ id: 1 }, { age: 29 });
87
+
88
+ // Delete data
89
+ await userDb.del({ id: 1 });
90
+ ```
91
+
92
+ #### Advanced Queries
93
+
94
+ ```javascript
95
+ // Conditional queries
96
+ const adultUsers = await userDb.get({
97
+ age: { _gte: 18 }
98
+ });
99
+
100
+ // Complex queries with sorting and pagination
101
+ const users = await userDb.get({
102
+ status: 'active',
103
+ age: { _gte: 18, _lte: 65 }
104
+ }, {
105
+ order: 'created_at desc',
106
+ limit: 10,
107
+ offset: 0
108
+ });
109
+
110
+ // Transaction operations
111
+ const transaction = await db.start();
112
+ try {
113
+ await userDb.add({ username: 'Alice', email: 'alice@example.com' });
114
+ await userDb.add({ username: 'Bob', email: 'bob@example.com' });
115
+ await db.commit(transaction);
116
+ } catch (error) {
117
+ await db.rollback(transaction);
118
+ throw error;
119
+ }
120
+ ```
121
+
122
+ ## API Reference
123
+
124
+ ### Sqlite Class
125
+
126
+ #### Constructor
127
+ ```javascript
128
+ new Sqlite(config)
129
+ ```
130
+
131
+ #### Configuration Parameters
132
+
133
+ | Parameter | Type | Default | Description |
134
+ |-----------|------|---------|-------------|
135
+ | dir | string | './db/' | Database file storage directory |
136
+ | database | string | 'test' | Database filename (without .sqlite extension) |
137
+ | charset | string | 'utf8mb4' | Character set |
138
+ | timezone | string | '+08:00' | Timezone offset |
139
+ | connect_timeout | number | 20000 | Connection timeout in milliseconds |
140
+ | acquire_timeout | number | 20000 | Connection acquisition timeout |
141
+ | query_timeout | number | 20000 | Query timeout |
142
+ | connection_limit | number | 1 | Connection limit (>1 enables connection pool) |
143
+ | enable_keep_alive | boolean | true | Enable keep-alive mechanism |
144
+ | keep_alive_initial_delay | number | 10000 | Keep-alive initial delay |
145
+ | enable_reconnect | boolean | true | Enable automatic reconnection |
146
+ | reconnect_interval | number | 1000 | Reconnection interval |
147
+ | max_reconnect_attempts | number | 5 | Maximum reconnection attempts |
148
+ | wait_for_connections | boolean | true | Wait for available connections |
149
+ | pool_min | number | 1 | Connection pool minimum connections |
150
+ | pool_max | number | 5 | Connection pool maximum connections |
151
+ | pool_acquire_timeout | number | 30000 | Connection pool acquisition timeout |
152
+ | pool_idle_timeout | number | 60000 | Connection pool idle timeout |
153
+ | pool_reap_interval | number | 1000 | Connection pool cleanup interval |
154
+
155
+ #### Main Methods
156
+
157
+ - `open()` - Open database connection
158
+ - `close()` - Close database connection
159
+ - `db()` - Get database manager instance
160
+ - `run(sql, params)` - Execute query SQL
161
+ - `exec(sql, params)` - Execute non-query SQL
162
+
163
+ ### DB Class
164
+
165
+ #### Table Operations
166
+ - `addTable(table, field, type, auto, commit, timeout)` - Create table
167
+ - `dropTable(table, timeout)` - Drop table
168
+ - `renameTable(table, new_table, timeout)` - Rename table
169
+ - `emptyTable(table, timeout)` - Empty table
170
+ - `hasTable(table, timeout)` - Check if table exists
171
+
172
+ #### Field Operations
173
+ - `addField(table, field, type, len, def, not_null, auto, comment, timeout)` - Add field
174
+ - `dropField(table, field, timeout)` - Drop field
175
+ - `renameField(table, field, new_field, type, timeout)` - Rename field
176
+ - `fields(table, field_name, timeout)` - Get table fields
177
+ - `hasField(table, field, timeout)` - Check if field exists
178
+
179
+ #### Data Operations
180
+ - `add(data, timeout)` - Insert data
181
+ - `set(where, data, timeout)` - Update data
182
+ - `del(where, timeout)` - Delete data
183
+ - `get(where, options, timeout)` - Query data
184
+ - `count(where, timeout)` - Count records
185
+ - `sum(field, where, timeout)` - Sum field values
186
+ - `max(field, where, timeout)` - Get maximum value
187
+ - `min(field, where, timeout)` - Get minimum value
188
+
189
+ #### Transaction Operations
190
+ - `start()` - Start transaction
191
+ - `commit(transaction)` - Commit transaction
192
+ - `rollback(transaction)` - Rollback transaction
193
+
194
+ #### Utility Methods
195
+ - `new(table, key)` - Create new DB instance with table and key
196
+ - `table(table)` - Set table name
197
+ - `key(key)` - Set primary key
198
+ - `size(size)` - Set batch size
199
+ - `getTableData(table, batchSize, timeout)` - Get table data in batches
200
+
201
+ ## Query Conditions
202
+
203
+ ### Comparison Operators
204
+ - `_eq` - Equal to
205
+ - `_neq` - Not equal to
206
+ - `_gt` - Greater than
207
+ - `_gte` - Greater than or equal to
208
+ - `_lt` - Less than
209
+ - `_lte` - Less than or equal to
210
+
211
+ ### Logical Operators
212
+ - `_and` - Logical AND
213
+ - `_or` - Logical OR
214
+ - `_not` - Logical NOT
215
+
216
+ ### Array Operators
217
+ - `_in` - In array
218
+ - `_nin` - Not in array
219
+
220
+ ### String Operators
221
+ - `_like` - Like pattern
222
+ - `_nlike` - Not like pattern
223
+
224
+ ### Null Operators
225
+ - `_null` - Is null
226
+ - `_nnull` - Is not null
227
+
228
+ ## Query Options
229
+
230
+ ### Pagination
231
+ - `limit` - Number of records to return
232
+ - `offset` - Number of records to skip
233
+
234
+ ### Sorting
235
+ - `order` - Sort order (e.g., 'name asc', 'created_at desc')
236
+
237
+ ### Field Selection
238
+ - `field` - Specific fields to return
239
+ - `group` - Group by fields
240
+
241
+ ## Error Handling
242
+
243
+ All methods include comprehensive error handling:
244
+
245
+ ```javascript
246
+ try {
247
+ const result = await db.get({ id: 1 });
248
+ } catch (error) {
249
+ console.error('Query failed:', error.message);
250
+ }
251
+ ```
252
+
253
+ ## Development Specifications
254
+
255
+ ### Code Style
256
+ - Use 2-space indentation
257
+ - Single quotes for strings
258
+ - One class per file or multiple functions
259
+
260
+ ### Naming Conventions
261
+ - Class names: PascalCase
262
+ - Function/method names: camelCase
263
+ - Parameters/variables: snake_case
264
+ - Constants: UPPER_SNAKE_CASE
265
+ - Maximum name length: 20 characters
266
+ - Prefer single-word names
267
+
268
+ ### Error Handling
269
+ - Parameter validation using `throw new TypeError()`
270
+ - Use try...catch for calling other class methods
271
+ - Chinese error messages (for Chinese version)
272
+
273
+ ### Performance
274
+ - Method length ≤ 40 lines
275
+ - Single line ≤ 100 characters
276
+ - Each class focuses on single responsibility
277
+ - Each method does one thing only
278
+
279
+ ## License
280
+
281
+ MIT License - see LICENSE file for details.
282
+
283
+ ## Contributing
284
+
285
+ 1. Fork the repository
286
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
287
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
288
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
289
+ 5. Open a Pull Request
290
+
291
+ ## Support
292
+
293
+ For issues and questions, please open an issue on the GitHub repository.