-
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
How to enabe CLS? #58
Comments
Since import {Sequelize} from 'sequelize-typescript';
Sequelize['useCLS'](namespace); |
@RobinBuschmann I can't use the way you mention. the compiler throw an error |
This is caused by the But we should add |
@RobinBuschmann yeah adding it to the typing would be nice. |
@RobinBuschmann still the same error by using select statement didn't execute the transaction it's executing the default. |
@mengkheang your screens looking good to me. According to the sequelize docs it seems to be working like it should: http://docs.sequelizejs.com/manual/tutorial/transactions.html |
@RobinBuschmann as you can see in the sql log the select statement doesn't execute with the transaction id |
@mengkheang Sry, I didn't notice. Did you tried it with pure sequelize? |
@RobinBuschmann haven't tried it yet let me try it and see what happen. |
@RobinBuschmann it's working with pure sequelize const cls = require('continuation-local-storage'),
namespace = cls.createNamespace('sequelize-transaction');
const Sequelize = require('sequelize');
Sequelize.useCLS(namespace);
const sequelize = new Sequelize('test', 'root', 'password', {
host: 'localhost',
dialect: 'mysql'
});
const Project = sequelize.define('project', {
title: Sequelize.STRING,
description: Sequelize.TEXT
});
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
sequelize.transaction(async function(t1) {
console.log('check cls:', namespace.get('transaction') === t1); // true
await Project.find();
});
})
.catch((err) => {
console.error('Unable to connect to the database:', err);
}); SQL log Executing (deafc788-28ea-4fb8-80b2-63603c818a8c): START TRANSACTION;
check cls: true
Executing (deafc788-28ea-4fb8-80b2-63603c818a8c): SELECT `id`, `title`, `description`, `createdAt`, `updatedAt` FROM `projects` AS `project` LIMIT 1;
Executing (deafc788-28ea-4fb8-80b2-63603c818a8c): COMMIT; |
@RobinBuschmann finally got it's working using it origin class import * as cls from 'continuation-local-storage';
import * as SequelizeOrigin from 'sequelize';
import { Sequelize } from 'sequelize-typescript';
import { mysql as mysqlConfig } from '../config';
const namespace = cls.createNamespace('sequelize-transaction');
(SequelizeOrigin as any).useCLS(namespace); |
@mengkheang so you set useCLS on the origin sequelize class, continued using a sequelize-typescript sequelize instance and it is working now? Strange, but good to here that you got it to work. However this need to be investigated. Thanks for posting this! |
@RobinBuschmann yeah I set useCLS on the origin sequelize class but I use sequelize-typescript instance and it's working. |
@mengkheang what sorcery is this? |
@BruceHem hack |
I have cls working on mine, and seems to be working correctly. here: const ns = cls.createNamespace('sequelize-transaction');
class Database {
private _sequelize: Sequelize;
constructor() {
(Sequelize as any)['useCLS'](ns);
this._sequelize = new Sequelize({
database: config.database,
dialect: config.dialect,
host: config.host,
username: config.username,
password: config.password,
define: {
freezeTableName: true,
underscored: true,
},
});
this._sequelize.addModels([
// loaded all models.
]);
}
getSequelize() {
return this._sequelize;
}
} |
Here there's another workaround to enable CLS using
|
In this link show how to enable cls for sequelize.
What are we can do it in sequelize-typescript?
The text was updated successfully, but these errors were encountered: