mm_mongodb 1.5.3 → 1.5.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.
@@ -0,0 +1,211 @@
1
+ 'use strict';
2
+
3
+ const namingConventionPlugin = require('mm_eslint');
4
+
5
+ /**
6
+ * 基于个人规则的ESLint配置
7
+ * 符合最新ESLint Flat Config格式
8
+ */
9
+
10
+ module.exports = [
11
+ {
12
+ files: ['**/*.js'],
13
+ plugins: {
14
+ 'naming-convention': namingConventionPlugin,
15
+ 'jsdoc': require('eslint-plugin-jsdoc')
16
+ },
17
+ languageOptions: {
18
+ ecmaVersion: 'latest',
19
+ sourceType: 'module',
20
+ parserOptions: {
21
+ ecmaVersion: 'latest',
22
+ sourceType: 'module'
23
+ }
24
+ },
25
+
26
+ // 命名规范规则 - 由自定义插件处理
27
+ rules: {
28
+ // 自定义命名规范插件规则(优先使用)
29
+ 'naming-convention/class-name': 'error',
30
+ 'naming-convention/function-name': 'error',
31
+ 'naming-convention/method-name': 'error',
32
+ 'naming-convention/variable-name': 'warn',
33
+ 'naming-convention/constant-name': 'error',
34
+ 'naming-convention/private-method-naming': 'warn',
35
+ 'naming-convention/private-variable-naming': 'warn',
36
+ 'naming-convention/param-name': 'warn',
37
+ 'naming-convention/property-name': 'warn',
38
+ 'naming-convention/instance-property': 'warn',
39
+
40
+ // 禁用与命名规范插件冲突的默认规则
41
+ 'camelcase': 'off',
42
+ 'id-match': 'off',
43
+ 'new-cap': 'off',
44
+ 'id-length': 'off',
45
+ 'id-denylist': 'off',
46
+ 'id-blacklist': 'off'
47
+ }
48
+ },
49
+
50
+ {
51
+ // 代码风格规则
52
+ rules: {
53
+ // 最大行长度100字符
54
+ 'max-len': ['error', {
55
+ code: 100,
56
+ ignoreComments: true,
57
+ ignoreUrls: true,
58
+ ignoreStrings: true,
59
+ ignoreTemplateLiterals: true
60
+ }],
61
+
62
+ // 缩进2空格
63
+ 'indent': ['error', 2, {
64
+ SwitchCase: 1,
65
+ VariableDeclarator: 1,
66
+ outerIIFEBody: 1,
67
+ MemberExpression: 1,
68
+ FunctionDeclaration: { parameters: 1, body: 1 },
69
+ FunctionExpression: { parameters: 1, body: 1 },
70
+ CallExpression: { arguments: 1 },
71
+ ArrayExpression: 1,
72
+ ObjectExpression: 1,
73
+ ImportDeclaration: 1,
74
+ flatTernaryExpressions: false,
75
+ ignoreComments: false
76
+ }],
77
+
78
+ // 单引号
79
+ 'quotes': ['error', 'single', {
80
+ avoidEscape: true,
81
+ allowTemplateLiterals: true
82
+ }],
83
+
84
+ // 禁止制表符
85
+ 'no-tabs': 'error',
86
+
87
+ // 行尾分号
88
+ 'semi': ['error', 'always'],
89
+
90
+ // 逗号风格
91
+ 'comma-style': ['error', 'last'],
92
+
93
+ // 逗号后必须有空格,逗号前不能有空格
94
+ 'comma-spacing': ['error', {
95
+ before: false,
96
+ after: true
97
+ }]
98
+ }
99
+ },
100
+
101
+ {
102
+ // 错误处理规则
103
+ rules: {
104
+ // 必须进行参数校验
105
+ 'no-unused-vars': ['error', {
106
+ args: 'all',
107
+ argsIgnorePattern: '^_',
108
+ caughtErrors: 'all',
109
+ caughtErrorsIgnorePattern: '^_',
110
+ destructuredArrayIgnorePattern: '^_',
111
+ varsIgnorePattern: '^_',
112
+ ignoreRestSiblings: true
113
+ }],
114
+
115
+ // 建议使用try-catch
116
+ 'no-unsafe-finally': 'warn',
117
+
118
+ // 禁止直接抛出字符串
119
+ 'no-throw-literal': 'error'
120
+ }
121
+ },
122
+
123
+ {
124
+ // 最佳实践规则
125
+ rules: {
126
+ // 优先使用async/await
127
+ 'prefer-promise-reject-errors': 'error',
128
+
129
+ // 避免副作用
130
+ 'no-param-reassign': ['error', {
131
+ props: true,
132
+ ignorePropertyModificationsFor: [
133
+ 'acc', // for reduce accumulators
134
+ 'accumulator', // for reduce accumulators
135
+ 'e', // for e.returnvalue
136
+ 'ctx', // for Koa routing
137
+ 'context', // for Koa routing
138
+ 'req', // for Express requests
139
+ 'request', // for Express requests
140
+ 'res', // for Express responses
141
+ 'response', // for Express responses
142
+ '$scope', // for Angular 1 scopes
143
+ 'staticContext' // for ReactRouter context
144
+ ]
145
+ }],
146
+
147
+ // 优先链式编程
148
+ 'prefer-object-spread': 'error',
149
+
150
+ // 优先函数式编程
151
+ 'prefer-arrow-callback': 'error',
152
+
153
+ // 方法长度限制
154
+ 'max-lines-per-function': ['warn', {
155
+ max: 40,
156
+ skipBlankLines: true,
157
+ skipComments: true
158
+ }],
159
+
160
+ // 复杂度限制
161
+ 'complexity': ['warn', 10]
162
+ }
163
+ },
164
+
165
+ {
166
+ // JSDoc规则
167
+ rules: {
168
+ // 要求公开方法有JSDoc注释
169
+ 'jsdoc/require-jsdoc': ['warn', {
170
+ publicOnly: true,
171
+ require: {
172
+ FunctionDeclaration: true,
173
+ MethodDefinition: true,
174
+ ClassDeclaration: true,
175
+ ArrowFunctionExpression: false,
176
+ FunctionExpression: false
177
+ }
178
+ }],
179
+
180
+ // 检查JSDoc语法
181
+ 'jsdoc/check-alignment': 'warn',
182
+ 'jsdoc/check-indentation': 'warn',
183
+ 'jsdoc/check-param-names': 'warn',
184
+ 'jsdoc/check-syntax': 'warn',
185
+ 'jsdoc/check-tag-names': 'warn',
186
+ 'jsdoc/check-types': 'warn',
187
+ 'jsdoc/no-undefined-types': 'warn',
188
+ 'jsdoc/require-description': 'warn',
189
+ 'jsdoc/require-param': 'warn',
190
+ 'jsdoc/require-param-description': 'warn',
191
+ 'jsdoc/require-param-name': 'warn',
192
+ 'jsdoc/require-param-type': 'warn',
193
+ 'jsdoc/require-returns': 'warn',
194
+ 'jsdoc/require-returns-check': 'warn',
195
+ 'jsdoc/require-returns-description': 'warn',
196
+ 'jsdoc/require-returns-type': 'warn',
197
+ 'jsdoc/valid-types': 'warn'
198
+ }
199
+ },
200
+
201
+ {
202
+ // 忽略规则
203
+ ignores: [
204
+ 'node_modules/**',
205
+ 'dist/**',
206
+ 'build/**',
207
+ 'coverage/**',
208
+ '*.min.js'
209
+ ]
210
+ }
211
+ ];
package/index.js CHANGED
@@ -12,31 +12,32 @@ const MongoClient = require('mongodb').MongoClient;
12
12
  * @description MongoDB帮助类
13
13
  */
14
14
  class MongoDB extends Base {
15
+ static config = {
16
+ // 服务器地址
17
+ host: "localhost",
18
+ // 端口号
19
+ port: 27017,
20
+ // 数据库名
21
+ database: "mm",
22
+ // 用户名
23
+ user: "",
24
+ // 密码
25
+ password: "",
26
+ // 连接选项 (MongoDB驱动7.x版本支持的参数)
27
+ min_pool_size: 10,
28
+ max_pool_size: 100,
29
+ connect_timeout_ms: 10000,
30
+ server_selection_timeout_ms: 5000,
31
+ retry_writes: true
32
+ };
33
+
15
34
  /**
16
35
  * @description 创建MongoDB帮助类函数 (构造函数)
17
36
  * @param {Object} config 配置参数
18
37
  * @constructor
19
38
  */
20
39
  constructor(config) {
21
- super();
22
- this.config = {
23
- // 服务器地址
24
- host: "localhost",
25
- // 端口号
26
- port: 27017,
27
- // 数据库名
28
- database: "mm",
29
- // 用户名
30
- user: "",
31
- // 密码
32
- password: "",
33
- // 连接选项 (MongoDB驱动7.x版本支持的参数)
34
- min_pool_size: 10,
35
- max_pool_size: 100,
36
- connect_timeout_ms: 10000,
37
- server_selection_timeout_ms: 5000,
38
- retry_writes: true
39
- };
40
+ super(MongoDB.config);
40
41
 
41
42
  // 数据库连接器
42
43
  this.conn = null;
@@ -102,7 +103,7 @@ MongoDB.prototype._withRetry = async function (operation, operationName) {
102
103
  return await operation();
103
104
  } catch (error) {
104
105
  lastError = error;
105
- this.logger('error', `${operationName} 第${attempt}次尝试失败:`, error);
106
+ this.log('error', `${operationName} 第${attempt}次尝试失败:`, error);
106
107
 
107
108
  // 如果不是最后一次尝试,则等待后重试
108
109
  if (attempt < maxAttempts) {
@@ -138,16 +139,16 @@ MongoDB.prototype._ensureConnected = async function () {
138
139
 
139
140
  // 检查连接是否成功
140
141
  if (this.db && this.db.collection) {
141
- this.logger('debug', `[_ensureConnected] 连接成功,db对象存在,开始初始化collection,table=${this.table}`);
142
+ this.log('debug', `[_ensureConnected] 连接成功,db对象存在,开始初始化collection,table=${this.table}`);
142
143
  this.connected = true;
143
144
  this.connectAttempts = 0;
144
145
 
145
146
  // 保存collection引用到实例变量
146
147
  try {
147
148
  this.collection = this.db.collection(this.table);
148
- this.logger('debug', `[_ensureConnected] 成功初始化collection: ${this.table}`);
149
+ this.log('debug', `[_ensureConnected] 成功初始化collection: ${this.table}`);
149
150
  } catch (err) {
150
- this.logger('error', `[_ensureConnected] 初始化collection失败:`, err);
151
+ this.log('error', `[_ensureConnected] 初始化collection失败:`, err);
151
152
  this.collection = null;
152
153
  }
153
154
 
@@ -156,14 +157,14 @@ MongoDB.prototype._ensureConnected = async function () {
156
157
  await this._ensureIndexes();
157
158
  }
158
159
 
159
- this.logger('info', `成功连接到数据库,表名: ${this.table}`);
160
+ this.log('info', `成功连接到数据库,表名: ${this.table}`);
160
161
  } else {
161
- this.logger('error', `[_ensureConnected] 连接失败,db对象为空或不具有collection方法`);
162
+ this.log('error', `[_ensureConnected] 连接失败,db对象为空或不具有collection方法`);
162
163
  throw new Error('数据库连接失败,db对象为空');
163
164
  }
164
165
  }
165
166
  } catch (error) {
166
- this.logger('error', `连接失败 (尝试 ${this.connectAttempts}/${this.maxConnectAttempts}):`, error);
167
+ this.log('error', `连接失败 (尝试 ${this.connectAttempts}/${this.maxConnectAttempts}):`, error);
167
168
 
168
169
  // 重置连接Promise,允许下次重试
169
170
  this.connectPromise = null;
@@ -173,7 +174,7 @@ MongoDB.prototype._ensureConnected = async function () {
173
174
  throw error;
174
175
  } else {
175
176
  // 超过重试次数,记录严重错误
176
- this.logger('error', '达到最大连接重试次数,连接失败');
177
+ this.log('error', '达到最大连接重试次数,连接失败');
177
178
  this.emit('error', new Error('数据库连接失败,已达到最大重试次数'));
178
179
  }
179
180
  }
@@ -207,7 +208,7 @@ MongoDB.prototype._ensureIndexes = async function () {
207
208
  } catch (error) {
208
209
  // 忽略索引已存在的错误
209
210
  if (error.code !== 85) {
210
- this.logger('error', '创建索引失败:', error);
211
+ this.log('error', '创建索引失败:', error);
211
212
  }
212
213
  }
213
214
  };
@@ -230,7 +231,7 @@ MongoDB.prototype.get = async function (key) {
230
231
  });
231
232
  return result ? result.value : null;
232
233
  } catch (error) {
233
- this.logger('error', '获取缓存失败:', error);
234
+ this.log('error', '获取缓存失败:', error);
234
235
  this.emit('error', error);
235
236
  }
236
237
  };
@@ -280,7 +281,7 @@ MongoDB.prototype.clear = async function (pattern) {
280
281
  error,
281
282
  pattern
282
283
  });
283
- this.logger('error', '清空缓存失败:', error);
284
+ this.log('error', '清空缓存失败:', error);
284
285
  return false;
285
286
  }
286
287
  };
@@ -313,7 +314,7 @@ MongoDB.prototype.keys = async function (pattern = '*') {
313
314
  });
314
315
  return results.map(item => item.key);
315
316
  } catch (error) {
316
- this.logger('error', '获取键列表失败:', error);
317
+ this.log('error', '获取键列表失败:', error);
317
318
  return [];
318
319
  }
319
320
  };
@@ -357,12 +358,11 @@ MongoDB.prototype.ttl = async function (key, seconds) {
357
358
 
358
359
  // 直接尝试获取collection,使用实例的table属性
359
360
  if (!this.collection && this.db && this.db.collection) {
360
- this.logger('debug', `[ttl] 设置模式:直接获取collection, table=${this.table}`);
361
+ this.log('debug', `[ttl] 设置模式:直接获取collection, table=${this.table}`);
361
362
  try {
362
363
  this.collection = this.db.collection(this.table);
363
- this.logger('debug', `[ttl] 设置模式:成功获取collection`);
364
364
  } catch (err) {
365
- this.logger('error', `[ttl] 设置模式:获取collection失败:`, err);
365
+ this.log('error', `[ttl] 设置模式:获取collection失败:`, err);
366
366
  this.collection = null;
367
367
  }
368
368
  }
@@ -425,12 +425,12 @@ MongoDB.prototype.ttl = async function (key, seconds) {
425
425
 
426
426
  // 直接尝试获取collection,使用实例的table属性
427
427
  if (!this.collection && this.db && this.db.collection) {
428
- this.logger('debug', `[ttl] 直接获取collection, table=${this.table}`);
428
+ this.log('debug', `[ttl] 直接获取collection, table=${this.table}`);
429
429
  try {
430
430
  this.collection = this.db.collection(this.table);
431
- this.logger('debug', `[ttl] 成功获取collection`);
431
+ this.log('debug', `[ttl] 成功获取collection`);
432
432
  } catch (err) {
433
- this.logger('error', `[ttl] 获取collection失败:`, err);
433
+ this.log('error', `[ttl] 获取collection失败:`, err);
434
434
  this.collection = null;
435
435
  }
436
436
  }
@@ -442,17 +442,17 @@ MongoDB.prototype.ttl = async function (key, seconds) {
442
442
 
443
443
  // 查询键是否存在及过期时间
444
444
  const item = await this.collection.findOne({ key });
445
- this.logger('debug', `[ttl] 查询键结果: item=${JSON.stringify(item)}`);
445
+ this.log('debug', `[ttl] 查询键结果: item=${JSON.stringify(item)}`);
446
446
 
447
447
  if (!item) {
448
448
  // 键不存在返回-2
449
- this.logger('debug', `[ttl] 键 ${key} 不存在,返回-2`);
449
+ this.log('debug', `[ttl] 键 ${key} 不存在,返回-2`);
450
450
  return -2;
451
451
  }
452
452
 
453
453
  if (!item.expire_at) {
454
454
  // 没有过期时间返回-1
455
- this.logger('debug', `[ttl] 键 ${key} 没有过期时间,返回-1`);
455
+ this.log('debug', `[ttl] 键 ${key} 没有过期时间,返回-1`);
456
456
  return -1;
457
457
  }
458
458
 
@@ -471,7 +471,7 @@ MongoDB.prototype.ttl = async function (key, seconds) {
471
471
  }
472
472
  } catch (error) {
473
473
  this.emit('ttl:error', { key, seconds, error });
474
- this.logger('error', `[ttl] 操作失败:`, error);
474
+ this.log('error', `[ttl] 操作失败:`, error);
475
475
  return arguments.length === 1 ? -2 : false;
476
476
  }
477
477
  };
@@ -509,19 +509,19 @@ MongoDB.prototype.persist = async function (key) {
509
509
 
510
510
  // 直接尝试获取collection,不依赖_ensureConnected的复杂逻辑
511
511
  if (!this.collection && this.db && this.db.collection) {
512
- this.logger('debug', `[persist] 直接获取collection, table=${this.table}`);
512
+ this.log('debug', `[persist] 直接获取collection, table=${this.table}`);
513
513
  try {
514
514
  this.collection = this.db.collection(this.table);
515
- this.logger('debug', `[persist] 成功获取collection`);
515
+ this.log('debug', `[persist] 成功获取collection`);
516
516
  } catch (err) {
517
- this.logger('error', `[persist] 获取collection失败:`, err);
517
+ this.log('error', `[persist] 获取collection失败:`, err);
518
518
  this.collection = null;
519
519
  }
520
520
  }
521
521
 
522
522
  // 再次检查collection
523
523
  if (!this.collection) {
524
- this.logger('error', 'persist: collection is undefined');
524
+ this.log('error', 'persist: collection is undefined');
525
525
  this.emit('persist:error', {
526
526
  key: finalKey,
527
527
  error: new Error('collection is undefined')
@@ -566,7 +566,7 @@ MongoDB.prototype.persist = async function (key) {
566
566
  });
567
567
  return success;
568
568
  } catch (error) {
569
- this.logger('error', 'persist error:', error);
569
+ this.log('error', 'persist error:', error);
570
570
  this.emit('persist:error', {
571
571
  key,
572
572
  error
@@ -605,7 +605,7 @@ MongoDB.prototype._add = async function (key, value, expire) {
605
605
  }
606
606
  return true;
607
607
  } catch (error) {
608
- this.logger('error', '添加缓存失败:', error);
608
+ this.log('error', '添加缓存失败:', error);
609
609
  return false;
610
610
  }
611
611
  };
@@ -630,7 +630,7 @@ MongoDB.prototype.add = async function (key, value, seconds) {
630
630
 
631
631
  // 如果事件处理器标记为取消,则返回false
632
632
  if (beforeEventData.cancel) {
633
- this.logger('debug', `add操作被事件处理器取消 [${key}]`);
633
+ this.log('debug', `add操作被事件处理器取消 [${key}]`);
634
634
  return false;
635
635
  }
636
636
 
@@ -679,7 +679,7 @@ MongoDB.prototype.add = async function (key, value, seconds) {
679
679
  });
680
680
  return true;
681
681
  } catch (error) {
682
- this.logger('error', `添加缓存失败 [${key}]:`, error);
682
+ this.log('error', `添加缓存失败 [${key}]:`, error);
683
683
  // 触发错误事件
684
684
  this.emit('add:error', {
685
685
  key,
@@ -714,7 +714,7 @@ MongoDB.prototype._addNum = async function (key, value = 1) {
714
714
  await this._add(key, newValue);
715
715
  return newValue;
716
716
  } catch (error) {
717
- this.logger('error', '增加整数值失败:', error);
717
+ this.log('error', '增加整数值失败:', error);
718
718
  return false;
719
719
  }
720
720
  };
@@ -749,7 +749,7 @@ MongoDB.prototype.addInt = async function (key, value = 1, seconds = 0) {
749
749
 
750
750
  // 如果事件处理器标记为取消,则返回默认值
751
751
  if (beforeEventData.cancel) {
752
- this.logger('debug', 'addInt操作被事件处理器取消');
752
+ this.log('debug', 'addInt操作被事件处理器取消');
753
753
  return 0;
754
754
  }
755
755
 
@@ -794,7 +794,7 @@ MongoDB.prototype.addInt = async function (key, value = 1, seconds = 0) {
794
794
  seconds,
795
795
  error
796
796
  });
797
- this.logger('error', '增加整数值失败:', error);
797
+ this.log('error', '增加整数值失败:', error);
798
798
  // 与CacheBase保持一致,错误时返回默认值而不是抛出异常
799
799
  return 0;
800
800
  }
@@ -824,7 +824,7 @@ MongoDB.prototype.addFloat = async function (key, value = 1.0, seconds = 0) {
824
824
 
825
825
  // 如果事件处理器标记为取消,则返回默认值
826
826
  if (beforeEventData.cancel) {
827
- this.logger('debug', 'addFloat操作被事件处理器取消');
827
+ this.log('debug', 'addFloat操作被事件处理器取消');
828
828
  return 0.0;
829
829
  }
830
830
 
@@ -869,7 +869,7 @@ MongoDB.prototype.addFloat = async function (key, value = 1.0, seconds = 0) {
869
869
  seconds,
870
870
  error
871
871
  });
872
- this.logger('error', '增加浮点数值失败:', error);
872
+ this.log('error', '增加浮点数值失败:', error);
873
873
  // 与CacheBase保持一致,错误时返回默认值而不是抛出异常
874
874
  return 0.0;
875
875
  }
@@ -923,7 +923,7 @@ MongoDB.prototype.addStr = async function (key, value) {
923
923
  value,
924
924
  error
925
925
  });
926
- this.logger('error', '添加字符串失败:', error);
926
+ this.log('error', '添加字符串失败:', error);
927
927
  return 0;
928
928
  }
929
929
  };
@@ -960,7 +960,7 @@ MongoDB.prototype.sort = async function (key, options = {}) {
960
960
 
961
961
  return sorted;
962
962
  } catch (error) {
963
- this.logger('error', '排序失败:', error);
963
+ this.log('error', '排序失败:', error);
964
964
  return [];
965
965
  }
966
966
  };
@@ -994,7 +994,7 @@ MongoDB.prototype.hset = async function (key, field, value) {
994
994
  await this._add(key, hash);
995
995
  return true; // hset操作成功返回true
996
996
  } catch (error) {
997
- this.logger('error', '哈希表设置失败:', error);
997
+ this.log('error', '哈希表设置失败:', error);
998
998
  throw error;
999
999
  }
1000
1000
  };
@@ -1024,7 +1024,7 @@ MongoDB.prototype.hget = async function (key, field) {
1024
1024
  }
1025
1025
  return item.value[field] !== undefined ? item.value[field] : undefined;
1026
1026
  } catch (error) {
1027
- this.logger('error', '哈希表获取失败:', error);
1027
+ this.log('error', '哈希表获取失败:', error);
1028
1028
  return null; // 与Redis保持一致,错误时返回null
1029
1029
  }
1030
1030
  };
@@ -1049,7 +1049,7 @@ MongoDB.prototype.hgetall = async function (key) {
1049
1049
  }
1050
1050
  return item.value;
1051
1051
  } catch (error) {
1052
- this.logger('error', '哈希表获取全部失败:', error);
1052
+ this.log('error', '哈希表获取全部失败:', error);
1053
1053
  return {};
1054
1054
  }
1055
1055
  };
@@ -1109,7 +1109,7 @@ MongoDB.prototype.hdel = async function (key, fields) {
1109
1109
 
1110
1110
  return deletedCount;
1111
1111
  } catch (error) {
1112
- this.logger('error', 'hdel操作失败:', error);
1112
+ this.log('error', 'hdel操作失败:', error);
1113
1113
  return 0;
1114
1114
  }
1115
1115
  };
@@ -1131,7 +1131,7 @@ MongoDB.prototype.has = async function (key) {
1131
1131
  });
1132
1132
  return result !== null;
1133
1133
  } catch (error) {
1134
- this.logger('error', '[has] 检查键是否存在失败:', error);
1134
+ this.log('error', '[has] 检查键是否存在失败:', error);
1135
1135
  throw error;
1136
1136
  }
1137
1137
  };
@@ -1157,7 +1157,7 @@ MongoDB.prototype.expire = async function (key, seconds) {
1157
1157
 
1158
1158
  // 如果事件处理器标记为取消,则返回默认值
1159
1159
  if (beforeEventData.cancel) {
1160
- this.logger('debug', 'expire操作被事件处理器取消');
1160
+ this.log('debug', 'expire操作被事件处理器取消');
1161
1161
  return false;
1162
1162
  }
1163
1163
 
@@ -1181,7 +1181,7 @@ MongoDB.prototype.expire = async function (key, seconds) {
1181
1181
  return result === true || (typeof result === 'number' && result >= -2);
1182
1182
  } catch (error) {
1183
1183
  // 记录错误日志
1184
- this.logger('error', 'expire方法执行错误', { error: error.message, key, seconds });
1184
+ this.log('error', 'expire方法执行错误', { error: error.message, key, seconds });
1185
1185
 
1186
1186
  // 触发错误事件
1187
1187
  this.emit('expire:error', {
@@ -1211,7 +1211,7 @@ MongoDB.prototype.incr = async function (key, seconds = 0) {
1211
1211
 
1212
1212
  // 如果事件处理器标记为取消,则返回默认值
1213
1213
  if (beforeEventData.cancel) {
1214
- this.logger('debug', 'incr操作被事件处理器取消');
1214
+ this.log('debug', 'incr操作被事件处理器取消');
1215
1215
  return 0;
1216
1216
  }
1217
1217
 
@@ -1246,7 +1246,7 @@ MongoDB.prototype.incr = async function (key, seconds = 0) {
1246
1246
  seconds,
1247
1247
  error
1248
1248
  });
1249
- this.logger('error', '递增操作失败:', error);
1249
+ this.log('error', '递增操作失败:', error);
1250
1250
  return 0;
1251
1251
  }
1252
1252
  };
@@ -1268,7 +1268,7 @@ MongoDB.prototype.decr = async function (key, seconds = 0) {
1268
1268
 
1269
1269
  // 如果事件处理器标记为取消,则返回默认值
1270
1270
  if (beforeEventData.cancel) {
1271
- this.logger('debug', 'decr操作被事件处理器取消');
1271
+ this.log('debug', 'decr操作被事件处理器取消');
1272
1272
  return 0;
1273
1273
  }
1274
1274
 
@@ -1303,7 +1303,7 @@ MongoDB.prototype.decr = async function (key, seconds = 0) {
1303
1303
  seconds,
1304
1304
  error
1305
1305
  });
1306
- this.logger('error', '递减操作失败:', error);
1306
+ this.log('error', '递减操作失败:', error);
1307
1307
  return 0;
1308
1308
  }
1309
1309
  };
@@ -1327,7 +1327,7 @@ MongoDB.prototype.incrby = async function (key, value = 1, seconds = 0) {
1327
1327
 
1328
1328
  // 如果事件处理器标记为取消,则返回默认值
1329
1329
  if (beforeEventData.cancel) {
1330
- this.logger('debug', 'incrby操作被事件处理器取消');
1330
+ this.log('debug', 'incrby操作被事件处理器取消');
1331
1331
  return 0;
1332
1332
  }
1333
1333
 
@@ -1368,7 +1368,7 @@ MongoDB.prototype.incrby = async function (key, value = 1, seconds = 0) {
1368
1368
  seconds,
1369
1369
  error
1370
1370
  });
1371
- this.logger('error', '递增操作失败:', error);
1371
+ this.log('error', '递增操作失败:', error);
1372
1372
  return 0;
1373
1373
  }
1374
1374
  };
@@ -1392,7 +1392,7 @@ MongoDB.prototype.decrby = async function (key, value = 1, seconds = 0) {
1392
1392
 
1393
1393
  // 如果事件处理器标记为取消,则返回默认值
1394
1394
  if (beforeEventData.cancel) {
1395
- this.logger('debug', 'decrby操作被事件处理器取消');
1395
+ this.log('debug', 'decrby操作被事件处理器取消');
1396
1396
  return 0;
1397
1397
  }
1398
1398
 
@@ -1433,7 +1433,7 @@ MongoDB.prototype.decrby = async function (key, value = 1, seconds = 0) {
1433
1433
  seconds,
1434
1434
  error
1435
1435
  });
1436
- this.logger('error', '递减操作失败:', error);
1436
+ this.log('error', '递减操作失败:', error);
1437
1437
  return 0;
1438
1438
  }
1439
1439
  };
@@ -1457,7 +1457,7 @@ MongoDB.prototype.incrbyfloat = async function (key, value = 1.0, seconds = 0) {
1457
1457
 
1458
1458
  // 如果事件处理器标记为取消,则返回默认值
1459
1459
  if (beforeEventData.cancel) {
1460
- this.logger('debug', 'incrbyfloat操作被事件处理器取消');
1460
+ this.log('debug', 'incrbyfloat操作被事件处理器取消');
1461
1461
  return 0.0;
1462
1462
  }
1463
1463
 
@@ -1498,7 +1498,7 @@ MongoDB.prototype.incrbyfloat = async function (key, value = 1.0, seconds = 0) {
1498
1498
  seconds,
1499
1499
  error
1500
1500
  });
1501
- this.logger('error', '浮点递增操作失败:', error);
1501
+ this.log('error', '浮点递增操作失败:', error);
1502
1502
  return 0.0;
1503
1503
  }
1504
1504
  };
@@ -1545,7 +1545,7 @@ MongoDB.prototype.exists = async function (key) {
1545
1545
  return result !== null;
1546
1546
  }
1547
1547
  } catch (error) {
1548
- this.logger('error', '检查键是否存在失败:', error);
1548
+ this.log('error', '检查键是否存在失败:', error);
1549
1549
  throw error;
1550
1550
  }
1551
1551
  };
@@ -1575,7 +1575,7 @@ MongoDB.prototype._set = async function (key, value, expire) {
1575
1575
  );
1576
1576
  return true;
1577
1577
  } catch (error) {
1578
- this.logger('error', '设置缓存失败:', error);
1578
+ this.log('error', '设置缓存失败:', error);
1579
1579
  return false;
1580
1580
  } // 与CacheBase保持一致,返回false而不是抛出异常
1581
1581
  };
@@ -1601,7 +1601,7 @@ MongoDB.prototype.hmset = async function (key, ...fieldValues) {
1601
1601
  try {
1602
1602
  // 参数验证
1603
1603
  if (!key || typeof key !== 'string') {
1604
- this.logger('warn', 'hmset: 键名无效');
1604
+ this.log('warn', 'hmset: 键名无效');
1605
1605
  return 0;
1606
1606
  }
1607
1607
 
@@ -1650,7 +1650,7 @@ MongoDB.prototype.hmset = async function (key, ...fieldValues) {
1650
1650
 
1651
1651
  return true; // 操作成功返回true
1652
1652
  } catch (error) {
1653
- this.logger('error', '[hmset] 哈希表批量设置失败:', error);
1653
+ this.log('error', '[hmset] 哈希表批量设置失败:', error);
1654
1654
  return false; // 操作失败返回false
1655
1655
  }
1656
1656
  };
@@ -1687,7 +1687,7 @@ MongoDB.prototype.hmget = async function (key, fields) {
1687
1687
 
1688
1688
  return values;
1689
1689
  } catch (error) {
1690
- this.logger('error', 'hmget操作失败:', error);
1690
+ this.log('error', 'hmget操作失败:', error);
1691
1691
  // 错误时返回与fields长度相同的null数组
1692
1692
  const fieldArray = Array.isArray(fields) ? fields : [fields];
1693
1693
  return fieldArray.map(() => null);
@@ -1735,7 +1735,7 @@ MongoDB.prototype.hdelMulti = async function (key, fields) {
1735
1735
  }
1736
1736
  return deleted;
1737
1737
  } catch (error) {
1738
- this.logger('error', '增加整数值失败:', error);
1738
+ this.log('error', '增加整数值失败:', error);
1739
1739
  return false;
1740
1740
  }
1741
1741
  };
@@ -1761,7 +1761,7 @@ MongoDB.prototype.hvals = async function (key) {
1761
1761
  }
1762
1762
  return Object.values(item.value);
1763
1763
  } catch (error) {
1764
- this.logger('error', '哈希表获取所有值失败:', error);
1764
+ this.log('error', '哈希表获取所有值失败:', error);
1765
1765
  return [];
1766
1766
  }
1767
1767
  };
@@ -1787,7 +1787,7 @@ MongoDB.prototype.hkeys = async function (key) {
1787
1787
  }
1788
1788
  return Object.keys(item.value);
1789
1789
  } catch (error) {
1790
- this.logger('error', '哈希表获取所有字段名失败:', error);
1790
+ this.log('error', '哈希表获取所有字段名失败:', error);
1791
1791
  return [];
1792
1792
  }
1793
1793
  };
@@ -1815,7 +1815,7 @@ MongoDB.prototype.hlen = async function (key) {
1815
1815
  // 使用Object.keys获取自有属性数量,这比for-in循环更高效
1816
1816
  return Object.keys(item.value).length;
1817
1817
  } catch (error) {
1818
- this.logger('error', 'hlen操作失败:', error);
1818
+ this.log('error', 'hlen操作失败:', error);
1819
1819
  return 0;
1820
1820
  }
1821
1821
  };
@@ -1845,7 +1845,7 @@ MongoDB.prototype.hexists = async function (key, field) {
1845
1845
  }
1846
1846
  return field in item.value;
1847
1847
  } catch (error) {
1848
- this.logger('error', '哈希表字段是否存在失败:', error);
1848
+ this.log('error', '哈希表字段是否存在失败:', error);
1849
1849
  return false;
1850
1850
  }
1851
1851
  };
@@ -1874,7 +1874,7 @@ MongoDB.prototype.rpush = async function (key, value) {
1874
1874
  await this._add(key, list);
1875
1875
  return list.length;
1876
1876
  } catch (error) {
1877
- this.logger('error', '列表尾部添加失败:', error);
1877
+ this.log('error', '列表尾部添加失败:', error);
1878
1878
  return 0;
1879
1879
  }
1880
1880
  };
@@ -1903,7 +1903,7 @@ MongoDB.prototype.lpush = async function (key, value) {
1903
1903
  await this._add(key, list);
1904
1904
  return list.length;
1905
1905
  } catch (error) {
1906
- this.logger('error', '列表头部添加失败:', error);
1906
+ this.log('error', '列表头部添加失败:', error);
1907
1907
  return 0;
1908
1908
  }
1909
1909
  };
@@ -1946,7 +1946,7 @@ MongoDB.prototype.lrange = async function (key, start, stop) {
1946
1946
 
1947
1947
  return item.value.slice(startIndex, endIndex + 1);
1948
1948
  } catch (error) {
1949
- this.logger('error', '列表范围查询失败:', error);
1949
+ this.log('error', '列表范围查询失败:', error);
1950
1950
  return [];
1951
1951
  }
1952
1952
  };
@@ -1976,7 +1976,7 @@ MongoDB.prototype.lindex = async function (key, index) {
1976
1976
 
1977
1977
  return item.value[realIndex];
1978
1978
  } catch (error) {
1979
- this.logger('error', '获取列表索引元素失败:', error);
1979
+ this.log('error', '获取列表索引元素失败:', error);
1980
1980
  return null;
1981
1981
  }
1982
1982
  };
@@ -2013,7 +2013,7 @@ MongoDB.prototype.lset = async function (key, index, value) {
2013
2013
  });
2014
2014
  return true;
2015
2015
  } catch (error) {
2016
- this.logger('error', '设置列表索引元素失败:', error);
2016
+ this.log('error', '设置列表索引元素失败:', error);
2017
2017
  return false;
2018
2018
  }
2019
2019
  };
@@ -2072,7 +2072,7 @@ MongoDB.prototype.lrem = async function (key, count, value) {
2072
2072
 
2073
2073
  return removedCount;
2074
2074
  } catch (error) {
2075
- this.logger('error', '删除列表元素失败:', error);
2075
+ this.log('error', '删除列表元素失败:', error);
2076
2076
  return 0;
2077
2077
  }
2078
2078
  };
@@ -2103,7 +2103,7 @@ MongoDB.prototype.ltrim = async function (key, start, stop) {
2103
2103
  });
2104
2104
  return true;
2105
2105
  } catch (error) {
2106
- this.logger('error', '增加整数值失败:', error);
2106
+ this.log('error', '增加整数值失败:', error);
2107
2107
  return false;
2108
2108
  }
2109
2109
  };
@@ -2129,7 +2129,7 @@ MongoDB.prototype.llen = async function (key) {
2129
2129
  }
2130
2130
  return item.value.length;
2131
2131
  } catch (error) {
2132
- this.logger('error', '获取列表长度失败:', error);
2132
+ this.log('error', '获取列表长度失败:', error);
2133
2133
  return 0;
2134
2134
  }
2135
2135
  };
@@ -2170,7 +2170,7 @@ MongoDB.prototype.saddMulti = async function (key, members) {
2170
2170
 
2171
2171
  return addedCount;
2172
2172
  } catch (error) {
2173
- this.logger('error', '集合并发添加失败:', error);
2173
+ this.log('error', '集合并发添加失败:', error);
2174
2174
  return 0;
2175
2175
  }
2176
2176
  };
@@ -2213,7 +2213,7 @@ MongoDB.prototype.sremMulti = async function (key, members) {
2213
2213
 
2214
2214
  return removedCount;
2215
2215
  } catch (error) {
2216
- this.logger('error', '集合并发删除失败:', error);
2216
+ this.log('error', '集合并发删除失败:', error);
2217
2217
  return 0;
2218
2218
  }
2219
2219
  };
@@ -2259,7 +2259,7 @@ MongoDB.prototype.sdiff = async function (...keys) {
2259
2259
  // 找出在baseSet中但不在其他所有集合中的元素
2260
2260
  return baseSet.filter(member => !otherSetsStr.has(JSON.stringify(member)));
2261
2261
  } catch (error) {
2262
- this.logger('error', '集合差集失败:', error);
2262
+ this.log('error', '集合差集失败:', error);
2263
2263
  return [];
2264
2264
  }
2265
2265
  };
@@ -2317,7 +2317,7 @@ MongoDB.prototype.sinter = async function (...keys) {
2317
2317
  return otherSetsStr.every(set => set.has(memberStr));
2318
2318
  });
2319
2319
  } catch (error) {
2320
- this.logger('error', '集合交集失败:', error);
2320
+ this.log('error', '集合交集失败:', error);
2321
2321
  return [];
2322
2322
  }
2323
2323
  };
@@ -2364,7 +2364,7 @@ MongoDB.prototype.sunion = async function (...keys) {
2364
2364
 
2365
2365
  return Array.from(uniqueMap.values());
2366
2366
  } catch (error) {
2367
- this.logger('error', '集合并集失败:', error);
2367
+ this.log('error', '集合并集失败:', error);
2368
2368
  return [];
2369
2369
  }
2370
2370
  };
@@ -2393,7 +2393,7 @@ MongoDB.prototype.scard = async function (key) {
2393
2393
  const uniqueItems = new Set(item.value.map(m => JSON.stringify(m)));
2394
2394
  return uniqueItems.size;
2395
2395
  } catch (error) {
2396
- this.logger('error', '获取集合成员数量失败:', error);
2396
+ this.log('error', '获取集合成员数量失败:', error);
2397
2397
  return 0;
2398
2398
  }
2399
2399
  };
@@ -2444,7 +2444,7 @@ MongoDB.prototype.sismember = async function (key, member) {
2444
2444
  member,
2445
2445
  error
2446
2446
  });
2447
- this.logger('error', '检查集合成员失败:', error);
2447
+ this.log('error', '检查集合成员失败:', error);
2448
2448
  return false;
2449
2449
  }
2450
2450
  };
@@ -2472,7 +2472,7 @@ MongoDB.prototype.clearExpire = async function () {
2472
2472
  this.emit('clearExpire:error', {
2473
2473
  error
2474
2474
  });
2475
- this.logger('error', '清空过期数据失败:', error);
2475
+ this.log('error', '清空过期数据失败:', error);
2476
2476
  return false;
2477
2477
  }
2478
2478
  };
@@ -2498,7 +2498,7 @@ MongoDB.prototype.mget = async function (...keys) {
2498
2498
  // 返回与输入键顺序对应的数组,不存在的键对应位置返回undefined
2499
2499
  return keyArray.map(key => resultMap.hasOwnProperty(key) ? resultMap[key] : undefined);
2500
2500
  } catch (error) {
2501
- this.logger('error', '批量获取失败:', error);
2501
+ this.log('error', '批量获取失败:', error);
2502
2502
  throw error;
2503
2503
  }
2504
2504
  };
@@ -2520,7 +2520,7 @@ MongoDB.prototype.mset = async function (keyValues, seconds = 0) {
2520
2520
 
2521
2521
  // 如果事件处理器标记为取消,则返回false
2522
2522
  if (beforeEventData.cancel) {
2523
- this.logger('debug', 'mset操作被事件处理器取消');
2523
+ this.log('debug', 'mset操作被事件处理器取消');
2524
2524
  return false;
2525
2525
  }
2526
2526
 
@@ -2532,7 +2532,7 @@ MongoDB.prototype.mset = async function (keyValues, seconds = 0) {
2532
2532
 
2533
2533
  // 参数验证,确保keyValues是对象且不为null
2534
2534
  if (typeof finalData !== 'object' || finalData === null || Array.isArray(finalData)) {
2535
- this.logger('warn', 'mset: 参数必须是非空对象');
2535
+ this.log('warn', 'mset: 参数必须是非空对象');
2536
2536
  return false;
2537
2537
  }
2538
2538
 
@@ -2555,7 +2555,7 @@ MongoDB.prototype.mset = async function (keyValues, seconds = 0) {
2555
2555
  seconds,
2556
2556
  error
2557
2557
  });
2558
- this.logger('error', '批量设置失败:', error);
2558
+ this.log('error', '批量设置失败:', error);
2559
2559
  return false;
2560
2560
  }
2561
2561
  };
@@ -2601,7 +2601,7 @@ MongoDB.prototype.zadd = async function (key, score, member) {
2601
2601
  await this._add(key, sortedSet);
2602
2602
  return true;
2603
2603
  } catch (error) {
2604
- this.logger('error', '增加浮点数值失败:', error);
2604
+ this.log('error', '增加浮点数值失败:', error);
2605
2605
  return false;
2606
2606
  }
2607
2607
  };
@@ -2639,7 +2639,7 @@ MongoDB.prototype.zrangebyscore = async function (key, min, max) {
2639
2639
  .filter(item => item.score >= min && item.score <= max)
2640
2640
  .map(item => item.member);
2641
2641
  } catch (error) {
2642
- this.logger('error', '有序集合范围查询失败:', error);
2642
+ this.log('error', '有序集合范围查询失败:', error);
2643
2643
  return [];
2644
2644
  }
2645
2645
  };
@@ -2665,7 +2665,7 @@ MongoDB.prototype.zcard = async function (key) {
2665
2665
  }
2666
2666
  return item.value.length;
2667
2667
  } catch (error) {
2668
- this.logger('error', '有序集合成员数量失败:', error);
2668
+ this.log('error', '有序集合成员数量失败:', error);
2669
2669
  return 0;
2670
2670
  }
2671
2671
  };
@@ -2701,7 +2701,7 @@ MongoDB.prototype.zrem = async function (key, member) {
2701
2701
 
2702
2702
  return removedCount;
2703
2703
  } catch (error) {
2704
- this.logger('error', '有序集合删除成员失败:', error);
2704
+ this.log('error', '有序集合删除成员失败:', error);
2705
2705
  return 0;
2706
2706
  }
2707
2707
  };
@@ -2731,7 +2731,7 @@ MongoDB.prototype.zscore = async function (key, member) {
2731
2731
  const found = item.value.find(item => JSON.stringify(item.member) === memberStr);
2732
2732
  return found ? found.score : null;
2733
2733
  } catch (error) {
2734
- this.logger('error', '获取成员分数失败:', error);
2734
+ this.log('error', '获取成员分数失败:', error);
2735
2735
  return null;
2736
2736
  }
2737
2737
  };
@@ -2764,7 +2764,7 @@ MongoDB.prototype.lpop = async function (key) {
2764
2764
  });
2765
2765
  return popped;
2766
2766
  } catch (error) {
2767
- this.logger('error', '列表弹出头部元素失败:', error);
2767
+ this.log('error', '列表弹出头部元素失败:', error);
2768
2768
  return null;
2769
2769
  }
2770
2770
  };
@@ -2797,7 +2797,7 @@ MongoDB.prototype.rpop = async function (key) {
2797
2797
  });
2798
2798
  return popped;
2799
2799
  } catch (error) {
2800
- this.logger('error', '列表弹出尾部元素失败:', error);
2800
+ this.log('error', '列表弹出尾部元素失败:', error);
2801
2801
  return null;
2802
2802
  }
2803
2803
  };
@@ -2832,7 +2832,7 @@ MongoDB.prototype.smembers = async function (key) {
2832
2832
  });
2833
2833
  return Array.from(uniqueMap.values());
2834
2834
  } catch (error) {
2835
- this.logger('error', '获取集合所有成员失败:', error);
2835
+ this.log('error', '获取集合所有成员失败:', error);
2836
2836
  return [];
2837
2837
  }
2838
2838
  };
@@ -2853,7 +2853,7 @@ MongoDB.prototype.findOne = async function (query, options = {}) {
2853
2853
  // 不需要额外的sanitize,MongoDB驱动会处理
2854
2854
  return await this.db.collection(this.table).findOne(query, options);
2855
2855
  } catch (error) {
2856
- this.logger('error', '查询单条数据失败:', error);
2856
+ this.log('error', '查询单条数据失败:', error);
2857
2857
  return null;
2858
2858
  }
2859
2859
  };
@@ -2906,21 +2906,21 @@ MongoDB.prototype.open = async function () {
2906
2906
 
2907
2907
  // 设置连接事件监听
2908
2908
  this.conn.on('error', (err) => {
2909
- this.logger('error', '连接错误:', err);
2909
+ this.log('error', '连接错误:', err);
2910
2910
  this.connected = false;
2911
2911
  });
2912
2912
 
2913
2913
  this.conn.on('close', () => {
2914
- this.logger('warn', '连接已关闭');
2914
+ this.log('warn', '连接已关闭');
2915
2915
  this.connected = false;
2916
2916
  });
2917
2917
 
2918
- this.logger('info',
2918
+ this.log('info',
2919
2919
  `成功连接到 ${this.config.host}:${this.config.port}/${this.config.database}`
2920
2920
  );
2921
2921
  return true;
2922
2922
  } catch (error) {
2923
- this.logger('error', 'MongoDB连接失败:', error);
2923
+ this.log('error', 'MongoDB连接失败:', error);
2924
2924
  }
2925
2925
  }, '数据库连接');
2926
2926
  }
@@ -2932,8 +2932,8 @@ MongoDB.prototype.open = async function () {
2932
2932
  */
2933
2933
  MongoDB.prototype.close = async function () {
2934
2934
  if (this.conn) {
2935
- this.logger('info', `关闭连接`);
2936
- await this.conn.close().catch(err => this.logger('error', '关闭MongoDB连接时出错:', err));
2935
+ this.log('info', `关闭连接`);
2936
+ await this.conn.close().catch(err => this.log('error', '关闭MongoDB连接时出错:', err));
2937
2937
  this.conn = null;
2938
2938
  this.db = null;
2939
2939
  this.connected = false;
@@ -2978,13 +2978,13 @@ MongoDB.prototype.createIndex = async function (keys, options = {}) {
2978
2978
  throw new Error('数据库未连接或未指定表名');
2979
2979
  }
2980
2980
 
2981
- this.logger('info',
2981
+ this.log('info',
2982
2982
  `创建索引: ${this.table}, keys: ${JSON.stringify(keys)}, options: ${JSON.stringify(options)}`
2983
2983
  );
2984
2984
 
2985
2985
  return await this.db.collection(this.table).createIndex(keys, options);
2986
2986
  } catch (error) {
2987
- this.logger('error', `创建索引失败: ${error.message}`);
2987
+ this.log('error', `创建索引失败: ${error.message}`);
2988
2988
  throw error;
2989
2989
  }
2990
2990
  };
@@ -3015,12 +3015,12 @@ MongoDB.prototype.createTTLIndex = async function (keys, expireAfterSeconds) {
3015
3015
  expireAfterSeconds: expireAfterSeconds
3016
3016
  };
3017
3017
 
3018
- this.logger('info',
3018
+ this.log('info',
3019
3019
  `创建TTL索引: ${this.table}, keys: ${JSON.stringify(keys)}, 过期时间: ${expireAfterSeconds}秒`);
3020
3020
 
3021
3021
  return await this.db.collection(this.table).createIndex(keys, options);
3022
3022
  } catch (error) {
3023
- this.logger('error', `创建TTL索引失败: ${error.message}`);
3023
+ this.log('error', `创建TTL索引失败: ${error.message}`);
3024
3024
  throw error;
3025
3025
  }
3026
3026
  };
@@ -3036,10 +3036,10 @@ MongoDB.prototype.getIndexes = async function () {
3036
3036
  throw new Error('未指定表名');
3037
3037
  }
3038
3038
 
3039
- this.logger('info', `获取索引: ${this.table}`);
3039
+ this.log('info', `获取索引: ${this.table}`);
3040
3040
  return await this.db.collection(this.table).indexes();
3041
3041
  } catch (error) {
3042
- this.logger('error', `[getIndexes] 获取索引失败: ${error.message}`);
3042
+ this.log('error', `[getIndexes] 获取索引失败: ${error.message}`);
3043
3043
  return [];
3044
3044
  }
3045
3045
  };
@@ -3058,11 +3058,11 @@ MongoDB.prototype.dropIndex = async function (indexName) {
3058
3058
  throw new TypeError('索引名称不能为空');
3059
3059
  }
3060
3060
  try {
3061
- this.logger('info', `[dropIndex] 删除索引: ${this.table}, 索引名: ${indexName}`);
3061
+ this.log('info', `[dropIndex] 删除索引: ${this.table}, 索引名: ${indexName}`);
3062
3062
  await this.db.collection(this.table).dropIndex(indexName);
3063
3063
  return true;
3064
3064
  } catch (error) {
3065
- this.logger('error', `[dropIndex] 删除索引失败: ${error.message}`);
3065
+ this.log('error', `[dropIndex] 删除索引失败: ${error.message}`);
3066
3066
  throw error;
3067
3067
  }
3068
3068
  };
@@ -3077,11 +3077,11 @@ MongoDB.prototype.dropTable = async function () {
3077
3077
  throw new Error('未指定表名');
3078
3078
  }
3079
3079
  try {
3080
- this.logger('info', `[dropTable] 删除集合: ${this.table}`);
3080
+ this.log('info', `[dropTable] 删除集合: ${this.table}`);
3081
3081
  await this.db.collection(this.table).drop();
3082
3082
  return true;
3083
3083
  } catch (error) {
3084
- this.logger('error', `[dropTable] 删除集合失败: ${error.message}`);
3084
+ this.log('error', `[dropTable] 删除集合失败: ${error.message}`);
3085
3085
  throw error;
3086
3086
  }
3087
3087
  };
@@ -3140,7 +3140,7 @@ MongoDB.prototype.get = async function (key) {
3140
3140
  });
3141
3141
  return result ? result.value : null;
3142
3142
  } catch (error) {
3143
- this.logger('error', `获取键值失败: ${error.message}`);
3143
+ this.log('error', `获取键值失败: ${error.message}`);
3144
3144
  return null;
3145
3145
  }
3146
3146
  };
@@ -3174,7 +3174,7 @@ MongoDB.prototype.setForList = async function (key, array, seconds = 0) {
3174
3174
  this.emit('setForList:before', beforeEventData);
3175
3175
 
3176
3176
  if (beforeEventData.cancel) {
3177
- this.logger('debug', `[setForList] setForList操作被事件处理器取消 [${key}]`);
3177
+ this.log('debug', `操作被事件处理器取消 [${key}]`);
3178
3178
  return 0;
3179
3179
  }
3180
3180
 
@@ -3206,7 +3206,7 @@ MongoDB.prototype.setForList = async function (key, array, seconds = 0) {
3206
3206
  value: array,
3207
3207
  error
3208
3208
  });
3209
- this.logger('error', `[setForList] setForList操作失败: ${error.message}`);
3209
+ this.log('error', `[setForList] setForList操作失败: ${error.message}`);
3210
3210
  return 0;
3211
3211
  }
3212
3212
  };
@@ -3235,7 +3235,7 @@ MongoDB.prototype.addForList = async function (key, value, seconds = 0) {
3235
3235
  this.emit('addForList:before', beforeEventData);
3236
3236
 
3237
3237
  if (beforeEventData.cancel) {
3238
- this.logger('debug', `addForList操作被事件处理器取消 [${key}]`);
3238
+ this.log('debug', `addForList操作被事件处理器取消 [${key}]`);
3239
3239
  return 0;
3240
3240
  }
3241
3241
 
@@ -3276,7 +3276,7 @@ MongoDB.prototype.addForList = async function (key, value, seconds = 0) {
3276
3276
  value,
3277
3277
  error
3278
3278
  });
3279
- this.logger('error', `addForList操作失败: ${error.message}`);
3279
+ this.log('error', `addForList操作失败: ${error.message}`);
3280
3280
  return 0;
3281
3281
  }
3282
3282
  };
@@ -3309,7 +3309,7 @@ MongoDB.prototype.hasForList = async function (key, value) {
3309
3309
  const valueStr = JSON.stringify(value);
3310
3310
  return array.some(item => JSON.stringify(item) === valueStr);
3311
3311
  } catch (error) {
3312
- this.logger('error', `hasForList操作失败: ${error.message}`);
3312
+ this.log('error', `hasForList操作失败: ${error.message}`);
3313
3313
  return false;
3314
3314
  }
3315
3315
  };
@@ -3347,7 +3347,7 @@ MongoDB.prototype.getForList = async function (key, start = 0, stop = -1) {
3347
3347
 
3348
3348
  return result;
3349
3349
  } catch (error) {
3350
- this.logger('error', `getForList操作失败: ${error.message}`);
3350
+ this.log('error', `getForList操作失败: ${error.message}`);
3351
3351
  return [];
3352
3352
  }
3353
3353
  };
@@ -3370,7 +3370,7 @@ MongoDB.prototype.clearForList = async function (key) {
3370
3370
  // 使用set方法直接设置为空数组,与其他模块行为一致
3371
3371
  return await this.set(key, []);
3372
3372
  } catch (error) {
3373
- this.logger('error', `clearForList操作失败: ${error.message}`);
3373
+ this.log('error', `clearForList操作失败: ${error.message}`);
3374
3374
  return false;
3375
3375
  }
3376
3376
  };
@@ -3382,7 +3382,7 @@ MongoDB.prototype.has = async function (key) {
3382
3382
  const exists = await this.exists(key);
3383
3383
  return exists > 0;
3384
3384
  } catch (error) {
3385
- this.logger('error', `检查键是否存在失败: ${error.message}`);
3385
+ this.log('error', `检查键是否存在失败: ${error.message}`);
3386
3386
  throw error;
3387
3387
  }
3388
3388
  };
@@ -3426,7 +3426,7 @@ MongoDB.prototype.exists = async function (key) {
3426
3426
  throw new TypeError('key必须是非空字符串或非空字符串数组');
3427
3427
  }
3428
3428
  } catch (error) {
3429
- this.logger('error', `检查键是否存在失败: ${error.message}`);
3429
+ this.log('error', `检查键是否存在失败: ${error.message}`);
3430
3430
  throw error;
3431
3431
  }
3432
3432
  };
@@ -3471,7 +3471,7 @@ MongoDB.prototype.del = async function (key) {
3471
3471
 
3472
3472
  // 如果事件处理器标记为取消,则返回false
3473
3473
  if (beforeEventData.cancel) {
3474
- this.logger('debug', `del操作被事件处理器取消 [${key}]`);
3474
+ this.log('debug', `del操作被事件处理器取消 [${key}]`);
3475
3475
  return false;
3476
3476
  }
3477
3477
 
@@ -3521,7 +3521,7 @@ MongoDB.prototype.del = async function (key) {
3521
3521
  key,
3522
3522
  error: error
3523
3523
  });
3524
- this.logger('error', '删除缓存失败:', error);
3524
+ this.log('error', '删除缓存失败:', error);
3525
3525
  return false;
3526
3526
  }
3527
3527
  };
@@ -3547,7 +3547,7 @@ MongoDB.prototype.add = async function (key, value, seconds) {
3547
3547
 
3548
3548
  // 如果事件处理器标记为取消,则返回false
3549
3549
  if (beforeEventData.cancel) {
3550
- this.logger('debug', `add操作被事件处理器取消 [${key}]`);
3550
+ this.log('debug', `add操作被事件处理器取消 [${key}]`);
3551
3551
  return false;
3552
3552
  }
3553
3553
 
@@ -3605,7 +3605,7 @@ MongoDB.prototype.add = async function (key, value, seconds) {
3605
3605
  ttl: seconds,
3606
3606
  error
3607
3607
  });
3608
- this.logger('error', `添加键值失败: ${error.message}`);
3608
+ this.log('error', `添加键值失败: ${error.message}`);
3609
3609
  return false;
3610
3610
  }
3611
3611
  };
@@ -3621,7 +3621,7 @@ MongoDB.prototype.set = async function (key, value, seconds) {
3621
3621
  try {
3622
3622
  // 键名必须是非空字符串
3623
3623
  if (!key || typeof key !== 'string') {
3624
- this.logger('error', '参数错误: key必须是非空字符串');
3624
+ this.log('error', '参数错误: key必须是非空字符串');
3625
3625
  return false;
3626
3626
  }
3627
3627
 
@@ -3636,7 +3636,7 @@ MongoDB.prototype.set = async function (key, value, seconds) {
3636
3636
 
3637
3637
  // 如果事件处理器标记为取消,则返回false
3638
3638
  if (beforeEventData.cancel) {
3639
- this.logger('debug', `set操作被事件处理器取消 [${key}]`);
3639
+ this.log('debug', `set操作被事件处理器取消 [${key}]`);
3640
3640
  return false;
3641
3641
  }
3642
3642
 
@@ -3691,7 +3691,7 @@ MongoDB.prototype.set = async function (key, value, seconds) {
3691
3691
  ttl: seconds,
3692
3692
  error
3693
3693
  });
3694
- this.logger('error', `设置键值失败: ${error.message}`);
3694
+ this.log('error', `设置键值失败: ${error.message}`);
3695
3695
  return false;
3696
3696
  }
3697
3697
  };
@@ -3861,7 +3861,7 @@ MongoDB.prototype.find = async function (query, options = {}) {
3861
3861
  const cursor = this.db.collection(this.table).find(safeQuery, options);
3862
3862
  return await cursor.toArray();
3863
3863
  } catch (error) {
3864
- this.logger('error', '查询失败:', error);
3864
+ this.log('error', '查询失败:', error);
3865
3865
  return [];
3866
3866
  }
3867
3867
  };
@@ -3878,7 +3878,7 @@ MongoDB.prototype.count = async function (query = {}) {
3878
3878
  const safeQuery = this._sanitizeQuery(query);
3879
3879
  return await this.db.collection(this.table).countDocuments(safeQuery);
3880
3880
  } catch (error) {
3881
- this.logger('error', '计算文档数量失败:', error);
3881
+ this.log('error', '计算文档数量失败:', error);
3882
3882
  return 0;
3883
3883
  }
3884
3884
  };
@@ -3897,7 +3897,7 @@ MongoDB.prototype._sanitizeQuery = function (query) {
3897
3897
  // 防止NoSQL注入,如 {$ne: null} 等操作符注入
3898
3898
  if (key.startsWith('$') && typeof key === 'string' && !Array.isArray(value) && typeof value === 'object' &&
3899
3899
  value !== null) {
3900
- this.logger('warn', '检测到可能的操作符注入尝试:', key);
3900
+ this.log('warn', '检测到可能的操作符注入尝试:', key);
3901
3901
  continue;
3902
3902
  }
3903
3903
  sanitized[key] = value;
@@ -3917,7 +3917,7 @@ MongoDB.prototype.startTransaction = async function () {
3917
3917
  session.startTransaction();
3918
3918
  return session;
3919
3919
  } catch (error) {
3920
- this.logger('error', '[startTransaction] 启动事务失败:', error);
3920
+ this.log('error', '[startTransaction] 启动事务失败:', error);
3921
3921
  throw error;
3922
3922
  }
3923
3923
  };
@@ -3932,7 +3932,7 @@ MongoDB.prototype.commitTransaction = async function (session) {
3932
3932
  await session.commitTransaction();
3933
3933
  await session.endSession();
3934
3934
  } catch (error) {
3935
- this.logger('error', '[commitTransaction] 提交事务失败:', error);
3935
+ this.log('error', '[commitTransaction] 提交事务失败:', error);
3936
3936
  throw error;
3937
3937
  }
3938
3938
  };
@@ -3947,16 +3947,11 @@ MongoDB.prototype.rollbackTransaction = async function (session) {
3947
3947
  await session.abortTransaction();
3948
3948
  await session.endSession();
3949
3949
  } catch (error) {
3950
- this.logger('error', '[rollbackTransaction] 回滚事务失败:', error);
3950
+ this.log('error', '[rollbackTransaction] 回滚事务失败:', error);
3951
3951
  throw error;
3952
3952
  }
3953
3953
  };
3954
3954
 
3955
- /**
3956
- * 导出MongoDB类
3957
- */
3958
- exports.MongoDB = MongoDB;
3959
-
3960
3955
  /**
3961
3956
  * 确保连接池对象存在
3962
3957
  */
@@ -3992,7 +3987,7 @@ function mongodbAdmin(scope, config) {
3992
3987
  return obj;
3993
3988
  }
3994
3989
 
3995
- /**
3996
- * @module 导出MongoDB管理器
3997
- */
3998
- exports.mongodbAdmin = mongodbAdmin;
3990
+ module.exports = {
3991
+ MongoDB,
3992
+ mongodbAdmin
3993
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_mongodb",
3
- "version": "1.5.3",
3
+ "version": "1.5.5",
4
4
  "description": "MongoDB数据库操作模块,提供简洁易用的API、缓存功能以及完整的Redis兼容接口,支持事件系统、事务和连接池管理",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "homepage": "https://gitee.com/qiuwenwu91/mm_mongodb#readme",
38
38
  "dependencies": {
39
- "mm_expand": "^1.9.7",
39
+ "mm_expand": "^2.0.0",
40
40
  "mongodb": "^7.0.0"
41
41
  }
42
42
  }
package/test.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const MongoDB = require('./index.js').MongoDB;
2
- const mongodb_admin = require('./index.js').mongodb_admin;
2
+ const mongodbAdmin = require('./index.js').mongodbAdmin;
3
3
 
4
4
  async function test() {
5
5
  try {
@@ -12,7 +12,7 @@ async function test() {
12
12
  password: '',
13
13
  database: 'mm'
14
14
  };
15
- var rs = mongodb_admin('sys', config);
15
+ var rs = mongodbAdmin('sys', config);
16
16
  await rs.open();
17
17
  await rs.addTable('cache_test');
18
18