This commit is contained in:
robin
2022-04-13 07:54:19 +08:00
parent f88b5e9325
commit b5f918776e
5 changed files with 128 additions and 7 deletions
+31 -3
View File
@@ -6,7 +6,7 @@ module.exports = {
method : ["get", "post"],
middleware: [],
params : {
"age": "int"
"age": "int?"
},
return : {},
controller: async (ctx, app) => {
@@ -19,7 +19,7 @@ module.exports = {
// const {count, rows} = await app.table("User").where(where).findAndCountAll()
// const User = await app.table("User").save(data)
// const res = {
// "创建单个数据" : await app.table("User").data({uid: "6666"}).save(),
// // "创建单个数据" : await app.table("User").data({uid: "6666"}).save(),
// "查询单个数据" : await app.table("User").where({id: 2}).find(),
// "查询单个数据,不存在就创建" : await app.table("User").where({id: 999}).data({uid: "6666"}).findOrCreate(),
// "查询所有数据" : await app.table("User").where({uid: "6666"}).findAll(),
@@ -49,7 +49,35 @@ module.exports = {
// const res = await app.table("User").data("age").max() // 查询最大值
// const res = await app.table("User").data("age").min() // 查询最小值
const res = await app.jsonTasks([
{"table": "User", "where": {id: 999}, "find": "", "as": "user_data"},//as 设置结果别名
// {
// "table" : "UserInfo",
// "data" : {
// "phone": "13126000000",
// "uid": "$as.user_data.id",//根据别名 user_data 返回的结果中的id值作为数据保存
// }, "save": ""
// },
{
"table" : "UserInfo",
"where" : {
"uid": "$as.user_data.id",//根据别名 user_data 返回的结果中的id值作为条件查询
}, "find": ""
},
ctx.body = {}
// {"table": "User", "data": "age", "min": ""},
// {
// "as": "user_list", "jsonTasks": [ //整个任务设置别名
// {"setTransaction": ""}, // 开始事务
// {"table": "User", "data": "age", "sum": ""},
// {"table": "User", "data": "age", "max": ""},
// {"table": "User", "data": "age", "min": ""},
// {"commitTransaction": ""}, //提交事务
// ]
// },
])
ctx.body = {res}
}
}
+8
View File
@@ -0,0 +1,8 @@
module.exports = {
doc : "用户信息表",
model: {
uid : {type: "STRING", comment: '用户id'},
phone: {type: "STRING", comment: '手机号', defaultValue: null,},
}
}
Binary file not shown.
+88 -4
View File
@@ -13,7 +13,7 @@ const EventEmitter = require('events');
const schedule = require("node-schedule");
const Sequelize = require("sequelize");
const Parameter = require('parameter');
const objectPath = require("object-path");
module.exports = class Bamboo extends Koa {
constructor(agrs, options) {
super(options)
@@ -58,7 +58,6 @@ module.exports = class Bamboo extends Koa {
clearInterval(initInterval)
}
}, 1000)
}
async initDB() {
@@ -106,7 +105,7 @@ module.exports = class Bamboo extends Koa {
}
registeredParameter(){
registeredParameter() {
this.parameter = new Parameter();
}
@@ -287,7 +286,8 @@ module.exports = class Bamboo extends Koa {
if (validate) {
console.error(validate);
this.errorException.ParameterException(validate);
}else{
}
else {
await obj.controller(ctx, this.application)
next();
}
@@ -928,6 +928,88 @@ module.exports = class Bamboo extends Koa {
return {count, rows: rows.map(item => item.dataValues)}
}
/**
* json批量任务
* @param {array} taskList json格式的任务列表.
*/
async jsonTasks(taskList) {
const res = []
for (let tk of taskList) {
const tkKeys = Object.keys(tk)
for (let key of tkKeys) {
if (key === "as") { continue; }
if (key === "where"||key === "data") {
const findJsonRes = this.findJsonValue(tk[key], (n) => {
if (typeof n === "string") {
// console.log(tk[key],n);
if (n.indexOf("$as") !== -1) {
return true
}
}
return false
})
if (findJsonRes) {
const jsonPath = findJsonRes.path.join(".")
const jsonPath2 = objectPath.get(tk[key], jsonPath);//$as.user_data.id
const asName = jsonPath2.split(".")[1]
const asFilter = res.filter(item => {
// console.log(Object.keys(item), asName);
return Object.keys(item)[0] === asName
})
// console.log(asFilter);
// console.log(jsonPath2);
if (asFilter.length > 0) {
// console.log(objectPath.get(asFilter[0], jsonPath2.split(".").slice(1).join("."))); //999
// console.log(jsonPath2.split(".").slice(1).join("."));//user_data.id
// console.log(tk[key]);//user_data.id
objectPath.set(tk[key], jsonPath, objectPath.get(asFilter[0], jsonPath2.split(".").slice(1).join(".")));
}
}
}
console.log(tk[key]);
const tkRes = await this[key](tk[key])
if (
key === "save" ||
key === "count" ||
key === "sum" ||
key === "max" ||
key === "min" ||
key === "find" ||
key === "findAll" ||
key === "findOrCreate" ||
key === "jsonTasks" ||
key === "findAndCountAll"
) {
console.log(tk["as"]);
res.push(tk["as"] ? {[tk["as"]]: tkRes} : tkRes)
}
}
}
return res
}
//深度遍历JSON,搜索值
findJsonValue(n, value, path) {
if (n === value || (xe.isFunction(value) && value(n))) {
return {value: n, path}
}
path = path || []
// 获取所有的子节点,并遍历
if (typeof n ==="object") {
const nkeys = Object.keys(n)
for (let k of nkeys) {
// concat() 方法用于连接两个或多个数组
const res = this.findJsonValue(n[k], value, path.concat(k));
if (res) {
return res
}
}
}
}
get application() {
return {
config : this.config,
@@ -961,6 +1043,8 @@ module.exports = class Bamboo extends Koa {
findOrCreate : this.findOrCreate,
findAndCountAll : this.findAndCountAll,
resetDbData : this.resetDbData,
jsonTasks : this.jsonTasks,
findJsonValue : this.findJsonValue,
//======= 简写函数
// S: this.sqlite.models,
// M: this.mysql.models,
+1
View File
@@ -53,6 +53,7 @@
"log4js": "^6.4.4",
"mysql2": "^2.3.3",
"node-schedule": "^2.1.0",
"object-path": "^0.11.8",
"parameter": "^3.6.0",
"pm2": "^5.1.2",
"require-all": "^3.0.0",