This commit is contained in:
robin
2022-04-12 03:06:19 +08:00
parent 344562baf4
commit f88b5e9325
6 changed files with 124 additions and 20 deletions
+11 -12
View File
@@ -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 = {}
}
}
+4
View File
@@ -0,0 +1,4 @@
'use strict';
const bodyParser = require('koa-bodyparser')
// 错误处理
module.exports = bodyParser()
Binary file not shown.
+1
View File
@@ -1,5 +1,6 @@
'use strict';
module.exports = {
register: ["bodyparser", "error", "time", "koaLogger", "verify"],
time : {
params: '123'
},
+103 -5
View File
@@ -12,6 +12,7 @@ const Router = require("koa-router");
const EventEmitter = require('events');
const schedule = require("node-schedule");
const Sequelize = require("sequelize");
const Parameter = require('parameter');
module.exports = class Bamboo extends Koa {
constructor(agrs, options) {
@@ -20,11 +21,16 @@ module.exports = class Bamboo extends Koa {
this.fulfill = false //启动完成
this.modelAmend = false //模型文件是否有改动
this.config = {}
this.utils = this.registeredContextUtils(xe)
this.utils = {}
this.registeredContextUtils({"xe": xe})
this.registeredParameter()
// const ajv = new Ajv({})
// this.registeredContextUtils({"ajv": ajv})
this.logger = null
this.Sequelize = null
this.mysql = null
this.sqlite = null
this.ajv = null
this.event = new EventEmitter()
this.registeredConfig()
this.setLogger()
@@ -100,6 +106,11 @@ module.exports = class Bamboo extends Koa {
}
registeredParameter(){
this.parameter = new Parameter();
}
//注册 mysql数据库
async registeredDB() {
console.log('注册 数据库');
@@ -272,9 +283,14 @@ module.exports = class Bamboo extends Koa {
visit: (obj) => {
for (let methodElement of obj.method) {
router[methodElement](obj.path, async (ctx, next) => {
ctx['logger'] = this.logger
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,
}
}
}
+2
View File
@@ -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",