mm_api 1.1.1 → 1.1.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/lib/api.js +17 -4
- package/lib/manager.js +5 -1
- package/package.json +34 -32
package/lib/api.js
CHANGED
|
@@ -19,6 +19,10 @@ function loadOptional(id) {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
const Param = loadOptional("mm_param/lib/param.js"); // 是MM自带的参数机制,可以不使用
|
|
22
|
+
// sql驱动:优先使用 mm_dao 的 Dao(dao.json 查询构建器,契约 run(query, body, db)),
|
|
23
|
+
// 回退到 mm_sql 的 Sql(仅支持 run(sql, param, timeout) 原始SQL执行)
|
|
24
|
+
const mm_dao = loadOptional("mm_dao");
|
|
25
|
+
const Dao = mm_dao ? mm_dao.Dao : null;
|
|
22
26
|
const Sql = loadOptional("mm_sql/lib/sql.js"); // 是MM自带的sql机制,可以不使用
|
|
23
27
|
|
|
24
28
|
/** param 实例缓存(模块级,替代原全局 param 模块列表) */
|
|
@@ -268,6 +272,7 @@ Api.prototype._loadParam = function (file_path) {
|
|
|
268
272
|
if (!cfg) {
|
|
269
273
|
return null;
|
|
270
274
|
}
|
|
275
|
+
cfg.name = cfg.name || this.config.name;
|
|
271
276
|
cfg.config_file = p;
|
|
272
277
|
cfg.dir = path.dirname(p);
|
|
273
278
|
this.param = new Param(cfg, this);
|
|
@@ -296,11 +301,15 @@ Api.prototype.getSql = function (file) {
|
|
|
296
301
|
|
|
297
302
|
/**
|
|
298
303
|
* 加载sql模板配置(方案B:读取 sql.json 作为驱动配置,替代旧脚本动态加载)
|
|
304
|
+
* 优先用 mm_dao 的 Dao 构建驱动:Dao 实现了 run(query, body, db) 的旧契约,
|
|
305
|
+
* 能按 sql.json/dao.json 配置将请求参数拼装成 SQL,并通过第三参传入的
|
|
306
|
+
* 数据管理器(db 或 db.new(table) 表模型)执行。仅当 mm_dao 缺失时才
|
|
307
|
+
* 回退到 mm_sql 的 Sql(只支持 run(sql, param, timeout) 原始SQL)。
|
|
299
308
|
* @param {string} file_path sql模板文件路径
|
|
300
309
|
* @returns {object|null} sql驱动实例,未配置或加载失败返回 null
|
|
301
310
|
*/
|
|
302
311
|
Api.prototype._loadSql = function (file_path) {
|
|
303
|
-
if (!file_path || !Sql) {
|
|
312
|
+
if (!file_path || (!Dao && !Sql)) {
|
|
304
313
|
return null;
|
|
305
314
|
}
|
|
306
315
|
var p = file.fullname(file_path, this.getDir());
|
|
@@ -316,7 +325,11 @@ Api.prototype._loadSql = function (file_path) {
|
|
|
316
325
|
cfg.name = cfg.name || this.config.name;
|
|
317
326
|
cfg.config_file = p;
|
|
318
327
|
cfg.dir = path.dirname(p);
|
|
319
|
-
|
|
328
|
+
if (Dao) {
|
|
329
|
+
this.sql = new Dao(cfg, this);
|
|
330
|
+
} else {
|
|
331
|
+
this.sql = new Sql(cfg, this);
|
|
332
|
+
}
|
|
320
333
|
this.updateSql(this.sql);
|
|
321
334
|
return this.sql;
|
|
322
335
|
};
|
|
@@ -450,7 +463,7 @@ Api.prototype.saveFile = function (files) {
|
|
|
450
463
|
* @private
|
|
451
464
|
*/
|
|
452
465
|
Api.prototype._getState = async function (db, token) {
|
|
453
|
-
var db1 = db.new("
|
|
466
|
+
var db1 = db.new("sys_user_state", "user_id");
|
|
454
467
|
var state = await db1.getObj(
|
|
455
468
|
{
|
|
456
469
|
token,
|
|
@@ -464,7 +477,7 @@ Api.prototype._getState = async function (db, token) {
|
|
|
464
477
|
}
|
|
465
478
|
var now = new Date();
|
|
466
479
|
if (date.interval(state.time_sign, now, "day") <= 30) {
|
|
467
|
-
db.table = "
|
|
480
|
+
db.table = "sys_user";
|
|
468
481
|
var user = await db.getObj({
|
|
469
482
|
user_id: state.user_id,
|
|
470
483
|
});
|
package/lib/manager.js
CHANGED
|
@@ -228,6 +228,7 @@ ApiManager.prototype.run = async function (ctx, options = {}) {
|
|
|
228
228
|
let args = this._getRunArgs(ctx, options, arguments[2]);
|
|
229
229
|
let path = args.ctx.request.path;
|
|
230
230
|
let drives = this._sortedDrives();
|
|
231
|
+
let result = null;
|
|
231
232
|
|
|
232
233
|
for (var i = 0; i < drives.length; i++) {
|
|
233
234
|
var mod = drives[i];
|
|
@@ -237,12 +238,15 @@ ApiManager.prototype.run = async function (ctx, options = {}) {
|
|
|
237
238
|
}
|
|
238
239
|
var ret = await mod.run(args.params, args.ctx, args.options);
|
|
239
240
|
if (ret) {
|
|
241
|
+
// 同时写入上下文与数据管理器,兼容两种结果读取方式
|
|
240
242
|
args.ctx.result = ret;
|
|
243
|
+
args.options.ret = ret;
|
|
244
|
+
result = ret;
|
|
241
245
|
break;
|
|
242
246
|
}
|
|
243
247
|
}
|
|
244
248
|
}
|
|
245
|
-
return
|
|
249
|
+
return result;
|
|
246
250
|
};
|
|
247
251
|
|
|
248
252
|
/**
|
package/package.json
CHANGED
|
@@ -1,34 +1,36 @@
|
|
|
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
|
-
|
|
2
|
+
"name": "mm_api",
|
|
3
|
+
"version": "1.1.3",
|
|
4
|
+
"description": "API框架,用于实现web api和mvc模式路由请求,负责配置路由路径、访问权限、校验机制;基于 mm_corejs,内置管理器池,无全局/原型扩展依赖",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"mm_mod",
|
|
8
|
+
"mm_port",
|
|
9
|
+
"mm_in_port",
|
|
10
|
+
"api",
|
|
11
|
+
"mvc",
|
|
12
|
+
"router",
|
|
13
|
+
"oauth",
|
|
14
|
+
"API管理器"
|
|
15
|
+
],
|
|
16
|
+
"layer": "port",
|
|
17
|
+
"group": "web",
|
|
18
|
+
"author": "邱文武",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_api.git"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"mm_corejs": "^1.1.4",
|
|
26
|
+
"mm_dao": "^1.8.2",
|
|
27
|
+
"mm_helper": "^1.0.0",
|
|
28
|
+
"mm_machine": "^3.5.4",
|
|
29
|
+
"mm_param": "^1.1.0",
|
|
30
|
+
"mm_ret": "^1.4.8",
|
|
31
|
+
"mm_sql": "^1.8.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"test": "node test.js && node --test tests/*.test.js"
|
|
35
|
+
}
|
|
34
36
|
}
|