All right, so now that we've refactor our app to work with Prisma instead of our abstract repository,
let's go ahead and now update our reservations Dockerfile, which needs some updates to work with this
Prisma setup.
So head back into the Docker file and we're going to make a few changes.
So the first of which is in the development stage of our Docker file, we're going to go ahead and copy
over our Prisma schema directory into the container.
So that's so that we have our migrations copied over as well as the schema Prisma.
Because there's two things we want to accomplish in this Docker file.
The first of which is actually generate the client types that we know our application needs to build
and run.
Secondly, we want to actually trigger these migrations to run once we start our application up.
So our production database is always in line with our schema.
Every time we start up the app.
So we need this Prisma folder to do this.
Let's go ahead.
And after this copy command we go ahead and also copy apps slash reservations, slash Prisma and copy
this into the Prisma folder locally because we now want to actually run the Prisma generate command
to generate our client types from our schema.
And Prisma will automatically look for a Prisma folder in the current directory when we run this command,
and it will have the Prisma folder from reservations because we're copying it in this line.
So after we npm install, let's now run npm prisma generate.
So this is going to generate the client types which in our case we've already done after we.
So now that we have our Prisma client types generated and available when we build our code, let's now
scroll down to the last part of our Docker image.
The second stage here, our production stage where we actually start the application up.
So currently we're just calling node to start up our built main.js file.
I want to continue to do this, but beforehand I want to execute any database migrations that we have
to do.
Now I want to actually abstract this process away into a package.json script, since we're going to
be doing the same thing for any service that utilizes Prisma.
So let's head into our package.json, which we know is the one that's going to be copied over.
And let's modify our start prod command here.
So I want to actually migrate beforehand.
So let's create a new migrate script that's going to do just that.
It's going to call npm prisma migrate deploy.
Now we're going to need to make sure we have Prisma available here as a strict dependency.
So let's go ahead and add it now to our package.json.
So I'm going to use the latest version available to me.
So this is the prisma CLI we need at runtime to execute these migrations.
So this migrate deploy command here is essentially going to check to see for any of our migration scripts
in the migrations directory and apply them against the running database if we need to, to get our schema
up to date.
Now let's actually call this before we start prod I'm going to run p npm.
Run, migrate, and then we'll run.
And to start the node command.
And let's get rid of this path.
Here we'll have the Docker image itself provide the path to the JavaScript file.
So now we're running this Prisma migration.
Before we start up to apply any existing migrations we need to.
And then starting up our node process.
Let's go back to the Docker file now where we can go ahead and actually call this.
So instead of calling node directly here we're going to use the new npm script.
So we'll call p npm.
Run, and these are each separate entries in this array here.
Start prod and then finally specify the path.
So make sure we have commas in between each of these commands.
And now we can verify this by going back to the reservations folder.
And I'll run Docker build.
Give it a tag of reservations and specify the docker file here.
And go up to the root of our project where we know we're running the build from.
So go ahead and verify that this Docker image builds correctly.
So our Docker image is built correctly.
Now I want to update our Docker compose to actually to run with Prisma properly.
Let's head back to our Docker compose file and take a look at our reservations service.
So right now to start up reservations we're overriding our Docker files command here.
So we know our new Docker file command executes the migrations and then starts up while Docker compose.
This command is just going to start up our container.
So locally the same thing I want to happen if we need to execute any migrations, I want it to happen
automatically to ensure our schema is always up to date with the database.
And then I also want to generate our types to make sure we always have the latest TypeScript code based
off of our Prisma schema.
So let's go ahead and enhance this command to execute migrations and generate our types.
So to do this I'm going to use a multi command syntax here.
So use sh dash.
C so this is going to get executed in our alpine container.
And now we provide quotes for the command we want to run.
So we'll keep the start.
Dev.
This is the last part of the command we want to run to start up the app.
However the first part here I'll include and and then we're going to run npm prisma migrate.
Deploy.
So importantly, remember that we're actually mounting our existing file system onto the container using
this volume mount here.
So what that means is we're actually going to be overriding the Docker files file system with our own.
And that means we need to specify the path to the reservations Prisma schema, since it's no longer
going to be at the root where we provided it.
Firstly here in our Docker file.
So instead of being at the root, we're now going to have our own file system.
So we need to provide the schema argument to migrate, deploy and say our schema is now inside of apps
reservations slash prisma slash Schema.prisma.
And so this matches the file path in our local file system here.
Reservations Prisma Schema.prisma.
So what I'll do next is simply copy this migrate deploy command we're using now.
And I'll include one more command between these two.
Paste in.
And now let's go ahead and update this command.
So instead of migrate deploy we're going to be running npm prisma generate.
And we still provide the same path to the schema here.
Finally, before starting up I want to specify now that we actually have a dependency for reservations,
we need the Postgres database to be running first.
So we don't try to execute migrations before it's running.
So let's add a depends on key and then supply the Postgres container name.
And this is just ensures when we start up our reservations container we always will have Postgres running
first.
And this is of course the Postgres container we defined below.
Also don't forget to do a fresh p npm install recursive to make sure you have the latest dependencies,
including prisma at the root.
Then we can run docker compose up reservations.
So I've just gone ahead and executed the command.
And let's walk through the output here together.
So as soon as this docker compose started up here, you can see the first command we know we're running
is to deploy, which is essentially going to run any migrations we have against our new Postgres Docker
container.
So you can see here it applied the one migration we already have from running locally, our reservation
migration which creates the reservations table.
And then we went ahead and generated the latest schema types using Prisma generate.
And then finally we started up our application which is now successfully running with Prisma.
So let's go ahead and now refactor the auth service as well so we can test our application end to end
using Prisma.