mm_sqlite 1.2.1 → 1.2.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/README.md +98 -348
- package/README_EN.md +293 -0
- package/db.js +742 -690
- package/eslint.config.js +235 -0
- package/index.js +676 -676
- package/link_model.js +96 -99
- package/package.json +11 -4
- package/sql.js +1341 -1239
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.
|