Gathering detailed insights and metrics for egg-sequelize-autotrx
Gathering detailed insights and metrics for egg-sequelize-autotrx
Gathering detailed insights and metrics for egg-sequelize-autotrx
Gathering detailed insights and metrics for egg-sequelize-autotrx
npm install egg-sequelize-autotrx
Typescript
Module System
Min. Node Version
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
This plugin helps to do transaction auto pass down and solve the nested transaction issue.
After CLS enabled, nested transaction will have unexpected result:
1async nestedTrx () { 2 await this.ctx.model.transaction(async () => { 3 await this.ctx.model.M1.create() 4 await this.ctx.model.transaction(async () => { 5 await this.ctx.model.M2.create() 6 await this.ctx.model.M3.create() 7 }) 8 await this.ctx.model.M4.create() 9 }) 10}
If error throw out from M4 creation, transaction will rollback creation for M1 and M4. M2 and M3 will be committed. To make all the operations commit and rollback together through out all the transactions, you need help from this plugin.
Internally, this plugin solves the problem by passing parent transaction down to the child transaction:
1async nestedTrx () { 2 await this.ctx.model.transaction(async parentTrx => { 3 await this.ctx.model.M1.create() 4 await this.ctx.model.transaction({ transaction: parentTrx }, async () => { 5 await this.ctx.model.M2.create() 6 await this.ctx.model.M3.create() 7 }) 8 await this.ctx.model.M4.create() 9 }) 10}
1$ npm i egg-sequelize-autotrx --save
You need to use egg-sequelize plugin first, and have it CLS enabled with cls-hooked:
1// config.xx.js 2const mySequelize = require('sequelize') 3const clsNamespace = require('cls-hooked').createNamespace('your-namespace') 4mySequelize.useCLS(clsNamespace) 5 6module.exports = appInfo => { 7 const config = exports = {} 8 9 // use customized sequelize. https://github.com/eggjs/egg-sequelize#customize-sequelize 10 config.sequelize = { 11 Sequelize: mySequelize, 12 dialect: 'mysql', 13 // ... 14 } 15}
1// config.xx.js 2const mySequelize = require('sequelize') 3const clsNamespace = require('cls-hooked').createNamespace('your-namespace') 4 5// create multiple namespaces for multiple customized sequelize 6mySequelize.useCLS(clsNamespace) 7 8module.exports = appInfo => { 9 const config = exports = {} 10 11 // for multiple datasource, you need setup CLS of each your specific sequelize with different namespaces. https://github.com/eggjs/egg-sequelize#multiple-datasources 12 config.sequelize = { 13 Sequelize: mySequelize, 14 datasources: [{ 15 delegate: 'model1', 16 dialect: 'mysql' 17 // ... 18 }, { 19 delegate: 'model2', 20 dialect: 'mysql' 21 // ... 22 }] 23 }
enable sequelize-autotrx plugin:
1// {app_root}/config/plugin.js 2exports.sequelizeAutotrx = { 3 enable: true, 4 package: 'egg-sequelize-autotrx', 5}
1// {app_root}/config/config.default.js 2exports.sequelizeAutotrx = { 3 // no config required here 4}
see config/config.default.js for more detail.
Let's see a real case:
1// controller.js 2async nestedTransactionTest () { 3 await this.ctx.model.transaction(async () => { 4 await this.ctx.model.Project.create() 5 await this.innerTrx() 6 await this.ctx.model.User.create() 7 }) 8} 9 10async innerTrxCanBeExecAlone () { 11 await this.nestedTrx() 12} 13 14// this transaction can be execute alone, and also can be nested into another transaction 15async innerTrx () { 16 await this.ctx.model.transaction(async () => { 17 // other model operations 18 }) 19}
If you need your nested transaction commit by itself, you can do:
1async innerTrx () { 2 await this.ctx.model.transaction({ transaction: null }, async () => { 3 // specify the transaction as null, so it will not populated from parent transaction from cls 4 }) 5}
Please open an issue here.
No vulnerabilities found.
No security vulnerabilities found.