数据库插件

This commit is contained in:
robin
2022-06-03 15:18:47 +08:00
parent 8150466cbd
commit ba9ca43729
25 changed files with 123 additions and 219 deletions
+16
View File
@@ -0,0 +1,16 @@
'use strict';
module.exports = {
doc : "默认页面",
path : "cms/index/",
method : ["get", "post"],
middleware: [],
params : {
"age": "int?"
},
return : {},
fun : async (ctx, app) => {
const User = await app.table("User").where(where).findAll()
ctx.body = {res}
}
}
-84
View File
@@ -1,84 +0,0 @@
'use strict';
module.exports = {
doc : "默认页面",
path : "cms/index/",
method : ["get", "post"],
middleware: [],
params : {
"age": "int?"
},
return : {},
fun: async (ctx, app) => {
console.log(ctx.request.body);
// app.err.ParameterException()
// 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)
// 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").where({id: 2}).find(),
// "查询单个数据,不存在就创建" : await app.table("User").where({id: 999}).data({uid: "6666"}).findOrCreate(),
// "查询所有数据" : await app.table("User").where({uid: "6666"}).findAll(),
// "查询所有数据(时间区间)" : await app.table("User").time(['2022-04-09', '2022-04-10']).findAll(),
// "查询所有数据(当月数据)" : await app.table("User").time('month').findAll(),
// "查询所有数据(30分钟内的数据)": await app.table("User").time(30).findAll(),
// "查询所有数据(分页)" : await app.table("User").where({uid: "6666"}).page(1).limit(1).findAll(),
// "查询所有数据+总行数(分页)" : await app.table("User").where({uid: "6666"}).page(1).limit(1).findAndCountAll(),
// }
/*
await app.setTransaction() // 设置事务
await app.table("User").data({uid: "6666"}).save()
await app.table("User").where({id: 2}).find()
await app.table("User").data({uid: "6666"}).save()
await app.table("User").where({id: 2}).find()
await app.commitTransaction() //提交事务,如果设置了事务不提交,任务不会执行
*/
// await app.table("User").where({id: 1032}).data({age: 1}).setInc() // 字段值+1
// 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 = 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() // 查询最小值
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": ""
// },
{
"as": "user_info",
"table" : "UserInfo",
"where" : {
"uid": "$as.user_data.id",//根据别名 user_data 返回的结果中的id值作为条件查询
}, "find": ""
},
// {"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}
}
}
+12
View File
@@ -0,0 +1,12 @@
'use strict';
// 定时任务
module.exports = {
time: {
cron: '0/5 * * * * ?',
// every: 2000,
// limit: 100,
},
fun: (app) => {
console.log("test",new Date());
}
}
+12
View File
@@ -0,0 +1,12 @@
'use strict';
// 定时任务
module.exports = {
time: {
cron: '0/5 * * * * ?',
// every: 2000,
// limit: 100,
},
fun: (app) => {
console.log("test2",new Date());
}
}
View File
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
path:"app/*/api/*.js"
}
+13
View File
@@ -0,0 +1,13 @@
/**
* 加载api路由
* 加载app/api目录下的文件到路由
*/
const config = require("./config")
module.exports = async (app) => {
let list = await app.load(config.path)
list.forEach(item => {
app.use(async (ctx, next) => {
return await item.res.fun(ctx, next)
})
})
}
+10
View File
@@ -0,0 +1,10 @@
module.exports = {
path:"app/*/schedule/*.js",
bullmq:{
port: 6379, // Redis port
host: "192.168.1.26", // Redis host
// username: "", // needs Redis >= 6
password: "9715115286",
db: 0, // Defaults to 0
},
}
+38
View File
@@ -0,0 +1,38 @@
/**
* 分布式定时任务
* 加载schedule文件夹下的任务
* https://blog.pincman.com/archives/18/
* https://docs.bullmq.io
*
*/
const config = require("./config")
// const schedule = require("./schedule")
const mq = require("./queue")
module.exports = async (app) => {
app.mq = mq
const queueName = "boobam_schedule"
const queue = mq.Queue(queueName);
mq.QueueScheduler(queueName);//处理延时任务
await queue.obliterate();//清空所有定时任务
const list = await app.load(config.path)
//等待所有插件载入完成后,启动Worker
app.willReadyList.push(async () => {
for (let el of list) {
mq.Worker(queueName, async job => {
if (job.name===el.parse.name){
el.res.fun(app)
}
});
await queue.add(el.parse.name,{},{
repeat: el.res.time,
})
}
})
}
+9
View File
@@ -0,0 +1,9 @@
const {Queue, QueueScheduler, Worker,QueueEvents} = require("bullmq")
const config = require("./config")
module.exports = {
Queue: (name) => new Queue(name, {connection: config.bullmq}),
QueueScheduler: (name) => new QueueScheduler(name, {connection: config.bullmq}),
QueueEvents: (name) => new QueueEvents(name, {connection: config.bullmq}),
Worker: (name, ops) => new Worker(name, ops, {connection: config.bullmq}),
Job: (ops) => new Job(ops, {connection: config.bullmq}),
}
+1 -1
View File
@@ -42,7 +42,7 @@ module.exports = async (app) => {
const api = new Api(sequelize, sequelize.models) const api = new Api(sequelize, sequelize.models)
app.db = api app.db = api
// console.log(api); // console.log(api);
console.log(await app.db.table("User").find()); // console.log(await app.db.table("User").find());
//等待所有插件载入完成后,自动更新数据库字段 //等待所有插件载入完成后,自动更新数据库字段
//把所有模型的md5值存入到redis里面去,哪个文件的md5有变动就更新哪个表的字段. //把所有模型的md5值存入到redis里面去,哪个文件的md5有变动就更新哪个表的字段.
+1
View File
@@ -10,5 +10,6 @@ const Redis = require("ioredis");
module.exports = async (app) => { module.exports = async (app) => {
const redis = new Redis(config); const redis = new Redis(config);
app.redis = redis app.redis = redis
app.Redis = Redis
} }
+7 -1
View File
@@ -14,7 +14,13 @@ module.exports = async (app) => {
app.io = io app.io = io
//默认命名空间 //默认命名空间
io.on('connection', socket => { io.on('connection', socket => {
socket.emit('open', "id:"+socket.id+"连接成功!");//通知客户端已连接 //通知客户端已连接
socket.emit('open', {
msg:"ok",
data:{
id:socket.id
}
});
console.log('连接=>',"id:"+socket.id); console.log('连接=>',"id:"+socket.id);
//监听disconnect事件 //监听disconnect事件
socket.on('disconnect', (eventName, callback) => { socket.on('disconnect', (eventName, callback) => {
+1
View File
@@ -46,6 +46,7 @@
"ajv": "^8.11.0", "ajv": "^8.11.0",
"babel-loader": "^8.2.3", "babel-loader": "^8.2.3",
"bamboo_smf": "^1.0.5", "bamboo_smf": "^1.0.5",
"bullmq": "^1.85.1",
"clean-webpack-plugin": "^4.0.0", "clean-webpack-plugin": "^4.0.0",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"glob": "^8.0.1", "glob": "^8.0.1",
-12
View File
@@ -1,12 +0,0 @@
'use strict';
// 定时任务
module.exports = [
{
name : "",
time : "*/3 * * * * *",
run : true,//系统启动时立即执行一次
schedule: (app) => {
// console.log('每3秒我执行一次' + app.utils.VERSION)
}
},
]
-9
View File
@@ -1,9 +0,0 @@
'use strict';
// 监听事件
module.exports = {
time : "*/3 * * * * *",
run : true,//系统启动时立即执行一次
schedule: (app) => {
// console.log('每3秒我执行一次' + app.utils.VERSION)
}
}
Binary file not shown.
-7
View File
@@ -1,7 +0,0 @@
module.exports = {
doc : "mysql模型文件的m5记录",
model: {
fileName: {type: "STRING", comment: '文件名称'},
md5 : {type: "TEXT", comment: 'md5记录'},
}
}
-25
View File
@@ -1,25 +0,0 @@
module.exports = {
doc : "数据权限表(只在默认api生效)",
model: {
code : {type: "STRING", comment: '角色代码'},
table: {type: "STRING", comment: '表名'},
key : {type: "STRING", comment: '表字段'},
crud: {
type : "ENUM", comment: 'crud权限',
values: [
"C", // 创建
"R", // 读取
"U", // 更新
"D", // 删除
]
},
value: {
type : "ENUM", comment: '状态',
values: [
0, // 禁止
1, //通行
],
defaultValue: 0,
},
}
}
-16
View File
@@ -1,16 +0,0 @@
module.exports = {
doc : "页面元素是否显示权限表",
model: {
code : {type: "STRING", comment: '角色代码'},
key : {type: "STRING", comment: '元素id', unique: true},
value: {
type : "ENUM", comment: '状态',
values : [
//注意,一般页面元素会很多,默认是显示状态
0, // 通行
1, //禁止
],
defaultValue: 0,
},
}
}
-15
View File
@@ -1,15 +0,0 @@
module.exports = {
doc : "页面权限表",
model: {
code : {type: "STRING", comment: '角色代码'},
key : {type: "STRING", comment: '页面标识,一般是页面url',unique: true},
value: {
type : "ENUM", comment: '状态',
values: [
0, // 禁止
1, //通行
],
defaultValue: 0,
},
}
}
-16
View File
@@ -1,16 +0,0 @@
module.exports = {
doc : "角色",
model: {
cname: {type: "STRING", comment: '角色别名'},
code : {type: "STRING", comment: '角色代码'},
super: {
type : "ENUM", comment: '是否为超级管理员(不校验权限)',
values : [
0, // api地址
1, //url地址
],
defaultValue: 0,
},
doc : {type: "TEXT", comment: '说明'},
}
}
-14
View File
@@ -1,14 +0,0 @@
module.exports = {
doc : "路由表表",
model: {
name: {type: "STRING", comment: '路由名称'},
key : {type: "STRING", comment: '路由标识,一般是路由url或页面url'},
type: {
type : "ENUM", comment: '字段类型',
values: [
'api', // api地址
'url', //url地址
]
},
}
}
-19
View File
@@ -1,19 +0,0 @@
module.exports = {
doc : "系统设置",
model: {
title: {type: "STRING", comment: '字段标题'},
doc : {type: "TEXT", comment: '字段说明'},
type : {
type : "ENUM", comment: '字段类型',
values: [
'int', // 整数
'string', //字符串
'json', // json
'rich' // 富文本
],
defaultValue: 'string',
},
code : {type: "STRING", comment: '字段code'},
value: {type: "TEXT", comment: '值'},
}
}