mm_dao 1.9.0 → 1.9.1
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 +4 -0
- package/lib/manager.js +39 -0
- package/lib/tools/dao/index.js +73 -0
- package/lib/tools/index.js +16 -0
- package/package.json +63 -63
- package/tool/dao/index.js +7 -0
- package/tool/dao/tool.json +70 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
| `dao` | DAO 登记与表结构 | 登记清单、表/字段模型、已装载驱动 |# mm_sql
|
|
2
2
|
|
|
3
3
|
[中文](./README.md) | [English](./README_EN.md)
|
|
4
4
|
|
|
@@ -543,4 +543,13 @@ const account = await dao.account('player1');
|
|
|
543
543
|
|
|
544
544
|
## 许可证
|
|
545
545
|
|
|
546
|
-
MIT
|
|
546
|
+
MIT
|
|
547
|
+
|
|
548
|
+
## 工具契约(LLM 可调用)
|
|
549
|
+
|
|
550
|
+
本模块 `tool/` 下登记以下工具契约,实现位于 `lib/tools/<name>/`,`tool/<name>/index.js` 仅为薄适配层。
|
|
551
|
+
其它模块可直接 `require('<pkg>').tools.<name>` 复用实现,无需经过工具注册面。
|
|
552
|
+
|
|
553
|
+
| 工具名 | 标题 | 说明 |
|
|
554
|
+
| --- | --- | --- |
|
|
555
|
+
| `dao` | SQL 模板与 DAO 登记 | 模板清单、登记、重载、执行 |
|
package/index.js
CHANGED
|
@@ -24,3 +24,7 @@ module.exports = {
|
|
|
24
24
|
return admin.get(scope || 'os', config);
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
|
+
|
|
28
|
+
/* mm-tool-logic-export 工具能力层:其它模块可 require("mm_dao").tools.<name> 直接引用 */
|
|
29
|
+
Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
|
|
30
|
+
get: function () { return require('./lib/tools'); } });
|
package/lib/manager.js
CHANGED
|
@@ -2,6 +2,7 @@ const path = require("path");
|
|
|
2
2
|
const Manager = require('mm_machine').Manager;
|
|
3
3
|
const Dao = require("./dao");
|
|
4
4
|
const { array } = require("mm_helper");
|
|
5
|
+
const { getSql, sqlAdmin } = require('mm_sql');
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Sql模板管理器类(融合自 mm_os com/sql)
|
|
@@ -62,6 +63,8 @@ class DaoManager extends Manager {
|
|
|
62
63
|
*/
|
|
63
64
|
constructor(config, parent) {
|
|
64
65
|
super({ ...DaoManager.config, ...config }, parent);
|
|
66
|
+
// 管理器级 Sql 实例(惰性创建,见 _getSql;仅 1.8.x 兼容入口 db()/load() 使用)
|
|
67
|
+
this._sql = null;
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
|
|
@@ -202,4 +205,40 @@ DaoManager.prototype.run = async function (name, query, body, db) {
|
|
|
202
205
|
return null;
|
|
203
206
|
};
|
|
204
207
|
|
|
208
|
+
/**
|
|
209
|
+
* 取本作用域的 Sql 实例(与驱动 Dao._getSql 同一套规则)
|
|
210
|
+
* 有 config.sql 时按该配置取得/创建同名 Sql 实例,否则复用主库 os,
|
|
211
|
+
* 保证「管理器级入口」与「驱动级入口」指向同一个数据源。
|
|
212
|
+
* @returns {object} Sql 实例
|
|
213
|
+
* @private
|
|
214
|
+
*/
|
|
215
|
+
DaoManager.prototype._getSql = function () {
|
|
216
|
+
if (!this._sql) {
|
|
217
|
+
var name = this.config.name || 'os';
|
|
218
|
+
var sql_config = this.config.sql;
|
|
219
|
+
this._sql = sql_config ? sqlAdmin(name, sql_config) : getSql();
|
|
220
|
+
}
|
|
221
|
+
return this._sql;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 获取数据库管理器(1.8.x 兼容入口)
|
|
226
|
+
* 1.9.0 将 db() 下沉到驱动(dao.db()),但 mm_api 与既有应用仍按管理器级入口调用,
|
|
227
|
+
* 缺失时直接抛 TypeError,故障面覆盖全部登录态接口(getLoginState → _getMainDb)。
|
|
228
|
+
* 此处按 1.8.x 语义补齐:返回本作用域的数据库管理器,不改变驱动级新入口的行为。
|
|
229
|
+
* @returns {object} 数据库管理器实例
|
|
230
|
+
*/
|
|
231
|
+
DaoManager.prototype.db = function () {
|
|
232
|
+
return this._getSql().db();
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 读取并执行 .sql 脚本(1.8.x 兼容入口,README 仍按此方式示例)
|
|
237
|
+
* @param {string} file SQL 文件路径
|
|
238
|
+
* @returns {Promise<object>} 执行结果统计对象
|
|
239
|
+
*/
|
|
240
|
+
DaoManager.prototype.load = function (file) {
|
|
241
|
+
return this._getSql().load(file);
|
|
242
|
+
};
|
|
243
|
+
|
|
205
244
|
module.exports = DaoManager;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* dao —— DAO 登记与表结构工具
|
|
4
|
+
* 清单来自管理器 getInfos,表结构来自模块 Dao 类的 getModel,能力层只做投影。
|
|
5
|
+
*/
|
|
6
|
+
const { daoAdmin, Dao } = require('../../../index.js');
|
|
7
|
+
|
|
8
|
+
/** 登记项投影字段:DAO 关注表名与脚本目录 */
|
|
9
|
+
const FIELDS = ['name', 'title', 'table', 'dir', 'desc'];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 投影登记项
|
|
13
|
+
* @param {object} cfg 登记配置
|
|
14
|
+
* @returns {object} 投影
|
|
15
|
+
*/
|
|
16
|
+
function project(cfg) {
|
|
17
|
+
const out = {};
|
|
18
|
+
for (const k of FIELDS) out[k] = cfg[k] === undefined ? '' : cfg[k];
|
|
19
|
+
out.fields = Array.isArray(cfg.fields) ? cfg.fields.length : 0;
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 取实例配置:优先显式 config,其次按 name 取登记项
|
|
25
|
+
* @param {object} mgr 管理器
|
|
26
|
+
* @param {object} p 参数
|
|
27
|
+
* @returns {object} 配置
|
|
28
|
+
*/
|
|
29
|
+
function resolveConfig(mgr, p) {
|
|
30
|
+
if (p.config && typeof p.config === 'object') return p.config;
|
|
31
|
+
if (p.name) {
|
|
32
|
+
const cfg = typeof mgr.getInfo === 'function' ? mgr.getInfo(p.name) : null;
|
|
33
|
+
if (cfg) return cfg;
|
|
34
|
+
}
|
|
35
|
+
throw new TypeError('schema 需要 config 或已登记的 name');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function main(params, ctx, options) {
|
|
39
|
+
void options;
|
|
40
|
+
const p = params || {};
|
|
41
|
+
const action = p.action || 'list';
|
|
42
|
+
const write = (data) => { ctx.output = data; return data; };
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const mgr = daoAdmin(p.scope || 'os');
|
|
46
|
+
|
|
47
|
+
if (action === 'list') {
|
|
48
|
+
const rows = typeof mgr.getInfos === 'function' ? mgr.getInfos() : [];
|
|
49
|
+
const list = rows.map(project);
|
|
50
|
+
return write({ code: 0, msg: list.length ? '成功' : '当前作用域没有已登记的 DAO', count: list.length, list });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (action === 'schema') {
|
|
54
|
+
const cfg = resolveConfig(mgr, p);
|
|
55
|
+
const inst = new Dao(cfg);
|
|
56
|
+
const model = typeof inst.getModel === 'function' ? inst.getModel() : null;
|
|
57
|
+
if (!model) return write({ code: 10002, msg: '该 DAO 配置无法生成模型', name: cfg.name || '' });
|
|
58
|
+
return write({ code: 0, msg: '成功', name: cfg.name || '', table: model.table || '', schema: model });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (action === 'mods') {
|
|
62
|
+
const mods = typeof mgr.getMods === 'function' ? mgr.getMods() : {};
|
|
63
|
+
const keys = Array.isArray(mods) ? mods.map((o) => (o && (o.name || o.config && o.config.name)) || '') : Object.keys(mods || {});
|
|
64
|
+
return write({ code: 0, msg: '成功', count: keys.length, mods: keys.filter(Boolean) });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return write({ code: 10001, msg: '不支持的操作: ' + action });
|
|
68
|
+
} catch (error) {
|
|
69
|
+
return write({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
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,65 +1,65 @@
|
|
|
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
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
2
|
+
"name": "mm_dao",
|
|
3
|
+
"version": "1.9.1",
|
|
4
|
+
"description": "一个通用的SQL帮助类,支持通过切换db_type实现对不同数据库的操作,包括MySQL和SQLite等;另提供 manager.js/drive.js 的SQL模板管理器(融合自 mm_os com/sql,基于 mm_corejs,内置管理器池,无全局/原型扩展依赖)",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "node tests/test.js",
|
|
8
|
+
"test": "node tests/test.js && node --test tests/scope.test.js",
|
|
9
|
+
"lint": "eslint . --config eslint.config.js",
|
|
10
|
+
"lint:fix": "eslint . --config eslint.config.js --fix",
|
|
11
|
+
"lint:check": "eslint . --config eslint.config.js --max-warnings 0"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_dao.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"mm_mod",
|
|
19
|
+
"mm_infra",
|
|
20
|
+
"sql",
|
|
21
|
+
"mysql",
|
|
22
|
+
"sql server",
|
|
23
|
+
"sqlite",
|
|
24
|
+
"database",
|
|
25
|
+
"async",
|
|
26
|
+
"await",
|
|
27
|
+
"promise",
|
|
28
|
+
"add",
|
|
29
|
+
"del",
|
|
30
|
+
"set",
|
|
31
|
+
"get",
|
|
32
|
+
"query",
|
|
33
|
+
"run",
|
|
34
|
+
"exec",
|
|
35
|
+
"orm",
|
|
36
|
+
"多数据库",
|
|
37
|
+
"database-adapter"
|
|
38
|
+
],
|
|
39
|
+
"layer": "infrastructure",
|
|
40
|
+
"group": "persistence",
|
|
41
|
+
"author": "邱文武",
|
|
42
|
+
"license": "ISC",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://gitee.com/qiuwenwu91/mm_dao/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://gitee.com/qiuwenwu91/mm_dao#readme",
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=12.0.0"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"mm_admin": "^1.1.0",
|
|
52
|
+
"mm_dbs": "^1.0.1",
|
|
53
|
+
"mm_eslint": "^1.0.0",
|
|
54
|
+
"mm_excel": "^1.2.4",
|
|
55
|
+
"mm_helper": "^1.0.0",
|
|
56
|
+
"mm_machine": "^6.0.0",
|
|
57
|
+
"mm_ret": "^1.4.8",
|
|
58
|
+
"mm_sql": "^1.8.0"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"eslint": "^10.9.1",
|
|
62
|
+
"eslint-plugin-jsdoc": "^64.2.1",
|
|
63
|
+
"mm_eslint": "^1.7.7"
|
|
64
|
+
}
|
|
65
65
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dao",
|
|
3
|
+
"title": "DAO 登记与表结构",
|
|
4
|
+
"description": "基于 mm_dao 的数据访问对象能力:列出已登记 DAO、取某个 DAO 的表与字段模型、查看管理器已装载的驱动键。适用场景:确认某张表由哪个 DAO 负责、按配置核对表字段与主键、排查 DAO 是否已装载。工作流提示:schema 传 config(DAO 配置对象)或已登记项的 name;只读自省,不写库。",
|
|
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(表与字段模型)/mods(已装载驱动)",
|
|
15
|
+
"enum": [
|
|
16
|
+
"list",
|
|
17
|
+
"schema",
|
|
18
|
+
"mods"
|
|
19
|
+
],
|
|
20
|
+
"default": "list"
|
|
21
|
+
},
|
|
22
|
+
"scope": {
|
|
23
|
+
"type": "string",
|
|
24
|
+
"description": "作用域,缺省 os",
|
|
25
|
+
"default": "os"
|
|
26
|
+
},
|
|
27
|
+
"name": {
|
|
28
|
+
"type": "string",
|
|
29
|
+
"description": "已登记的 DAO 名(schema 可选)"
|
|
30
|
+
},
|
|
31
|
+
"config": {
|
|
32
|
+
"type": "object",
|
|
33
|
+
"description": "DAO 配置(schema 可选,与 name 二选一)"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"required": [
|
|
37
|
+
"action"
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
"returns": {
|
|
41
|
+
"type": "object",
|
|
42
|
+
"properties": {
|
|
43
|
+
"code": {
|
|
44
|
+
"type": "number",
|
|
45
|
+
"description": "0 表示成功,1xxxx 表示业务/执行异常"
|
|
46
|
+
},
|
|
47
|
+
"msg": {
|
|
48
|
+
"type": "string",
|
|
49
|
+
"description": "结果描述"
|
|
50
|
+
},
|
|
51
|
+
"list": {
|
|
52
|
+
"type": "array",
|
|
53
|
+
"description": "登记清单(list)"
|
|
54
|
+
},
|
|
55
|
+
"schema": {
|
|
56
|
+
"type": "object",
|
|
57
|
+
"description": "表/字段模型(schema)"
|
|
58
|
+
},
|
|
59
|
+
"mods": {
|
|
60
|
+
"type": "array",
|
|
61
|
+
"description": "已装载驱动键(mode)"
|
|
62
|
+
},
|
|
63
|
+
"error": {
|
|
64
|
+
"type": "object",
|
|
65
|
+
"description": "异常对象 { name, message }"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"handler": "manager"
|
|
70
|
+
}
|