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