mm_dbs 1.0.2 → 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 CHANGED
@@ -1,3 +1,12 @@
1
- ## 数据库框架(mm_db)
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 Admin = require('mm_corejs').Admin;
2
+ const { reg } = require('mm_admin');
3
3
 
4
- const admin = new 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
- if (!scope) {
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'); } });
@@ -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.2",
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": "infra",
16
- "group": "data",
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
- "mm_corejs": "^1.1.4",
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,7 @@
1
+ 'use strict';
2
+ /**
3
+ * dbs —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/dbs/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_dbs').tools.dbs 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/dbs');
@@ -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
+ }