WEBVTT

00:00.110 --> 00:05.930
Hey everyone, today I want to look into adding Prisma into our application, which has been a very

00:05.930 --> 00:08.870
heavily requested topic in the course.

00:08.900 --> 00:16.400
Now, Prisma is a very popular ORM tool to allow us to interact with a database similar to how we're

00:16.400 --> 00:22.790
utilizing mongoose right now for MongoDB or type ORM for a SQL based database.

00:22.790 --> 00:25.910
We can also use Prisma as our ORM.

00:26.480 --> 00:31.010
Now, one of the great features about Prisma is that it's going to allow us to actually generate the

00:31.010 --> 00:36.620
types for our code, the TypeScript for our code based off of the database schema we provide, which

00:36.620 --> 00:40.430
is going to be really nice because it's going to cut down on a lot of boilerplate code.

00:40.460 --> 00:46.040
Now I have a separate video that goes into depth starting with Prisma from a fresh project.

00:46.040 --> 00:51.800
So if you'd like to learn more about Prisma from the very beginning, you can simply check that video

00:51.800 --> 00:52.010
out.

00:52.010 --> 00:54.770
I'll leave it in the resources below.

00:54.770 --> 01:01.280
Otherwise, let's look and see how we can plug Prisma into our existing application and swap it out

01:01.280 --> 01:03.740
for our existing MongoDB database.

01:03.740 --> 01:09.050
The first thing we're going to do is actually run a Postgres server in our Docker compose.

01:09.050 --> 01:15.410
So we're going to be utilizing Postgres as our database of choice that Prisma is going to connect to.

01:15.560 --> 01:21.740
So let's open up our Docker compose and scroll down to our existing Mongo image.

01:21.740 --> 01:25.520
So this is the container we're running for our database right now.

01:25.940 --> 01:30.530
Let's swap this out for a new Postgres image instead.

01:30.530 --> 01:35.750
So for the image we'll specify Postgres and then we'll provide ports here.

01:35.750 --> 01:38.960
So we're going to expose the Postgres ports.

01:38.960 --> 01:42.050
The default Postgres port is 5432.

01:42.050 --> 01:45.410
Let's map to our machines 5432 as well.

01:45.410 --> 01:51.950
And this is so we can still connect to our database locally using a GUI if we want, but also so that

01:51.950 --> 01:58.070
we can execute database migrations locally outside of Docker, which we're going to need to do to get

01:58.070 --> 02:02.240
our database in line with our schema that we'll define later on.

02:02.240 --> 02:07.370
So we want this port open so that we can access the container on our local host.

02:07.370 --> 02:11.540
Finally we want to set a password for the database.

02:11.540 --> 02:18.620
So I'll add an environment section and specify the environment variable Postgres password.

02:18.620 --> 02:22.040
And I'll just give this a dummy password of Postgres for now.

02:22.640 --> 02:28.940
So with this docker compose updated, we should now be able to run in our project docker compose up

02:28.940 --> 02:30.200
Postgres.

02:30.200 --> 02:33.140
So go ahead and let your image pull in the container.

02:33.140 --> 02:36.020
Start up to know that we have a running database.

02:37.100 --> 02:37.490
All right.

02:37.490 --> 02:42.200
So my container started up and we're listening for traffic on this 5432.

02:42.230 --> 02:42.620
All right.

02:42.620 --> 02:46.280
So I've gone ahead and opened up my database GUI tool of choice.

02:46.280 --> 02:51.680
And that's going to be Pgadmin, which is a simple GUI to allow us to establish a connection to our

02:51.680 --> 02:54.110
database and view our data.

02:54.410 --> 02:56.720
I'll leave a link below if you want to check it out.

02:57.110 --> 03:05.180
So Pgadmin open, I'm going to go ahead and right click my servers to register a new server, and I'll

03:05.180 --> 03:06.950
call this local.

03:06.950 --> 03:08.990
Now for the connection parameters.

03:08.990 --> 03:14.360
We'll give it a host name of localhost because we're connecting to our local machine.

03:14.360 --> 03:20.030
And then 5432, which we know we're port forwarding Postgres port that we've exposed on our running

03:20.030 --> 03:20.570
container.

03:20.570 --> 03:22.970
So that's where traffic will get forwarded to.

03:23.540 --> 03:28.910
Finally for the password we'll provide that Postgres password that we added before.

03:28.910 --> 03:31.370
So our username and password are the same.

03:31.370 --> 03:32.900
I'll click on save.

03:32.900 --> 03:37.400
And now we can see we have this local database running.

03:37.400 --> 03:42.290
I have a few other dummy databases from previous development running right now.

03:42.290 --> 03:46.010
So this means we can connect to our database server.

03:46.010 --> 03:51.830
Let's go ahead and now plug in Prisma into our application so that we can get started with creating

03:51.830 --> 03:54.440
our own database for this application.

03:54.440 --> 03:55.040
All right.

03:55.040 --> 04:01.940
Let's go ahead and get started with refactoring our reservation service first to use Prisma instead

04:01.940 --> 04:02.990
of MongoDB.

04:03.350 --> 04:11.240
So the first thing we're going to do is actually create a new package.json inside of our reservation

04:11.240 --> 04:12.200
service.

04:12.200 --> 04:18.710
So just like we've done before in our auth service and notification service to get some dependencies

04:18.710 --> 04:24.440
specific to just this running service, we're going to do the same thing now in our reservation service,

04:24.440 --> 04:30.860
because I want to install Prisma client just for this one service here in our Monorepo.

04:30.890 --> 04:35.450
I don't want to share this Prisma client with other services.

04:35.450 --> 04:40.850
Now, the reason why is because of the type generation that Prisma client offers us.

04:40.850 --> 04:45.860
We're going to be running this type generation in multiple different services, and we don't want these

04:45.860 --> 04:47.750
types to overwrite one another.

04:47.750 --> 04:52.880
We want each service to have its own independent Prisma client and its own set of types.

04:52.880 --> 04:55.190
As they are independent microservices.

04:55.190 --> 04:56.810
We want to keep them separate.

04:56.810 --> 04:59.750
So let's go ahead and copy a.

05:00.190 --> 05:08.080
Package JSON from our notification service and paste it into reservations and start replacing it out.

05:08.080 --> 05:13.840
So we'll call this reservations and we can remove these dependencies.

05:13.840 --> 05:16.060
We don't need node mailer in this service.

05:16.060 --> 05:20.770
So now what we can do is we can CD into the reservations folder.

05:20.770 --> 05:25.090
And let's go ahead and npm install some dependencies.

05:25.090 --> 05:30.460
So we're going to want to install Prisma client which is again going to be this client library that's

05:30.460 --> 05:35.290
going to establish the connection to our database and generate the types we need.

05:35.290 --> 05:41.860
However we're also going to install the Prisma dependency in this Prisma dependency is essentially a

05:41.860 --> 05:49.090
CLI tool that's going to allow us to run migrations against the database based off of migration files

05:49.090 --> 05:55.300
we generate, to always ensure our database schema is in line with our schema definition.

05:55.300 --> 05:58.510
So go ahead and install these two dependencies.

05:59.050 --> 06:02.950
I'm also going to go ahead and install a development dependency.

06:02.950 --> 06:06.130
And this is going to be the dot env cli.

06:06.160 --> 06:13.510
Now we're going to need this dot m CLI to actually be able to specify different dot m files before we

06:13.510 --> 06:15.850
call the Prisma migrate command.

06:15.850 --> 06:18.190
And we'll see why this is in a little bit.

06:18.190 --> 06:20.770
For now just install dot m cli.

06:20.770 --> 06:24.520
And now we have all the dependencies we need in reservations.
