-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Make event bus subscriptions transaction-safe
Fixes #1107. Relates to #520. This commit introduces a new mechanism to guarantee that event subscribers do not run into issues with database transactions. It works by postponing the publishing on an event until the associated transaction has completed. Thus the subscriber can be assured that all data changes in the transaction are available to read right away.
- Loading branch information
1 parent
d35306f
commit f0fd662
Showing
11 changed files
with
241 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectConnection } from '@nestjs/typeorm'; | ||
import { merge, Subject } from 'rxjs'; | ||
import { filter, map, take } from 'rxjs/operators'; | ||
import { Connection, EntitySubscriberInterface } from 'typeorm'; | ||
import { EntityManager } from 'typeorm/entity-manager/EntityManager'; | ||
import { QueryRunner } from 'typeorm/query-runner/QueryRunner'; | ||
import { TransactionCommitEvent } from 'typeorm/subscriber/event/TransactionCommitEvent'; | ||
import { TransactionRollbackEvent } from 'typeorm/subscriber/event/TransactionRollbackEvent'; | ||
|
||
export interface TransactionSubscriberEvent { | ||
/** | ||
* Connection used in the event. | ||
*/ | ||
connection: Connection; | ||
/** | ||
* QueryRunner used in the event transaction. | ||
* All database operations in the subscribed event listener should be performed using this query runner instance. | ||
*/ | ||
queryRunner: QueryRunner; | ||
/** | ||
* EntityManager used in the event transaction. | ||
* All database operations in the subscribed event listener should be performed using this entity manager instance. | ||
*/ | ||
manager: EntityManager; | ||
} | ||
|
||
/** | ||
* This subscriber listens to all transaction commit/rollback events emitted by TypeORM | ||
* so that we can be notified as soon as a particular queryRunner's transactions ends. | ||
* | ||
* This is used by the {@link EventBus} to prevent events from being published until their | ||
* associated transactions are complete. | ||
*/ | ||
@Injectable() | ||
export class TransactionSubscriber implements EntitySubscriberInterface { | ||
private commit$ = new Subject<TransactionSubscriberEvent>(); | ||
private rollback$ = new Subject<TransactionSubscriberEvent>(); | ||
|
||
constructor(@InjectConnection() private connection: Connection) { | ||
if (!connection.subscribers.find(subscriber => subscriber.constructor === TransactionSubscriber)) { | ||
connection.subscribers.push(this); | ||
} | ||
} | ||
|
||
afterTransactionCommit(event: TransactionCommitEvent) { | ||
this.commit$.next(event); | ||
} | ||
|
||
afterTransactionRollback(event: TransactionRollbackEvent) { | ||
this.rollback$.next(event); | ||
} | ||
|
||
awaitRelease(queryRunner: QueryRunner): Promise<QueryRunner> { | ||
if (queryRunner.isTransactionActive) { | ||
return merge(this.commit$, this.rollback$) | ||
.pipe( | ||
filter(event => event.queryRunner === queryRunner), | ||
take(1), | ||
map(event => event.queryRunner), | ||
) | ||
.toPromise(); | ||
} else { | ||
return Promise.resolve(queryRunner); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.