Skip to content
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

Open
SMAH1 opened this issue Jul 17, 2017 · 17 comments
Open

How to enabe CLS? #58

SMAH1 opened this issue Jul 17, 2017 · 17 comments

Comments

@SMAH1
Copy link

SMAH1 commented Jul 17, 2017

In this link show how to enable cls for sequelize.

What are we can do it in sequelize-typescript?

@RobinBuschmann
Copy link
Member

Since Sequelize of sequelize-typescript extends Sequelize of sequelize libraray, useCLS is available. But this method is not part of the typescript type definitions, so if you want to make the compiler happy, you need to access useCLS like this:

import {Sequelize} from 'sequelize-typescript';

Sequelize['useCLS'](namespace);

@SMAH1 SMAH1 closed this as completed Jul 18, 2017
@thormengkheang
Copy link

@RobinBuschmann I can't use the way you mention. the compiler throw an error
image

@RobinBuschmann
Copy link
Member

This is caused by the noImplicitAny: true tsc compiler option. You can avoid this via Sequelize['useCLS' as any](namespace).

But we should add useCLS to the typings.

@thormengkheang
Copy link

@RobinBuschmann yeah adding it to the typing would be nice.

@thormengkheang
Copy link

@RobinBuschmann still the same error by using Sequelize['useCLS' as any](namespace). I tried (Sequelize as any)['useCLS'](namespace); still didn't work

image

select statement didn't execute the transaction it's executing the default.
image

@RobinBuschmann
Copy link
Member

@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

@thormengkheang
Copy link

@RobinBuschmann as you can see in the sql log the select statement doesn't execute with the transaction id

@RobinBuschmann
Copy link
Member

@mengkheang Sry, I didn't notice. Did you tried it with pure sequelize?

@thormengkheang
Copy link

thormengkheang commented Oct 27, 2017

@RobinBuschmann haven't tried it yet let me try it and see what happen.

@thormengkheang
Copy link

@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;

@thormengkheang
Copy link

@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);

@RobinBuschmann
Copy link
Member

@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!

@thormengkheang
Copy link

@RobinBuschmann yeah I set useCLS on the origin sequelize class but I use sequelize-typescript instance and it's working.

@chanlito
Copy link
Contributor

@mengkheang what sorcery is this?

@thormengkheang
Copy link

@BruceHem hack

@blankstar85
Copy link
Contributor

blankstar85 commented Nov 11, 2017

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;
  }
}

@santiagodoldan
Copy link
Contributor

Here there's another workaround to enable CLS using sequelize-typescript

  (Sequelize as any).__proto__.useCLS(namespace)

__proto__ returns the original Sequelize class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants