In this video #34 of our Next.js + MySQL Job Portal Application, I’ll show you how to properly use Transactions in backend/server actions using Drizzle ORM.
When a user registers, we insert data into three connected tables: ✅ Users table ✅ Applicant/Employer table (based on role) ✅ Sessions table
Since these three operations depend on each other, this is the perfect real-life example to understand transactions. ---------------------------------------------------------------------------------- 📂 Source Code 👉 GitHub Repo: https://github.com/thapatechnical/job-portal-nextjs
⌛TIMELINE⏳ 0:00 – Intro to transactions 1:10 – Why we need a transaction 3:10 – Using tx in our backend code 4:05 – Passing tx as an argument 5:15 – Typing tx parameter in TypeScript 7:20 – Using tx inside session function 10:00 – Next video update
Hello, what's up guys? Welcome to the next session of MySQL Present Job Portal Application.
Today is our day number 34 and today we are going to see transactions in our application.
Yes guys, transaction is such a term which is very important when it comes to your backend and database.
Because transaction makes sure that if you are doing 5 or 3 or 2 jobs at the same time,
then all the 5 jobs will be completed or none of the jobs will be completed. This is your transaction.
And if you don't know what is transaction in database,
then you will get motivation and I will get the motivation to keep uploading videos for you guys for free.
Otherwise, there is a lot of charge for all this.
Alright, if you are not checking out the previous video, then do check it out.
So, let's see why we need transaction here.
We have this odd.action.ts, basically our server action,
where whenever the user is registering, logging in and finally, logging out.
We have completed all the functionality.
Now, when he is registering, there are some things that are happening one by one, step by step.
So, wherever you have step by step, first this step, then this, then this, then this.
In this way, where you get step by step things,
there I know very well that if any step gets messed up in the middle,
then I don't want the previous one, I don't want the one after that because it got messed up in the middle.
I want from start till end very well.
So, transaction works there.
So, here you can see clearly, this is my query where I am inserting the user's data in our registration table.
Whenever any user is filling our form in this way, then what we have?
After that, we are making sure on the role that if the user, I mean, if the applicant is selected,
then his data will also go to the applicant table.
If he is not an applicant, if he is an employer, then the data will also go to the employer's table.
Not only that, once the data is inserted in the user's table,
then the applicant or employer is based on the role.
After that, one more very important thing, and that is, we were playing with sessions.
So, for that too, we are calling a function in this way, where we are creating a session,
storing the session token in the session table, then adding it in the cookies.
So, a lot of things are happening step by step.
So, we can add these steps in the transaction,
so that,
again, await db.we need to go with the transactions.
I guess, I guess, okay.
Expected one argument got two.
Type of database.
Give me the type of the first parameter of the callback inside db.transaction.
It is said in this way.
And that is what?
It means, db.transaction.
Look at this.
Give me the type of the first parameter in db.transaction.
And what did I do?
In this way, we passed it here.
And by the way, it remains the same.
You have to copy and paste it.
Its type is the same.
You can't do anything.
So, if you feel like it, copy it.
Otherwise, you understand that I have told you what it means.
Give me the type of the first parameter in db.transaction.
We got it.
You don't have to write it yourself.
You will understand whatever it is.
I have written it here.
Now, I have to pass this db here.
Because nothing is being used here at the moment.
But in this create user session, my db is definitely being used.
So, I will pass my tx to this function, which is another function of mine.
Now, here, object literal may only specify known properties.
And tx does not exist in type create session.
Here, I am giving a comma.
Now, look.
It remains the same.
Again, you have added 1, 2, 3, 4, and 5 here.
But our create session, how many are there here?
Here, if you see, create session data.
Here, there are only 4 in type.
So, I will have to define tx in its type as well.
And here, we have a db client.
Now, here, I would like to tell you one thing.
I am making our transaction optional.
If you have given the transaction, then use tx.
If not, then use normal db.
Okay.
This means this.
And my work is done.
Now, I don't have any warning here.
Look.
Now, that warning is gone.
Okay.
Now, this is my create session.
This is my create session.
I am using tx here as well.
And instead of db, I am using tx here.
This is it.
My work is done.
So, okay.
tx is possibly undefined.
And by the way, now here, this tx is possibly undefined,
so do one thing.
Equal to db.
So, here, equal to db means,
you understand this.
Either I can use db.
If the transaction has been given, then it will use the transaction.
For now, I have given the transaction.
So, transaction.
If it was not given, then db could have been used by default.
So, this is your transaction.
Now, look here.
Brother, all these three queries have come inside my transaction.
First, it should be stored in the database.
It is stored in the database.
So, after that, what was his role?
Was he an applicant?
Was he an employer?
So, based on role, basically, his ID will be added.
And finally, I passed the transaction in the session create as well.
So, everything should be equal.
So, that's why we saw the transaction.
And we saw a new thing.
If we want to define the type of our transaction,
then you can see its type like this.
Parameter, type of db transaction.
The first one, obviously, the first one in the array,
you select it like this.
Whatever it is, at least you know what the funda is.
So, yeah, guys, this is it.
There is no need to register for today's video clip.
Because everything is set.
There is no tension.
And finally, from tomorrow, we will work on our dashboard.
It will be a lot of fun.
I hope you liked this video a lot.
Because till now, in our series,
we have seen our complete authentication,
the entire login, logout, registration,
session, all the transactions.
We understood it by understanding the database.
So, I hope you are enjoying it step by step.
You are getting to learn a lot.
So, guys, if anyone has any doubt,
do comment and let me know.
If you liked the video, please like it.
And if you want me to upload videos for you guys for free,
So, please, you can support me.
You don't have to do anything.
Just subscribe to the channel.
Like and share the video with your friends.
Okay, our next target is 8 lakh subscribers.
I haven't subscribed yet.
Please do it.
See you in the next video.
Till then, guys, thank you so much.
Take care.