数据库插件

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());
}
}