WEBVTT

00:00.140 --> 00:06.230
All right, so now that we've refactor our app to work with Prisma instead of our abstract repository,

00:06.230 --> 00:11.750
let's go ahead and now update our reservations Dockerfile, which needs some updates to work with this

00:11.750 --> 00:13.010
Prisma setup.

00:13.070 --> 00:17.240
So head back into the Docker file and we're going to make a few changes.

00:17.270 --> 00:22.520
So the first of which is in the development stage of our Docker file, we're going to go ahead and copy

00:22.520 --> 00:26.420
over our Prisma schema directory into the container.

00:26.420 --> 00:32.480
So that's so that we have our migrations copied over as well as the schema Prisma.

00:32.480 --> 00:36.050
Because there's two things we want to accomplish in this Docker file.

00:36.050 --> 00:42.140
The first of which is actually generate the client types that we know our application needs to build

00:42.140 --> 00:42.800
and run.

00:42.830 --> 00:49.280
Secondly, we want to actually trigger these migrations to run once we start our application up.

00:49.280 --> 00:52.820
So our production database is always in line with our schema.

00:52.850 --> 00:54.830
Every time we start up the app.

00:55.160 --> 00:58.220
So we need this Prisma folder to do this.

00:58.220 --> 00:59.060
Let's go ahead.

00:59.060 --> 01:08.030
And after this copy command we go ahead and also copy apps slash reservations, slash Prisma and copy

01:08.030 --> 01:14.360
this into the Prisma folder locally because we now want to actually run the Prisma generate command

01:14.360 --> 01:17.540
to generate our client types from our schema.

01:17.540 --> 01:23.720
And Prisma will automatically look for a Prisma folder in the current directory when we run this command,

01:23.720 --> 01:28.880
and it will have the Prisma folder from reservations because we're copying it in this line.

01:28.880 --> 01:35.480
So after we npm install, let's now run npm prisma generate.

01:35.480 --> 01:40.010
So this is going to generate the client types which in our case we've already done after we.

01:40.190 --> 01:45.500
So now that we have our Prisma client types generated and available when we build our code, let's now

01:45.500 --> 01:49.340
scroll down to the last part of our Docker image.

01:49.340 --> 01:55.280
The second stage here, our production stage where we actually start the application up.

01:55.280 --> 01:59.810
So currently we're just calling node to start up our built main.js file.

01:59.810 --> 02:05.630
I want to continue to do this, but beforehand I want to execute any database migrations that we have

02:05.630 --> 02:06.170
to do.

02:06.170 --> 02:11.480
Now I want to actually abstract this process away into a package.json script, since we're going to

02:11.480 --> 02:15.140
be doing the same thing for any service that utilizes Prisma.

02:15.230 --> 02:21.410
So let's head into our package.json, which we know is the one that's going to be copied over.

02:21.410 --> 02:24.380
And let's modify our start prod command here.

02:24.380 --> 02:27.470
So I want to actually migrate beforehand.

02:27.470 --> 02:31.220
So let's create a new migrate script that's going to do just that.

02:31.220 --> 02:36.650
It's going to call npm prisma migrate deploy.

02:36.650 --> 02:42.860
Now we're going to need to make sure we have Prisma available here as a strict dependency.

02:42.860 --> 02:47.270
So let's go ahead and add it now to our package.json.

02:47.270 --> 02:50.240
So I'm going to use the latest version available to me.

02:50.240 --> 02:55.250
So this is the prisma CLI we need at runtime to execute these migrations.

02:55.250 --> 03:01.820
So this migrate deploy command here is essentially going to check to see for any of our migration scripts

03:01.820 --> 03:09.170
in the migrations directory and apply them against the running database if we need to, to get our schema

03:09.170 --> 03:10.250
up to date.

03:10.250 --> 03:16.610
Now let's actually call this before we start prod I'm going to run p npm.

03:17.980 --> 03:20.770
Run, migrate, and then we'll run.

03:20.770 --> 03:23.170
And to start the node command.

03:23.170 --> 03:24.730
And let's get rid of this path.

03:24.730 --> 03:29.440
Here we'll have the Docker image itself provide the path to the JavaScript file.

03:29.440 --> 03:31.870
So now we're running this Prisma migration.

03:31.870 --> 03:35.530
Before we start up to apply any existing migrations we need to.

03:35.530 --> 03:38.140
And then starting up our node process.

03:38.290 --> 03:43.000
Let's go back to the Docker file now where we can go ahead and actually call this.

03:43.000 --> 03:47.380
So instead of calling node directly here we're going to use the new npm script.

03:47.380 --> 03:49.330
So we'll call p npm.

03:49.950 --> 03:54.390
Run, and these are each separate entries in this array here.

03:54.420 --> 03:58.170
Start prod and then finally specify the path.

03:58.170 --> 04:01.890
So make sure we have commas in between each of these commands.

04:01.890 --> 04:07.440
And now we can verify this by going back to the reservations folder.

04:07.440 --> 04:09.420
And I'll run Docker build.

04:09.450 --> 04:15.570
Give it a tag of reservations and specify the docker file here.

04:15.570 --> 04:19.920
And go up to the root of our project where we know we're running the build from.

04:19.920 --> 04:23.370
So go ahead and verify that this Docker image builds correctly.

04:23.370 --> 04:26.400
So our Docker image is built correctly.

04:26.400 --> 04:32.130
Now I want to update our Docker compose to actually to run with Prisma properly.

04:32.130 --> 04:37.860
Let's head back to our Docker compose file and take a look at our reservations service.

04:37.860 --> 04:43.410
So right now to start up reservations we're overriding our Docker files command here.

04:43.410 --> 04:49.980
So we know our new Docker file command executes the migrations and then starts up while Docker compose.

04:49.980 --> 04:52.500
This command is just going to start up our container.

04:52.500 --> 04:57.960
So locally the same thing I want to happen if we need to execute any migrations, I want it to happen

04:57.960 --> 05:02.790
automatically to ensure our schema is always up to date with the database.

05:02.790 --> 05:09.660
And then I also want to generate our types to make sure we always have the latest TypeScript code based

05:09.660 --> 05:11.490
off of our Prisma schema.

05:11.490 --> 05:17.550
So let's go ahead and enhance this command to execute migrations and generate our types.

05:17.550 --> 05:23.070
So to do this I'm going to use a multi command syntax here.

05:23.070 --> 05:24.720
So use sh dash.

05:24.750 --> 05:28.620
C so this is going to get executed in our alpine container.

05:28.740 --> 05:32.790
And now we provide quotes for the command we want to run.

05:32.790 --> 05:34.260
So we'll keep the start.

05:34.260 --> 05:34.590
Dev.

05:34.590 --> 05:38.280
This is the last part of the command we want to run to start up the app.

05:38.280 --> 05:46.620
However the first part here I'll include and and then we're going to run npm prisma migrate.

05:48.030 --> 05:48.900
Deploy.

05:49.410 --> 05:56.010
So importantly, remember that we're actually mounting our existing file system onto the container using

05:56.010 --> 05:57.600
this volume mount here.

05:58.200 --> 06:03.780
So what that means is we're actually going to be overriding the Docker files file system with our own.

06:03.780 --> 06:10.080
And that means we need to specify the path to the reservations Prisma schema, since it's no longer

06:10.080 --> 06:13.140
going to be at the root where we provided it.

06:13.140 --> 06:15.210
Firstly here in our Docker file.

06:16.120 --> 06:19.810
So instead of being at the root, we're now going to have our own file system.

06:19.810 --> 06:28.060
So we need to provide the schema argument to migrate, deploy and say our schema is now inside of apps

06:28.060 --> 06:32.920
reservations slash prisma slash Schema.prisma.

06:32.920 --> 06:37.240
And so this matches the file path in our local file system here.

06:37.330 --> 06:40.210
Reservations Prisma Schema.prisma.

06:40.210 --> 06:46.090
So what I'll do next is simply copy this migrate deploy command we're using now.

06:46.210 --> 06:49.840
And I'll include one more command between these two.

06:50.350 --> 06:51.490
Paste in.

06:51.490 --> 06:54.400
And now let's go ahead and update this command.

06:54.400 --> 07:01.480
So instead of migrate deploy we're going to be running npm prisma generate.

07:01.480 --> 07:06.580
And we still provide the same path to the schema here.

07:06.940 --> 07:13.510
Finally, before starting up I want to specify now that we actually have a dependency for reservations,

07:13.510 --> 07:16.960
we need the Postgres database to be running first.

07:16.960 --> 07:20.350
So we don't try to execute migrations before it's running.

07:20.350 --> 07:26.680
So let's add a depends on key and then supply the Postgres container name.

07:26.680 --> 07:32.230
And this is just ensures when we start up our reservations container we always will have Postgres running

07:32.230 --> 07:32.800
first.

07:32.800 --> 07:36.280
And this is of course the Postgres container we defined below.

07:36.280 --> 07:42.880
Also don't forget to do a fresh p npm install recursive to make sure you have the latest dependencies,

07:42.880 --> 07:45.190
including prisma at the root.

07:45.550 --> 07:49.930
Then we can run docker compose up reservations.

07:49.930 --> 07:52.780
So I've just gone ahead and executed the command.

07:52.780 --> 07:55.900
And let's walk through the output here together.

07:56.410 --> 08:01.570
So as soon as this docker compose started up here, you can see the first command we know we're running

08:01.570 --> 08:08.020
is to deploy, which is essentially going to run any migrations we have against our new Postgres Docker

08:08.020 --> 08:08.860
container.

08:08.860 --> 08:16.360
So you can see here it applied the one migration we already have from running locally, our reservation

08:16.360 --> 08:19.150
migration which creates the reservations table.

08:19.150 --> 08:25.720
And then we went ahead and generated the latest schema types using Prisma generate.

08:25.900 --> 08:32.290
And then finally we started up our application which is now successfully running with Prisma.

08:32.290 --> 08:39.100
So let's go ahead and now refactor the auth service as well so we can test our application end to end

08:39.130 --> 08:40.390
using Prisma.
