mm_dao 1.8.9 → 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 +11 -45
- package/lib/dao.js +45 -1
- package/lib/manager.js +20 -19
- package/lib/tools/dao/index.js +73 -0
- package/lib/tools/index.js +16 -0
- package/package.json +63 -63
- package/tests/main.test.js +3 -1
- package/tests/query.test.js +3 -1
- package/tests/scope.test.js +9 -28
- 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
|
@@ -1,29 +1,11 @@
|
|
|
1
1
|
const Dao = require('./lib/dao');
|
|
2
2
|
const Manager = require('./lib/manager');
|
|
3
3
|
const { Sql } = require('mm_sql');
|
|
4
|
-
const
|
|
4
|
+
const { reg } = require('mm_admin');
|
|
5
5
|
|
|
6
|
-
const admin =
|
|
6
|
+
const admin = reg('mm_dao');
|
|
7
7
|
admin.setClass(Manager);
|
|
8
8
|
|
|
9
|
-
/**
|
|
10
|
-
* 数据域配置模板:config.sql 中含 {org_id} 占位符时只登记、不实例化,
|
|
11
|
-
* 供 daoAdmin(域, { org_id }) 按机构派生独立库
|
|
12
|
-
* @type {Object}
|
|
13
|
-
*/
|
|
14
|
-
const templates = {};
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 按机构生成数据源配置(替换 {org_id} 占位符,深拷贝避免污染模板)
|
|
18
|
-
* @param {object} sql_config 数据源配置模板
|
|
19
|
-
* @param {number} org_id 机构ID
|
|
20
|
-
* @returns {object} 该机构的数据源配置
|
|
21
|
-
*/
|
|
22
|
-
function bindOrg(sql_config, org_id) {
|
|
23
|
-
const cfg = JSON.parse(JSON.stringify(sql_config));
|
|
24
|
-
return JSON.parse(JSON.stringify(cfg).replace(/\{org_id\}/g, String(org_id)));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
9
|
module.exports = {
|
|
28
10
|
Sql,
|
|
29
11
|
Dao,
|
|
@@ -32,33 +14,17 @@ module.exports = {
|
|
|
32
14
|
/**
|
|
33
15
|
* 获取指定作用域的 DAO 管理器
|
|
34
16
|
* 作用域即数据域:os(系统)、user(用户)、org(机构)、project(项目)、client(客户端)、app(应用)。
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* 按机构分库:数据源配置里用 {org_id} 占位时,该域只登记模板;
|
|
38
|
-
* 用 daoAdmin('org', { org_id: 3 }) 取用时,派生实例 org_3,库名按模板替换为 org_3.db。
|
|
17
|
+
* 同一作用域复用同一实例;数据源由驱动(lib/dao.js)按配置决定:
|
|
18
|
+
* config.sql 指定时该作用域绑定独立数据源,未配置时复用主库。
|
|
39
19
|
* @param {string} [scope='os'] 作用域名称
|
|
40
|
-
* @param {object} [config] 配置参数,config.sql
|
|
41
|
-
* @returns {object
|
|
20
|
+
* @param {object} [config] 配置参数,config.sql 指定数据源
|
|
21
|
+
* @returns {object} DAO 管理器实例
|
|
42
22
|
*/
|
|
43
23
|
daoAdmin(scope, config) {
|
|
44
|
-
|
|
45
|
-
scope = 'os';
|
|
46
|
-
}
|
|
47
|
-
// 1) 登记模板:含 {org_id} 的数据源不建固定实例,等按机构取用时再派生
|
|
48
|
-
if (config && config.sql && /\{org_id\}/.test(JSON.stringify(config.sql))) {
|
|
49
|
-
templates[scope] = config.sql;
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
// 2) 按机构派生:daoAdmin('org', { org_id: 3 }) → 实例名 org_3,库按模板替换
|
|
53
|
-
if (config && config.org_id) {
|
|
54
|
-
const org_id = parseInt(config.org_id) || 0;
|
|
55
|
-
const name = scope + '_' + org_id;
|
|
56
|
-
const tpl = config.sql || templates[scope];
|
|
57
|
-
const next = Object.assign({}, config);
|
|
58
|
-
delete next.org_id;
|
|
59
|
-
next.sql = tpl ? bindOrg(tpl, org_id) : undefined;
|
|
60
|
-
return admin.get(name, next);
|
|
61
|
-
}
|
|
62
|
-
return admin.get(scope, config);
|
|
24
|
+
return admin.get(scope || 'os', config);
|
|
63
25
|
}
|
|
64
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/dao.js
CHANGED
|
@@ -2,6 +2,7 @@ const path = require('path');
|
|
|
2
2
|
const Item = require('mm_machine').Mod;
|
|
3
3
|
const { string, array, file } = require('mm_helper');
|
|
4
4
|
const { getRet } = require('mm_ret');
|
|
5
|
+
const { getSql, sqlAdmin } = require('mm_sql');
|
|
5
6
|
const dbAdmin = require('mm_dbs');
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -138,6 +139,10 @@ class Dao extends Item {
|
|
|
138
139
|
log: false,
|
|
139
140
|
// 驱动目录(方案B:手动配置,替代目录扫描)
|
|
140
141
|
dir: '',
|
|
142
|
+
// 作用域名称(数据源实例名,由管理器注入,如 os / user / org)
|
|
143
|
+
scope: 'os',
|
|
144
|
+
// 数据源配置(由管理器注入;配置后本驱动按作用域绑定独立数据源,未配置时复用主库)
|
|
145
|
+
sql: null,
|
|
141
146
|
};
|
|
142
147
|
|
|
143
148
|
/**
|
|
@@ -152,6 +157,9 @@ class Dao extends Item {
|
|
|
152
157
|
// 父级管理器引用(用于默认配置回退与插件查找)
|
|
153
158
|
this.parent = parent || null;
|
|
154
159
|
|
|
160
|
+
// 本驱动绑定的 Sql 实例(惰性创建,见 _getSql)
|
|
161
|
+
this._sql = null;
|
|
162
|
+
|
|
155
163
|
// 初始化运行时数据结构
|
|
156
164
|
this._preset();
|
|
157
165
|
}
|
|
@@ -241,14 +249,50 @@ Dao.prototype.after = async function (params, db) {
|
|
|
241
249
|
*/
|
|
242
250
|
Dao.prototype.updateCache = async function (table, sql) {};
|
|
243
251
|
|
|
252
|
+
/**
|
|
253
|
+
* 获取本驱动绑定的 Sql 实例(数据源由本驱动配置决定)
|
|
254
|
+
* 作用域即数据域:os / user / org / project / client / app 各自可独立配置数据库;
|
|
255
|
+
* 未单独配置 sql 时复用进程级主库,便于同库分域与后续平滑拆库。
|
|
256
|
+
* @returns {object} Sql 实例
|
|
257
|
+
* @private
|
|
258
|
+
*/
|
|
259
|
+
Dao.prototype._getSql = function () {
|
|
260
|
+
if (!this._sql) {
|
|
261
|
+
var name = this.config.scope || 'os';
|
|
262
|
+
var sql_config = this.config.sql;
|
|
263
|
+
this._sql = sql_config ? sqlAdmin(name, sql_config) : getSql();
|
|
264
|
+
}
|
|
265
|
+
return this._sql;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 获取数据库管理器(委托本驱动绑定的 Sql 实例)
|
|
270
|
+
* @returns {object} 数据库管理器实例
|
|
271
|
+
*/
|
|
272
|
+
Dao.prototype.db = function () {
|
|
273
|
+
return this._getSql().db();
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* 读取并执行 .sql 文件脚本(委托本驱动绑定的 Sql 实例)
|
|
278
|
+
* @param {string} fpath SQL 文件路径
|
|
279
|
+
* @returns {Promise<object>} 执行结果统计对象
|
|
280
|
+
*/
|
|
281
|
+
Dao.prototype.load = function (fpath) {
|
|
282
|
+
return this._getSql().load(fpath);
|
|
283
|
+
};
|
|
284
|
+
|
|
244
285
|
/**
|
|
245
286
|
* @ 执行修改
|
|
246
287
|
* @param {object} query 查询url参数
|
|
247
288
|
* @param {object} body 修改时置入body的参数
|
|
248
|
-
* @param {object} db
|
|
289
|
+
* @param {object} [db] 数据库管理器,不传时由本驱动按自身配置选择数据源
|
|
249
290
|
* @returns {object} 返回执行结果
|
|
250
291
|
*/
|
|
251
292
|
Dao.prototype.run = async function (query, body, db) {
|
|
293
|
+
if (!db) {
|
|
294
|
+
db = this.db();
|
|
295
|
+
}
|
|
252
296
|
var params = {
|
|
253
297
|
query,
|
|
254
298
|
body,
|
package/lib/manager.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const path = require("path");
|
|
2
2
|
const Manager = require('mm_machine').Manager;
|
|
3
3
|
const Dao = require("./dao");
|
|
4
|
-
const { getSql, sqlAdmin } = require("mm_sql");
|
|
5
4
|
const { array } = require("mm_helper");
|
|
5
|
+
const { getSql, sqlAdmin } = require('mm_sql');
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Sql模板管理器类(融合自 mm_os com/sql)
|
|
@@ -63,7 +63,7 @@ class DaoManager extends Manager {
|
|
|
63
63
|
*/
|
|
64
64
|
constructor(config, parent) {
|
|
65
65
|
super({ ...DaoManager.config, ...config }, parent);
|
|
66
|
-
//
|
|
66
|
+
// 管理器级 Sql 实例(惰性创建,见 _getSql;仅 1.8.x 兼容入口 db()/load() 使用)
|
|
67
67
|
this._sql = null;
|
|
68
68
|
}
|
|
69
69
|
}
|
|
@@ -113,6 +113,9 @@ DaoManager.prototype.add = function (config) {
|
|
|
113
113
|
user_path: this.config.user_path,
|
|
114
114
|
static_path: this.config.static_path,
|
|
115
115
|
run_path: this.config.run_path,
|
|
116
|
+
// 作用域与数据源配置注入驱动,由驱动决定使用哪个数据库(见 dao._getSql)
|
|
117
|
+
scope: this.config.name,
|
|
118
|
+
sql: this.config.sql,
|
|
116
119
|
...config,
|
|
117
120
|
},
|
|
118
121
|
this,
|
|
@@ -187,44 +190,42 @@ DaoManager.prototype.sort = function () {
|
|
|
187
190
|
|
|
188
191
|
/**
|
|
189
192
|
* 执行sql
|
|
193
|
+
* 数据源由驱动按自身配置决定(见 dao._getSql),本管理器不介入数据库选择
|
|
190
194
|
* @param {string} name 名称
|
|
191
195
|
* @param {object} query 查询参数
|
|
192
196
|
* @param {object} body 修改参数
|
|
193
|
-
* @param {object} [db]
|
|
197
|
+
* @param {object} [db] 数据库管理器,不传时由驱动按自身配置选择数据源
|
|
194
198
|
* @returns {object} 执行结果
|
|
195
199
|
*/
|
|
196
200
|
DaoManager.prototype.run = async function (name, query, body, db) {
|
|
197
201
|
var obj = this.get(name);
|
|
198
202
|
if (obj) {
|
|
199
|
-
return await obj.run(query, body, db
|
|
203
|
+
return await obj.run(query, body, db);
|
|
200
204
|
}
|
|
201
205
|
return null;
|
|
202
206
|
};
|
|
203
207
|
|
|
204
208
|
/**
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
* @returns {
|
|
209
|
+
* 取本作用域的 Sql 实例(与驱动 Dao._getSql 同一套规则)
|
|
210
|
+
* 有 config.sql 时按该配置取得/创建同名 Sql 实例,否则复用主库 os,
|
|
211
|
+
* 保证「管理器级入口」与「驱动级入口」指向同一个数据源。
|
|
212
|
+
* @returns {object} Sql 实例
|
|
209
213
|
* @private
|
|
210
214
|
*/
|
|
211
215
|
DaoManager.prototype._getSql = function () {
|
|
212
216
|
if (!this._sql) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
// 该作用域显式配置了数据源:按名取独立实例
|
|
217
|
-
this._sql = sqlAdmin(name, sql_config);
|
|
218
|
-
} else {
|
|
219
|
-
// 未单独配置:复用主库,避免出现未选库的空连接
|
|
220
|
-
this._sql = getSql();
|
|
221
|
-
}
|
|
217
|
+
var name = this.config.name || 'os';
|
|
218
|
+
var sql_config = this.config.sql;
|
|
219
|
+
this._sql = sql_config ? sqlAdmin(name, sql_config) : getSql();
|
|
222
220
|
}
|
|
223
221
|
return this._sql;
|
|
224
222
|
};
|
|
225
223
|
|
|
226
224
|
/**
|
|
227
|
-
*
|
|
225
|
+
* 获取数据库管理器(1.8.x 兼容入口)
|
|
226
|
+
* 1.9.0 将 db() 下沉到驱动(dao.db()),但 mm_api 与既有应用仍按管理器级入口调用,
|
|
227
|
+
* 缺失时直接抛 TypeError,故障面覆盖全部登录态接口(getLoginState → _getMainDb)。
|
|
228
|
+
* 此处按 1.8.x 语义补齐:返回本作用域的数据库管理器,不改变驱动级新入口的行为。
|
|
228
229
|
* @returns {object} 数据库管理器实例
|
|
229
230
|
*/
|
|
230
231
|
DaoManager.prototype.db = function () {
|
|
@@ -232,7 +233,7 @@ DaoManager.prototype.db = function () {
|
|
|
232
233
|
};
|
|
233
234
|
|
|
234
235
|
/**
|
|
235
|
-
* 读取并执行 .sql
|
|
236
|
+
* 读取并执行 .sql 脚本(1.8.x 兼容入口,README 仍按此方式示例)
|
|
236
237
|
* @param {string} file SQL 文件路径
|
|
237
238
|
* @returns {Promise<object>} 执行结果统计对象
|
|
238
239
|
*/
|
|
@@ -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
|
}
|
package/tests/main.test.js
CHANGED
|
@@ -25,7 +25,9 @@ function makeTable() {
|
|
|
25
25
|
daoAdmin('main_test', {
|
|
26
26
|
sql: { way: 'sqlite', sqlite: { dir: db_dir, database: 'main_test' } }
|
|
27
27
|
});
|
|
28
|
-
|
|
28
|
+
const manager = daoAdmin('main_test');
|
|
29
|
+
const drive = manager.get('demo') || manager.add({ name: 'demo', table: 'demo' });
|
|
30
|
+
return drive.db().new('demo', 'id');
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
/**
|
package/tests/query.test.js
CHANGED
|
@@ -33,7 +33,9 @@ function makeTable() {
|
|
|
33
33
|
daoAdmin('query_test', {
|
|
34
34
|
sql: { way: 'sqlite', sqlite: { dir: db_dir, database: 'query_test' } }
|
|
35
35
|
});
|
|
36
|
-
|
|
36
|
+
const manager = daoAdmin('query_test');
|
|
37
|
+
const drive = manager.get('demo') || manager.add({ name: 'demo', table: 'demo' });
|
|
38
|
+
return drive.db().new('demo', 'id');
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
test('控制参数不进入查询条件,真值赋给数据管理器', () => {
|
package/tests/scope.test.js
CHANGED
|
@@ -39,15 +39,16 @@ test('不同域的管理器相互独立', () => {
|
|
|
39
39
|
assert.strictEqual(org.config.name, 'test_org_only');
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
test('配置 sql
|
|
42
|
+
test('配置 sql 时驱动按域绑定独立数据源', () => {
|
|
43
43
|
ensureDir();
|
|
44
44
|
const manager = daoAdmin('test_bound', {
|
|
45
45
|
sql: { way: 'sqlite', sqlite: { dir: db_dir, database: 'test_bound' } }
|
|
46
46
|
});
|
|
47
|
-
|
|
47
|
+
const dao = manager.add({ name: 'probe_bound', table: 'probe' });
|
|
48
|
+
assert.strictEqual(dao.db().getParent().config.database, 'test_bound');
|
|
48
49
|
});
|
|
49
50
|
|
|
50
|
-
test('
|
|
51
|
+
test('两个域配置不同数据库时驱动取用的数据源互不干扰', () => {
|
|
51
52
|
ensureDir();
|
|
52
53
|
const a = daoAdmin('test_db_a', {
|
|
53
54
|
sql: { way: 'sqlite', sqlite: { dir: db_dir, database: 'db_a' } }
|
|
@@ -55,37 +56,17 @@ test('两个域配置不同数据库时互不干扰', () => {
|
|
|
55
56
|
const b = daoAdmin('test_db_b', {
|
|
56
57
|
sql: { way: 'sqlite', sqlite: { dir: db_dir, database: 'db_b' } }
|
|
57
58
|
});
|
|
58
|
-
assert.strictEqual(a.db().getParent().config.database, 'db_a');
|
|
59
|
-
assert.strictEqual(b.db().getParent().config.database, 'db_b');
|
|
59
|
+
assert.strictEqual(a.add({ name: 'probe_a', table: 'probe' }).db().getParent().config.database, 'db_a');
|
|
60
|
+
assert.strictEqual(b.add({ name: 'probe_b', table: 'probe' }).db().getParent().config.database, 'db_b');
|
|
60
61
|
});
|
|
61
62
|
|
|
62
|
-
test('未配置 sql
|
|
63
|
+
test('未配置 sql 的域驱动回落主库实例', () => {
|
|
63
64
|
const manager = daoAdmin('test_fallback');
|
|
64
|
-
assert.strictEqual(manager._getSql(), getSql());
|
|
65
|
+
assert.strictEqual(manager.add({ name: 'probe_fallback', table: 'probe' })._getSql(), getSql());
|
|
65
66
|
});
|
|
66
67
|
|
|
67
68
|
test('默认域为 os', () => {
|
|
68
69
|
const manager = daoAdmin();
|
|
69
70
|
assert.strictEqual(manager.config.name, 'os');
|
|
70
|
-
assert.strictEqual(manager._getSql(), getSql());
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test('含 {org_id} 的数据源配置只登记模板,不建实例', () => {
|
|
74
|
-
const ret = daoAdmin('test_org_tpl', {
|
|
75
|
-
sql: { way: 'sqlite', sqlite: { dir: db_dir, database: 'tpl_{org_id}' } }
|
|
76
|
-
});
|
|
77
|
-
assert.strictEqual(ret, null);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test('按机构派生出独立库,且同机构复用', () => {
|
|
81
|
-
ensureDir();
|
|
82
|
-
daoAdmin('test_org_tpl2', {
|
|
83
|
-
sql: { way: 'sqlite', sqlite: { dir: db_dir, database: 'tpl2_{org_id}' } }
|
|
84
|
-
});
|
|
85
|
-
const one = daoAdmin('test_org_tpl2', { org_id: 1 });
|
|
86
|
-
const two = daoAdmin('test_org_tpl2', { org_id: 2 });
|
|
87
|
-
assert.strictEqual(one.db().database(), 'tpl2_1');
|
|
88
|
-
assert.strictEqual(two.db().database(), 'tpl2_2');
|
|
89
|
-
assert.strictEqual(daoAdmin('test_org_tpl2', { org_id: 1 }), one);
|
|
90
|
-
assert.strictEqual(one.config.name, 'test_org_tpl2_1');
|
|
71
|
+
assert.strictEqual(manager.add({ name: 'probe_os', table: 'probe' })._getSql(), getSql());
|
|
91
72
|
});
|
|
@@ -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
|
+
}
|