Alright, so in this latest lecture, we've updated all of our dependencies in our Prisma branch.
And this includes Prisma at v7. Now Prisma v7 introduces a few breaking changes to how we've
previously configured Prisma. And so in this lecture, we're going to walk through these
changes to see how you can upgrade your application. So I'm going to leave a link
below to these changes to the repository that show you the exact changes we made in this commit.
So you can review all them. But let's go ahead and jump in to see the changes. Now the primary
change here in Prisma is if we go to off, for example, where we know we have Prisma setup,
you'll notice that we now have a new file called Prisma config.ts here at the root.
Now in Prisma v7, we have this new file, which is sort of like drizzle config. If you use drizzle
before where we define configuration inside of TypeScript directly. So you can see here,
we're importing dot m, which is the actual library that sgs config uses under the hood
to load environment variables. And so this is the default configuration we import from Prisma
config to find this config file where we define the path to our schemas, the path to our migrations.
And finally, data source where we actually provide the database connection URL.
Now you can use the m function from Prisma, which will use dot m under the hood to extract
an environment variable. So in this case, we're extracting our database URL, which we know we've
set up to point to our Postgres DB. Now importantly, there are some additional
dependencies we need for this Prisma seven integration. So make sure you have PG installed,
which is a node driver for Postgres, as well as dot m directly, which we use to read in
environment variables. And finally, you're going to want Prisma slash adapter dash PG
to have an adapter to Postgres that we can use in our node client.
So with this Prisma config TS configured, we also have our Prisma folder here as before.
And so inside of here, we have our schema dot Prisma file, which has changed a bit.
So notice first here, our provider has been renamed to Prisma client without dash j s.
Also, our output has changed. So previously, we actually generated output to node modules.
Now we generate it to source. So inside of our source folder, we have the generated types,
which is better for our import resolution for our code. And this is also means it's inside
of our source control. So we can actually track our changes to our generated code.
Lastly, we no longer have binary targets. But instead, we should have module format that is
set to common j s. Because in latest Prisma seven, Prisma only generates an ESM by default,
well, we know nest j s is still only supported in common j s. So we need to tell it to generate as
common j s, which is what we do here. Now the final change we made is removed the database URL
from the data source DB, we no longer provide the connection string here in the schema Prisma,
that instead now comes from the Prisma config TS file. Now importantly, we also do have some
changes to our source code as well. So make sure we go ahead and open up our Prisma service.
So we'll start off with the reservations. We have our Prisma service, which we know was our database
service. Previously, all we had to do here was extend the Prisma client and then call this
connect. But now we actually have to instantiate the underlying connection to the database that we
are using. And in this case, this is why we set up this Prisma adapter PG dependency before,
because now inside of the constructor, the first thing we're doing here is creating a new pool
object. So this pool object is a connection pool inside of Postgres. And a connection pool is
essentially just multiple connections that can be reused and shared. Instead of having to close and
reopen each new connection to the database, we can maintain this pool of connections which are
always open and reuse so that we don't have to go through the overhead of establishing a new
connection each time. So this is great for performance and for concurrent requests to the
database in our app. And then with this pool object, we simply pass it into the Prisma PG
constructor. And so this is the Prisma adapter PG, this is the adapter to connect Prisma and Postgres.
So we create a Prisma PG object here and pass that adapter directly to the Prisma service
super here. And so this is the updated constructor for our Prisma service to
successfully establish a connection to the database. You should have this in both the Prisma
service here for reservations, as well as in auth, which is the other service that we are using Prisma
for. So this should look just the same in both. Now importantly, also note that we are extending
the Prisma client from our generated folder, right? We don't import from node modules anymore.
Instead, now we are actually using the generated code. So we told Prisma in the schema Prisma,
that our generated path is now in source directly. So we have this all under source control as well,
which is really nice. So these are the changes we need to the actual TypeScript itself. So this is
all the source code changes we need. The other changes we're going to need are for our Docker
file in Docker Compose. So we want to go into our Docker file for reservations to start. And let's
take a look at the changes we've made. The first thing we've changed is we've removed previously,
we had this copy statement here to copy our Prisma directory inside of reservations,
we no longer need to do this or run Prisma generate or build inside of the development
phase here. Instead, we're going to move this all to the production stage of our Docker build.
Now importantly, we already have the Prisma directory available thanks to our apps reservations
copy statement here. And so now inside of our production stage, you can see here on line 39.
So this is the important Prisma related bit that we've added. So now we are copying from the first
stage, the development stage where we know we have the reservations directory and the Prisma
files inside. In this case, we're copying over our new Prisma config.ts file, which we know Prisma
is going to reference to generate types and execute migrations. So we want to copy this TS
file over into the final image. And that's what we do here to ensure we have it. And then we go
ahead and copy over the rest of the Prisma directory. So we have the actual raw migrations
that we know need to apply. And that is what we're doing in the last step here. So we've updated our
run command. So there's no longer start prod directly. Instead, beforehand, we're running
some commands where we CD into reservations, run our migrate deploy command, which is going to
automatically apply our migrations against the database. And then finally, we CD back to the
root of the app and execute Node JS against our main JavaScript file. So these are the steps we
need to make sure we apply our migrations. Before we start our container, and that's exactly what
we want to do. So this looks great. You'll notice that we have this same set of changes to the auth
Docker file. So make sure we copy this over. And now the last change that we had to make for Prisma
seven is to then update our Docker compose as well. So previously, our start command for our
containers, we were able to just run migrate deploy directly and pass a schema argument to
point to the Prisma schema. However, in Prisma seven, this is now deprecated, we can no longer
apply that schema argument. So notice here that our start command for for example, our reservation
service is now changed. Instead of running migrate deploy directly, we follow the same pattern that
we just shown you in the Docker file where we CD into reservations, run the migrate deploy command
to apply our migrations. We then also run Prisma generate to generate our types,
and then run back to the root and run our start dev to start the dev server. So just like we did
in the Docker file here, we're just updating this to work for the latest Prisma seven to deploy our
changes and generate the types without the schema flag now. And so make sure you apply the same
change, of course against off here as well. And so this is the last change we need for Prisma
seven. With this in place, we should have everything we need to start up our containers
and continue to use our application in production with Prisma latest version seven.