mm_mongodb 2.0.5 → 2.1.0
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/index.js +7 -7
- package/lib/tools/index.js +16 -0
- package/lib/tools/mongodb/index.js +75 -0
- package/package.json +51 -50
- package/tool/mongodb/index.js +7 -0
- package/tool/mongodb/tool.json +165 -0
package/index.js
CHANGED
|
@@ -3,22 +3,22 @@
|
|
|
3
3
|
* @description 参考 mm_redis 结构,index.js 为薄包装,类定义在 lib/mongodb.js
|
|
4
4
|
*/
|
|
5
5
|
const MongoDB = require('./lib/mongodb');
|
|
6
|
-
const
|
|
6
|
+
const { reg } = require('mm_admin');
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* MongoDB 管理器实例(Admin 工厂)
|
|
10
10
|
* 支持 os.js 调用:require('mm_mongodb').admin.setServer(os) -> admin.get('os', config)
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
const admin = reg('mm_mongodb');
|
|
13
13
|
admin.setClass(MongoDB);
|
|
14
14
|
|
|
15
15
|
module.exports = {
|
|
16
16
|
MongoDB,
|
|
17
17
|
admin,
|
|
18
18
|
mongodbAdmin(scope, config) {
|
|
19
|
-
|
|
20
|
-
scope = 'os';
|
|
21
|
-
}
|
|
22
|
-
return admin.get(scope, config);
|
|
19
|
+
return admin.get(scope || 'os', config);
|
|
23
20
|
}
|
|
24
|
-
};
|
|
21
|
+
};
|
|
22
|
+
/* mm-tool-logic-export 工具能力层:其它模块可 require("<pkg>").tools.<name> 直接引用 */
|
|
23
|
+
Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
|
|
24
|
+
get: function () { return require('./lib/tools'); } });
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
/** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
|
|
5
|
+
module.exports = new Proxy({}, {
|
|
6
|
+
get(_t, key) {
|
|
7
|
+
if (typeof key !== 'string') return undefined;
|
|
8
|
+
if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
|
|
9
|
+
return require('./' + key);
|
|
10
|
+
},
|
|
11
|
+
has(_t, key) {
|
|
12
|
+
return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
|
|
13
|
+
},
|
|
14
|
+
ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
|
|
15
|
+
getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
|
|
16
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MongoDB管理器
|
|
3
|
+
* 管理 MongoDB 连接和文档操作(基于 mm_mongodb)
|
|
4
|
+
*/
|
|
5
|
+
async function main(params, ctx, options) {
|
|
6
|
+
const { mongodbAdmin } = require('mm_mongodb');
|
|
7
|
+
var action = params.action || '';
|
|
8
|
+
var collection_name = params.collection || '';
|
|
9
|
+
var key = params.key || {};
|
|
10
|
+
var value = params.value || {};
|
|
11
|
+
var ttl = params.ttl || 0;
|
|
12
|
+
var field = params.field;
|
|
13
|
+
var body = params.body || {};
|
|
14
|
+
var options = params.options || {};
|
|
15
|
+
|
|
16
|
+
var opts = this.getOptions();
|
|
17
|
+
|
|
18
|
+
var db = mongodbAdmin('os', opts);
|
|
19
|
+
db.table = collection_name;
|
|
20
|
+
|
|
21
|
+
const SIMPLE = [
|
|
22
|
+
'get', 'set', 'del', 'has', 'exists', 'add', 'getset',
|
|
23
|
+
'expire', 'pexpire', 'persist', 'ttl',
|
|
24
|
+
'incr', 'decr', 'incrby', 'decrby', 'incrbyfloat',
|
|
25
|
+
'keys', 'clear', 'clearExpire', 'dbsize', 'flushdb', 'isConnected',
|
|
26
|
+
'mget', 'mset',
|
|
27
|
+
'strlen', 'append', 'setrange',
|
|
28
|
+
'hgetall', 'hkeys', 'hvals', 'hlen', 'hdel', 'hmset', 'hmget',
|
|
29
|
+
'setForList', 'addForList', 'getForList', 'clearForList', 'hasForList',
|
|
30
|
+
'sadd', 'srem', 'sismember', 'smembers', 'scard', 'sinter', 'sunion', 'sdiff',
|
|
31
|
+
'zcard', 'zrem', 'zcount', 'zrevrange',
|
|
32
|
+
'publish', 'unsubscribe'
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
const FIELD_METHODS = [
|
|
36
|
+
'hget', 'hset', 'hexists',
|
|
37
|
+
'zadd', 'zscore', 'zrank', 'zrevrank', 'zincrby'
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const MONGO_METHODS = [
|
|
41
|
+
'findOne', 'find', 'addObj', 'addList', 'delete', 'count',
|
|
42
|
+
'addTable', 'createIndex', 'createTTLIndex', 'getIndexes',
|
|
43
|
+
'dropIndex', 'dropTable'
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
if (SIMPLE.indexOf(action) !== -1) {
|
|
47
|
+
ctx.output = await db[action](key, value, ttl);
|
|
48
|
+
} else if (FIELD_METHODS.indexOf(action) !== -1) {
|
|
49
|
+
ctx.output = await db[action](key, field, value, ttl);
|
|
50
|
+
} else if (action === 'hsetObj') {
|
|
51
|
+
ctx.output = await db.hmset(key, body);
|
|
52
|
+
} else if (action === 'msetObj') {
|
|
53
|
+
ctx.output = await db.mset(body, ttl);
|
|
54
|
+
} else if (action === 'aggregate') {
|
|
55
|
+
await db._ensureConnected();
|
|
56
|
+
ctx.output = await db.db.collection(collection_name).aggregate(value).toArray();
|
|
57
|
+
} else if (action === 'setting') {
|
|
58
|
+
ctx.output = await db.setting(key, body, options);
|
|
59
|
+
} else if (MONGO_METHODS.indexOf(action) !== -1) {
|
|
60
|
+
ctx.output = await db[action](key, value);
|
|
61
|
+
} else if (action === 'runSql') {
|
|
62
|
+
// MongoDB 不支持执行 SQL,返回 NOT_SUPPORTED 保持接口一致
|
|
63
|
+
ctx.output = {
|
|
64
|
+
error: { code: 'NOT_SUPPORTED', message: 'MongoDB不支持执行SQL' }
|
|
65
|
+
};
|
|
66
|
+
} else {
|
|
67
|
+
ctx.output = {
|
|
68
|
+
error: { code: 'MGR_UNKNOWN_ACTION', message: '不支持的操作: ' + action }
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return ctx.output;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
module.exports = { main };
|
package/package.json
CHANGED
|
@@ -1,52 +1,53 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
2
|
+
"name": "mm_mongodb",
|
|
3
|
+
"version": "2.1.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
|
+
"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": "infrastructure",
|
|
36
|
+
"group": "persistence",
|
|
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_admin": "^1.1.0",
|
|
45
|
+
"mm_eslint": "^1.0.0",
|
|
46
|
+
"mm_logger": "^1.0.0",
|
|
47
|
+
"mm_mod": "^1.0.1",
|
|
48
|
+
"mongodb": "^7.6.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"mm_eslint": "^1.7.7"
|
|
52
|
+
}
|
|
52
53
|
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mongodb",
|
|
3
|
+
"handler": "manager",
|
|
4
|
+
"title": "MongoDB管理器",
|
|
5
|
+
"description": "管理 MongoDB,兼容 Redis 风格与原生 CRUD。适用场景:文档存储、复杂查询或索引时。工作流提示:action/collection 必填,Redis 风格用 key/value/field/ttl,原生操作配 findOne/find/addObj/delete 等,返回随操作而定。",
|
|
6
|
+
"type": "data",
|
|
7
|
+
"params": {
|
|
8
|
+
"type": "object",
|
|
9
|
+
"properties": {
|
|
10
|
+
"action": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"description": "操作类型",
|
|
13
|
+
"enum": [
|
|
14
|
+
"get",
|
|
15
|
+
"set",
|
|
16
|
+
"del",
|
|
17
|
+
"has",
|
|
18
|
+
"exists",
|
|
19
|
+
"add",
|
|
20
|
+
"getset",
|
|
21
|
+
"expire",
|
|
22
|
+
"pexpire",
|
|
23
|
+
"persist",
|
|
24
|
+
"ttl",
|
|
25
|
+
"incr",
|
|
26
|
+
"decr",
|
|
27
|
+
"incrby",
|
|
28
|
+
"decrby",
|
|
29
|
+
"incrbyfloat",
|
|
30
|
+
"keys",
|
|
31
|
+
"clear",
|
|
32
|
+
"clearExpire",
|
|
33
|
+
"dbsize",
|
|
34
|
+
"flushdb",
|
|
35
|
+
"isConnected",
|
|
36
|
+
"mget",
|
|
37
|
+
"mset",
|
|
38
|
+
"msetObj",
|
|
39
|
+
"strlen",
|
|
40
|
+
"append",
|
|
41
|
+
"setrange",
|
|
42
|
+
"hget",
|
|
43
|
+
"hset",
|
|
44
|
+
"hgetall",
|
|
45
|
+
"hkeys",
|
|
46
|
+
"hvals",
|
|
47
|
+
"hlen",
|
|
48
|
+
"hdel",
|
|
49
|
+
"hmset",
|
|
50
|
+
"hmget",
|
|
51
|
+
"hexists",
|
|
52
|
+
"hsetObj",
|
|
53
|
+
"setForList",
|
|
54
|
+
"addForList",
|
|
55
|
+
"getForList",
|
|
56
|
+
"clearForList",
|
|
57
|
+
"hasForList",
|
|
58
|
+
"sadd",
|
|
59
|
+
"srem",
|
|
60
|
+
"sismember",
|
|
61
|
+
"smembers",
|
|
62
|
+
"scard",
|
|
63
|
+
"sinter",
|
|
64
|
+
"sunion",
|
|
65
|
+
"sdiff",
|
|
66
|
+
"zadd",
|
|
67
|
+
"zrem",
|
|
68
|
+
"zscore",
|
|
69
|
+
"zrank",
|
|
70
|
+
"zrevrank",
|
|
71
|
+
"zincrby",
|
|
72
|
+
"zcard",
|
|
73
|
+
"zcount",
|
|
74
|
+
"zrevrange",
|
|
75
|
+
"publish",
|
|
76
|
+
"unsubscribe",
|
|
77
|
+
"aggregate",
|
|
78
|
+
"findOne",
|
|
79
|
+
"find",
|
|
80
|
+
"addObj",
|
|
81
|
+
"addList",
|
|
82
|
+
"delete",
|
|
83
|
+
"count",
|
|
84
|
+
"setting",
|
|
85
|
+
"addTable",
|
|
86
|
+
"createIndex",
|
|
87
|
+
"createTTLIndex",
|
|
88
|
+
"getIndexes",
|
|
89
|
+
"dropIndex",
|
|
90
|
+
"dropTable",
|
|
91
|
+
"runSql"
|
|
92
|
+
]
|
|
93
|
+
},
|
|
94
|
+
"collection": {
|
|
95
|
+
"type": "string",
|
|
96
|
+
"description": "集合名称"
|
|
97
|
+
},
|
|
98
|
+
"sql": {
|
|
99
|
+
"type": "string",
|
|
100
|
+
"description": "SQL脚本内容(runSql 操作需要,MongoDB不支持SQL,返回 NOT_SUPPORTED)"
|
|
101
|
+
},
|
|
102
|
+
"key": {
|
|
103
|
+
"description": "键/查询条件"
|
|
104
|
+
},
|
|
105
|
+
"value": {
|
|
106
|
+
"description": "值/增量值/成员等"
|
|
107
|
+
},
|
|
108
|
+
"ttl": {
|
|
109
|
+
"type": "number",
|
|
110
|
+
"description": "过期时间(秒)",
|
|
111
|
+
"min": 0
|
|
112
|
+
},
|
|
113
|
+
"field": {
|
|
114
|
+
"type": "string",
|
|
115
|
+
"description": "哈希字段名/有序集合成员名(哈希和有序集合操作)"
|
|
116
|
+
},
|
|
117
|
+
"body": {
|
|
118
|
+
"type": "object",
|
|
119
|
+
"description": "批量数据对象/修改数据(setting/hsetObj/msetObj 操作)"
|
|
120
|
+
},
|
|
121
|
+
"options": {
|
|
122
|
+
"type": "object",
|
|
123
|
+
"description": "查询选项(findOne/find/createIndex 等操作)"
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
"required": [
|
|
127
|
+
"action",
|
|
128
|
+
"collection"
|
|
129
|
+
]
|
|
130
|
+
},
|
|
131
|
+
"returns": {
|
|
132
|
+
"description": "管理操作结果,返回值类型根据操作而定",
|
|
133
|
+
"type": "object"
|
|
134
|
+
},
|
|
135
|
+
"options": {
|
|
136
|
+
"uri": {
|
|
137
|
+
"type": "string",
|
|
138
|
+
"description": "MongoDB连接URI",
|
|
139
|
+
"default": "mongodb://localhost:27017"
|
|
140
|
+
},
|
|
141
|
+
"database": {
|
|
142
|
+
"type": "string",
|
|
143
|
+
"description": "数据库名",
|
|
144
|
+
"default": "mm"
|
|
145
|
+
},
|
|
146
|
+
"user": {
|
|
147
|
+
"type": "string",
|
|
148
|
+
"description": "数据库用户名",
|
|
149
|
+
"default": "root"
|
|
150
|
+
},
|
|
151
|
+
"password": {
|
|
152
|
+
"type": "string",
|
|
153
|
+
"description": "数据库密码",
|
|
154
|
+
"default": "asd159357"
|
|
155
|
+
},
|
|
156
|
+
"pool_size": {
|
|
157
|
+
"type": "number",
|
|
158
|
+
"description": "连接池大小",
|
|
159
|
+
"default": 10,
|
|
160
|
+
"min": 1,
|
|
161
|
+
"max": 100
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
"group": "storage"
|
|
165
|
+
}
|