-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sequelize.useCLS not working? #1083
Comments
OK, it seems to be a bug. Sequelize.useCLS(ns); doesn't set the namespace in the original Sequelize class. After some debugging... this works. // import * as SequelizeOrigin from 'sequelize';
import { Sequelize } from 'sequelize-typescript';
import * as cls from 'cls-hooked';
const ns = cls.createNamespace('sequelize-transaction');
// Sequelize.useCLS(ns); // Doesn't work!
// Sequelize['useCLS'](ns); // Doesn't work!
(Sequelize as any).__proto__.useCLS(ns); // This works
// (SequelizeOrigin as any).useCLS(ns); // This works too The interceptor needs to be changed too, to return Promise<Observable> which resolves after the observable finishes. import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { firstValueFrom, Observable } from 'rxjs';
import { Transaction } from 'sequelize';
import { Sequelize } from 'sequelize-typescript';
@Injectable()
export class TransactionInterceptor implements NestInterceptor {
constructor(private readonly sequelize: Sequelize) {}
async intercept(
context: ExecutionContext,
next: CallHandler,
): Promise<Observable<any>> {
return this.sequelize.transaction((tx: Transaction) => {
const ctx = GqlExecutionContext.create(context);
ctx.getContext().transaction = tx;
return firstValueFrom(next.handle());
});
}
} Kind regards, |
Is this still an issue? It should have been fixed in Sequelize 6.7.0 by sequelize/sequelize#13510 |
Issue
Configuration for CLS (Continuation-Local Storage) seems not to work or I'm doing something wrong.
CRUD operations don't join existing transaction.
Any chance to get it done right?
Versions
Issue type
Actual behavior
CRUD operations not joining current transaction.
Expected behavior
Opposite :)
Steps to reproduce
Configuration (Somewhere on application init, before module initialization)
Interceptor
Not working version
Working version (the uggly one, passing transaction to each statement)
The text was updated successfully, but these errors were encountered: