Chapters
00:00 - Our End Goal
00:45 - Setting Up Our Organization Create Route & Controller
03:15 - Defining The Organization Create Page
06:18 - Storing A New Organization
10:02 - Making the User An Organization Admin
12:18 - Stubbing the Organization with Default Difficulties, Statuses, & Access Levels
16:38 - Managed Transactions & Improving our Promise Flow
18:40 - Calling Our Store Organization Action
19:38 - Redirecting New Users to our Onboarding Page
20:00 - Testing It Out
Join The Discussion! (4 Comments)
Please sign in or sign up for free to join in on the dicussion.
tigerwolf974
Hello,
In this video, you are using a transaction with
db.transaction
.You apply the transaction to the
await Organization.create()
.However, for
this.#assignAdmin
andthis.#createDefaults
, you are not directly using it. Yet, these two calls are included in the callback function.If an issue occurs in
assignAdmin
orcreateDefaults
, will the transaction still perform a rollback? Or will these two functions not trigger a rollback?Please sign in or sign up for free to reply
tomgobich
Hi Tigerwolf!
Lucid cascades the transaction to relationship operations for us! So, when we attach the transaction to the operation that creates the organization, the transaction reference will be kept in the organization instance.
This transaction reference is then automatically used when we perform subsequent operations with this organization, which includes related operations.
The managed transaction, then, wraps the callback within a try/catch block. The transaction is committed if the callback completes and no errors are thrown. Otherwise, if an error is caught, the transaction is rolled back. Essentially doing the below for us in a nicer syntax.
So, as long as we don't eat the errors thrown by the operations we're performing, the managed transaction will catch it and roll back our transaction. This is regardless of how we structure our code within the managed transaction, including splitting it into additional methods.
So, to answer your question, If an issue occurs in
assignAdmin
orcreateDefaults
the managed transaction will catch this and roll back our transaction for us! You can see this for yourself by trying to assign the user arole_id
that doesn't exist. Since we're using a foreign key constraint here, attempting to do so will throw an error.Please sign in or sign up for free to reply
tigerwolf974
Thank you, I had missed that information in the documentation. My English level is not very high, so there is some information that I sometimes overlook.
Thank you very much for this detailed and illustrated response.
Please sign in or sign up for free to reply
tomgobich
Anytime and no worries at all! That's completely understandable!! 😊
Please sign in or sign up for free to reply