viniciusjssouza / typeorm-transactional-tests

Add transactional tests to typeorm projects
MIT License
54 stars 11 forks source link

fix: support multiple data-source #21

Open HeemanKimZig opened 1 year ago

HeemanKimZig commented 1 year ago

Hello,

I hope this message finds you well. I wanted to inform you about a bug I've encountered while using your open-source package.

In a NESTJS + TypeORM, our project utilizes two datasources, A and B. However, when using more than one datasource, I've identified a particular issue: the queryRunner seems to execute against datasource A regardless of whether we're trying to access data from datasource A or B.

To address this, I've made the following modifications to the code, which resolve this problem when working in an environment with two datasources:

  private monkeyPatchQueryRunnerCreation(queryRunner: QueryRunnerWrapper): void {
    this.originQueryRunnerFunction = DataSource.prototype.createQueryRunner; 
    DataSource.prototype.createQueryRunner = () => queryRunner;
    // replace this
    this.originQueryRunnerFunction = this.connection.createQueryRunner;
    this.connection.createQueryRunner = () => queryRunner;
  }

  private restoreQueryRunnerCreation(): void {
   DataSource.prototype.createQueryRunner = this.originQueryRunnerFunction; 
    // replace this
    this.connection.createQueryRunner = this.originQueryRunnerFunction;
  }

I believe the root cause of this issue lies in how the queryRunner is interacting with multiple datasources. By making the above changes, I've been able to rectify the problem within our project.

I kindly request you to review and consider applying these changes. Your assistance in addressing this matter would be greatly appreciated.

Thank you.

clintonb commented 1 year ago

I just encountered this issue. @viniciusjssouza can you review and release, please?