egg-sequelize-autotrx
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -7,20 +7,36 @@ module.exports = (options, app) => async (ctx, next) => { | ||
const { namespace } = app.config.sequelizeAutoTrx | ||
const sequelizeConfig = app.config.sequelize | ||
if (!ctx.model.transaction.__injected) { | ||
const oldTrx = ctx.model.transaction | ||
ctx.model.transaction = async task => { | ||
if (sequelizeConfig.datasources) { // multiple datasources | ||
sequelizeConfig.datasources.forEach(datasource => { | ||
inject(ctx, getDelegate(datasource), namespace) | ||
}) | ||
} else { // single datasource | ||
inject(ctx, getDelegate(sequelizeConfig), namespace) | ||
} | ||
await next() | ||
} | ||
// refer: https://github.com/eggjs/egg-sequelize#multiple-datasources | ||
const getDelegate = datasource => datasource.delegate || 'model' | ||
const inject = (ctx, delegate, namespace) => { | ||
const model = ctx[delegate] | ||
if (!model.transaction.__injected) { | ||
const oldTrx = model.transaction | ||
model.transaction = async task => { | ||
let transaction = namespace.get('transaction') | ||
if (transaction) { | ||
console.log('-- has parent trx') | ||
return oldTrx.call(ctx.model, { transaction }, task) | ||
app.loggers.coreLogger.info(`[egg-sequelize-autotrx] ctx.[${delegate}].transaction call injected transaction method`) | ||
return oldTrx.call(model, { transaction }, task) | ||
} else { | ||
console.log('-- no parent trx') | ||
return oldTrx.call(ctx.model, task) | ||
app.loggers.coreLogger.info(`[egg-sequelize-autotrx] ctx.[${delegate}].transaction call original transaction method`) | ||
return oldTrx.call(model, task) | ||
} | ||
} | ||
ctx.model.transaction.__injected = true | ||
console.log('ctx.model.transaction injected') | ||
model.transaction.__injected = true | ||
app.loggers.coreLogger.info(`[egg-sequelize-autotrx] ctx.[${delegate}].transaction is injected`) | ||
} | ||
await next() | ||
} |
{ | ||
"name": "egg-sequelize-autotrx", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Auto transaction based on cls-hooked for egg-sequelize plugin", | ||
@@ -5,0 +5,0 @@ "eggPlugin": { |
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
8641
50