egg-tx

An Egg transaction plug-in, it supports Mysql, Mongo database, it can do request interface level transaction management.

Dependent plug-ins

  • To use the Mysql database you need to enable itegg-sequelizeThe plug-in.
  • To use the Mongo database you need to turn it onegg-mongooseThe plug-in.

The installation

$ npm i egg-tx --save
Copy the code

Open the plug-in

// {app_root}/config/plugin.js
exports.tx = {
  enable: true.package: 'egg-tx'};Copy the code

configuration

// {app_root}/config/config.default.js
exports.tx = {
    reqAction: ['POST'.'PUT'.'DELETE'].dbType:'mysql'
};
Copy the code
  • ReqAction: Transaction management for all requests for the specified action. The values of this array can be GET, POST, PUT, DELETE, HEAD, PATCH, and OPTIONS (default values are POST, PUT, and DELETE).
  • DbType: the type of database used. This value can be mysql or mongo (default is mysql)

Using the example

You can obtain the transaction session object for this request via ctx.tx.session, provided it is managed by the transaction manager.

mysql
await this.ctx.model.User.create(user, {
    transaction: this.ctx.tx.session,
});
Copy the code
mongo
await this.ctx.model.User.insertMany([
  { username: 'lyTongXue'.password: '123456'{},],session: this.ctx.tx.session });
Copy the code

annotations

@tx

Interface methods that use this annotation will conduct transaction management even if the reqAction configuration item does not contain a request for that action type.

// {app_root}/app/controller/{controller_name}.js
/**
* @tx
*/
async create(){
}
Copy the code
@txIgnore

Even if the reqAction configuration item contains a request for that action type, interface methods using this annotation will not do transaction management.

// {app_root}/app/controller/{controller_name}.js
/**
* @txIgnore
*/
async index(){
}
Copy the code

Questions communication

1. Are there any jsDoc requirements for interface methods?

  • JsDoc must be “right next to” the method, as in:
//

/**
* @TX
*/
async create(){
}

//

/**
* @TX
*/


async create(){
}
Copy the code

Please go to Egg Issues for asynchronous communication.

License

MIT