Ready to get started?
Join Adocasts Plus for $8/mo, or sign into an existing Adocasts Plus account, to get access to all of our lessons.
Building with AdonisJS & Inertia #5.3
Onboarding Newly Registered Users
We'll create our onboarding flow for newly registered users. Before users can enter the application, they'll need to have at least one organization set up so everything works smoothly.
- Created by
- @tomgobich
- Published
Join the Discussion 4 comments
-
Hello,
In this video, you are using a transaction withdb.transaction.
You apply the transaction to theawait Organization.create().
However, forthis.#assignAdminandthis.#createDefaults, you are not directly using it. Yet, these two calls are included in the callback function.
If an issue occurs inassignAdminorcreateDefaults, will the transaction still perform a rollback? Or will these two functions not trigger a rollback?1-
Responding to tigerwolf974
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.
return db.transaction(async (trx) => { const organization = await Organization.create(data, { client: trx }) organization.$trx // 👈 holds the transaction // ... })Copied!This transaction reference is then automatically used when we perform subsequent operations with this organization, which includes related operations.
return db.transaction(async (trx) => { const organization = await Organization.create(data, { client: trx }) // 👇 passes $trx on the organization along with the attach db operation return organization.related('users').attach({ [user.id]: { role_id: Roles.ADMIN, }, }) // ... })Copied!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.
const trx = await db.transaction() try { const organization = await Organization.create(data, { client: trx }) // 👇 passes $trx on the organization along with the attach db operation return organization.related('users').attach({ [user.id]: { role_id: Roles.ADMIN, }, }) // ... await trx.commit() } catch (error) { await trx.rollback() }Copied!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
assignAdminorcreateDefaultsthe 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_idthat doesn't exist. Since we're using a foreign key constraint here, attempting to do so will throw an error.static async #assignAdmin(organization: Organization, user: User) { return organization.related('users').attach({ [user.id]: { role_id: 45, //Roles.ADMIN, // 👈 replace admin with some non-existant id }, }) }Copied!1-
Responding to tomgobich
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.1-
Responding to tigerwolf974
-
-
-