Hey everyone, today I want to look into adding Prisma into our application, which has been a very
heavily requested topic in the course.
Now, Prisma is a very popular ORM tool to allow us to interact with a database similar to how we're
utilizing mongoose right now for MongoDB or type ORM for a SQL based database.
We can also use Prisma as our ORM.
Now, one of the great features about Prisma is that it's going to allow us to actually generate the
types for our code, the TypeScript for our code based off of the database schema we provide, which
is going to be really nice because it's going to cut down on a lot of boilerplate code.
Now I have a separate video that goes into depth starting with Prisma from a fresh project.
So if you'd like to learn more about Prisma from the very beginning, you can simply check that video
out.
I'll leave it in the resources below.
Otherwise, let's look and see how we can plug Prisma into our existing application and swap it out
for our existing MongoDB database.
The first thing we're going to do is actually run a Postgres server in our Docker compose.
So we're going to be utilizing Postgres as our database of choice that Prisma is going to connect to.
So let's open up our Docker compose and scroll down to our existing Mongo image.
So this is the container we're running for our database right now.
Let's swap this out for a new Postgres image instead.
So for the image we'll specify Postgres and then we'll provide ports here.
So we're going to expose the Postgres ports.
The default Postgres port is 5432.
Let's map to our machines 5432 as well.
And this is so we can still connect to our database locally using a GUI if we want, but also so that
we can execute database migrations locally outside of Docker, which we're going to need to do to get
our database in line with our schema that we'll define later on.
So we want this port open so that we can access the container on our local host.
Finally we want to set a password for the database.
So I'll add an environment section and specify the environment variable Postgres password.
And I'll just give this a dummy password of Postgres for now.
So with this docker compose updated, we should now be able to run in our project docker compose up
Postgres.
So go ahead and let your image pull in the container.
Start up to know that we have a running database.
All right.
So my container started up and we're listening for traffic on this 5432.
All right.
So I've gone ahead and opened up my database GUI tool of choice.
And that's going to be Pgadmin, which is a simple GUI to allow us to establish a connection to our
database and view our data.
I'll leave a link below if you want to check it out.
So Pgadmin open, I'm going to go ahead and right click my servers to register a new server, and I'll
call this local.
Now for the connection parameters.
We'll give it a host name of localhost because we're connecting to our local machine.
And then 5432, which we know we're port forwarding Postgres port that we've exposed on our running
container.
So that's where traffic will get forwarded to.
Finally for the password we'll provide that Postgres password that we added before.
So our username and password are the same.
I'll click on save.
And now we can see we have this local database running.
I have a few other dummy databases from previous development running right now.
So this means we can connect to our database server.
Let's go ahead and now plug in Prisma into our application so that we can get started with creating
our own database for this application.
All right.
Let's go ahead and get started with refactoring our reservation service first to use Prisma instead
of MongoDB.
So the first thing we're going to do is actually create a new package.json inside of our reservation
service.
So just like we've done before in our auth service and notification service to get some dependencies
specific to just this running service, we're going to do the same thing now in our reservation service,
because I want to install Prisma client just for this one service here in our Monorepo.
I don't want to share this Prisma client with other services.
Now, the reason why is because of the type generation that Prisma client offers us.
We're going to be running this type generation in multiple different services, and we don't want these
types to overwrite one another.
We want each service to have its own independent Prisma client and its own set of types.
As they are independent microservices.
We want to keep them separate.
So let's go ahead and copy a.
Package JSON from our notification service and paste it into reservations and start replacing it out.
So we'll call this reservations and we can remove these dependencies.
We don't need node mailer in this service.
So now what we can do is we can CD into the reservations folder.
And let's go ahead and npm install some dependencies.
So we're going to want to install Prisma client which is again going to be this client library that's
going to establish the connection to our database and generate the types we need.
However we're also going to install the Prisma dependency in this Prisma dependency is essentially a
CLI tool that's going to allow us to run migrations against the database based off of migration files
we generate, to always ensure our database schema is in line with our schema definition.
So go ahead and install these two dependencies.
I'm also going to go ahead and install a development dependency.
And this is going to be the dot env cli.
Now we're going to need this dot m CLI to actually be able to specify different dot m files before we
call the Prisma migrate command.
And we'll see why this is in a little bit.
For now just install dot m cli.
And now we have all the dependencies we need in reservations.