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/package.json CHANGED
@@ -1,42 +1,52 @@
1
- {
2
- "name": "mm_mongodb",
3
- "version": "2.0.3",
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
- "mongodb",
18
- "redis",
19
- "cache",
20
- "async",
21
- "await",
22
- "promise",
23
- "transaction",
24
- "index",
25
- "pool",
26
- "nosql",
27
- "database",
28
- "redis-compatible",
29
- "event",
30
- "ttl"
31
- ],
32
- "author": "邱文武",
33
- "license": "ISC",
34
- "bugs": {
35
- "url": "https://gitee.com/qiuwenwu91/mm_mongodb/issues"
36
- },
37
- "homepage": "https://gitee.com/qiuwenwu91/mm_mongodb#readme",
38
- "dependencies": {
39
- "mm_expand": "^2.5.4",
40
- "mongodb": "^7.6.0"
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
- $.log.debug(`✅ ${name}`);
25
+ logger.debug(`✅ ${name}`);
22
26
  } catch (error) {
23
27
  this.results.push({ name, passed: false, error: error.message });
24
- $.log.debug(`❌ ${name} - ${error.message}`);
28
+ logger.debug(`❌ ${name} - ${error.message}`);
25
29
  }
26
30
  }
27
31
 
28
32
  displayResults() {
29
- $.log.debug('\n========== 测试结果 ==========');
33
+ logger.debug('\n========== 测试结果 ==========');
30
34
  const passRate = (this.passedTests / this.totalTests * 100).toFixed(2);
31
- $.log.debug(`通过: ${this.passedTests}/${this.totalTests} (${passRate}%)`);
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
- $.log.debug('\n失败的测试:');
40
+ logger.debug('\n失败的测试:');
37
41
  failedTests.forEach(test => {
38
- $.log.debug(` - ${test.name}: ${test.error}`);
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
- $.log.debug('[MongoDB] ==============================');
64
- $.log.debug('[MongoDB] 开始兼容性测试(对齐 mm_redis / mm_cachebase 接口)');
65
- $.log.debug('[MongoDB] ==============================');
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
- $.log.debug('[MongoDB] \n========== 测试基础方法 ==========');
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
- $.log.debug('[MongoDB] \n========== 测试哈希表方法 ==========');
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
- $.log.debug('[MongoDB] \n========== 测试批量操作 ==========');
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
- $.log.debug('[MongoDB] \n========== 测试过期时间 ==========');
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
- $.log.debug('[MongoDB] \n========== 测试数值操作 ==========');
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
- $.log.debug('[MongoDB] \n========== 测试字符串操作 ==========');
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
- $.log.debug('[MongoDB] \n========== 测试集合操作 ==========');
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
- $.log.debug('[MongoDB] \n========== 测试有序集合操作 ==========');
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
- $.log.debug('[MongoDB] \n========== 测试列表操作 ==========');
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
- $.log.debug('[MongoDB] \n========== 测试发布订阅 ==========');
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
- $.log.debug('[MongoDB] \n========== 测试事件触发(冒号风格) ==========');
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
- $.log.debug('[MongoDB] \n========== 测试管理功能 ==========');
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
- $.log.debug(`[MongoDB] \n测试完成!通过率: ${passRate}%`);
790
+ logger.debug(`[MongoDB] \n测试完成!通过率: ${passRate}%`);
787
791
 
788
792
  // 清理所有测试数据
789
- $.log.debug('[MongoDB] \n清理所有测试数据...');
793
+ logger.debug('[MongoDB] \n清理所有测试数据...');
790
794
  await cleanupKeys(cache, allTestKeys);
791
795
 
792
796
  // 关闭连接
793
- $.log.debug('[MongoDB] \n关闭连接...');
797
+ logger.debug('[MongoDB] \n关闭连接...');
794
798
  await cache.close();
795
799
 
796
- $.log.debug('[MongoDB] \n==============================');
797
- $.log.debug('[MongoDB] 所有兼容性测试完成!');
798
- $.log.debug('[MongoDB] ==============================');
800
+ logger.debug('[MongoDB] \n==============================');
801
+ logger.debug('[MongoDB] 所有兼容性测试完成!');
802
+ logger.debug('[MongoDB] ==============================');
799
803
 
800
804
  } catch (error) {
801
- $.log.error('[MongoDB] 测试过程中出现错误:', error);
805
+ logger.error('[MongoDB] 测试过程中出现错误:', error);
802
806
  }
803
807
  }
804
808
 
805
809
  test().then(() => {
806
810
  // 测试成功完成
807
- $.log.debug('[MongoDB] 测试脚本正常退出');
811
+ logger.debug('[MongoDB] 测试脚本正常退出');
808
812
  process.exit(0);
809
813
  }).catch(err => {
810
- $.log.error('[MongoDB] 未捕获的错误:', err);
814
+ logger.error('[MongoDB] 未捕获的错误:', err);
811
815
  process.exit(1);
812
816
  });
813
817
 
814
818
  // 添加超时保护
815
819
  setTimeout(() => {
816
- $.log.warn('[MongoDB] 测试脚本超时');
820
+ logger.warn('[MongoDB] 测试脚本超时');
817
821
  process.exit(1);
818
822
  }, 30000); // 30秒超时(MongoDB 操作可能较慢)
package/test2.js CHANGED
@@ -1,9 +1,13 @@
1
- const { MongoDB, mongoDB_admin } = require('./index.js');
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
- $.log.debug("测试开始");
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
- $.log.debug("创建表bs_keyValue");
19
+ logger.debug("创建表bs_keyValue");
16
20
  } catch (e) {
17
- // $.log.debug("创建表错误提示:", e);
21
+ // logger.debug("创建表错误提示:", e);
18
22
  //TODO handle the exception
19
23
  }
20
24
  mb.table = "mm_table";
21
25
 
22
- $.log.debug("查询", mb.conn);
26
+ logger.debug("查询", mb.conn);
23
27
  // 直接使用find方法查询数据
24
28
  var ret = await mb.find({ name: '李四' });
25
- $.log.debug("查询李四", ret);
29
+ logger.debug("查询李四", ret);
26
30
 
27
31
  ret = await mb.find({ name: '张三' });
28
- $.log.debug("查询张三", ret);
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
- $.log.debug("添加一条数据", ret);
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
- $.log.debug("添加一条数据", ret);
52
+ logger.debug("添加一条数据", ret);
49
53
 
50
54
  // 查询
51
55
  ret = await mb.find({ name: "张三" });
52
- $.log.debug("查询张三", ret);
56
+ logger.debug("查询张三", ret);
53
57
 
54
58
  // 关闭连接
55
59
  await mb.close();
56
- $.log.debug("测试完成");
60
+ logger.debug("测试完成");
57
61
  }
58
62
  test();