使用处理器
Yao 内建数据原子操作、网络请求、流程控制等一系列的处理器。这些处理器可以在数据接口、数据表格、数据图表、数据流等场景下使用,可以实现 80% 的业务逻辑处理需求。
处理器支持使用数据流、JS脚本、GRPC插件编写。
处理器
处理器是完成特定功能的函数 Process( ...args:any[] ) any
, 由处理器名称、参数表和返回值构成。处理器包含以下类型:
处理器 | 说明 |
---|---|
xiang.*.* | 内建处理器 |
models.<模型名称>.* | 内建处理器,数据模型原子操作 |
flows.<数据流名称> | 内建处理器,通过数据流编写的处理器 |
scripts.<脚本名称>.* | 内建处理器,通过 JavaScript 脚本编写处理器 |
plugins.<插件名称>.* | 通过 GRPC 插件编写处理器 |
session.* | 系统内建,会话数据处理器 |
使用方式
在命令行中使用
可以使用 run
命令,运行处理器 yao run <process> [args...]
。
bash
yao run models.pet.Get '::{}'
在 API 中使用
在 API 中指定 process
设置处理器。 query
设置处理器的参数表。
json
{
...
"paths": [
{
"path": "/search",
"method": "GET",
"process": "models.pet.Paginate",
"query": [":query-param", "$query.page", "$quey.pagesize"],
"out": {
"status": 200,
"type": "application/json"
}
}
]
...
}
在数据流节点中使用
在数据流节点中通过 process
指定处理器, args
传入处理器参数表。
json
{
...
"nodes": [
{
"name": "宠物",
"process": "models.pet.Find",
"args": [1, { "select": ["id","name"] }]
},
{
"name": "打印",
"process": "yao.sys.Print",
"args": ["{{$res.宠物}}"]
}
]
...
}
同样方式也可以用在看板、大屏、图表的数据流节点上。
json
{
...
"nodes": [
{
"name": "类型汇总",
"process": "flows.stat.kind",
"args": [["狗","猫"]]
}
]
...
}
在数据表格中使用
使用 process
重载表格数据读取逻辑
json
{
...
"apis": {
"search": {
"process": "flows.pet.latest"
},
"find": {
"process": "flows.pet.find"
}
}
...
}
用作表格 Hook
,处理上下文数据
json
{
...
"hooks": {
"after:search": "flows.pet.fliter.data",
"after:find": "flows.pet.fliter.row"
}
...
}
用作表格 Guard
,处理验证逻辑,在 table 的apis
中写入以下内容:
json
{
...
"apis": {
"save": {
"guard": "bearer-jwt,scripts.user.login"
},
"delete": {
"guard": "bearer-jwt"
}
},
...
}
scripts/user.js
内容,能够打印出请求的 url 路径,参数,以及请求头部,配合Exception
能够做到验证某些数据或者权限
javascript
function login(path, params, query, payload, headers) {
console.log([path, params, query, payload, headers]);
throw new Exception('未登录', 403);
}