mm_mongodb 1.5.2 → 1.5.4
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/eslint.config.js +68 -0
- package/index.js +133 -134
- package/package.json +2 -2
- package/test.js +2 -2
package/eslint.config.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const jsdoc = require('eslint-plugin-jsdoc');
|
|
2
|
+
|
|
3
|
+
module.exports = [
|
|
4
|
+
{
|
|
5
|
+
files: ['lib/**/*.js'],
|
|
6
|
+
languageOptions: {
|
|
7
|
+
ecmaVersion: 2020,
|
|
8
|
+
sourceType: 'script'
|
|
9
|
+
},
|
|
10
|
+
plugins: {
|
|
11
|
+
jsdoc: jsdoc
|
|
12
|
+
},
|
|
13
|
+
rules: {
|
|
14
|
+
// JSDoc相关规则 - 设置为error级别强制要求
|
|
15
|
+
'jsdoc/require-jsdoc': ['error', {
|
|
16
|
+
require: {
|
|
17
|
+
FunctionDeclaration: true,
|
|
18
|
+
MethodDefinition: true,
|
|
19
|
+
ClassDeclaration: true,
|
|
20
|
+
ArrowFunctionExpression: true
|
|
21
|
+
}
|
|
22
|
+
}],
|
|
23
|
+
'jsdoc/check-param-names': 'off',
|
|
24
|
+
'jsdoc/check-tag-names': 'off',
|
|
25
|
+
'jsdoc/check-types': 'off',
|
|
26
|
+
'jsdoc/require-param': 'off',
|
|
27
|
+
'jsdoc/require-param-description': 'off',
|
|
28
|
+
'jsdoc/require-returns': 'off',
|
|
29
|
+
'jsdoc/require-returns-description': 'off',
|
|
30
|
+
|
|
31
|
+
// 基础语法规则
|
|
32
|
+
'no-unused-vars': ['error', { args: 'none', vars: 'all' }],
|
|
33
|
+
'no-console': 'warn',
|
|
34
|
+
|
|
35
|
+
// 代码风格规则
|
|
36
|
+
'indent': ['error', 2],
|
|
37
|
+
'quotes': ['error', 'single'],
|
|
38
|
+
'semi': ['error', 'always'],
|
|
39
|
+
'comma-dangle': ['error', 'never'],
|
|
40
|
+
'no-trailing-spaces': 'error',
|
|
41
|
+
'eol-last': 'error',
|
|
42
|
+
'max-len': ['error', { code: 100 }],
|
|
43
|
+
'max-lines-per-function': ['error', { max: 40 }],
|
|
44
|
+
'complexity': ['error', 12],
|
|
45
|
+
'max-depth': ['error', 4],
|
|
46
|
+
'max-params': ['error', 4],
|
|
47
|
+
|
|
48
|
+
// 命名规则
|
|
49
|
+
'id-length': ['error', {
|
|
50
|
+
min: 1,
|
|
51
|
+
max: 20,
|
|
52
|
+
properties: 'always'
|
|
53
|
+
}]
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
files: ['test/**/*.js'],
|
|
58
|
+
languageOptions: {
|
|
59
|
+
ecmaVersion: 2020,
|
|
60
|
+
sourceType: 'script'
|
|
61
|
+
},
|
|
62
|
+
rules: {
|
|
63
|
+
// 测试文件中允许使用console
|
|
64
|
+
'no-console': 'off',
|
|
65
|
+
'no-unused-vars': 'off'
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
];
|
package/index.js
CHANGED
|
@@ -102,7 +102,7 @@ MongoDB.prototype._withRetry = async function (operation, operationName) {
|
|
|
102
102
|
return await operation();
|
|
103
103
|
} catch (error) {
|
|
104
104
|
lastError = error;
|
|
105
|
-
this.
|
|
105
|
+
this.log('error', `${operationName} 第${attempt}次尝试失败:`, error);
|
|
106
106
|
|
|
107
107
|
// 如果不是最后一次尝试,则等待后重试
|
|
108
108
|
if (attempt < maxAttempts) {
|
|
@@ -138,16 +138,16 @@ MongoDB.prototype._ensureConnected = async function () {
|
|
|
138
138
|
|
|
139
139
|
// 检查连接是否成功
|
|
140
140
|
if (this.db && this.db.collection) {
|
|
141
|
-
this.
|
|
141
|
+
this.log('debug', `[_ensureConnected] 连接成功,db对象存在,开始初始化collection,table=${this.table}`);
|
|
142
142
|
this.connected = true;
|
|
143
143
|
this.connectAttempts = 0;
|
|
144
144
|
|
|
145
145
|
// 保存collection引用到实例变量
|
|
146
146
|
try {
|
|
147
147
|
this.collection = this.db.collection(this.table);
|
|
148
|
-
this.
|
|
148
|
+
this.log('debug', `[_ensureConnected] 成功初始化collection: ${this.table}`);
|
|
149
149
|
} catch (err) {
|
|
150
|
-
this.
|
|
150
|
+
this.log('error', `[_ensureConnected] 初始化collection失败:`, err);
|
|
151
151
|
this.collection = null;
|
|
152
152
|
}
|
|
153
153
|
|
|
@@ -156,14 +156,14 @@ MongoDB.prototype._ensureConnected = async function () {
|
|
|
156
156
|
await this._ensureIndexes();
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
this.
|
|
159
|
+
this.log('info', `成功连接到数据库,表名: ${this.table}`);
|
|
160
160
|
} else {
|
|
161
|
-
this.
|
|
161
|
+
this.log('error', `[_ensureConnected] 连接失败,db对象为空或不具有collection方法`);
|
|
162
162
|
throw new Error('数据库连接失败,db对象为空');
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
165
|
} catch (error) {
|
|
166
|
-
this.
|
|
166
|
+
this.log('error', `连接失败 (尝试 ${this.connectAttempts}/${this.maxConnectAttempts}):`, error);
|
|
167
167
|
|
|
168
168
|
// 重置连接Promise,允许下次重试
|
|
169
169
|
this.connectPromise = null;
|
|
@@ -173,7 +173,7 @@ MongoDB.prototype._ensureConnected = async function () {
|
|
|
173
173
|
throw error;
|
|
174
174
|
} else {
|
|
175
175
|
// 超过重试次数,记录严重错误
|
|
176
|
-
this.
|
|
176
|
+
this.log('error', '达到最大连接重试次数,连接失败');
|
|
177
177
|
this.emit('error', new Error('数据库连接失败,已达到最大重试次数'));
|
|
178
178
|
}
|
|
179
179
|
}
|
|
@@ -207,7 +207,7 @@ MongoDB.prototype._ensureIndexes = async function () {
|
|
|
207
207
|
} catch (error) {
|
|
208
208
|
// 忽略索引已存在的错误
|
|
209
209
|
if (error.code !== 85) {
|
|
210
|
-
this.
|
|
210
|
+
this.log('error', '创建索引失败:', error);
|
|
211
211
|
}
|
|
212
212
|
}
|
|
213
213
|
};
|
|
@@ -230,7 +230,7 @@ MongoDB.prototype.get = async function (key) {
|
|
|
230
230
|
});
|
|
231
231
|
return result ? result.value : null;
|
|
232
232
|
} catch (error) {
|
|
233
|
-
this.
|
|
233
|
+
this.log('error', '获取缓存失败:', error);
|
|
234
234
|
this.emit('error', error);
|
|
235
235
|
}
|
|
236
236
|
};
|
|
@@ -280,7 +280,7 @@ MongoDB.prototype.clear = async function (pattern) {
|
|
|
280
280
|
error,
|
|
281
281
|
pattern
|
|
282
282
|
});
|
|
283
|
-
this.
|
|
283
|
+
this.log('error', '清空缓存失败:', error);
|
|
284
284
|
return false;
|
|
285
285
|
}
|
|
286
286
|
};
|
|
@@ -313,7 +313,7 @@ MongoDB.prototype.keys = async function (pattern = '*') {
|
|
|
313
313
|
});
|
|
314
314
|
return results.map(item => item.key);
|
|
315
315
|
} catch (error) {
|
|
316
|
-
this.
|
|
316
|
+
this.log('error', '获取键列表失败:', error);
|
|
317
317
|
return [];
|
|
318
318
|
}
|
|
319
319
|
};
|
|
@@ -357,12 +357,11 @@ MongoDB.prototype.ttl = async function (key, seconds) {
|
|
|
357
357
|
|
|
358
358
|
// 直接尝试获取collection,使用实例的table属性
|
|
359
359
|
if (!this.collection && this.db && this.db.collection) {
|
|
360
|
-
this.
|
|
360
|
+
this.log('debug', `[ttl] 设置模式:直接获取collection, table=${this.table}`);
|
|
361
361
|
try {
|
|
362
362
|
this.collection = this.db.collection(this.table);
|
|
363
|
-
this.logger('debug', `[ttl] 设置模式:成功获取collection`);
|
|
364
363
|
} catch (err) {
|
|
365
|
-
this.
|
|
364
|
+
this.log('error', `[ttl] 设置模式:获取collection失败:`, err);
|
|
366
365
|
this.collection = null;
|
|
367
366
|
}
|
|
368
367
|
}
|
|
@@ -425,12 +424,12 @@ MongoDB.prototype.ttl = async function (key, seconds) {
|
|
|
425
424
|
|
|
426
425
|
// 直接尝试获取collection,使用实例的table属性
|
|
427
426
|
if (!this.collection && this.db && this.db.collection) {
|
|
428
|
-
this.
|
|
427
|
+
this.log('debug', `[ttl] 直接获取collection, table=${this.table}`);
|
|
429
428
|
try {
|
|
430
429
|
this.collection = this.db.collection(this.table);
|
|
431
|
-
this.
|
|
430
|
+
this.log('debug', `[ttl] 成功获取collection`);
|
|
432
431
|
} catch (err) {
|
|
433
|
-
this.
|
|
432
|
+
this.log('error', `[ttl] 获取collection失败:`, err);
|
|
434
433
|
this.collection = null;
|
|
435
434
|
}
|
|
436
435
|
}
|
|
@@ -442,17 +441,17 @@ MongoDB.prototype.ttl = async function (key, seconds) {
|
|
|
442
441
|
|
|
443
442
|
// 查询键是否存在及过期时间
|
|
444
443
|
const item = await this.collection.findOne({ key });
|
|
445
|
-
this.
|
|
444
|
+
this.log('debug', `[ttl] 查询键结果: item=${JSON.stringify(item)}`);
|
|
446
445
|
|
|
447
446
|
if (!item) {
|
|
448
447
|
// 键不存在返回-2
|
|
449
|
-
this.
|
|
448
|
+
this.log('debug', `[ttl] 键 ${key} 不存在,返回-2`);
|
|
450
449
|
return -2;
|
|
451
450
|
}
|
|
452
451
|
|
|
453
452
|
if (!item.expire_at) {
|
|
454
453
|
// 没有过期时间返回-1
|
|
455
|
-
this.
|
|
454
|
+
this.log('debug', `[ttl] 键 ${key} 没有过期时间,返回-1`);
|
|
456
455
|
return -1;
|
|
457
456
|
}
|
|
458
457
|
|
|
@@ -471,7 +470,7 @@ MongoDB.prototype.ttl = async function (key, seconds) {
|
|
|
471
470
|
}
|
|
472
471
|
} catch (error) {
|
|
473
472
|
this.emit('ttl:error', { key, seconds, error });
|
|
474
|
-
this.
|
|
473
|
+
this.log('error', `[ttl] 操作失败:`, error);
|
|
475
474
|
return arguments.length === 1 ? -2 : false;
|
|
476
475
|
}
|
|
477
476
|
};
|
|
@@ -509,19 +508,19 @@ MongoDB.prototype.persist = async function (key) {
|
|
|
509
508
|
|
|
510
509
|
// 直接尝试获取collection,不依赖_ensureConnected的复杂逻辑
|
|
511
510
|
if (!this.collection && this.db && this.db.collection) {
|
|
512
|
-
this.
|
|
511
|
+
this.log('debug', `[persist] 直接获取collection, table=${this.table}`);
|
|
513
512
|
try {
|
|
514
513
|
this.collection = this.db.collection(this.table);
|
|
515
|
-
this.
|
|
514
|
+
this.log('debug', `[persist] 成功获取collection`);
|
|
516
515
|
} catch (err) {
|
|
517
|
-
this.
|
|
516
|
+
this.log('error', `[persist] 获取collection失败:`, err);
|
|
518
517
|
this.collection = null;
|
|
519
518
|
}
|
|
520
519
|
}
|
|
521
520
|
|
|
522
521
|
// 再次检查collection
|
|
523
522
|
if (!this.collection) {
|
|
524
|
-
this.
|
|
523
|
+
this.log('error', 'persist: collection is undefined');
|
|
525
524
|
this.emit('persist:error', {
|
|
526
525
|
key: finalKey,
|
|
527
526
|
error: new Error('collection is undefined')
|
|
@@ -566,7 +565,7 @@ MongoDB.prototype.persist = async function (key) {
|
|
|
566
565
|
});
|
|
567
566
|
return success;
|
|
568
567
|
} catch (error) {
|
|
569
|
-
this.
|
|
568
|
+
this.log('error', 'persist error:', error);
|
|
570
569
|
this.emit('persist:error', {
|
|
571
570
|
key,
|
|
572
571
|
error
|
|
@@ -605,7 +604,7 @@ MongoDB.prototype._add = async function (key, value, expire) {
|
|
|
605
604
|
}
|
|
606
605
|
return true;
|
|
607
606
|
} catch (error) {
|
|
608
|
-
this.
|
|
607
|
+
this.log('error', '添加缓存失败:', error);
|
|
609
608
|
return false;
|
|
610
609
|
}
|
|
611
610
|
};
|
|
@@ -630,7 +629,7 @@ MongoDB.prototype.add = async function (key, value, seconds) {
|
|
|
630
629
|
|
|
631
630
|
// 如果事件处理器标记为取消,则返回false
|
|
632
631
|
if (beforeEventData.cancel) {
|
|
633
|
-
this.
|
|
632
|
+
this.log('debug', `add操作被事件处理器取消 [${key}]`);
|
|
634
633
|
return false;
|
|
635
634
|
}
|
|
636
635
|
|
|
@@ -679,7 +678,7 @@ MongoDB.prototype.add = async function (key, value, seconds) {
|
|
|
679
678
|
});
|
|
680
679
|
return true;
|
|
681
680
|
} catch (error) {
|
|
682
|
-
this.
|
|
681
|
+
this.log('error', `添加缓存失败 [${key}]:`, error);
|
|
683
682
|
// 触发错误事件
|
|
684
683
|
this.emit('add:error', {
|
|
685
684
|
key,
|
|
@@ -714,7 +713,7 @@ MongoDB.prototype._addNum = async function (key, value = 1) {
|
|
|
714
713
|
await this._add(key, newValue);
|
|
715
714
|
return newValue;
|
|
716
715
|
} catch (error) {
|
|
717
|
-
this.
|
|
716
|
+
this.log('error', '增加整数值失败:', error);
|
|
718
717
|
return false;
|
|
719
718
|
}
|
|
720
719
|
};
|
|
@@ -749,7 +748,7 @@ MongoDB.prototype.addInt = async function (key, value = 1, seconds = 0) {
|
|
|
749
748
|
|
|
750
749
|
// 如果事件处理器标记为取消,则返回默认值
|
|
751
750
|
if (beforeEventData.cancel) {
|
|
752
|
-
this.
|
|
751
|
+
this.log('debug', 'addInt操作被事件处理器取消');
|
|
753
752
|
return 0;
|
|
754
753
|
}
|
|
755
754
|
|
|
@@ -794,7 +793,7 @@ MongoDB.prototype.addInt = async function (key, value = 1, seconds = 0) {
|
|
|
794
793
|
seconds,
|
|
795
794
|
error
|
|
796
795
|
});
|
|
797
|
-
this.
|
|
796
|
+
this.log('error', '增加整数值失败:', error);
|
|
798
797
|
// 与CacheBase保持一致,错误时返回默认值而不是抛出异常
|
|
799
798
|
return 0;
|
|
800
799
|
}
|
|
@@ -824,7 +823,7 @@ MongoDB.prototype.addFloat = async function (key, value = 1.0, seconds = 0) {
|
|
|
824
823
|
|
|
825
824
|
// 如果事件处理器标记为取消,则返回默认值
|
|
826
825
|
if (beforeEventData.cancel) {
|
|
827
|
-
this.
|
|
826
|
+
this.log('debug', 'addFloat操作被事件处理器取消');
|
|
828
827
|
return 0.0;
|
|
829
828
|
}
|
|
830
829
|
|
|
@@ -869,7 +868,7 @@ MongoDB.prototype.addFloat = async function (key, value = 1.0, seconds = 0) {
|
|
|
869
868
|
seconds,
|
|
870
869
|
error
|
|
871
870
|
});
|
|
872
|
-
this.
|
|
871
|
+
this.log('error', '增加浮点数值失败:', error);
|
|
873
872
|
// 与CacheBase保持一致,错误时返回默认值而不是抛出异常
|
|
874
873
|
return 0.0;
|
|
875
874
|
}
|
|
@@ -923,7 +922,7 @@ MongoDB.prototype.addStr = async function (key, value) {
|
|
|
923
922
|
value,
|
|
924
923
|
error
|
|
925
924
|
});
|
|
926
|
-
this.
|
|
925
|
+
this.log('error', '添加字符串失败:', error);
|
|
927
926
|
return 0;
|
|
928
927
|
}
|
|
929
928
|
};
|
|
@@ -960,7 +959,7 @@ MongoDB.prototype.sort = async function (key, options = {}) {
|
|
|
960
959
|
|
|
961
960
|
return sorted;
|
|
962
961
|
} catch (error) {
|
|
963
|
-
this.
|
|
962
|
+
this.log('error', '排序失败:', error);
|
|
964
963
|
return [];
|
|
965
964
|
}
|
|
966
965
|
};
|
|
@@ -994,7 +993,7 @@ MongoDB.prototype.hset = async function (key, field, value) {
|
|
|
994
993
|
await this._add(key, hash);
|
|
995
994
|
return true; // hset操作成功返回true
|
|
996
995
|
} catch (error) {
|
|
997
|
-
this.
|
|
996
|
+
this.log('error', '哈希表设置失败:', error);
|
|
998
997
|
throw error;
|
|
999
998
|
}
|
|
1000
999
|
};
|
|
@@ -1024,7 +1023,7 @@ MongoDB.prototype.hget = async function (key, field) {
|
|
|
1024
1023
|
}
|
|
1025
1024
|
return item.value[field] !== undefined ? item.value[field] : undefined;
|
|
1026
1025
|
} catch (error) {
|
|
1027
|
-
this.
|
|
1026
|
+
this.log('error', '哈希表获取失败:', error);
|
|
1028
1027
|
return null; // 与Redis保持一致,错误时返回null
|
|
1029
1028
|
}
|
|
1030
1029
|
};
|
|
@@ -1049,7 +1048,7 @@ MongoDB.prototype.hgetall = async function (key) {
|
|
|
1049
1048
|
}
|
|
1050
1049
|
return item.value;
|
|
1051
1050
|
} catch (error) {
|
|
1052
|
-
this.
|
|
1051
|
+
this.log('error', '哈希表获取全部失败:', error);
|
|
1053
1052
|
return {};
|
|
1054
1053
|
}
|
|
1055
1054
|
};
|
|
@@ -1109,7 +1108,7 @@ MongoDB.prototype.hdel = async function (key, fields) {
|
|
|
1109
1108
|
|
|
1110
1109
|
return deletedCount;
|
|
1111
1110
|
} catch (error) {
|
|
1112
|
-
this.
|
|
1111
|
+
this.log('error', 'hdel操作失败:', error);
|
|
1113
1112
|
return 0;
|
|
1114
1113
|
}
|
|
1115
1114
|
};
|
|
@@ -1131,7 +1130,7 @@ MongoDB.prototype.has = async function (key) {
|
|
|
1131
1130
|
});
|
|
1132
1131
|
return result !== null;
|
|
1133
1132
|
} catch (error) {
|
|
1134
|
-
this.
|
|
1133
|
+
this.log('error', '[has] 检查键是否存在失败:', error);
|
|
1135
1134
|
throw error;
|
|
1136
1135
|
}
|
|
1137
1136
|
};
|
|
@@ -1157,7 +1156,7 @@ MongoDB.prototype.expire = async function (key, seconds) {
|
|
|
1157
1156
|
|
|
1158
1157
|
// 如果事件处理器标记为取消,则返回默认值
|
|
1159
1158
|
if (beforeEventData.cancel) {
|
|
1160
|
-
this.
|
|
1159
|
+
this.log('debug', 'expire操作被事件处理器取消');
|
|
1161
1160
|
return false;
|
|
1162
1161
|
}
|
|
1163
1162
|
|
|
@@ -1181,7 +1180,7 @@ MongoDB.prototype.expire = async function (key, seconds) {
|
|
|
1181
1180
|
return result === true || (typeof result === 'number' && result >= -2);
|
|
1182
1181
|
} catch (error) {
|
|
1183
1182
|
// 记录错误日志
|
|
1184
|
-
this.
|
|
1183
|
+
this.log('error', 'expire方法执行错误', { error: error.message, key, seconds });
|
|
1185
1184
|
|
|
1186
1185
|
// 触发错误事件
|
|
1187
1186
|
this.emit('expire:error', {
|
|
@@ -1211,7 +1210,7 @@ MongoDB.prototype.incr = async function (key, seconds = 0) {
|
|
|
1211
1210
|
|
|
1212
1211
|
// 如果事件处理器标记为取消,则返回默认值
|
|
1213
1212
|
if (beforeEventData.cancel) {
|
|
1214
|
-
this.
|
|
1213
|
+
this.log('debug', 'incr操作被事件处理器取消');
|
|
1215
1214
|
return 0;
|
|
1216
1215
|
}
|
|
1217
1216
|
|
|
@@ -1246,7 +1245,7 @@ MongoDB.prototype.incr = async function (key, seconds = 0) {
|
|
|
1246
1245
|
seconds,
|
|
1247
1246
|
error
|
|
1248
1247
|
});
|
|
1249
|
-
this.
|
|
1248
|
+
this.log('error', '递增操作失败:', error);
|
|
1250
1249
|
return 0;
|
|
1251
1250
|
}
|
|
1252
1251
|
};
|
|
@@ -1268,7 +1267,7 @@ MongoDB.prototype.decr = async function (key, seconds = 0) {
|
|
|
1268
1267
|
|
|
1269
1268
|
// 如果事件处理器标记为取消,则返回默认值
|
|
1270
1269
|
if (beforeEventData.cancel) {
|
|
1271
|
-
this.
|
|
1270
|
+
this.log('debug', 'decr操作被事件处理器取消');
|
|
1272
1271
|
return 0;
|
|
1273
1272
|
}
|
|
1274
1273
|
|
|
@@ -1303,7 +1302,7 @@ MongoDB.prototype.decr = async function (key, seconds = 0) {
|
|
|
1303
1302
|
seconds,
|
|
1304
1303
|
error
|
|
1305
1304
|
});
|
|
1306
|
-
this.
|
|
1305
|
+
this.log('error', '递减操作失败:', error);
|
|
1307
1306
|
return 0;
|
|
1308
1307
|
}
|
|
1309
1308
|
};
|
|
@@ -1327,7 +1326,7 @@ MongoDB.prototype.incrby = async function (key, value = 1, seconds = 0) {
|
|
|
1327
1326
|
|
|
1328
1327
|
// 如果事件处理器标记为取消,则返回默认值
|
|
1329
1328
|
if (beforeEventData.cancel) {
|
|
1330
|
-
this.
|
|
1329
|
+
this.log('debug', 'incrby操作被事件处理器取消');
|
|
1331
1330
|
return 0;
|
|
1332
1331
|
}
|
|
1333
1332
|
|
|
@@ -1368,7 +1367,7 @@ MongoDB.prototype.incrby = async function (key, value = 1, seconds = 0) {
|
|
|
1368
1367
|
seconds,
|
|
1369
1368
|
error
|
|
1370
1369
|
});
|
|
1371
|
-
this.
|
|
1370
|
+
this.log('error', '递增操作失败:', error);
|
|
1372
1371
|
return 0;
|
|
1373
1372
|
}
|
|
1374
1373
|
};
|
|
@@ -1392,7 +1391,7 @@ MongoDB.prototype.decrby = async function (key, value = 1, seconds = 0) {
|
|
|
1392
1391
|
|
|
1393
1392
|
// 如果事件处理器标记为取消,则返回默认值
|
|
1394
1393
|
if (beforeEventData.cancel) {
|
|
1395
|
-
this.
|
|
1394
|
+
this.log('debug', 'decrby操作被事件处理器取消');
|
|
1396
1395
|
return 0;
|
|
1397
1396
|
}
|
|
1398
1397
|
|
|
@@ -1433,7 +1432,7 @@ MongoDB.prototype.decrby = async function (key, value = 1, seconds = 0) {
|
|
|
1433
1432
|
seconds,
|
|
1434
1433
|
error
|
|
1435
1434
|
});
|
|
1436
|
-
this.
|
|
1435
|
+
this.log('error', '递减操作失败:', error);
|
|
1437
1436
|
return 0;
|
|
1438
1437
|
}
|
|
1439
1438
|
};
|
|
@@ -1457,7 +1456,7 @@ MongoDB.prototype.incrbyfloat = async function (key, value = 1.0, seconds = 0) {
|
|
|
1457
1456
|
|
|
1458
1457
|
// 如果事件处理器标记为取消,则返回默认值
|
|
1459
1458
|
if (beforeEventData.cancel) {
|
|
1460
|
-
this.
|
|
1459
|
+
this.log('debug', 'incrbyfloat操作被事件处理器取消');
|
|
1461
1460
|
return 0.0;
|
|
1462
1461
|
}
|
|
1463
1462
|
|
|
@@ -1498,7 +1497,7 @@ MongoDB.prototype.incrbyfloat = async function (key, value = 1.0, seconds = 0) {
|
|
|
1498
1497
|
seconds,
|
|
1499
1498
|
error
|
|
1500
1499
|
});
|
|
1501
|
-
this.
|
|
1500
|
+
this.log('error', '浮点递增操作失败:', error);
|
|
1502
1501
|
return 0.0;
|
|
1503
1502
|
}
|
|
1504
1503
|
};
|
|
@@ -1545,7 +1544,7 @@ MongoDB.prototype.exists = async function (key) {
|
|
|
1545
1544
|
return result !== null;
|
|
1546
1545
|
}
|
|
1547
1546
|
} catch (error) {
|
|
1548
|
-
this.
|
|
1547
|
+
this.log('error', '检查键是否存在失败:', error);
|
|
1549
1548
|
throw error;
|
|
1550
1549
|
}
|
|
1551
1550
|
};
|
|
@@ -1575,7 +1574,7 @@ MongoDB.prototype._set = async function (key, value, expire) {
|
|
|
1575
1574
|
);
|
|
1576
1575
|
return true;
|
|
1577
1576
|
} catch (error) {
|
|
1578
|
-
this.
|
|
1577
|
+
this.log('error', '设置缓存失败:', error);
|
|
1579
1578
|
return false;
|
|
1580
1579
|
} // 与CacheBase保持一致,返回false而不是抛出异常
|
|
1581
1580
|
};
|
|
@@ -1601,7 +1600,7 @@ MongoDB.prototype.hmset = async function (key, ...fieldValues) {
|
|
|
1601
1600
|
try {
|
|
1602
1601
|
// 参数验证
|
|
1603
1602
|
if (!key || typeof key !== 'string') {
|
|
1604
|
-
this.
|
|
1603
|
+
this.log('warn', 'hmset: 键名无效');
|
|
1605
1604
|
return 0;
|
|
1606
1605
|
}
|
|
1607
1606
|
|
|
@@ -1650,7 +1649,7 @@ MongoDB.prototype.hmset = async function (key, ...fieldValues) {
|
|
|
1650
1649
|
|
|
1651
1650
|
return true; // 操作成功返回true
|
|
1652
1651
|
} catch (error) {
|
|
1653
|
-
this.
|
|
1652
|
+
this.log('error', '[hmset] 哈希表批量设置失败:', error);
|
|
1654
1653
|
return false; // 操作失败返回false
|
|
1655
1654
|
}
|
|
1656
1655
|
};
|
|
@@ -1687,7 +1686,7 @@ MongoDB.prototype.hmget = async function (key, fields) {
|
|
|
1687
1686
|
|
|
1688
1687
|
return values;
|
|
1689
1688
|
} catch (error) {
|
|
1690
|
-
this.
|
|
1689
|
+
this.log('error', 'hmget操作失败:', error);
|
|
1691
1690
|
// 错误时返回与fields长度相同的null数组
|
|
1692
1691
|
const fieldArray = Array.isArray(fields) ? fields : [fields];
|
|
1693
1692
|
return fieldArray.map(() => null);
|
|
@@ -1735,7 +1734,7 @@ MongoDB.prototype.hdelMulti = async function (key, fields) {
|
|
|
1735
1734
|
}
|
|
1736
1735
|
return deleted;
|
|
1737
1736
|
} catch (error) {
|
|
1738
|
-
this.
|
|
1737
|
+
this.log('error', '增加整数值失败:', error);
|
|
1739
1738
|
return false;
|
|
1740
1739
|
}
|
|
1741
1740
|
};
|
|
@@ -1761,7 +1760,7 @@ MongoDB.prototype.hvals = async function (key) {
|
|
|
1761
1760
|
}
|
|
1762
1761
|
return Object.values(item.value);
|
|
1763
1762
|
} catch (error) {
|
|
1764
|
-
this.
|
|
1763
|
+
this.log('error', '哈希表获取所有值失败:', error);
|
|
1765
1764
|
return [];
|
|
1766
1765
|
}
|
|
1767
1766
|
};
|
|
@@ -1787,7 +1786,7 @@ MongoDB.prototype.hkeys = async function (key) {
|
|
|
1787
1786
|
}
|
|
1788
1787
|
return Object.keys(item.value);
|
|
1789
1788
|
} catch (error) {
|
|
1790
|
-
this.
|
|
1789
|
+
this.log('error', '哈希表获取所有字段名失败:', error);
|
|
1791
1790
|
return [];
|
|
1792
1791
|
}
|
|
1793
1792
|
};
|
|
@@ -1815,7 +1814,7 @@ MongoDB.prototype.hlen = async function (key) {
|
|
|
1815
1814
|
// 使用Object.keys获取自有属性数量,这比for-in循环更高效
|
|
1816
1815
|
return Object.keys(item.value).length;
|
|
1817
1816
|
} catch (error) {
|
|
1818
|
-
this.
|
|
1817
|
+
this.log('error', 'hlen操作失败:', error);
|
|
1819
1818
|
return 0;
|
|
1820
1819
|
}
|
|
1821
1820
|
};
|
|
@@ -1845,7 +1844,7 @@ MongoDB.prototype.hexists = async function (key, field) {
|
|
|
1845
1844
|
}
|
|
1846
1845
|
return field in item.value;
|
|
1847
1846
|
} catch (error) {
|
|
1848
|
-
this.
|
|
1847
|
+
this.log('error', '哈希表字段是否存在失败:', error);
|
|
1849
1848
|
return false;
|
|
1850
1849
|
}
|
|
1851
1850
|
};
|
|
@@ -1874,7 +1873,7 @@ MongoDB.prototype.rpush = async function (key, value) {
|
|
|
1874
1873
|
await this._add(key, list);
|
|
1875
1874
|
return list.length;
|
|
1876
1875
|
} catch (error) {
|
|
1877
|
-
this.
|
|
1876
|
+
this.log('error', '列表尾部添加失败:', error);
|
|
1878
1877
|
return 0;
|
|
1879
1878
|
}
|
|
1880
1879
|
};
|
|
@@ -1903,7 +1902,7 @@ MongoDB.prototype.lpush = async function (key, value) {
|
|
|
1903
1902
|
await this._add(key, list);
|
|
1904
1903
|
return list.length;
|
|
1905
1904
|
} catch (error) {
|
|
1906
|
-
this.
|
|
1905
|
+
this.log('error', '列表头部添加失败:', error);
|
|
1907
1906
|
return 0;
|
|
1908
1907
|
}
|
|
1909
1908
|
};
|
|
@@ -1946,7 +1945,7 @@ MongoDB.prototype.lrange = async function (key, start, stop) {
|
|
|
1946
1945
|
|
|
1947
1946
|
return item.value.slice(startIndex, endIndex + 1);
|
|
1948
1947
|
} catch (error) {
|
|
1949
|
-
this.
|
|
1948
|
+
this.log('error', '列表范围查询失败:', error);
|
|
1950
1949
|
return [];
|
|
1951
1950
|
}
|
|
1952
1951
|
};
|
|
@@ -1976,7 +1975,7 @@ MongoDB.prototype.lindex = async function (key, index) {
|
|
|
1976
1975
|
|
|
1977
1976
|
return item.value[realIndex];
|
|
1978
1977
|
} catch (error) {
|
|
1979
|
-
this.
|
|
1978
|
+
this.log('error', '获取列表索引元素失败:', error);
|
|
1980
1979
|
return null;
|
|
1981
1980
|
}
|
|
1982
1981
|
};
|
|
@@ -2013,7 +2012,7 @@ MongoDB.prototype.lset = async function (key, index, value) {
|
|
|
2013
2012
|
});
|
|
2014
2013
|
return true;
|
|
2015
2014
|
} catch (error) {
|
|
2016
|
-
this.
|
|
2015
|
+
this.log('error', '设置列表索引元素失败:', error);
|
|
2017
2016
|
return false;
|
|
2018
2017
|
}
|
|
2019
2018
|
};
|
|
@@ -2072,7 +2071,7 @@ MongoDB.prototype.lrem = async function (key, count, value) {
|
|
|
2072
2071
|
|
|
2073
2072
|
return removedCount;
|
|
2074
2073
|
} catch (error) {
|
|
2075
|
-
this.
|
|
2074
|
+
this.log('error', '删除列表元素失败:', error);
|
|
2076
2075
|
return 0;
|
|
2077
2076
|
}
|
|
2078
2077
|
};
|
|
@@ -2103,7 +2102,7 @@ MongoDB.prototype.ltrim = async function (key, start, stop) {
|
|
|
2103
2102
|
});
|
|
2104
2103
|
return true;
|
|
2105
2104
|
} catch (error) {
|
|
2106
|
-
this.
|
|
2105
|
+
this.log('error', '增加整数值失败:', error);
|
|
2107
2106
|
return false;
|
|
2108
2107
|
}
|
|
2109
2108
|
};
|
|
@@ -2129,7 +2128,7 @@ MongoDB.prototype.llen = async function (key) {
|
|
|
2129
2128
|
}
|
|
2130
2129
|
return item.value.length;
|
|
2131
2130
|
} catch (error) {
|
|
2132
|
-
this.
|
|
2131
|
+
this.log('error', '获取列表长度失败:', error);
|
|
2133
2132
|
return 0;
|
|
2134
2133
|
}
|
|
2135
2134
|
};
|
|
@@ -2170,7 +2169,7 @@ MongoDB.prototype.saddMulti = async function (key, members) {
|
|
|
2170
2169
|
|
|
2171
2170
|
return addedCount;
|
|
2172
2171
|
} catch (error) {
|
|
2173
|
-
this.
|
|
2172
|
+
this.log('error', '集合并发添加失败:', error);
|
|
2174
2173
|
return 0;
|
|
2175
2174
|
}
|
|
2176
2175
|
};
|
|
@@ -2213,7 +2212,7 @@ MongoDB.prototype.sremMulti = async function (key, members) {
|
|
|
2213
2212
|
|
|
2214
2213
|
return removedCount;
|
|
2215
2214
|
} catch (error) {
|
|
2216
|
-
this.
|
|
2215
|
+
this.log('error', '集合并发删除失败:', error);
|
|
2217
2216
|
return 0;
|
|
2218
2217
|
}
|
|
2219
2218
|
};
|
|
@@ -2259,7 +2258,7 @@ MongoDB.prototype.sdiff = async function (...keys) {
|
|
|
2259
2258
|
// 找出在baseSet中但不在其他所有集合中的元素
|
|
2260
2259
|
return baseSet.filter(member => !otherSetsStr.has(JSON.stringify(member)));
|
|
2261
2260
|
} catch (error) {
|
|
2262
|
-
this.
|
|
2261
|
+
this.log('error', '集合差集失败:', error);
|
|
2263
2262
|
return [];
|
|
2264
2263
|
}
|
|
2265
2264
|
};
|
|
@@ -2317,7 +2316,7 @@ MongoDB.prototype.sinter = async function (...keys) {
|
|
|
2317
2316
|
return otherSetsStr.every(set => set.has(memberStr));
|
|
2318
2317
|
});
|
|
2319
2318
|
} catch (error) {
|
|
2320
|
-
this.
|
|
2319
|
+
this.log('error', '集合交集失败:', error);
|
|
2321
2320
|
return [];
|
|
2322
2321
|
}
|
|
2323
2322
|
};
|
|
@@ -2364,7 +2363,7 @@ MongoDB.prototype.sunion = async function (...keys) {
|
|
|
2364
2363
|
|
|
2365
2364
|
return Array.from(uniqueMap.values());
|
|
2366
2365
|
} catch (error) {
|
|
2367
|
-
this.
|
|
2366
|
+
this.log('error', '集合并集失败:', error);
|
|
2368
2367
|
return [];
|
|
2369
2368
|
}
|
|
2370
2369
|
};
|
|
@@ -2393,7 +2392,7 @@ MongoDB.prototype.scard = async function (key) {
|
|
|
2393
2392
|
const uniqueItems = new Set(item.value.map(m => JSON.stringify(m)));
|
|
2394
2393
|
return uniqueItems.size;
|
|
2395
2394
|
} catch (error) {
|
|
2396
|
-
this.
|
|
2395
|
+
this.log('error', '获取集合成员数量失败:', error);
|
|
2397
2396
|
return 0;
|
|
2398
2397
|
}
|
|
2399
2398
|
};
|
|
@@ -2444,7 +2443,7 @@ MongoDB.prototype.sismember = async function (key, member) {
|
|
|
2444
2443
|
member,
|
|
2445
2444
|
error
|
|
2446
2445
|
});
|
|
2447
|
-
this.
|
|
2446
|
+
this.log('error', '检查集合成员失败:', error);
|
|
2448
2447
|
return false;
|
|
2449
2448
|
}
|
|
2450
2449
|
};
|
|
@@ -2472,7 +2471,7 @@ MongoDB.prototype.clearExpire = async function () {
|
|
|
2472
2471
|
this.emit('clearExpire:error', {
|
|
2473
2472
|
error
|
|
2474
2473
|
});
|
|
2475
|
-
this.
|
|
2474
|
+
this.log('error', '清空过期数据失败:', error);
|
|
2476
2475
|
return false;
|
|
2477
2476
|
}
|
|
2478
2477
|
};
|
|
@@ -2498,7 +2497,7 @@ MongoDB.prototype.mget = async function (...keys) {
|
|
|
2498
2497
|
// 返回与输入键顺序对应的数组,不存在的键对应位置返回undefined
|
|
2499
2498
|
return keyArray.map(key => resultMap.hasOwnProperty(key) ? resultMap[key] : undefined);
|
|
2500
2499
|
} catch (error) {
|
|
2501
|
-
this.
|
|
2500
|
+
this.log('error', '批量获取失败:', error);
|
|
2502
2501
|
throw error;
|
|
2503
2502
|
}
|
|
2504
2503
|
};
|
|
@@ -2520,7 +2519,7 @@ MongoDB.prototype.mset = async function (keyValues, seconds = 0) {
|
|
|
2520
2519
|
|
|
2521
2520
|
// 如果事件处理器标记为取消,则返回false
|
|
2522
2521
|
if (beforeEventData.cancel) {
|
|
2523
|
-
this.
|
|
2522
|
+
this.log('debug', 'mset操作被事件处理器取消');
|
|
2524
2523
|
return false;
|
|
2525
2524
|
}
|
|
2526
2525
|
|
|
@@ -2532,7 +2531,7 @@ MongoDB.prototype.mset = async function (keyValues, seconds = 0) {
|
|
|
2532
2531
|
|
|
2533
2532
|
// 参数验证,确保keyValues是对象且不为null
|
|
2534
2533
|
if (typeof finalData !== 'object' || finalData === null || Array.isArray(finalData)) {
|
|
2535
|
-
this.
|
|
2534
|
+
this.log('warn', 'mset: 参数必须是非空对象');
|
|
2536
2535
|
return false;
|
|
2537
2536
|
}
|
|
2538
2537
|
|
|
@@ -2555,7 +2554,7 @@ MongoDB.prototype.mset = async function (keyValues, seconds = 0) {
|
|
|
2555
2554
|
seconds,
|
|
2556
2555
|
error
|
|
2557
2556
|
});
|
|
2558
|
-
this.
|
|
2557
|
+
this.log('error', '批量设置失败:', error);
|
|
2559
2558
|
return false;
|
|
2560
2559
|
}
|
|
2561
2560
|
};
|
|
@@ -2601,7 +2600,7 @@ MongoDB.prototype.zadd = async function (key, score, member) {
|
|
|
2601
2600
|
await this._add(key, sortedSet);
|
|
2602
2601
|
return true;
|
|
2603
2602
|
} catch (error) {
|
|
2604
|
-
this.
|
|
2603
|
+
this.log('error', '增加浮点数值失败:', error);
|
|
2605
2604
|
return false;
|
|
2606
2605
|
}
|
|
2607
2606
|
};
|
|
@@ -2639,7 +2638,7 @@ MongoDB.prototype.zrangebyscore = async function (key, min, max) {
|
|
|
2639
2638
|
.filter(item => item.score >= min && item.score <= max)
|
|
2640
2639
|
.map(item => item.member);
|
|
2641
2640
|
} catch (error) {
|
|
2642
|
-
this.
|
|
2641
|
+
this.log('error', '有序集合范围查询失败:', error);
|
|
2643
2642
|
return [];
|
|
2644
2643
|
}
|
|
2645
2644
|
};
|
|
@@ -2665,7 +2664,7 @@ MongoDB.prototype.zcard = async function (key) {
|
|
|
2665
2664
|
}
|
|
2666
2665
|
return item.value.length;
|
|
2667
2666
|
} catch (error) {
|
|
2668
|
-
this.
|
|
2667
|
+
this.log('error', '有序集合成员数量失败:', error);
|
|
2669
2668
|
return 0;
|
|
2670
2669
|
}
|
|
2671
2670
|
};
|
|
@@ -2701,7 +2700,7 @@ MongoDB.prototype.zrem = async function (key, member) {
|
|
|
2701
2700
|
|
|
2702
2701
|
return removedCount;
|
|
2703
2702
|
} catch (error) {
|
|
2704
|
-
this.
|
|
2703
|
+
this.log('error', '有序集合删除成员失败:', error);
|
|
2705
2704
|
return 0;
|
|
2706
2705
|
}
|
|
2707
2706
|
};
|
|
@@ -2731,7 +2730,7 @@ MongoDB.prototype.zscore = async function (key, member) {
|
|
|
2731
2730
|
const found = item.value.find(item => JSON.stringify(item.member) === memberStr);
|
|
2732
2731
|
return found ? found.score : null;
|
|
2733
2732
|
} catch (error) {
|
|
2734
|
-
this.
|
|
2733
|
+
this.log('error', '获取成员分数失败:', error);
|
|
2735
2734
|
return null;
|
|
2736
2735
|
}
|
|
2737
2736
|
};
|
|
@@ -2764,7 +2763,7 @@ MongoDB.prototype.lpop = async function (key) {
|
|
|
2764
2763
|
});
|
|
2765
2764
|
return popped;
|
|
2766
2765
|
} catch (error) {
|
|
2767
|
-
this.
|
|
2766
|
+
this.log('error', '列表弹出头部元素失败:', error);
|
|
2768
2767
|
return null;
|
|
2769
2768
|
}
|
|
2770
2769
|
};
|
|
@@ -2797,7 +2796,7 @@ MongoDB.prototype.rpop = async function (key) {
|
|
|
2797
2796
|
});
|
|
2798
2797
|
return popped;
|
|
2799
2798
|
} catch (error) {
|
|
2800
|
-
this.
|
|
2799
|
+
this.log('error', '列表弹出尾部元素失败:', error);
|
|
2801
2800
|
return null;
|
|
2802
2801
|
}
|
|
2803
2802
|
};
|
|
@@ -2832,7 +2831,7 @@ MongoDB.prototype.smembers = async function (key) {
|
|
|
2832
2831
|
});
|
|
2833
2832
|
return Array.from(uniqueMap.values());
|
|
2834
2833
|
} catch (error) {
|
|
2835
|
-
this.
|
|
2834
|
+
this.log('error', '获取集合所有成员失败:', error);
|
|
2836
2835
|
return [];
|
|
2837
2836
|
}
|
|
2838
2837
|
};
|
|
@@ -2853,7 +2852,7 @@ MongoDB.prototype.findOne = async function (query, options = {}) {
|
|
|
2853
2852
|
// 不需要额外的sanitize,MongoDB驱动会处理
|
|
2854
2853
|
return await this.db.collection(this.table).findOne(query, options);
|
|
2855
2854
|
} catch (error) {
|
|
2856
|
-
this.
|
|
2855
|
+
this.log('error', '查询单条数据失败:', error);
|
|
2857
2856
|
return null;
|
|
2858
2857
|
}
|
|
2859
2858
|
};
|
|
@@ -2906,21 +2905,21 @@ MongoDB.prototype.open = async function () {
|
|
|
2906
2905
|
|
|
2907
2906
|
// 设置连接事件监听
|
|
2908
2907
|
this.conn.on('error', (err) => {
|
|
2909
|
-
this.
|
|
2908
|
+
this.log('error', '连接错误:', err);
|
|
2910
2909
|
this.connected = false;
|
|
2911
2910
|
});
|
|
2912
2911
|
|
|
2913
2912
|
this.conn.on('close', () => {
|
|
2914
|
-
this.
|
|
2913
|
+
this.log('warn', '连接已关闭');
|
|
2915
2914
|
this.connected = false;
|
|
2916
2915
|
});
|
|
2917
2916
|
|
|
2918
|
-
this.
|
|
2917
|
+
this.log('info',
|
|
2919
2918
|
`成功连接到 ${this.config.host}:${this.config.port}/${this.config.database}`
|
|
2920
2919
|
);
|
|
2921
2920
|
return true;
|
|
2922
2921
|
} catch (error) {
|
|
2923
|
-
this.
|
|
2922
|
+
this.log('error', 'MongoDB连接失败:', error);
|
|
2924
2923
|
}
|
|
2925
2924
|
}, '数据库连接');
|
|
2926
2925
|
}
|
|
@@ -2932,8 +2931,8 @@ MongoDB.prototype.open = async function () {
|
|
|
2932
2931
|
*/
|
|
2933
2932
|
MongoDB.prototype.close = async function () {
|
|
2934
2933
|
if (this.conn) {
|
|
2935
|
-
this.
|
|
2936
|
-
await this.conn.close().catch(err => this.
|
|
2934
|
+
this.log('info', `关闭连接`);
|
|
2935
|
+
await this.conn.close().catch(err => this.log('error', '关闭MongoDB连接时出错:', err));
|
|
2937
2936
|
this.conn = null;
|
|
2938
2937
|
this.db = null;
|
|
2939
2938
|
this.connected = false;
|
|
@@ -2978,13 +2977,13 @@ MongoDB.prototype.createIndex = async function (keys, options = {}) {
|
|
|
2978
2977
|
throw new Error('数据库未连接或未指定表名');
|
|
2979
2978
|
}
|
|
2980
2979
|
|
|
2981
|
-
this.
|
|
2980
|
+
this.log('info',
|
|
2982
2981
|
`创建索引: ${this.table}, keys: ${JSON.stringify(keys)}, options: ${JSON.stringify(options)}`
|
|
2983
2982
|
);
|
|
2984
2983
|
|
|
2985
2984
|
return await this.db.collection(this.table).createIndex(keys, options);
|
|
2986
2985
|
} catch (error) {
|
|
2987
|
-
this.
|
|
2986
|
+
this.log('error', `创建索引失败: ${error.message}`);
|
|
2988
2987
|
throw error;
|
|
2989
2988
|
}
|
|
2990
2989
|
};
|
|
@@ -3015,12 +3014,12 @@ MongoDB.prototype.createTTLIndex = async function (keys, expireAfterSeconds) {
|
|
|
3015
3014
|
expireAfterSeconds: expireAfterSeconds
|
|
3016
3015
|
};
|
|
3017
3016
|
|
|
3018
|
-
this.
|
|
3017
|
+
this.log('info',
|
|
3019
3018
|
`创建TTL索引: ${this.table}, keys: ${JSON.stringify(keys)}, 过期时间: ${expireAfterSeconds}秒`);
|
|
3020
3019
|
|
|
3021
3020
|
return await this.db.collection(this.table).createIndex(keys, options);
|
|
3022
3021
|
} catch (error) {
|
|
3023
|
-
this.
|
|
3022
|
+
this.log('error', `创建TTL索引失败: ${error.message}`);
|
|
3024
3023
|
throw error;
|
|
3025
3024
|
}
|
|
3026
3025
|
};
|
|
@@ -3036,10 +3035,10 @@ MongoDB.prototype.getIndexes = async function () {
|
|
|
3036
3035
|
throw new Error('未指定表名');
|
|
3037
3036
|
}
|
|
3038
3037
|
|
|
3039
|
-
this.
|
|
3038
|
+
this.log('info', `获取索引: ${this.table}`);
|
|
3040
3039
|
return await this.db.collection(this.table).indexes();
|
|
3041
3040
|
} catch (error) {
|
|
3042
|
-
this.
|
|
3041
|
+
this.log('error', `[getIndexes] 获取索引失败: ${error.message}`);
|
|
3043
3042
|
return [];
|
|
3044
3043
|
}
|
|
3045
3044
|
};
|
|
@@ -3058,11 +3057,11 @@ MongoDB.prototype.dropIndex = async function (indexName) {
|
|
|
3058
3057
|
throw new TypeError('索引名称不能为空');
|
|
3059
3058
|
}
|
|
3060
3059
|
try {
|
|
3061
|
-
this.
|
|
3060
|
+
this.log('info', `[dropIndex] 删除索引: ${this.table}, 索引名: ${indexName}`);
|
|
3062
3061
|
await this.db.collection(this.table).dropIndex(indexName);
|
|
3063
3062
|
return true;
|
|
3064
3063
|
} catch (error) {
|
|
3065
|
-
this.
|
|
3064
|
+
this.log('error', `[dropIndex] 删除索引失败: ${error.message}`);
|
|
3066
3065
|
throw error;
|
|
3067
3066
|
}
|
|
3068
3067
|
};
|
|
@@ -3077,11 +3076,11 @@ MongoDB.prototype.dropTable = async function () {
|
|
|
3077
3076
|
throw new Error('未指定表名');
|
|
3078
3077
|
}
|
|
3079
3078
|
try {
|
|
3080
|
-
this.
|
|
3079
|
+
this.log('info', `[dropTable] 删除集合: ${this.table}`);
|
|
3081
3080
|
await this.db.collection(this.table).drop();
|
|
3082
3081
|
return true;
|
|
3083
3082
|
} catch (error) {
|
|
3084
|
-
this.
|
|
3083
|
+
this.log('error', `[dropTable] 删除集合失败: ${error.message}`);
|
|
3085
3084
|
throw error;
|
|
3086
3085
|
}
|
|
3087
3086
|
};
|
|
@@ -3140,7 +3139,7 @@ MongoDB.prototype.get = async function (key) {
|
|
|
3140
3139
|
});
|
|
3141
3140
|
return result ? result.value : null;
|
|
3142
3141
|
} catch (error) {
|
|
3143
|
-
this.
|
|
3142
|
+
this.log('error', `获取键值失败: ${error.message}`);
|
|
3144
3143
|
return null;
|
|
3145
3144
|
}
|
|
3146
3145
|
};
|
|
@@ -3174,7 +3173,7 @@ MongoDB.prototype.setForList = async function (key, array, seconds = 0) {
|
|
|
3174
3173
|
this.emit('setForList:before', beforeEventData);
|
|
3175
3174
|
|
|
3176
3175
|
if (beforeEventData.cancel) {
|
|
3177
|
-
this.
|
|
3176
|
+
this.log('debug', `操作被事件处理器取消 [${key}]`);
|
|
3178
3177
|
return 0;
|
|
3179
3178
|
}
|
|
3180
3179
|
|
|
@@ -3206,7 +3205,7 @@ MongoDB.prototype.setForList = async function (key, array, seconds = 0) {
|
|
|
3206
3205
|
value: array,
|
|
3207
3206
|
error
|
|
3208
3207
|
});
|
|
3209
|
-
this.
|
|
3208
|
+
this.log('error', `[setForList] setForList操作失败: ${error.message}`);
|
|
3210
3209
|
return 0;
|
|
3211
3210
|
}
|
|
3212
3211
|
};
|
|
@@ -3235,7 +3234,7 @@ MongoDB.prototype.addForList = async function (key, value, seconds = 0) {
|
|
|
3235
3234
|
this.emit('addForList:before', beforeEventData);
|
|
3236
3235
|
|
|
3237
3236
|
if (beforeEventData.cancel) {
|
|
3238
|
-
this.
|
|
3237
|
+
this.log('debug', `addForList操作被事件处理器取消 [${key}]`);
|
|
3239
3238
|
return 0;
|
|
3240
3239
|
}
|
|
3241
3240
|
|
|
@@ -3276,7 +3275,7 @@ MongoDB.prototype.addForList = async function (key, value, seconds = 0) {
|
|
|
3276
3275
|
value,
|
|
3277
3276
|
error
|
|
3278
3277
|
});
|
|
3279
|
-
this.
|
|
3278
|
+
this.log('error', `addForList操作失败: ${error.message}`);
|
|
3280
3279
|
return 0;
|
|
3281
3280
|
}
|
|
3282
3281
|
};
|
|
@@ -3309,7 +3308,7 @@ MongoDB.prototype.hasForList = async function (key, value) {
|
|
|
3309
3308
|
const valueStr = JSON.stringify(value);
|
|
3310
3309
|
return array.some(item => JSON.stringify(item) === valueStr);
|
|
3311
3310
|
} catch (error) {
|
|
3312
|
-
this.
|
|
3311
|
+
this.log('error', `hasForList操作失败: ${error.message}`);
|
|
3313
3312
|
return false;
|
|
3314
3313
|
}
|
|
3315
3314
|
};
|
|
@@ -3347,7 +3346,7 @@ MongoDB.prototype.getForList = async function (key, start = 0, stop = -1) {
|
|
|
3347
3346
|
|
|
3348
3347
|
return result;
|
|
3349
3348
|
} catch (error) {
|
|
3350
|
-
this.
|
|
3349
|
+
this.log('error', `getForList操作失败: ${error.message}`);
|
|
3351
3350
|
return [];
|
|
3352
3351
|
}
|
|
3353
3352
|
};
|
|
@@ -3370,7 +3369,7 @@ MongoDB.prototype.clearForList = async function (key) {
|
|
|
3370
3369
|
// 使用set方法直接设置为空数组,与其他模块行为一致
|
|
3371
3370
|
return await this.set(key, []);
|
|
3372
3371
|
} catch (error) {
|
|
3373
|
-
this.
|
|
3372
|
+
this.log('error', `clearForList操作失败: ${error.message}`);
|
|
3374
3373
|
return false;
|
|
3375
3374
|
}
|
|
3376
3375
|
};
|
|
@@ -3382,7 +3381,7 @@ MongoDB.prototype.has = async function (key) {
|
|
|
3382
3381
|
const exists = await this.exists(key);
|
|
3383
3382
|
return exists > 0;
|
|
3384
3383
|
} catch (error) {
|
|
3385
|
-
this.
|
|
3384
|
+
this.log('error', `检查键是否存在失败: ${error.message}`);
|
|
3386
3385
|
throw error;
|
|
3387
3386
|
}
|
|
3388
3387
|
};
|
|
@@ -3426,7 +3425,7 @@ MongoDB.prototype.exists = async function (key) {
|
|
|
3426
3425
|
throw new TypeError('key必须是非空字符串或非空字符串数组');
|
|
3427
3426
|
}
|
|
3428
3427
|
} catch (error) {
|
|
3429
|
-
this.
|
|
3428
|
+
this.log('error', `检查键是否存在失败: ${error.message}`);
|
|
3430
3429
|
throw error;
|
|
3431
3430
|
}
|
|
3432
3431
|
};
|
|
@@ -3471,7 +3470,7 @@ MongoDB.prototype.del = async function (key) {
|
|
|
3471
3470
|
|
|
3472
3471
|
// 如果事件处理器标记为取消,则返回false
|
|
3473
3472
|
if (beforeEventData.cancel) {
|
|
3474
|
-
this.
|
|
3473
|
+
this.log('debug', `del操作被事件处理器取消 [${key}]`);
|
|
3475
3474
|
return false;
|
|
3476
3475
|
}
|
|
3477
3476
|
|
|
@@ -3521,7 +3520,7 @@ MongoDB.prototype.del = async function (key) {
|
|
|
3521
3520
|
key,
|
|
3522
3521
|
error: error
|
|
3523
3522
|
});
|
|
3524
|
-
this.
|
|
3523
|
+
this.log('error', '删除缓存失败:', error);
|
|
3525
3524
|
return false;
|
|
3526
3525
|
}
|
|
3527
3526
|
};
|
|
@@ -3547,7 +3546,7 @@ MongoDB.prototype.add = async function (key, value, seconds) {
|
|
|
3547
3546
|
|
|
3548
3547
|
// 如果事件处理器标记为取消,则返回false
|
|
3549
3548
|
if (beforeEventData.cancel) {
|
|
3550
|
-
this.
|
|
3549
|
+
this.log('debug', `add操作被事件处理器取消 [${key}]`);
|
|
3551
3550
|
return false;
|
|
3552
3551
|
}
|
|
3553
3552
|
|
|
@@ -3605,7 +3604,7 @@ MongoDB.prototype.add = async function (key, value, seconds) {
|
|
|
3605
3604
|
ttl: seconds,
|
|
3606
3605
|
error
|
|
3607
3606
|
});
|
|
3608
|
-
this.
|
|
3607
|
+
this.log('error', `添加键值失败: ${error.message}`);
|
|
3609
3608
|
return false;
|
|
3610
3609
|
}
|
|
3611
3610
|
};
|
|
@@ -3621,7 +3620,7 @@ MongoDB.prototype.set = async function (key, value, seconds) {
|
|
|
3621
3620
|
try {
|
|
3622
3621
|
// 键名必须是非空字符串
|
|
3623
3622
|
if (!key || typeof key !== 'string') {
|
|
3624
|
-
this.
|
|
3623
|
+
this.log('error', '参数错误: key必须是非空字符串');
|
|
3625
3624
|
return false;
|
|
3626
3625
|
}
|
|
3627
3626
|
|
|
@@ -3636,7 +3635,7 @@ MongoDB.prototype.set = async function (key, value, seconds) {
|
|
|
3636
3635
|
|
|
3637
3636
|
// 如果事件处理器标记为取消,则返回false
|
|
3638
3637
|
if (beforeEventData.cancel) {
|
|
3639
|
-
this.
|
|
3638
|
+
this.log('debug', `set操作被事件处理器取消 [${key}]`);
|
|
3640
3639
|
return false;
|
|
3641
3640
|
}
|
|
3642
3641
|
|
|
@@ -3691,7 +3690,7 @@ MongoDB.prototype.set = async function (key, value, seconds) {
|
|
|
3691
3690
|
ttl: seconds,
|
|
3692
3691
|
error
|
|
3693
3692
|
});
|
|
3694
|
-
this.
|
|
3693
|
+
this.log('error', `设置键值失败: ${error.message}`);
|
|
3695
3694
|
return false;
|
|
3696
3695
|
}
|
|
3697
3696
|
};
|
|
@@ -3861,7 +3860,7 @@ MongoDB.prototype.find = async function (query, options = {}) {
|
|
|
3861
3860
|
const cursor = this.db.collection(this.table).find(safeQuery, options);
|
|
3862
3861
|
return await cursor.toArray();
|
|
3863
3862
|
} catch (error) {
|
|
3864
|
-
this.
|
|
3863
|
+
this.log('error', '查询失败:', error);
|
|
3865
3864
|
return [];
|
|
3866
3865
|
}
|
|
3867
3866
|
};
|
|
@@ -3878,7 +3877,7 @@ MongoDB.prototype.count = async function (query = {}) {
|
|
|
3878
3877
|
const safeQuery = this._sanitizeQuery(query);
|
|
3879
3878
|
return await this.db.collection(this.table).countDocuments(safeQuery);
|
|
3880
3879
|
} catch (error) {
|
|
3881
|
-
this.
|
|
3880
|
+
this.log('error', '计算文档数量失败:', error);
|
|
3882
3881
|
return 0;
|
|
3883
3882
|
}
|
|
3884
3883
|
};
|
|
@@ -3897,7 +3896,7 @@ MongoDB.prototype._sanitizeQuery = function (query) {
|
|
|
3897
3896
|
// 防止NoSQL注入,如 {$ne: null} 等操作符注入
|
|
3898
3897
|
if (key.startsWith('$') && typeof key === 'string' && !Array.isArray(value) && typeof value === 'object' &&
|
|
3899
3898
|
value !== null) {
|
|
3900
|
-
this.
|
|
3899
|
+
this.log('warn', '检测到可能的操作符注入尝试:', key);
|
|
3901
3900
|
continue;
|
|
3902
3901
|
}
|
|
3903
3902
|
sanitized[key] = value;
|
|
@@ -3917,7 +3916,7 @@ MongoDB.prototype.startTransaction = async function () {
|
|
|
3917
3916
|
session.startTransaction();
|
|
3918
3917
|
return session;
|
|
3919
3918
|
} catch (error) {
|
|
3920
|
-
this.
|
|
3919
|
+
this.log('error', '[startTransaction] 启动事务失败:', error);
|
|
3921
3920
|
throw error;
|
|
3922
3921
|
}
|
|
3923
3922
|
};
|
|
@@ -3932,7 +3931,7 @@ MongoDB.prototype.commitTransaction = async function (session) {
|
|
|
3932
3931
|
await session.commitTransaction();
|
|
3933
3932
|
await session.endSession();
|
|
3934
3933
|
} catch (error) {
|
|
3935
|
-
this.
|
|
3934
|
+
this.log('error', '[commitTransaction] 提交事务失败:', error);
|
|
3936
3935
|
throw error;
|
|
3937
3936
|
}
|
|
3938
3937
|
};
|
|
@@ -3947,7 +3946,7 @@ MongoDB.prototype.rollbackTransaction = async function (session) {
|
|
|
3947
3946
|
await session.abortTransaction();
|
|
3948
3947
|
await session.endSession();
|
|
3949
3948
|
} catch (error) {
|
|
3950
|
-
this.
|
|
3949
|
+
this.log('error', '[rollbackTransaction] 回滚事务失败:', error);
|
|
3951
3950
|
throw error;
|
|
3952
3951
|
}
|
|
3953
3952
|
};
|
|
@@ -3973,7 +3972,7 @@ if (!$.pool.mongoDB) {
|
|
|
3973
3972
|
* @param {Object} config 配置参数
|
|
3974
3973
|
* @return {Object} 返回一个MongoDB类实例
|
|
3975
3974
|
*/
|
|
3976
|
-
function
|
|
3975
|
+
function mongodbAdmin(scope, config) {
|
|
3977
3976
|
if (!scope) {
|
|
3978
3977
|
scope = 'sys';
|
|
3979
3978
|
}
|
|
@@ -3995,4 +3994,4 @@ function mongodb_admin(scope, config) {
|
|
|
3995
3994
|
/**
|
|
3996
3995
|
* @module 导出MongoDB管理器
|
|
3997
3996
|
*/
|
|
3998
|
-
exports.
|
|
3997
|
+
exports.mongodbAdmin = mongodbAdmin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_mongodb",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.4",
|
|
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": "^
|
|
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
|
|
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 =
|
|
15
|
+
var rs = mongodbAdmin('sys', config);
|
|
16
16
|
await rs.open();
|
|
17
17
|
await rs.addTable('cache_test');
|
|
18
18
|
|