Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

egg-sequelize-autotrx

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

egg-sequelize-autotrx

Auto transaction based on cls-hooked for egg-sequelize plugin

  • 0.0.21
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

egg-sequelize-autotrx

NPM version build status Test coverage David deps Known Vulnerabilities npm download

This plugin helps to do transaction auto pass down and solve the nested transaction issue.

Problems

After CLS enabled, nested transaction will have unexpected result:

async nestedTrx () {
  await this.ctx.model.transaction(async () => {
    await this.ctx.model.M1.create()
    await this.ctx.model.transaction(async () => {
      await this.ctx.model.M2.create()
      await this.ctx.model.M3.create()
    })
    await this.ctx.model.M4.create()
  })
}

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:

async nestedTrx () {
  await this.ctx.model.transaction(async parentTrx => {
    await this.ctx.model.M1.create()
    await this.ctx.model.transaction({ transaction: parentTrx }, async () => {
      await this.ctx.model.M2.create()
      await this.ctx.model.M3.create()
    })
    await this.ctx.model.M4.create()
  })
}

Install

$ npm i egg-sequelize-autotrx --save

Usage

You need to use egg-sequelize plugin first, and have it CLS enabled with cls-hooked:

single datasource

// config.xx.js
const mySequelize = require('sequelize')
const clsNamespace = require('cls-hooked').createNamespace('your-namespace')
mySequelize.useCLS(clsNamespace)

module.exports = appInfo => {
  const config = exports = {}

  // use customized sequelize. https://github.com/eggjs/egg-sequelize#customize-sequelize
  config.sequelize = {
    Sequelize: mySequelize, 
    dialect: 'mysql',
    // ...
  }
}

multiple datasource

// config.xx.js
const mySequelize1 = require('sequelize')
const mySequelize2 = require('sequelize')
const clsNamespace1 = require('cls-hooked').createNamespace('your-namespace1')
const clsNamespace2 = require('cls-hooked').createNamespace('your-namespace2')

// create multiple namespaces for multiple customized sequelize
mySequelize1.useCLS(clsNamespace1)
mySequelize2.useCLS(clsNamespace2)

module.exports = appInfo => {
  const config = exports = {}

  // for multiple datasource, you need setup CLS of each your specific sequelize with different namespaces. https://github.com/eggjs/egg-sequelize#multiple-datasources
  config.sequelize = {
    datasources: [{
      Sequelize: mySequelize1,
      delegate: 'model1',
      dialect: 'mysql'
      // ...
    }, {
      Sequelize: mySequelize2,
      delegate: 'model2',
      dialect: 'mysql'
      // ...
    }]
  }

enable sequelize-autotrx plugin:

// {app_root}/config/plugin.js
exports.sequelizeAutotrx = {
  enable: true,
  package: 'egg-sequelize-autotrx',
}

Configuration

// {app_root}/config/config.default.js
exports.sequelizeAutotrx = {
  // no config required here
}

see config/config.default.js for more detail.

Example

Let's see a real case:

// controller.js
async nestedTransactionTest () {
  await this.ctx.model.transaction(async () => {
    await this.ctx.model.Project.create()
    await this.innerTrx()
    await this.ctx.model.User.create()
  })
}

async innerTrxCanBeExecAlone () {
  await this.nestedTrx()
}

// this transaction can be execute alone, and also can be nested into another transaction
async innerTrx () {
  await this.ctx.model.transaction(async () => {
    // other model operations
  })
}

Questions & Suggestions

Please open an issue here.

License

MIT

Keywords

FAQs

Package last updated on 27 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc