WEBVTT

00:00.020 --> 00:05.750
So now that we have a pattern in place to run with Prisma, we're going to refactor auth, which is

00:05.750 --> 00:07.850
going to follow the same exact pattern.

00:07.880 --> 00:14.630
Let's start off by going to the package JSON and adding the dependencies we know we're going to need.

00:14.630 --> 00:22.820
So I'll stop my container and CD into the auth folder, which we know already has its own package,

00:22.820 --> 00:23.600
JSON.

00:23.600 --> 00:27.920
So we can utilize this to install the dependencies we're going to need.

00:27.950 --> 00:33.650
We'll p npm install add prisma client again and Prisma.

00:36.620 --> 00:42.710
Also go ahead and now install the development dependency the dot env cli.

00:43.010 --> 00:44.900
So now we have these dependencies.

00:44.900 --> 00:51.320
We can go back and start off by creating our Prisma folder where we're going to host our schema and

00:51.320 --> 00:52.640
any migrations.

00:52.820 --> 00:54.980
So let's create the.

00:55.530 --> 00:58.110
Schema dot prisma.

00:59.350 --> 01:05.920
And now I'll go ahead and copy the existing Schema.prisma we have for reservations, we can simply copy

01:05.920 --> 01:10.180
this and paste it in to auth, since it's going to be exactly the same.

01:10.180 --> 01:15.130
We're now outputting these types to our local auth node modules.

01:15.130 --> 01:17.620
We're going to be pulling off the database URL.

01:17.620 --> 01:21.580
And now we can change our model to instead be the reservation.

01:21.580 --> 01:23.860
We know we have the user model.

01:24.130 --> 01:26.980
So inside of the user we have the id.

01:26.980 --> 01:32.920
Again I want to declare as an integer that will default auto increment.

01:32.920 --> 01:34.540
So this will be the ad id.

01:35.200 --> 01:43.300
Then we have the email of type string, password string and finally rolls which is going to be a string

01:43.300 --> 01:43.990
array.

01:43.990 --> 01:46.540
So we can reference this as an array type.

01:46.540 --> 01:53.110
So with our schema in place let's go ahead and create a new dot m dot local where we know we're going

01:53.110 --> 01:55.180
to provide the database URL.

01:55.180 --> 01:59.500
So again I'll copy this from our env local in reservation.

01:59.500 --> 02:01.660
Now paste it in.

02:01.660 --> 02:04.630
And the only thing we have to swap out is the database.

02:04.630 --> 02:07.570
So instead of reservations is going to be the actual database.

02:07.570 --> 02:10.720
So instead it's going to be slash auth.

02:10.720 --> 02:18.340
Now we'll go into our package.json and I want to add the same migrate command that we've added in reservations.

02:18.340 --> 02:22.360
So just as we've been doing let's go ahead and copy that over.

02:22.360 --> 02:28.540
So we have access to it in this microservice as well, which we know is going to generate migrations.

02:28.540 --> 02:30.370
Just for this auth service.

02:30.370 --> 02:37.030
We're going to utilize dot env to use the dot env dot local and then execute our migrations and create

02:37.030 --> 02:39.070
new migration scripts for these.

02:39.070 --> 02:41.890
So let's go ahead and test everything out as we have it.

02:41.890 --> 02:46.090
Now I can run npm run migrate.

02:46.390 --> 02:50.470
So this is going to detect the Postgres server that we're running right now.

02:50.470 --> 02:55.150
And it's going to ask us for the name of the migration I'll call this user.

02:55.150 --> 03:00.820
So it's going to execute the migration and create a file for it as well as generate our types.

03:01.630 --> 03:07.600
So now we can see our migrations folder which includes this script to create the user table.

03:07.660 --> 03:14.260
Let's go ahead and start refactoring our auth service code now to work with Prisma.

03:14.260 --> 03:21.250
So firstly on the auth module let's swap out the validation schema to validate the m variables instead

03:21.250 --> 03:22.750
of the MongoDB URI.

03:22.780 --> 03:25.810
We're using the database URL.

03:25.810 --> 03:34.630
So next up, let's copy over our database URL from our m dot local and paste it into our dot env to

03:34.630 --> 03:36.730
replace the MongoDB URI.

03:36.760 --> 03:42.520
So this is going to be the database URL we provide to the running Docker container.

03:42.520 --> 03:46.420
And we'll swap out the host name for the Postgres container.

03:46.840 --> 03:53.830
Next up we'll go into our users folder where we actually are utilizing the current repository.

03:53.830 --> 03:59.230
And we can start off by removing our users repository which we're no longer utilizing.

03:59.230 --> 04:03.610
And I can go ahead and create a Prisma service inside of auth.

04:03.610 --> 04:09.010
Now, just like we did in reservations, let's create a new Prisma service that's going to be just for

04:09.010 --> 04:09.700
auth.

04:09.700 --> 04:15.670
We can copy the one in reservations we currently have and paste it into auth now.

04:15.670 --> 04:21.940
So the key difference here is now that we're extending the types that are local just for auth service

04:21.940 --> 04:23.980
here, as opposed to in reservations.

04:23.980 --> 04:31.240
This is a separate node modules folder as this path indicates because we're using this dot Prisma import.

04:31.240 --> 04:35.740
So these types are going to be specific to the schemas in each service.

04:35.740 --> 04:37.630
So now we have the Prisma service.

04:37.630 --> 04:45.310
Go back to our users module where we're going to go ahead and replace the users repository with now

04:45.310 --> 04:47.590
the Prisma service.

04:49.830 --> 04:57.150
Go ahead and remove all of these unused imports and the imports to the database module as well.

04:57.600 --> 05:03.930
Now we'll head back to the users controller and remove the import the user document here.

05:03.930 --> 05:08.970
And now we can actually reference the user type directly from Prisma client.

05:08.970 --> 05:15.750
So I'll go ahead and import user from dot Prisma slash client.

05:17.060 --> 05:22.760
Now let's go into the user service where we have to refactor this to use the Prisma service instead.

05:22.760 --> 05:28.880
So I'll go ahead and remove the users repository and remove it from the constructor as well.

05:28.880 --> 05:33.830
And let's instead inject the Prisma service.

05:39.790 --> 05:41.860
Now for the user creation.

05:41.860 --> 05:48.190
Let's go ahead and take out these properties that we're passing to users repository.

05:48.190 --> 05:55.030
And now let's call Prisma service dot user and create with the data object.

05:55.030 --> 05:57.400
And now specify these properties.

05:57.880 --> 06:04.360
So these are the properties that Prisma client expects because the schema matches this data object.

06:04.870 --> 06:15.790
For validate create user DTO to find the user we'll call this dot Prisma service dot user dot find.

06:16.610 --> 06:18.080
First or throw.

06:18.080 --> 06:24.080
And we have to use find first or throw instead of find unique because we're searching on a non unique

06:24.080 --> 06:24.740
property.

06:24.740 --> 06:26.030
The email here.

06:26.120 --> 06:32.030
So let's cut this out and replace it with the Where clause that we know Prisma expects.

06:32.030 --> 06:34.610
And look for that user by email.

06:34.610 --> 06:40.340
If we don't find them, we of course throw an error meaning that the user's email is valid.

06:40.340 --> 06:46.430
And now for verify user, we can use the same call to get the user.

06:46.430 --> 06:52.430
We can call Prisma service dot user dot find first or throw again.

06:52.430 --> 06:56.240
Because we again are looking for the user by email.

06:56.420 --> 07:01.100
So we replace this with a Where statement and look for the user by email.

07:01.100 --> 07:07.760
And finally and get user here we're going to go ahead and specify Prisma service.

07:09.310 --> 07:14.410
Dot user dot find unique or throw.

07:14.410 --> 07:21.610
And that's because I want to look for the user by ID, which is what the Get user DTO includes.

07:21.610 --> 07:32.440
So we'll have a where that checks to see for the users ID is equal to get user dto.id.

07:32.470 --> 07:34.540
Now we need to go ahead and update this.

07:34.540 --> 07:35.920
Get user DTO.

07:35.950 --> 07:44.050
I want it to just be ID instead of underscore ID, and then we need to convert it into the number that

07:44.050 --> 07:45.070
Prisma expects.

07:45.070 --> 07:46.630
Because right now it's a string.

07:46.630 --> 07:49.360
So we'll coerce this into a number.

07:50.370 --> 07:57.510
So a few other updates we need to make to work with Prisma is going to be going into our auth controller.

07:57.510 --> 08:02.250
And again we're not referencing the user document in the auth service anymore.

08:02.250 --> 08:08.580
Now we can reference the user directly from Prisma client, which is we know where the source of truth

08:08.580 --> 08:10.560
for the user entity lives.

08:10.890 --> 08:14.250
We'll also go into the auth service and make the same change.

08:14.250 --> 08:23.100
Replace user document out for user type from Prisma client and also update the user ID property here

08:23.100 --> 08:23.940
on login.

08:23.940 --> 08:32.190
This is going to be user.id now and it's no longer going to be a string value.

08:32.190 --> 08:34.200
So we'll just supply user ID.

08:34.440 --> 08:41.820
That also means we need to update the token payload interface user ID to now be a number which represents

08:41.820 --> 08:43.350
our Prisma model.

08:44.310 --> 08:50.760
Finally go into our auth strategies folder and into the JWT strategy.

08:51.820 --> 08:53.200
So now invalidate.

08:53.200 --> 09:00.550
Instead of supplying the get user DTO with underscore id, we simply use id and to turn this into a

09:00.550 --> 09:04.750
string which the function expects, I'll call two string on the number.

09:05.050 --> 09:08.830
So now we've refactored the auth service to work with Prisma.

09:08.860 --> 09:12.880
Let's go ahead and update our docker file and our Docker compose.

09:12.880 --> 09:15.340
Just the same we've done with reservations.
