mm_sqlite 1.4.0 → 1.4.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/index.js CHANGED
@@ -152,13 +152,8 @@ SQLite.prototype.getConn = async function (timeout) {
152
152
  await this.open(acquire_timeout);
153
153
  }
154
154
 
155
- try {
156
- // 统一从连接池获取连接
157
- return await this._pool.getConnection(acquire_timeout);
158
- } catch (error) {
159
- this.log('error', '获取连接失败', error);
160
- return null;
161
- }
155
+ // 统一从连接池获取连接
156
+ return await this._pool.getConnection(acquire_timeout);
162
157
  };
163
158
 
164
159
  /**
@@ -196,7 +191,7 @@ SQLite.prototype.run = async function (sql, params = [], timeout = 30000) {
196
191
  timeout_id = setTimeout(() => {
197
192
  reject(new Error(`SQL查询超时: ${timeout}ms`));
198
193
  }, timeout);
199
-
194
+
200
195
  // 执行SQL查询
201
196
  conn.all(sql, params || [], (error, rows) => {
202
197
  // 清除超时定时器
@@ -316,59 +311,53 @@ SQLite.prototype.read = async function (table, condition, options) {
316
311
  throw new TypeError('options must be object');
317
312
  }
318
313
 
319
- try {
320
- // 检查连接状态
321
- if (this._status !== 'connected') {
322
- throw new Error('数据库连接未建立');
323
- }
324
-
325
- // 构建基础SQL查询
326
- let sql = `SELECT * FROM ${table}`;
327
- const params = [];
314
+ // 检查连接状态
315
+ if (this._status !== 'connected') {
316
+ throw new Error('数据库连接未建立');
317
+ }
328
318
 
329
- // 处理条件
330
- if (condition && Object.keys(condition).length > 0) {
331
- const where_clauses = [];
332
- for (const [field, value] of Object.entries(condition)) {
333
- where_clauses.push(`${field} = ?`);
334
- params.push(value);
335
- }
336
- sql += ` WHERE ${where_clauses.join(' AND ')}`;
337
- }
319
+ // 构建基础SQL查询
320
+ let sql = `SELECT * FROM ${table}`;
321
+ const params = [];
338
322
 
339
- // 处理排序
340
- if (options && options.order_by) {
341
- sql += ` ORDER BY ${options.order_by}`;
323
+ // 处理条件
324
+ if (condition && Object.keys(condition).length > 0) {
325
+ const where_clauses = [];
326
+ for (const [field, value] of Object.entries(condition)) {
327
+ where_clauses.push(`${field} = ?`);
328
+ params.push(value);
342
329
  }
330
+ sql += ` WHERE ${where_clauses.join(' AND ')}`;
331
+ }
343
332
 
344
- // 处理分页
345
- if (options && options.limit) {
346
- sql += ` LIMIT ${options.limit}`;
347
- }
333
+ // 处理排序
334
+ if (options && options.order_by) {
335
+ sql += ` ORDER BY ${options.order_by}`;
336
+ }
348
337
 
349
- if (options && options.offset) {
350
- sql += ` OFFSET ${options.offset}`;
351
- }
338
+ // 处理分页
339
+ if (options && options.limit) {
340
+ sql += ` LIMIT ${options.limit}`;
341
+ }
352
342
 
353
- // 记录查询日志
354
- if (this.config.debug) {
355
- this.log('debug', '查询数据', { sql, params });
356
- }
343
+ if (options && options.offset) {
344
+ sql += ` OFFSET ${options.offset}`;
345
+ }
357
346
 
358
- // 执行查询
359
- const results = await this.run(sql, params);
347
+ // 记录查询日志
348
+ if (this.config.debug) {
349
+ this.log('debug', '查询数据', { sql, params });
350
+ }
360
351
 
361
- if (this.config.debug) {
362
- this.log('debug', '查询成功', { count: Array.isArray(results) ? results.length : 1 });
363
- }
352
+ // 执行查询
353
+ const results = await this.run(sql, params);
364
354
 
365
- // 确保返回数组格式
366
- return Array.isArray(results) ? results : [results];
367
- } catch (error) {
368
- this.log('error', '查询失败', error);
369
- // 返回空数组作为默认值,保持返回值类型一致
370
- return [];
355
+ if (this.config.debug) {
356
+ this.log('debug', '查询成功', { count: Array.isArray(results) ? results.length : 1 });
371
357
  }
358
+
359
+ // 确保返回数组格式
360
+ return Array.isArray(results) ? results : [results];
372
361
  };
373
362
 
374
363
  /**
@@ -415,7 +404,7 @@ SQLite.prototype.db = function () {
415
404
  * 开始事务
416
405
  * @returns {Promise<object>} 事务连接对象
417
406
  */
418
- SQLite.prototype.beginTrans = async function () {
407
+ SQLite.prototype.beginTransaction = async function () {
419
408
  try {
420
409
  // 检查连接状态
421
410
  if (this._status !== 'connected') {
@@ -515,7 +504,7 @@ SQLite.prototype.beginTrans = async function () {
515
504
  * @returns {Promise<*>} 回调函数的返回值
516
505
  * @throws {TypeError} 当callback参数无效时
517
506
  */
518
- SQLite.prototype.trans = async function (callback) {
507
+ SQLite.prototype.transaction = async function (callback) {
519
508
  // 参数校验
520
509
  if (typeof callback !== 'function') {
521
510
  throw new TypeError('callback must be function');
@@ -525,7 +514,7 @@ SQLite.prototype.trans = async function (callback) {
525
514
 
526
515
  try {
527
516
  // 开始事务
528
- trans = await this.beginTrans();
517
+ trans = await this.beginTransaction();
529
518
 
530
519
  // 执行回调函数,传入事务对象
531
520
  const result = await callback(trans);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_sqlite",
3
- "version": "1.4.0",
3
+ "version": "1.4.3",
4
4
  "description": "高性能SQLite数据库操作模块,提供与mm_mysql完全兼容的API接口,支持异步操作、事务处理和批量操作",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -38,12 +38,12 @@
38
38
  "node": ">=14.0.0"
39
39
  },
40
40
  "dependencies": {
41
- "mm_expand": "^2.2.8",
41
+ "mm_expand": "^2.3.1",
42
42
  "sqlite3": "^6.0.1",
43
43
  "sqlstring": "^2.3.3"
44
44
  },
45
45
  "devDependencies": {
46
- "eslint": "^10.2.0",
46
+ "eslint": "^10.2.1",
47
47
  "eslint-plugin-jsdoc": "^62.9.0",
48
48
  "mm_eslint": "^1.7.1"
49
49
  }
package/pool.js CHANGED
@@ -33,24 +33,19 @@ class SQLitePool {
33
33
  return;
34
34
  }
35
35
 
36
- try {
37
- // 创建最小连接数
38
- for (let i = 0; i < this.config.pool_min; i++) {
39
- const conn = await this._createConnection();
40
- this._pool_connections.push(conn);
41
- this._pool_available.push(conn);
42
- }
36
+ // 创建最小连接数
37
+ for (let i = 0; i < this.config.pool_min; i++) {
38
+ const conn = await this._createConnection();
39
+ this._pool_connections.push(conn);
40
+ this._pool_available.push(conn);
41
+ }
43
42
 
44
- // 启动连接回收器
45
- this._pool_reaper = setInterval(() => {
46
- this._reapIdleConnections();
47
- }, this.config.pool_reap_interval);
43
+ // 启动连接回收器
44
+ this._pool_reaper = setInterval(() => {
45
+ this._reapIdleConnections();
46
+ }, this.config.pool_reap_interval);
48
47
 
49
- this._is_initialized = true;
50
- } catch (error) {
51
- this.log('error', '连接池初始化失败', error);
52
- throw error;
53
- }
48
+ this._is_initialized = true;
54
49
  }
55
50
 
56
51
  /**