mm_mongodb 2.0.3 → 2.0.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.
package/README.md CHANGED
@@ -33,6 +33,10 @@ npm install mm_mongodb
33
33
 
34
34
  ```javascript
35
35
  const { MongoDB, mongodbAdmin } = require('mm_mongodb');
36
+ const { getLogger } = require('mm_logger');
37
+
38
+ // 日志记录器实例
39
+ const logger = getLogger();
36
40
 
37
41
  // 通过连接池管理器获取实例(推荐)
38
42
  const cache = mongodbAdmin('myapp');
@@ -46,11 +50,11 @@ async function example() {
46
50
 
47
51
  // 获取缓存
48
52
  const value = await cache.get("name");
49
- $.log.debug(value); // 输出: 张三
53
+ logger.debug(value); // 输出: 张三
50
54
 
51
55
  // 删除缓存(返回删除数量)
52
56
  const deleted = await cache.del("name");
53
- $.log.debug(deleted); // 输出: 1
57
+ logger.debug(deleted); // 输出: 1
54
58
 
55
59
  // 关闭连接
56
60
  await cache.close();
@@ -305,24 +309,30 @@ try {
305
309
  事件名采用**冒号风格**,与 mm_redis / mm_cachebase 完全一致。每个操作有 `:before`、`:after`、`:error` 三种事件。
306
310
 
307
311
  ```javascript
312
+ const { mongodbAdmin } = require('mm_mongodb');
313
+ const { getLogger } = require('mm_logger');
314
+
315
+ // 日志记录器实例
316
+ const logger = getLogger();
317
+
308
318
  const cache = mongodbAdmin('myapp');
309
319
  await cache.open();
310
320
 
311
321
  // 监听 set 前置事件,可修改参数或取消操作
312
322
  cache.on('set:before', (ctx) => {
313
- $.log.debug('即将设置键:', ctx.key, '值:', ctx.value);
323
+ logger.debug('即将设置键:', ctx.key, '值:', ctx.value);
314
324
  // 可修改 ctx.key / ctx.value / ctx.seconds
315
325
  // 设置 ctx.cancel = true 可取消操作
316
326
  });
317
327
 
318
328
  // 监听 set 后置事件
319
329
  cache.on('set:after', (data) => {
320
- $.log.debug('设置完成:', data.key, '成功:', data.success);
330
+ logger.debug('设置完成:', data.key, '成功:', data.success);
321
331
  });
322
332
 
323
333
  // 监听错误事件
324
334
  cache.on('set:error', (data) => {
325
- $.log.error('设置出错:', data.key, data.error);
335
+ logger.error('设置出错:', data.key, data.error);
326
336
  });
327
337
  ```
328
338
 
@@ -387,7 +397,7 @@ async function zsetExample() {
387
397
  ```javascript
388
398
  async function pubsubExample() {
389
399
  await cache.subscribe('news', (message) => {
390
- $.log.debug('收到消息:', message);
400
+ logger.debug('收到消息:', message);
391
401
  });
392
402
  await cache.publish('news', 'hello world'); // 接收者数量: 1
393
403
  }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * mm_mongodb 最小可运行示例
3
+ * @description 演示 MongoDB 类的实例化、工厂获取实例与核心方法存在性。
4
+ * 注意:不会真正连接 MongoDB 服务;host/port/database 为示例假值,实际使用时请替换为真实配置。
5
+ */
6
+ const { MongoDB, mongodbAdmin } = require('../index.js');
7
+
8
+ /** 示例假配置(实际使用时替换为真实 MongoDB 连接信息) */
9
+ const CONFIG = {
10
+ host: 'localhost',
11
+ port: 27017,
12
+ user: '',
13
+ password: '',
14
+ database: 'example_db'
15
+ };
16
+
17
+ /**
18
+ * 主函数
19
+ */
20
+ function main() {
21
+ console.log('== mm_mongodb 示例开始 ==');
22
+
23
+ // 实例化(构造函数会做连接参数校验,但不会建立真实连接)
24
+ const db = new MongoDB(CONFIG);
25
+ console.log('实例化 MongoDB 成功');
26
+ console.log('配置校验通过:', typeof db.config.host === 'string' && Number.isInteger(db.config.port));
27
+
28
+ // 通过工厂函数获取按作用域缓存的实例
29
+ const cache = mongodbAdmin('example', CONFIG);
30
+ console.log('mongodbAdmin 获取实例:', cache instanceof MongoDB);
31
+
32
+ // 核心方法存在性检查(不真正执行,避免连接外部服务)
33
+ const METHODS = ['open', 'close', 'set', 'get', 'del', 'exists', 'keys', 'hset', 'hget',
34
+ 'mset', 'mget', 'incr', 'decr', 'sadd', 'smembers', 'zadd', 'zrange', 'subscribe', 'publish'];
35
+ const missing = METHODS.filter((name) => typeof cache[name] !== 'function');
36
+ console.log('核心方法检查:', missing.length === 0 ? '全部存在' : '缺失: ' + missing.join(','));
37
+
38
+ console.log('== mm_mongodb 示例结束 ==');
39
+ }
40
+
41
+ main();