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

feat(mikro-orm): introduce transaction hooks #2378

Conversation

derevnjuk
Copy link
Contributor

@derevnjuk derevnjuk commented Jul 11, 2023

Information

Type Breaking change
Feature No

Usage example

The transaction hooks allow you to customize the default transaction behavior. These hooks enable you to execute custom code before and after committing data to the database. These transaction hooks provide a flexible way to extend the default transaction behavior and implement advanced patterns such as the Inbox pattern or domain event dispatching.

BeforeTransactionCommit Hook

The BeforeTransactionCommit interface allows you to define hooks that are executed right before committing data to the database. This hook provides a way to modify data within the same transaction context and perform additional operations before the transaction is committed.

To use the BeforeTransactionCommit hook, first, you have to implement the BeforeTransactionCommit interface:

import {BeforeTransactionCommit} from "@tsed/mikro-orm";
import {EntityManager} from "@mikro-orm/core";
import {Injectable} from "@tsed/di";

@Injectable()
export class Hooks implements BeforeTransactionCommit {
  $beforeTransactionCommit(em: EntityManager): Promise<unknown> | unknown {
    // Custom code executed before committing data
  }
}

Then just write your code inside the $beforeTransactionCommit method. This code will be executed before the transaction is committed.

AfterTransactionCommit Hook

The AfterTransactionCommit interface allows you to define hooks that are executed right after committing data to the database. This hook enables you to execute code after the data is committed, making multiple transactions.

To use the AfterTransactionCommit hook, you have to implement the AfterTransactionCommit interface:

import {AfterTransactionCommit} from "@tsed/mikro-orm";
import {EntityManager} from "@mikro-orm/core";
import {Injectable} from "@tsed/di";

@Injectable()
export class Hooks implements AfterTransactionCommit {
  $afterTransactionCommit(em: EntityManager): Promise<unknown> | unknown {
    // Custom code executed after committing data
  }
}

It's important to note that when using the AfterTransactionCommit hook, you need to handle eventual consistency and compensatory actions in case of failures on your own.

Todos

  • Tests
  • Coverage
  • Example
  • Documentation

closes #2370

@derevnjuk derevnjuk self-assigned this Jul 11, 2023
@derevnjuk derevnjuk force-pushed the feat_#2370/introduce_transaction_hooks_in_tsed_mikro-orm branch from 6c3a4b6 to f4310a8 Compare July 11, 2023 18:14
@derevnjuk derevnjuk requested a review from Romakita July 11, 2023 18:15
@derevnjuk derevnjuk force-pushed the feat_#2370/introduce_transaction_hooks_in_tsed_mikro-orm branch from f4310a8 to 6524774 Compare July 11, 2023 19:14
@derevnjuk derevnjuk marked this pull request as ready for review July 12, 2023 08:28
@derevnjuk derevnjuk force-pushed the feat_#2370/introduce_transaction_hooks_in_tsed_mikro-orm branch from 6524774 to b1bca7b Compare July 13, 2023 08:17
@derevnjuk
Copy link
Contributor Author

@Romakita, could you kindly review this when you have a moment? Thank you.

@Romakita
Copy link
Collaborator

Perfect :)
Go to merge when you are ready!

@derevnjuk derevnjuk merged commit 7caceab into tsedio:production Jul 20, 2023
19 checks passed
@derevnjuk derevnjuk deleted the feat_#2370/introduce_transaction_hooks_in_tsed_mikro-orm branch July 20, 2023 07:01
@Romakita
Copy link
Collaborator

🎉 This PR is included in version 7.34.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

Introduce transaction hooks in @tsed/mikro-orm
2 participants