From f88b5e9325338451e12b5aecff6b7cbe71615c41 Mon Sep 17 00:00:00 2001 From: robin <9715115286> Date: Tue, 12 Apr 2022 03:06:19 +0800 Subject: [PATCH] up --- app/controller/index.js | 23 ++++--- app/middleware/bodyparser.js | 4 ++ app/sqlite/database.sqlite | Bin 16384 -> 16384 bytes config/middleware.js | 3 +- lib/bamboo/index.js | 112 ++++++++++++++++++++++++++++++++--- package.json | 2 + 6 files changed, 124 insertions(+), 20 deletions(-) create mode 100644 app/middleware/bodyparser.js diff --git a/app/controller/index.js b/app/controller/index.js index 999551a..c2c33ad 100644 --- a/app/controller/index.js +++ b/app/controller/index.js @@ -5,19 +5,14 @@ module.exports = { path : "/", method : ["get", "post"], middleware: [], - params : {}, + params : { + "age": "int" + }, return : {}, controller: async (ctx, app) => { + console.log(ctx.request.body); // app.err.ParameterException() - app.event.emit('test.event2') - const where = { - // id: 4 - } - const data = { - // id: 1, - uid: "77", - } - + // app.event.emit('test.event2') // const User = await app.table("User").where(where).findAll() // const User = await app.table("User").time(["2022-04-9", "2022-04-10"]).findAll() // const User = await app.table("User").where(where).findOrCreate(data) @@ -48,9 +43,13 @@ module.exports = { // await app.table("User").where({id: 1032}).data({age: 2}).setInc() // 字段值+2 // const {res, value} = await app.table("User").where({id: 1032}).data({age: 2}).setDec(0) // 字段值-2,不能低于0,如果低于0返回false // const {res, value} = await app.table("User").where({id: 1032}).data({age: 2}).setDec(0,10) // 字段值-2,不能低于0,如果低于0设置为10 - const {res, value} = await app.table("User").where({id: 1032}).data({age: 2}).setInc(20, 10) // 字段值+2,不能大于20,如果大于20设置为10 + // const {res, value} = await app.table("User").where({id: 1032}).data({age: 2}).setInc(20, 10) // 字段值+2,不能大于20,如果大于20设置为10 + // const res = await app.table("User").count() // 统计 + // const res = await app.table("User").data("age").sum() // 求和 + // const res = await app.table("User").data("age").max() // 查询最大值 + // const res = await app.table("User").data("age").min() // 查询最小值 - ctx.body = {res, value} + ctx.body = {} } } diff --git a/app/middleware/bodyparser.js b/app/middleware/bodyparser.js new file mode 100644 index 0000000..facc9b8 --- /dev/null +++ b/app/middleware/bodyparser.js @@ -0,0 +1,4 @@ +'use strict'; +const bodyParser = require('koa-bodyparser') +// 错误处理 +module.exports = bodyParser() diff --git a/app/sqlite/database.sqlite b/app/sqlite/database.sqlite index 87e7ce472371f1c2ab3009aa04cf33d07e584418..7fdd09267a420e1ce0018f64c778055cfbf960d4 100644 GIT binary patch delta 57 zcmZo@U~Fh$oFFA+ypMr_fdz^g85opmCh8b58SmSe@R)~bVg2M { for (let methodElement of obj.method) { router[methodElement](obj.path, async (ctx, next) => { - ctx['logger'] = this.logger - await obj.controller(ctx, this.application) - next(); + const validate = this.parameter.validate(obj.params, ctx.request.body); + if (validate) { + console.error(validate); + this.errorException.ParameterException(validate); + }else{ + await obj.controller(ctx, this.application) + next(); + } }); } return obj @@ -338,9 +354,10 @@ module.exports = class Bamboo extends Koa { //注册 utils registeredContextUtils(args) { - console.log('注册 utils'); - console.log('xe version', this.xe.VERSION); - return args + this.utils = { + ...this.utils, + ...args + } } //logger @@ -689,6 +706,82 @@ module.exports = class Bamboo extends Koa { return res && res.dataValues || null } + /** + * 统计查询结果数 + */ + async count() { + if (!this.tableName) { this.errorException.HttpException("请传表名") } + const res = await this.mysql.models[this.tableName].count( + this.tableData, + { + where : this.tableWhere, + order : this.tableOrder || [['updatedAt', 'DESC']], // 时间排序 + group : this.tableGroup, + attributes : this.tableAttributes, + transaction: this.t + } + ) + this.resetDbData() + return res + } + + /** + * 求和 + */ + async sum() { + if (!this.tableName) { this.errorException.HttpException("请传表名") } + const res = await this.mysql.models[this.tableName].sum( + this.tableData, + { + where : this.tableWhere, + order : this.tableOrder || [['updatedAt', 'DESC']], // 时间排序 + group : this.tableGroup, + attributes : this.tableAttributes, + transaction: this.t + } + ) + this.resetDbData() + return res + } + + /** + * 查询最大值 + */ + async max() { + if (!this.tableName) { this.errorException.HttpException("请传表名") } + const res = await this.mysql.models[this.tableName].max( + this.tableData, + { + where : this.tableWhere, + order : this.tableOrder || [['updatedAt', 'DESC']], // 时间排序 + group : this.tableGroup, + attributes : this.tableAttributes, + transaction: this.t + } + ) + this.resetDbData() + return res + } + + /** + * 查询最小值 + */ + async min() { + if (!this.tableName) { this.errorException.HttpException("请传表名") } + const res = await this.mysql.models[this.tableName].min( + this.tableData, + { + where : this.tableWhere, + order : this.tableOrder || [['updatedAt', 'DESC']], // 时间排序 + group : this.tableGroup, + attributes : this.tableAttributes, + transaction: this.t + } + ) + this.resetDbData() + return res + } + /** * 更新数据 * @param {object} data 数据. @@ -859,6 +952,10 @@ module.exports = class Bamboo extends Koa { setTransaction : this.setTransaction, commitTransaction: this.commitTransaction, save : this.save, + count : this.count, + sum : this.sum, + max : this.max, + min : this.min, find : this.find, findAll : this.findAll, findOrCreate : this.findOrCreate, @@ -868,6 +965,7 @@ module.exports = class Bamboo extends Koa { // S: this.sqlite.models, // M: this.mysql.models, C: this.config, + U: this.utils, } } } diff --git a/package.json b/package.json index 7661948..6ded333 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "@babel/polyfill": "^7.4.4", "@babel/preset-env": "^7.16.8", "@babel/runtime": "^7.5.5", + "ajv": "^8.11.0", "babel-loader": "^8.2.3", "clean-webpack-plugin": "^4.0.0", "cross-env": "^7.0.3", @@ -52,6 +53,7 @@ "log4js": "^6.4.4", "mysql2": "^2.3.3", "node-schedule": "^2.1.0", + "parameter": "^3.6.0", "pm2": "^5.1.2", "require-all": "^3.0.0", "require-directory": "^2.1.1",