mm_mongodb 2.0.3 → 2.0.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/README.md +16 -6
- package/example/index.js +41 -0
- package/index.js +15 -2830
- package/lib/mongodb.js +2813 -0
- package/package.json +52 -42
- package/test.js +35 -31
- package/test2.js +15 -11
package/package.json
CHANGED
|
@@ -1,42 +1,52 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mm_mongodb",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"description": "MongoDB数据库操作模块,接口与 mm_redis / mm_cachebase 完全对齐:全异步API、冒号风格事件、统一签名与返回值,支持缓存功能、事务和连接池管理",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "node test.js",
|
|
8
|
-
"test:events": "node test_events.js",
|
|
9
|
-
"test:redis": "node test_redis_api.js",
|
|
10
|
-
"test:redis-compatibility": "node test_redis_compatibility.js"
|
|
11
|
-
},
|
|
12
|
-
"repository": {
|
|
13
|
-
"type": "git",
|
|
14
|
-
"url": "git+https://gitee.com/qiuwenwu91/mm_mongodb.git"
|
|
15
|
-
},
|
|
16
|
-
"keywords": [
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
}
|
|
42
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "mm_mongodb",
|
|
3
|
+
"version": "2.0.4",
|
|
4
|
+
"description": "MongoDB数据库操作模块,接口与 mm_redis / mm_cachebase 完全对齐:全异步API、冒号风格事件、统一签名与返回值,支持缓存功能、事务和连接池管理",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test.js",
|
|
8
|
+
"test:events": "node test_events.js",
|
|
9
|
+
"test:redis": "node test_redis_api.js",
|
|
10
|
+
"test:redis-compatibility": "node test_redis_compatibility.js"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_mongodb.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"mm_mod",
|
|
18
|
+
"mm_adapter",
|
|
19
|
+
"mm_mongodb",
|
|
20
|
+
"mongodb",
|
|
21
|
+
"redis",
|
|
22
|
+
"cache",
|
|
23
|
+
"async",
|
|
24
|
+
"await",
|
|
25
|
+
"promise",
|
|
26
|
+
"transaction",
|
|
27
|
+
"index",
|
|
28
|
+
"pool",
|
|
29
|
+
"nosql",
|
|
30
|
+
"database",
|
|
31
|
+
"redis-compatible",
|
|
32
|
+
"event",
|
|
33
|
+
"ttl"
|
|
34
|
+
],
|
|
35
|
+
"layer": "adapter",
|
|
36
|
+
"group": "mongodb",
|
|
37
|
+
"author": "邱文武",
|
|
38
|
+
"license": "ISC",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://gitee.com/qiuwenwu91/mm_mongodb/issues"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://gitee.com/qiuwenwu91/mm_mongodb#readme",
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"mm_corejs": "^1.1.4",
|
|
45
|
+
"mm_mod": "^1.0.1",
|
|
46
|
+
"mm_logger": "^1.0.0",
|
|
47
|
+
"mongodb": "^7.6.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"mm_eslint": "^1.7.7"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/test.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
const assert = require('assert');
|
|
2
|
+
const { getLogger } = require('mm_logger');
|
|
2
3
|
const MongoDB = require('./index.js').MongoDB;
|
|
3
4
|
const mongodbAdmin = require('./index.js').mongodbAdmin;
|
|
4
5
|
|
|
6
|
+
// 日志记录器实例
|
|
7
|
+
const logger = getLogger();
|
|
8
|
+
|
|
5
9
|
/**
|
|
6
10
|
* 测试运行器,与 mm_cachebase 测试风格对齐
|
|
7
11
|
*/
|
|
@@ -18,24 +22,24 @@ class TestRunner {
|
|
|
18
22
|
await testFunction();
|
|
19
23
|
this.passedTests++;
|
|
20
24
|
this.results.push({ name, passed: true });
|
|
21
|
-
|
|
25
|
+
logger.debug(`✅ ${name}`);
|
|
22
26
|
} catch (error) {
|
|
23
27
|
this.results.push({ name, passed: false, error: error.message });
|
|
24
|
-
|
|
28
|
+
logger.debug(`❌ ${name} - ${error.message}`);
|
|
25
29
|
}
|
|
26
30
|
}
|
|
27
31
|
|
|
28
32
|
displayResults() {
|
|
29
|
-
|
|
33
|
+
logger.debug('\n========== 测试结果 ==========');
|
|
30
34
|
const passRate = (this.passedTests / this.totalTests * 100).toFixed(2);
|
|
31
|
-
|
|
35
|
+
logger.debug(`通过: ${this.passedTests}/${this.totalTests} (${passRate}%)`);
|
|
32
36
|
|
|
33
37
|
// 显示失败的测试
|
|
34
38
|
const failedTests = this.results.filter(r => !r.passed);
|
|
35
39
|
if (failedTests.length > 0) {
|
|
36
|
-
|
|
40
|
+
logger.debug('\n失败的测试:');
|
|
37
41
|
failedTests.forEach(test => {
|
|
38
|
-
|
|
42
|
+
logger.debug(` - ${test.name}: ${test.error}`);
|
|
39
43
|
});
|
|
40
44
|
}
|
|
41
45
|
|
|
@@ -60,9 +64,9 @@ async function cleanupKeys(db, keys) {
|
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
async function test() {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
67
|
+
logger.debug('[MongoDB] ==============================');
|
|
68
|
+
logger.debug('[MongoDB] 开始兼容性测试(对齐 mm_redis / mm_cachebase 接口)');
|
|
69
|
+
logger.debug('[MongoDB] ==============================');
|
|
66
70
|
|
|
67
71
|
try {
|
|
68
72
|
// 使用默认配置创建实例
|
|
@@ -83,7 +87,7 @@ async function test() {
|
|
|
83
87
|
const allTestKeys = [];
|
|
84
88
|
|
|
85
89
|
// ==================== 测试基础方法 ====================
|
|
86
|
-
|
|
90
|
+
logger.debug('[MongoDB] \n========== 测试基础方法 ==========');
|
|
87
91
|
|
|
88
92
|
await runner.runTest('set和get方法', async () => {
|
|
89
93
|
await cache.set('test_key', 'test_value');
|
|
@@ -191,7 +195,7 @@ async function test() {
|
|
|
191
195
|
});
|
|
192
196
|
|
|
193
197
|
// ==================== 测试哈希表方法 ====================
|
|
194
|
-
|
|
198
|
+
logger.debug('[MongoDB] \n========== 测试哈希表方法 ==========');
|
|
195
199
|
|
|
196
200
|
await runner.runTest('hset和hget方法(返回Boolean)', async () => {
|
|
197
201
|
// hset 返回 true/false,对齐 mm_cachebase
|
|
@@ -282,7 +286,7 @@ async function test() {
|
|
|
282
286
|
});
|
|
283
287
|
|
|
284
288
|
// ==================== 测试批量操作 ====================
|
|
285
|
-
|
|
289
|
+
logger.debug('[MongoDB] \n========== 测试批量操作 ==========');
|
|
286
290
|
|
|
287
291
|
await runner.runTest('mset方法(对象参数)', async () => {
|
|
288
292
|
// mset 接受对象参数,对齐 mm_cachebase
|
|
@@ -306,7 +310,7 @@ async function test() {
|
|
|
306
310
|
});
|
|
307
311
|
|
|
308
312
|
// ==================== 测试过期时间 ====================
|
|
309
|
-
|
|
313
|
+
logger.debug('[MongoDB] \n========== 测试过期时间 ==========');
|
|
310
314
|
|
|
311
315
|
await runner.runTest('set带过期时间', async () => {
|
|
312
316
|
await cache.set('expire1', 'value1', 1); // 1秒过期
|
|
@@ -372,7 +376,7 @@ async function test() {
|
|
|
372
376
|
});
|
|
373
377
|
|
|
374
378
|
// ==================== 测试数值操作 ====================
|
|
375
|
-
|
|
379
|
+
logger.debug('[MongoDB] \n========== 测试数值操作 ==========');
|
|
376
380
|
|
|
377
381
|
await runner.runTest('incr方法', async () => {
|
|
378
382
|
await cache.del('counter');
|
|
@@ -476,7 +480,7 @@ async function test() {
|
|
|
476
480
|
});
|
|
477
481
|
|
|
478
482
|
// ==================== 测试字符串操作 ====================
|
|
479
|
-
|
|
483
|
+
logger.debug('[MongoDB] \n========== 测试字符串操作 ==========');
|
|
480
484
|
|
|
481
485
|
await runner.runTest('append方法', async () => {
|
|
482
486
|
await cache.set('str', 'hello');
|
|
@@ -515,7 +519,7 @@ async function test() {
|
|
|
515
519
|
});
|
|
516
520
|
|
|
517
521
|
// ==================== 测试集合操作 ====================
|
|
518
|
-
|
|
522
|
+
logger.debug('[MongoDB] \n========== 测试集合操作 ==========');
|
|
519
523
|
|
|
520
524
|
await runner.runTest('sadd/smembers/scard方法', async () => {
|
|
521
525
|
const added = await cache.sadd('set1', ['a', 'b', 'c']);
|
|
@@ -582,7 +586,7 @@ async function test() {
|
|
|
582
586
|
});
|
|
583
587
|
|
|
584
588
|
// ==================== 测试有序集合操作 ====================
|
|
585
|
-
|
|
589
|
+
logger.debug('[MongoDB] \n========== 测试有序集合操作 ==========');
|
|
586
590
|
|
|
587
591
|
await runner.runTest('zadd/zcard/zscore方法', async () => {
|
|
588
592
|
const added1 = await cache.zadd('zset1', 10, 'member1');
|
|
@@ -640,7 +644,7 @@ async function test() {
|
|
|
640
644
|
});
|
|
641
645
|
|
|
642
646
|
// ==================== 测试列表操作 ====================
|
|
643
|
-
|
|
647
|
+
logger.debug('[MongoDB] \n========== 测试列表操作 ==========');
|
|
644
648
|
|
|
645
649
|
await runner.runTest('setForList/addForList/getForList方法', async () => {
|
|
646
650
|
await cache.setForList('list1', ['a', 'b', 'c']);
|
|
@@ -678,7 +682,7 @@ async function test() {
|
|
|
678
682
|
});
|
|
679
683
|
|
|
680
684
|
// ==================== 测试发布订阅 ====================
|
|
681
|
-
|
|
685
|
+
logger.debug('[MongoDB] \n========== 测试发布订阅 ==========');
|
|
682
686
|
|
|
683
687
|
await runner.runTest('subscribe/publish方法', async () => {
|
|
684
688
|
let receivedMessage = null;
|
|
@@ -708,7 +712,7 @@ async function test() {
|
|
|
708
712
|
});
|
|
709
713
|
|
|
710
714
|
// ==================== 测试事件触发(冒号风格) ====================
|
|
711
|
-
|
|
715
|
+
logger.debug('[MongoDB] \n========== 测试事件触发(冒号风格) ==========');
|
|
712
716
|
|
|
713
717
|
await runner.runTest('set事件触发(冒号风格)', async () => {
|
|
714
718
|
return new Promise(async (resolve, reject) => {
|
|
@@ -764,7 +768,7 @@ async function test() {
|
|
|
764
768
|
});
|
|
765
769
|
|
|
766
770
|
// ==================== 测试管理功能 ====================
|
|
767
|
-
|
|
771
|
+
logger.debug('[MongoDB] \n========== 测试管理功能 ==========');
|
|
768
772
|
|
|
769
773
|
await runner.runTest('mongodbAdmin创建实例', async () => {
|
|
770
774
|
const cache1 = mongodbAdmin('test', config);
|
|
@@ -783,36 +787,36 @@ async function test() {
|
|
|
783
787
|
|
|
784
788
|
// 显示测试结果
|
|
785
789
|
const passRate = runner.displayResults();
|
|
786
|
-
|
|
790
|
+
logger.debug(`[MongoDB] \n测试完成!通过率: ${passRate}%`);
|
|
787
791
|
|
|
788
792
|
// 清理所有测试数据
|
|
789
|
-
|
|
793
|
+
logger.debug('[MongoDB] \n清理所有测试数据...');
|
|
790
794
|
await cleanupKeys(cache, allTestKeys);
|
|
791
795
|
|
|
792
796
|
// 关闭连接
|
|
793
|
-
|
|
797
|
+
logger.debug('[MongoDB] \n关闭连接...');
|
|
794
798
|
await cache.close();
|
|
795
799
|
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
800
|
+
logger.debug('[MongoDB] \n==============================');
|
|
801
|
+
logger.debug('[MongoDB] 所有兼容性测试完成!');
|
|
802
|
+
logger.debug('[MongoDB] ==============================');
|
|
799
803
|
|
|
800
804
|
} catch (error) {
|
|
801
|
-
|
|
805
|
+
logger.error('[MongoDB] 测试过程中出现错误:', error);
|
|
802
806
|
}
|
|
803
807
|
}
|
|
804
808
|
|
|
805
809
|
test().then(() => {
|
|
806
810
|
// 测试成功完成
|
|
807
|
-
|
|
811
|
+
logger.debug('[MongoDB] 测试脚本正常退出');
|
|
808
812
|
process.exit(0);
|
|
809
813
|
}).catch(err => {
|
|
810
|
-
|
|
814
|
+
logger.error('[MongoDB] 未捕获的错误:', err);
|
|
811
815
|
process.exit(1);
|
|
812
816
|
});
|
|
813
817
|
|
|
814
818
|
// 添加超时保护
|
|
815
819
|
setTimeout(() => {
|
|
816
|
-
|
|
820
|
+
logger.warn('[MongoDB] 测试脚本超时');
|
|
817
821
|
process.exit(1);
|
|
818
822
|
}, 30000); // 30秒超时(MongoDB 操作可能较慢)
|
package/test2.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const { getLogger } = require('mm_logger');
|
|
2
|
+
const { MongoDB, mongoDB_admin } = require('./index.js');
|
|
3
|
+
|
|
4
|
+
// 日志记录器实例
|
|
5
|
+
const logger = getLogger();
|
|
2
6
|
|
|
3
7
|
|
|
4
8
|
/* 调用示例 */
|
|
5
9
|
async function test() {
|
|
6
|
-
|
|
10
|
+
logger.debug("测试开始");
|
|
7
11
|
// 实例化构造函数,传入服务器地址和数据库名
|
|
8
12
|
var mb = new MongoDB("localhost");
|
|
9
13
|
mb.setConfig();
|
|
@@ -12,20 +16,20 @@ async function test() {
|
|
|
12
16
|
try {
|
|
13
17
|
// 创建数据表
|
|
14
18
|
await mb.addTable('bs_keyValue');
|
|
15
|
-
|
|
19
|
+
logger.debug("创建表bs_keyValue");
|
|
16
20
|
} catch (e) {
|
|
17
|
-
//
|
|
21
|
+
// logger.debug("创建表错误提示:", e);
|
|
18
22
|
//TODO handle the exception
|
|
19
23
|
}
|
|
20
24
|
mb.table = "mm_table";
|
|
21
25
|
|
|
22
|
-
|
|
26
|
+
logger.debug("查询", mb.conn);
|
|
23
27
|
// 直接使用find方法查询数据
|
|
24
28
|
var ret = await mb.find({ name: '李四' });
|
|
25
|
-
|
|
29
|
+
logger.debug("查询李四", ret);
|
|
26
30
|
|
|
27
31
|
ret = await mb.find({ name: '张三' });
|
|
28
|
-
|
|
32
|
+
logger.debug("查询张三", ret);
|
|
29
33
|
|
|
30
34
|
// 选择要操作的表
|
|
31
35
|
mb.table = 'mm_table';
|
|
@@ -36,7 +40,7 @@ $.log.debug("查询", mb.conn);
|
|
|
36
40
|
age: 21,
|
|
37
41
|
sex: true
|
|
38
42
|
});
|
|
39
|
-
|
|
43
|
+
logger.debug("添加一条数据", ret);
|
|
40
44
|
|
|
41
45
|
|
|
42
46
|
// 修改一条数据
|
|
@@ -45,14 +49,14 @@ $.log.debug("查询", mb.conn);
|
|
|
45
49
|
age: 24,
|
|
46
50
|
sex: false
|
|
47
51
|
});
|
|
48
|
-
|
|
52
|
+
logger.debug("添加一条数据", ret);
|
|
49
53
|
|
|
50
54
|
// 查询
|
|
51
55
|
ret = await mb.find({ name: "张三" });
|
|
52
|
-
|
|
56
|
+
logger.debug("查询张三", ret);
|
|
53
57
|
|
|
54
58
|
// 关闭连接
|
|
55
59
|
await mb.close();
|
|
56
|
-
|
|
60
|
+
logger.debug("测试完成");
|
|
57
61
|
}
|
|
58
62
|
test();
|