Easy way to handle nested transactions

If you use CLS, a very simple helper function will do the work.

const cls = require('continuation-local-storage');
const Sequelize = require('sequelize');
const NAMESPACE = 'your-namespace';

// Use CLS for Sequelize
Sequelize.cls = cls.createNamespace(NAMESPACE);
const sequelize = new Sequelize(...);

/* * * * * * * * * * * * * * * * * * * * *
 * THE MAGIC: Create a transaction wrapper
 * * * * * * * * * * * * * * * * * * * * */

function transaction(task) {
  return cls.getNamespace(NAMESPACE).get('transaction') ? task() : sequelize.transaction(task);
};

/* * * * * * * * * * * * * * * * * * * * *
 * Your code below
 * * * * * * * * * * * * * * * * * * * * */

function addUser(userName, password) {
  return transaction(function() {
    return AccountModel
      .create(...)
      .then(() => UserModel.create(...));
  });
}

function addTeam() {
  return transaction(function() {
    return TeamModel
      .create(...)
      .then(() => addUser(...));
  });
}


sequelize.transaction accepts an options object - If options.transaction is set, this will create a savepoint in the transaction (provided that the SQL dialects supports it), otherwise it will create a new transaction

http://docs.sequelizejs.com/en/latest/api/sequelize/#transactionoptions-promise

So you should be able to do simply

sequelize.transaction({ transaction }, x=>func(t));