From b5f918776ef9e5843061de0c537432c38f1c3db0 Mon Sep 17 00:00:00 2001 From: robin <9715115286> Date: Wed, 13 Apr 2022 07:54:19 +0800 Subject: [PATCH] up --- app/controller/index.js | 34 ++++++++++++-- app/model/UserInfo.js | 8 ++++ app/sqlite/database.sqlite | Bin 16384 -> 16384 bytes lib/bamboo/index.js | 92 +++++++++++++++++++++++++++++++++++-- package.json | 1 + 5 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 app/model/UserInfo.js diff --git a/app/controller/index.js b/app/controller/index.js index c2c33ad..ab88c94 100644 --- a/app/controller/index.js +++ b/app/controller/index.js @@ -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} } } diff --git a/app/model/UserInfo.js b/app/model/UserInfo.js new file mode 100644 index 0000000..8637425 --- /dev/null +++ b/app/model/UserInfo.js @@ -0,0 +1,8 @@ +module.exports = { + doc : "用户信息表", + model: { + uid : {type: "STRING", comment: '用户id'}, + phone: {type: "STRING", comment: '手机号', defaultValue: null,}, + + } +} diff --git a/app/sqlite/database.sqlite b/app/sqlite/database.sqlite index 7fdd09267a420e1ce0018f64c778055cfbf960d4..c38eb5c2227516375d418cdd19ab789a5355fa32 100644 GIT binary patch delta 136 zcmZo@U~Fh$oFFCSFo}VIfdz^g85pz~Ch8b5IZWD^@R)~bmE+`Eo+Fz%`Q9@E*_(gy zf8^&gQZO>MGBvg`w9qp&H{bkIezm+pvWbz2v7w2%v9U$6QA%2xk%@)5rICq|d6I=i enxTp1 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, diff --git a/package.json b/package.json index 6ded333..7e2ea08 100644 --- a/package.json +++ b/package.json @@ -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",