So now that we have a pattern in place to run with Prisma, we're going to refactor auth, which is
going to follow the same exact pattern.
Let's start off by going to the package JSON and adding the dependencies we know we're going to need.
So I'll stop my container and CD into the auth folder, which we know already has its own package,
JSON.
So we can utilize this to install the dependencies we're going to need.
We'll p npm install add prisma client again and Prisma.
Also go ahead and now install the development dependency the dot env cli.
So now we have these dependencies.
We can go back and start off by creating our Prisma folder where we're going to host our schema and
any migrations.
So let's create the.
Schema dot prisma.
And now I'll go ahead and copy the existing Schema.prisma we have for reservations, we can simply copy
this and paste it in to auth, since it's going to be exactly the same.
We're now outputting these types to our local auth node modules.
We're going to be pulling off the database URL.
And now we can change our model to instead be the reservation.
We know we have the user model.
So inside of the user we have the id.
Again I want to declare as an integer that will default auto increment.
So this will be the ad id.
Then we have the email of type string, password string and finally rolls which is going to be a string
array.
So we can reference this as an array type.
So with our schema in place let's go ahead and create a new dot m dot local where we know we're going
to provide the database URL.
So again I'll copy this from our env local in reservation.
Now paste it in.
And the only thing we have to swap out is the database.
So instead of reservations is going to be the actual database.
So instead it's going to be slash auth.
Now we'll go into our package.json and I want to add the same migrate command that we've added in reservations.
So just as we've been doing let's go ahead and copy that over.
So we have access to it in this microservice as well, which we know is going to generate migrations.
Just for this auth service.
We're going to utilize dot env to use the dot env dot local and then execute our migrations and create
new migration scripts for these.
So let's go ahead and test everything out as we have it.
Now I can run npm run migrate.
So this is going to detect the Postgres server that we're running right now.
And it's going to ask us for the name of the migration I'll call this user.
So it's going to execute the migration and create a file for it as well as generate our types.
So now we can see our migrations folder which includes this script to create the user table.
Let's go ahead and start refactoring our auth service code now to work with Prisma.
So firstly on the auth module let's swap out the validation schema to validate the m variables instead
of the MongoDB URI.
We're using the database URL.
So next up, let's copy over our database URL from our m dot local and paste it into our dot env to
replace the MongoDB URI.
So this is going to be the database URL we provide to the running Docker container.
And we'll swap out the host name for the Postgres container.
Next up we'll go into our users folder where we actually are utilizing the current repository.
And we can start off by removing our users repository which we're no longer utilizing.
And I can go ahead and create a Prisma service inside of auth.
Now, just like we did in reservations, let's create a new Prisma service that's going to be just for
auth.
We can copy the one in reservations we currently have and paste it into auth now.
So the key difference here is now that we're extending the types that are local just for auth service
here, as opposed to in reservations.
This is a separate node modules folder as this path indicates because we're using this dot Prisma import.
So these types are going to be specific to the schemas in each service.
So now we have the Prisma service.
Go back to our users module where we're going to go ahead and replace the users repository with now
the Prisma service.
Go ahead and remove all of these unused imports and the imports to the database module as well.
Now we'll head back to the users controller and remove the import the user document here.
And now we can actually reference the user type directly from Prisma client.
So I'll go ahead and import user from dot Prisma slash client.
Now let's go into the user service where we have to refactor this to use the Prisma service instead.
So I'll go ahead and remove the users repository and remove it from the constructor as well.
And let's instead inject the Prisma service.
Now for the user creation.
Let's go ahead and take out these properties that we're passing to users repository.
And now let's call Prisma service dot user and create with the data object.
And now specify these properties.
So these are the properties that Prisma client expects because the schema matches this data object.
For validate create user DTO to find the user we'll call this dot Prisma service dot user dot find.
First or throw.
And we have to use find first or throw instead of find unique because we're searching on a non unique
property.
The email here.
So let's cut this out and replace it with the Where clause that we know Prisma expects.
And look for that user by email.
If we don't find them, we of course throw an error meaning that the user's email is valid.
And now for verify user, we can use the same call to get the user.
We can call Prisma service dot user dot find first or throw again.
Because we again are looking for the user by email.
So we replace this with a Where statement and look for the user by email.
And finally and get user here we're going to go ahead and specify Prisma service.
Dot user dot find unique or throw.
And that's because I want to look for the user by ID, which is what the Get user DTO includes.
So we'll have a where that checks to see for the users ID is equal to get user dto.id.
Now we need to go ahead and update this.
Get user DTO.
I want it to just be ID instead of underscore ID, and then we need to convert it into the number that
Prisma expects.
Because right now it's a string.
So we'll coerce this into a number.
So a few other updates we need to make to work with Prisma is going to be going into our auth controller.
And again we're not referencing the user document in the auth service anymore.
Now we can reference the user directly from Prisma client, which is we know where the source of truth
for the user entity lives.
We'll also go into the auth service and make the same change.
Replace user document out for user type from Prisma client and also update the user ID property here
on login.
This is going to be user.id now and it's no longer going to be a string value.
So we'll just supply user ID.
That also means we need to update the token payload interface user ID to now be a number which represents
our Prisma model.
Finally go into our auth strategies folder and into the JWT strategy.
So now invalidate.
Instead of supplying the get user DTO with underscore id, we simply use id and to turn this into a
string which the function expects, I'll call two string on the number.
So now we've refactored the auth service to work with Prisma.
Let's go ahead and update our docker file and our Docker compose.
Just the same we've done with reservations.