-
-
Notifications
You must be signed in to change notification settings - Fork 504
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(core): implement transaction lifecycle hooks #1213
Conversation
Current lifecycle hooks cover entity and unit of work events, but transaction-related ones are missing. Add beforeTransactionStart, afterTransactionStart, beforeTransactionCommit, afterTransactionCommit, beforeTransactionRollback, and afterTransactionRollback Closes #1175
Btw, these hooks work correctly with nested transactions ( |
Looking good, thanks! Will have few code style hints, might change it myself as I want to fiddle around with this locally anyway to understand it better. Also few lines in That One thing that looks stinky is that you are using |
@B4nan glad to help.
I will check this today.
The idea here is that what the broadcaster is is agnostic to the connection and we could provide any extra context we want from the call site in the future. For example, we could choose to provide the unit of work when doing implicit transactions. I actually had planned to define the EventBroadcaster interface in the
Currently mongo doesn't allow for child transactions (save points), but they might in the future and in that case checking to see if it is a root transaction or not sounds like a general concern. Let me know your thoughts. I can easily move that to AbstractSqlConnection. |
this may be useful. also add tests for transaction hooks using mongo Related #1175
Pushed some of the changes mentioned, added tests for mongo, and fixed coverage errors. |
Thank you, @B4nan. |
Hi @rhyek, I was struggling with afterTransactionCommit hook because I didn't understand why Is there a reason why I've created the question in SO too: https://stackoverflow.com/questions/76851480/in-mikro-orm-is-it-possible-to-track-unit-of-work-changes-in-the-aftertransacti Thanks in advance. |
Current lifecycle hooks cover entity and unit of work events, but
transaction-related ones are missing. Add beforeTransactionStart,
afterTransactionStart, beforeTransactionCommit, afterTransactionCommit,
beforeTransactionRollback, and afterTransactionRollback
Closes #1175
Although #1175 mentions implementing "after" entity hooks post commit, they are not included in this PR as they are not needed. Refer to the
UserSubscriber
example class in test fileGH1175.test.ts
to see how to implement an ad-hocafterCommitCreate
hook using a combination ofafterTransactionStart
,afterCreate
, andafterTransactionCommit
.Currently, all this PR is missing right now is documentation. Please let me know if you need me to do that as well, but I was thinking maybe @B4nan could lend a hand with that as he might have a better idea regarding form, style, and content.