Hi,
I am facing an issue related to transaction, my code as below
import * as clsHooked from 'cls-hooked';
// import * as cls from 'continuation-local-storage';
import * as clsBluebird from 'cls-bluebird';
import { Sequelize, ISequelizeConfig } from 'sequelize-typescript';
import * as bluebird from 'bluebird';
const ns = clsHooked.createNamespace('sequelize-transaction-namespace');
clsBluebird(ns, bluebird);
Sequelize.useCLS(ns);
Then when I trying to execute create entity within transaction, but rollback does not work
return this.sequelize.transaction(async (trx) => {
await Permit.upsert({
companyId: 1,
userId: 1
});
throw new Error('Should be rollback');
});
To make this works, I have to pass the trx manually
return this.sequelize.transaction(async (trx) => {
await Permit.upsert({
companyId: 1,
userId: 1
}, { transaction: trx });
throw new Error('Should be rollback');
});
Do we have any solution or workaround to fix this issue?
Hey @stormit-vn according to the sequelize docs (http://docs.sequelizejs.com/manual/tutorial/transactions.html) there is no other way than passing them manually to get the transactions to work. Or have I overseen something?
@RobinBuschmann Rob actually, I got it working using below code
import { createNamespace } from "cls-hooked"
import { Sequelize } from "sequelize-typescript"
const namespace = createNamespace("sequelize-cls-namespace")
(Sequelize as any).__proto__.useCLS(namespace)
As you can see, we need to assign the namespace to the original Sequelize object, not the one sequelize-typescript returns.
Most helpful comment
@RobinBuschmann Rob actually, I got it working using below code
As you can see, we need to assign the namespace to the original Sequelize object, not the one sequelize-typescript returns.