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
+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,