一个 egg 事务插件,它支持 Mysql、Mongo 数据库,它能作到请求接口级别的事务管理。mysql
egg-sequelize
插件。egg-mongoose
插件。$ npm i egg-tx --save
复制代码
// {app_root}/config/plugin.js
exports.tx = {
enable: true,
package: 'egg-tx',
};
复制代码
// {app_root}/config/config.default.js
exports.tx = {
reqAction:['POST','PUT','DELETE'],
dbType:'mysql'
};
复制代码
你能够经过 ctx.tx.session
获取到本次请求的事务会话对象,前提是它已经被事务管理器所管理。git
await this.ctx.model.User.create(user, {
transaction: this.ctx.tx.session,
});
复制代码
await this.ctx.model.User.insertMany([
{ username: 'lyTongXue', password: '123456' },
], { session: this.ctx.tx.session });
复制代码
使用该注解的接口方法将会进行事务管理,即使 reqAction 配置项中未包含该动做类型的请求。github
// {app_root}/app/controller/{controller_name}.js
/** * @tx */
async create(){
}
复制代码
即使 reqAction 配置项中包含了该动做类型的请求,使用了该注解的接口方法将不会进行事务管理。sql
// {app_root}/app/controller/{controller_name}.js
/** * @txIgnore */
async index(){
}
复制代码
一、接口方法的 jsDoc 是否有必定要求?数据库
// --- 正确写法
/** * @TX */
async create(){
}
// --- 错误写法
/** * @TX */
async create(){
}
复制代码
请到 egg issues 异步交流。npm
MIT数组