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

Introduce transaction hooks in @tsed/mikro-orm #2370

Closed
2 tasks
derevnjuk opened this issue Jul 10, 2023 · 3 comments · Fixed by #2378
Closed
2 tasks

Introduce transaction hooks in @tsed/mikro-orm #2370

derevnjuk opened this issue Jul 10, 2023 · 3 comments · Fixed by #2378

Comments

@derevnjuk
Copy link
Contributor

Information

The current version of @tsed/mikro-orm lacks a comprehensive set of transaction hooks that allow developers to customize transaction behavior. To address this limitation, I propose the addition of two new interfaces, BeforeTransactionCommit and AfterTransactionCommit, which provide the ability to define hooks executed right before and after committing data to the database.

Example

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

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

  $beforeTransactionCommit(em: EntityManager): Promise<unknown> | unknown {
    // Custom code executed before committing data
  }
}

Use Case

The proposed transaction hooks would empower developers to implement advanced patterns such as the Inbox pattern or domain event dispatch. With BeforeTransactionCommit, developers can modify data within the same transaction context, allowing them to perform additional operations before the transaction is committed. Conversely, AfterTransactionCommit enables executing code after the data is committed, making it suitable for handling eventual consistency and compensatory actions in case of failures.

Acceptance criteria

  • The $beforeTransactionCommit event should be fired within the same transaction context before the data is committed to the database.
  • The $afterTransactionCommit method should be fired after the data is successfully committed to the database.
@derevnjuk derevnjuk self-assigned this Jul 10, 2023
derevnjuk added a commit to derevnjuk/tsed that referenced this issue Jul 10, 2023
derevnjuk added a commit to derevnjuk/tsed that referenced this issue Jul 11, 2023
@derevnjuk
Copy link
Contributor Author

Once #2293 is merged, this request will be addressed as well.

derevnjuk added a commit to derevnjuk/tsed that referenced this issue Jul 11, 2023
derevnjuk added a commit to derevnjuk/tsed that referenced this issue Jul 11, 2023
derevnjuk added a commit to derevnjuk/tsed that referenced this issue Jul 11, 2023
derevnjuk added a commit to derevnjuk/tsed that referenced this issue Jul 13, 2023
@github-actions
Copy link

🎉 Are you happy?

If you appreciated the support, know that it is free and is carried out on personal time ;)

A support, even a little bit makes a difference for me and continues to bring you answers!

github opencollective

@Romakita
Copy link
Collaborator

🎉 This issue has been resolved 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 a pull request may close this issue.

2 participants