mm_dbs 1.0.1 → 1.0.3
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 +11 -2
- package/index.js +8 -7
- package/lib/db.js +22 -2
- package/lib/tools/dbs/index.js +59 -0
- package/lib/tools/index.js +16 -0
- package/package.json +4 -4
- package/tool/dbs/index.js +7 -0
- package/tool/dbs/tool.json +65 -0
package/README.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
| `dbs` | 数据库结构与建表模型 | 库清单、建表模型与索引 |## 数据库框架(mm_db)
|
|
2
2
|
用于mysql数据库生成表或生成sql模板,通过设计数据库就可以生成sql模板,或通过db.json配置文件就可以生成mysql数据表
|
|
3
|
-
可以便捷的实现数据库架构设计和相应业务的API
|
|
3
|
+
可以便捷的实现数据库架构设计和相应业务的API
|
|
4
|
+
|
|
5
|
+
## 工具契约(LLM 可调用)
|
|
6
|
+
|
|
7
|
+
本模块 `tool/` 下登记以下工具契约,实现位于 `lib/tools/<name>/`,`tool/<name>/index.js` 仅为薄适配层。
|
|
8
|
+
其它模块可直接 `require('<pkg>').tools.<name>` 复用实现,无需经过工具注册面。
|
|
9
|
+
|
|
10
|
+
| 工具名 | 标题 | 说明 |
|
|
11
|
+
| --- | --- | --- |
|
|
12
|
+
| `dbs` | 数据库设计登记 | 库清单、设计配置、登记与重载 |
|
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const Manager = require('./lib/manager');
|
|
2
|
-
const
|
|
2
|
+
const { reg } = require('mm_admin');
|
|
3
3
|
|
|
4
|
-
const admin =
|
|
4
|
+
const admin = reg('mm_dbs');
|
|
5
5
|
admin.setClass(Manager);
|
|
6
6
|
|
|
7
7
|
module.exports = {
|
|
@@ -16,9 +16,10 @@ module.exports = {
|
|
|
16
16
|
* @returns {Manager} 数据库管理器实例
|
|
17
17
|
*/
|
|
18
18
|
dbAdmin(scope, config = {}, os) {
|
|
19
|
-
|
|
20
|
-
scope = 'os';
|
|
21
|
-
}
|
|
22
|
-
return admin.get(scope, config, os);
|
|
19
|
+
return admin.get(scope, config, os);
|
|
23
20
|
}
|
|
24
|
-
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/* mm-tool-logic-export 工具能力层:其它模块可 require("mm_dbs").tools.<name> 直接引用 */
|
|
24
|
+
Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
|
|
25
|
+
get: function () { return require('./lib/tools'); } });
|
package/lib/db.js
CHANGED
|
@@ -5,7 +5,7 @@ const { string, array, object, file } = require('mm_helper');
|
|
|
5
5
|
|
|
6
6
|
/** 关键词搜索默认SQL模板(用户表关联查询) */
|
|
7
7
|
const KEYWORD_DEFAULT =
|
|
8
|
-
"`user_id` in (SELECT `user_id` FROM `
|
|
8
|
+
"`user_id` in (SELECT `user_id` FROM `sys_user` WHERE `nickname` LIKE '%{0}%' OR `phone` LIKE '%{0}%' OR `wallet_address` LIKE '%{0}%')";
|
|
9
9
|
|
|
10
10
|
/** 关键词搜索默认提示 */
|
|
11
11
|
const KEYWORD_DEFAULT_TIP = '昵称、手机号、钱包地址';
|
|
@@ -970,13 +970,33 @@ Db.prototype._getEventApi = function () {
|
|
|
970
970
|
if (typeof getter === 'function') {
|
|
971
971
|
return getter();
|
|
972
972
|
}
|
|
973
|
+
var admin = this._getHostApiAdmin();
|
|
974
|
+
if (admin) {
|
|
975
|
+
return admin;
|
|
976
|
+
}
|
|
973
977
|
if (!this._event_api_warned) {
|
|
974
978
|
this._event_api_warned = true;
|
|
975
|
-
this.log('warn', '未配置 get_event_api
|
|
979
|
+
this.log('warn', '未配置 get_event_api 且宿主API管理器不可用,API目录判断使用默认路径');
|
|
976
980
|
}
|
|
977
981
|
return null;
|
|
978
982
|
};
|
|
979
983
|
|
|
984
|
+
/**
|
|
985
|
+
* 获取宿主API管理器工厂(未注入 get_event_api 时的回退)
|
|
986
|
+
* @returns {object|null} 支持按作用域取管理器的工厂;宿主不可用时返回null
|
|
987
|
+
* @private
|
|
988
|
+
*/
|
|
989
|
+
Db.prototype._getHostApiAdmin = function () {
|
|
990
|
+
var manager = this.parent;
|
|
991
|
+
var server = manager && typeof manager.getServer === 'function' ? manager.getServer() : null;
|
|
992
|
+
var container = server && server.container;
|
|
993
|
+
if (!container) {
|
|
994
|
+
return null;
|
|
995
|
+
}
|
|
996
|
+
var factory = container.get('api_admin');
|
|
997
|
+
return factory && typeof factory.get === 'function' ? factory : null;
|
|
998
|
+
};
|
|
999
|
+
|
|
980
1000
|
/**
|
|
981
1001
|
* 处理配置文件
|
|
982
1002
|
* @param {string} f 文件路径
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* dbs —— 数据库结构与建表模型工具
|
|
4
|
+
* DbManager 由 dbAdmin 构造(要求 config.name),建表模型来自模块 Db 类。
|
|
5
|
+
*/
|
|
6
|
+
const { dbAdmin, Db } = require('../../../index.js');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 取管理器:DbManager 构造必须带 name,缺省用作用域名占位
|
|
10
|
+
* @param {string} scope 作用域
|
|
11
|
+
* @returns {object} 管理器
|
|
12
|
+
*/
|
|
13
|
+
function managerOf(scope) {
|
|
14
|
+
const s = scope || 'os';
|
|
15
|
+
return dbAdmin(s, { name: s, scope: s });
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function main(params, ctx, options) {
|
|
19
|
+
void options;
|
|
20
|
+
const p = params || {};
|
|
21
|
+
const action = p.action || 'list';
|
|
22
|
+
const write = (data) => { ctx.output = data; return data; };
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
const mgr = managerOf(p.scope);
|
|
26
|
+
|
|
27
|
+
if (action === 'list') {
|
|
28
|
+
const rows = typeof mgr.getList === 'function' ? mgr.getList() : [];
|
|
29
|
+
const list = rows.map((o) => ({
|
|
30
|
+
name: (o && o.name) || '',
|
|
31
|
+
table: (o && o.table) || '',
|
|
32
|
+
engine: (o && o.engine) || (o && o.type) || '',
|
|
33
|
+
fields: Array.isArray(o && o.fields) ? o.fields.length : 0
|
|
34
|
+
}));
|
|
35
|
+
return write({ code: 0, msg: list.length ? '成功' : '当前作用域没有已登记的库', count: list.length, list });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (action === 'schema') {
|
|
39
|
+
if (!p.config || typeof p.config !== 'object') throw new TypeError('schema 需要 config');
|
|
40
|
+
const inst = new Db(p.config);
|
|
41
|
+
const model = typeof inst.getModel === 'function' ? inst.getModel() : null;
|
|
42
|
+
if (!model) return write({ code: 10002, msg: '该库配置无法生成模型' });
|
|
43
|
+
return write({
|
|
44
|
+
code: 0,
|
|
45
|
+
msg: '成功',
|
|
46
|
+
table: model.table || '',
|
|
47
|
+
key: model.key || '',
|
|
48
|
+
schema: model,
|
|
49
|
+
index: Array.isArray(model.index) ? model.index : []
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return write({ code: 10001, msg: '不支持的操作: ' + action });
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return write({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = { main };
|
|
@@ -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
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_dbs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "数据库框架,通过设计数据库生成sql模板,或通过db.json配置文件生成mysql数据表(基于 mm_corejs,内置管理器池,无全局/原型扩展依赖)",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
"schema",
|
|
13
13
|
"数据管理器"
|
|
14
14
|
],
|
|
15
|
-
"layer": "
|
|
16
|
-
"group": "
|
|
15
|
+
"layer": "infrastructure",
|
|
16
|
+
"group": "persistence",
|
|
17
17
|
"author": "邱文武",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"repository": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"url": "git+https://gitee.com/qiuwenwu91/mm_dbs.git"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"
|
|
24
|
+
"mm_admin": "^1.1.0",
|
|
25
25
|
"mm_mod": "^1.0.1",
|
|
26
26
|
"mm_helper": "^1.0.0"
|
|
27
27
|
},
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dbs",
|
|
3
|
+
"title": "数据库结构与建表模型",
|
|
4
|
+
"description": "基于 mm_dbs 的库结构能力:列出已登记的库、按配置生成建表模型(表名/主键/字段/索引)。适用场景:核对某库的表结构定义、确认主键与索引口径、建表前检查字段配置。工作流提示:schema 传 config(库配置对象);管理器的 getInfos 不可用,清单走 getList。",
|
|
5
|
+
"category": "数据库结构",
|
|
6
|
+
"group": "store",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"properties": {
|
|
12
|
+
"action": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "操作类型: list(登记清单)/schema(建表模型)",
|
|
15
|
+
"enum": [
|
|
16
|
+
"list",
|
|
17
|
+
"schema"
|
|
18
|
+
],
|
|
19
|
+
"default": "list"
|
|
20
|
+
},
|
|
21
|
+
"scope": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"description": "作用域,缺省 os",
|
|
24
|
+
"default": "os"
|
|
25
|
+
},
|
|
26
|
+
"config": {
|
|
27
|
+
"type": "object",
|
|
28
|
+
"description": "库配置(schema 必填)"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"required": [
|
|
32
|
+
"action"
|
|
33
|
+
]
|
|
34
|
+
},
|
|
35
|
+
"returns": {
|
|
36
|
+
"type": "object",
|
|
37
|
+
"properties": {
|
|
38
|
+
"code": {
|
|
39
|
+
"type": "number",
|
|
40
|
+
"description": "0 表示成功,1xxxx 表示业务/执行异常"
|
|
41
|
+
},
|
|
42
|
+
"msg": {
|
|
43
|
+
"type": "string",
|
|
44
|
+
"description": "结果描述"
|
|
45
|
+
},
|
|
46
|
+
"list": {
|
|
47
|
+
"type": "array",
|
|
48
|
+
"description": "登记清单(list)"
|
|
49
|
+
},
|
|
50
|
+
"schema": {
|
|
51
|
+
"type": "object",
|
|
52
|
+
"description": "建表模型(schema)"
|
|
53
|
+
},
|
|
54
|
+
"index": {
|
|
55
|
+
"type": "array",
|
|
56
|
+
"description": "索引定义(schema)"
|
|
57
|
+
},
|
|
58
|
+
"error": {
|
|
59
|
+
"type": "object",
|
|
60
|
+
"description": "异常对象 { name, message }"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"handler": "manager"
|
|
65
|
+
}
|