mm_mongodb 1.4.6 → 1.4.8
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 +102 -3
- package/index.js +498 -205
- package/package.json +3 -4
- package/test.js +2 -2
- package/test2.js +20 -19
- package/test_events.js +139 -0
- package/test_redis_api.js +135 -0
- package/test_redis_compatibility.js +155 -123
- package/cache.js +0 -2866
- package/compatibility_table.md +0 -85
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ MongoDB数据库操作模块,提供了简洁易用的API、缓存功能以及*
|
|
|
13
13
|
- ✅ 环境变量配置支持
|
|
14
14
|
- ✅ 完善的错误处理和重试机制
|
|
15
15
|
- ✅ 查询注入防护
|
|
16
|
-
- ✅
|
|
16
|
+
- ✅ 事件系统:支持监听关键操作的事件,包括操作前、操作后和错误事件
|
|
17
17
|
|
|
18
18
|
## 安装
|
|
19
19
|
|
|
@@ -140,6 +140,84 @@ await db.createIndex('users', { name: 1 }, { unique: true });
|
|
|
140
140
|
await db.createTTLIndex('logs', { createdAt: 1 }, 3600); // 3600秒后过期
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
+
## 事件系统
|
|
144
|
+
|
|
145
|
+
mm_mongodb提供了完整的事件系统,允许您监听各种数据库操作的前后事件,便于进行日志记录、性能监控、数据校验等操作。
|
|
146
|
+
|
|
147
|
+
### 支持的事件
|
|
148
|
+
|
|
149
|
+
| 事件名称 | 触发时机 | 事件参数 |
|
|
150
|
+
|--------|--------|--------|
|
|
151
|
+
| `set_before` | 在执行set操作前 | `{ table, key, value }` |
|
|
152
|
+
| `set_after` | 在执行set操作后 | `{ table, key, value, result }` |
|
|
153
|
+
| `set_error` | 在set操作出错时 | `{ table, key, value, error }` |
|
|
154
|
+
| `del_before` | 在执行del操作前 | `{ table, key }` |
|
|
155
|
+
| `del_after` | 在执行del操作后 | `{ table, key, result }` |
|
|
156
|
+
| `del_error` | 在del操作出错时 | `{ table, key, error }` |
|
|
157
|
+
| `add_before` | 在执行add操作前 | `{ table, key, value }` |
|
|
158
|
+
| `add_after` | 在执行add操作后 | `{ table, key, value, result, exists }` |
|
|
159
|
+
| `add_error` | 在add操作出错时 | `{ table, key, value, error }` |
|
|
160
|
+
|
|
161
|
+
### 事件参数说明
|
|
162
|
+
|
|
163
|
+
- `table`: 操作的表名
|
|
164
|
+
- `key`: 操作的键名
|
|
165
|
+
- `value`: 操作的值
|
|
166
|
+
- `result`: 操作的结果
|
|
167
|
+
- `error`: 错误信息(仅在错误事件中)
|
|
168
|
+
- `exists`: 键是否已存在(仅在add_after事件中)
|
|
169
|
+
|
|
170
|
+
### 使用示例
|
|
171
|
+
|
|
172
|
+
```javascript
|
|
173
|
+
const { MongoDB } = require('mm_mongodb');
|
|
174
|
+
const db = new MongoDB();
|
|
175
|
+
|
|
176
|
+
// 连接数据库
|
|
177
|
+
await db.open();
|
|
178
|
+
|
|
179
|
+
// 监听set操作前事件
|
|
180
|
+
db.on('set_before', (data) => {
|
|
181
|
+
console.log(`准备设置缓存: ${data.key} = ${JSON.stringify(data.value)}`);
|
|
182
|
+
// 可以在这里进行数据校验、权限检查等
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// 监听set操作后事件
|
|
186
|
+
db.on('set_after', (data) => {
|
|
187
|
+
console.log(`缓存设置成功: ${data.key}, 结果:`, data.result);
|
|
188
|
+
// 可以在这里记录日志、触发其他操作等
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// 监听add操作后事件,特别注意exists标记
|
|
192
|
+
db.on('add_after', (data) => {
|
|
193
|
+
if (data.exists) {
|
|
194
|
+
console.log(`键 ${data.key} 已存在,未添加新值`);
|
|
195
|
+
} else {
|
|
196
|
+
console.log(`成功添加新键: ${data.key}`);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// 监听错误事件
|
|
201
|
+
db.on('set_error', (data) => {
|
|
202
|
+
console.error(`设置缓存失败: ${data.key}, 错误:`, data.error);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// 执行操作,触发事件
|
|
206
|
+
await db.set('test_key', 'test_value');
|
|
207
|
+
|
|
208
|
+
// 测试add方法的exists标记
|
|
209
|
+
await db.add('test_key', 'another_value'); // 键已存在,exists将为true
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### 应用场景
|
|
213
|
+
|
|
214
|
+
1. **数据校验和验证**:在操作前事件中验证数据的有效性
|
|
215
|
+
2. **访问控制和权限管理**:检查是否有权限执行特定操作
|
|
216
|
+
3. **性能监控**:记录操作执行时间,监控系统性能
|
|
217
|
+
4. **审计日志**:记录所有关键操作的详细信息
|
|
218
|
+
5. **缓存一致性维护**:在数据变更时更新相关缓存
|
|
219
|
+
6. **业务规则执行**:在特定操作前后执行业务逻辑
|
|
220
|
+
|
|
143
221
|
## Redis兼容API使用指南
|
|
144
222
|
|
|
145
223
|
mm_mongodb提供了**完整的Redis兼容API**,使您可以使用熟悉的Redis命令与MongoDB进行交互。这对于从Redis迁移到MongoDB或在两者间切换使用非常有用。
|
|
@@ -335,6 +413,17 @@ await cache.addInt('counter', -5); // 减少5
|
|
|
335
413
|
|
|
336
414
|
mm_mongodb的缓存模块实现了事件系统,可以监听各种操作的发生。增删改类方法会触发相应的事件,而查询类方法不触发事件以提高性能。
|
|
337
415
|
|
|
416
|
+
### 事件命名规范
|
|
417
|
+
|
|
418
|
+
所有事件严格遵循以下命名格式:
|
|
419
|
+
- 操作前事件:`action_before`
|
|
420
|
+
- 操作后事件:`action_after`
|
|
421
|
+
- 操作错误事件:`action_error`
|
|
422
|
+
|
|
423
|
+
其中`action`为方法名,例如`set`、`del`、`add`等。
|
|
424
|
+
|
|
425
|
+
### 事件示例
|
|
426
|
+
|
|
338
427
|
```javascript
|
|
339
428
|
// 监听事件
|
|
340
429
|
cache.on('set_before', (data) => {
|
|
@@ -352,7 +441,7 @@ cache.on('set_error', (data) => {
|
|
|
352
441
|
|
|
353
442
|
### 支持事件的方法
|
|
354
443
|
|
|
355
|
-
|
|
444
|
+
以下增删改类方法支持事件触发,每种方法都会触发对应的`action_before`、`action_after`和`action_error`事件:
|
|
356
445
|
|
|
357
446
|
- 原子操作:incr, decr, addInt, addFloat
|
|
358
447
|
- 字符串操作:append, addStr
|
|
@@ -360,7 +449,17 @@ cache.on('set_error', (data) => {
|
|
|
360
449
|
- 集合操作:sadd, saddMulti, srem, sremMulti
|
|
361
450
|
- 哈希操作:hset, hmset, hdel, hdelMulti
|
|
362
451
|
- 有序集合操作:zadd, zrem
|
|
363
|
-
- 键操作:set, del, expire, mset
|
|
452
|
+
- 键操作:set, del, expire, mset, add
|
|
453
|
+
|
|
454
|
+
### 事件参数
|
|
455
|
+
|
|
456
|
+
事件回调函数接收的参数通常包含以下信息:
|
|
457
|
+
- 操作涉及的键(key)
|
|
458
|
+
- 操作涉及的值(value)
|
|
459
|
+
- 操作结果(仅在after事件中)
|
|
460
|
+
- 错误信息(仅在error事件中)
|
|
461
|
+
|
|
462
|
+
具体参数结构取决于触发事件的方法类型。
|
|
364
463
|
|
|
365
464
|
## 性能优化建议
|
|
366
465
|
|