egg-sequelize-autotrx
Advanced tools
Comparing version 0.0.19 to 0.0.20
@@ -27,7 +27,5 @@ module.exports = (options, app) => async (ctx, next) => { | ||
const namespace = model.constructor._cls | ||
console.log('--> ', namespace) | ||
if (!namespace) { | ||
console.log('!!! !!!! no namespace') | ||
throw new Error(`[egg-sequelize-autotrx] needs CLS enabled in sequelize, but CLS namespace is not found under ${delegate} datasource`) | ||
throw new Error(`[egg-sequelize-autotrx] needs CLS enabled in sequelize, but CLS namespace is not found under ctx.${delegate} datasource`) | ||
} | ||
@@ -34,0 +32,0 @@ |
{ | ||
"name": "egg-sequelize-autotrx", | ||
"version": "0.0.19", | ||
"version": "0.0.20", | ||
"description": "Auto transaction based on cls-hooked for egg-sequelize plugin", | ||
@@ -5,0 +5,0 @@ "eggPlugin": { |
@@ -23,6 +23,8 @@ # egg-sequelize-autotrx | ||
<!-- | ||
Description here. | ||
--> | ||
This plugin helps to do transaction auto pass down and solve the nested transaction issue. | ||
## Problems | ||
TODO desc the case with code ... | ||
## Install | ||
@@ -36,3 +38,27 @@ | ||
enable CLS of sequelize: | ||
```js | ||
// config.xx.js | ||
const mySequelize = require('sequelize') | ||
const clsNamespace = require('cls-hooked').createNamespace('your-namespace') | ||
mySequelize.useCLS(clsNamespace) | ||
module.exports = appInfo => { | ||
const config = exports = {} | ||
// single datasource case | ||
config.sequelize = { | ||
Sequelize: mySequelize, // use customized sequelize. https://github.com/eggjs/egg-sequelize#customize-sequelize | ||
dialect: '', | ||
// ... | ||
} | ||
// for multiple datasource, you need setup CLS of each your specific sequelize with different namespaces. https://github.com/eggjs/egg-sequelize#multiple-datasources | ||
} | ||
``` | ||
enable sequelize-autotrx plugin: | ||
```js | ||
// {app_root}/config/plugin.js | ||
@@ -42,3 +68,3 @@ exports.sequelizeAutotrx = { | ||
package: 'egg-sequelize-autotrx', | ||
}; | ||
} | ||
``` | ||
@@ -48,6 +74,8 @@ | ||
No configuration required: | ||
```js | ||
// {app_root}/config/config.default.js | ||
exports.sequelizeAutotrx = { | ||
}; | ||
} | ||
``` | ||
@@ -59,4 +87,36 @@ | ||
<!-- example here --> | ||
### single datasource | ||
```js | ||
// controller.js | ||
async nestedTransactionTest () { | ||
await this.ctx.model.transaction(async () => { | ||
// if any of below operations failed, will rollback all | ||
await this.createProject() | ||
await this.nestedTrx() | ||
await this.createUser() | ||
}) | ||
} | ||
async nestedTrxCanBeExecAlone () { | ||
await this.nestedTrx() | ||
} | ||
async createProject () { | ||
await this.ctx.model.Project.create() | ||
} | ||
async createUser () { | ||
await this.ctx.model.User.create() | ||
} | ||
async nestedTrx () { | ||
await this.ctx.model.transaction(async () => { | ||
// other model operations | ||
}) | ||
} | ||
``` | ||
### multiple datasource | ||
## Questions & Suggestions | ||
@@ -63,0 +123,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10168
124
50