一、定时任务
在程序开发的后端领域,定时任务是一个经常使用的功能,比如从数据库定时读取数据,存到缓存当中,定时删除日志,定时更新数据库文件。
定时任务需要按照Egg的约定,/app目录下,新建schedule文件夹。
以在控制台每5s打印一次当前时间戳为例,/app/schedule文件夹下新增logTime.js。
'use strict';
const Subscription = require('egg').Subscription;
class logTime extends Subscription {
static get schedule() {
return {
interval: '5s',
type: 'worker',
};
}
async subscribe() {
console.log(Date.now());
}
}
module.exports = logTime;
也可以使用更复杂的cron属性进行定时。cron属性有6个参数。
static get schedule(){
return {
// */秒(0~59) */分(0~59) */时(0~23) */天(1~31) */月(1~12) */星期(1~7) */年
cron: '*/5 * * * * * *', //表示5s
type:'worker'
};
}
示例:
- 每隔1分钟执行一次:
0 */1 * * * ?- 每天22点执行一次:
0 0 22 * * ?- 每月1号凌晨1点执行一次:
0 0 1 1 * ?- 每月最后一天23点执行一次:
0 0 23 L * ?- 每周周六凌晨3点实行一次:
0 0 3 ? * L- 在24分、30分执行一次:
0 24,30 * * * ?了解更多参考:juejin.cn/post/684490…
二、数据库操作
1、安装egg-mysql插件
npm i --save egg-mysql
2、配置egg-mysql插件
安装完的插件并不能正常使用,需要在plugin.js中配置插件。打开/config/plugin.js文件
exports.mysql = {
enable: true,
package: 'egg-mysql',
};
/config/config.default.js当中进行 进一步配置
exports.mysql = {
// 单数据库信息配置
client: {
// host
host: 127.0.0.1',
// 端口号
port: '3306',
// 用户名
user: 'my_test',
// 密码
password: '123456789',
// 数据库名
database: 'my_test',
},
// 是否加载到 app 上,默认开启
app: true,
// 是否加载到 agent 上,默认关闭
agent: false,
};
3、操作数据库
操作数据库的过程应该都在service里进行,在/service文件夹下面,新建一个文件mysqlTest.js文件,编写下面的方法,方法分别代表增、删、改、查。
'use strict';
const Service = require('egg').Service;
class MysqlService extends Service {
// 增
async mysqlAdd(params) {
try {
const { app } = this;
const res = await app.mysql.insert('students', params); // (表名,参数)
return res;
} catch (error) {
console.log(error);
return null;
}
}
// 删
async mysqlDel(id) {
try {
const { app } = this;
const res = await app.mysql.delete('students', id); // (表名,参数id)
return res;
} catch (error) {
console.log(error);
return null;
}
}
// 改
async mysqlUpdate(params) {
try {
const { app } = this;
const res = await app.mysql.update('students', params); // (表名,参数)
return res;
} catch (error) {
console.log(error);
return null;
}
}
// 查
async mysqlGet() {
try {
const { app } = this;
const res = await app.mysql.select('students');
return res;
} catch (error) {
console.log(error);
return null;
}
}
}
module.exports = MysqlService;
在/controller文件夹下面新增一个控制器mysqlTest.js文件。
'use strict';
const Controller = require('egg').Controller;
class MysqlController extends Controller {
// 增
async mysqlAdd() {
const { ctx } = this;
const params = {
name: '小刚',
age: 17,
};
await ctx.service.mysqlTest.mysqlAdd(params); // 添加
const res = await ctx.service.mysqlTest.mysqlGet(); // 查询
ctx.body = res;
}
//删
async mysqlDel() {
const { ctx } = this;
const id = { id: 1 }; // 必须传对象,如果传数字3会把所有数据删除
await ctx.service.mysqlTest.mysqlDel(id); // 修改
const res = await ctx.service.mysqlTest.mysqlGet(); // 查询
ctx.body = res;
}
//改
async mysqlUpdate() {
const { ctx } = this;
const params = {
id: 1,
name: '小刚刚',
age: 20,
};
await ctx.service.mysqlTest.mysqlUpdate(params); // 修改
const res = await ctx.service.mysqlTest.mysqlGet(); // 查询
ctx.body = res;
}
//查
async mysqlGet() {
const { ctx } = this;
const res = await ctx.service.mysqlTest.mysqlGet();
ctx.body = res;
}
}
module.exports = MysqlController;
路由配置
router.get('/mysqlAdd', controller.mysqlTest.mysqlAdd);
router.get('/mysqlDel', controller.mysqlTest.mysqlDel);
router.get('/mysqlUpdate', controller.mysqlTest.mysqlUpdate);
router.get('/mysqlGet', controller.mysqlTest.mysqlGet);
以上便是对mysql最基本的增删改查的操作
了解更多数据库相关操作方法:eggjs.org/zh-cn/tutor…
学习日期:2021/12/27
视频参考:www.bilibili.com/video/BV1s3…
仅供个人学习和记录