WEBVTT

00:00.290 --> 00:00.650
All right.

00:00.650 --> 00:08.030
So let's look at how we can implement Type ORM into our application and use it as our database of choice

00:08.030 --> 00:09.470
instead of MongoDB.

00:10.040 --> 00:15.740
Now, since we have an abstract repository in our application, it's going to be a lot easier to switch

00:15.770 --> 00:17.390
out which database we're using.

00:17.390 --> 00:25.010
Since all of our repositories in our microservices are extending this common one in our lives database

00:25.010 --> 00:26.090
folder.

00:26.120 --> 00:30.020
This abstract repository can be changed to work with type ORM.

00:30.440 --> 00:37.220
We'll of course have to update our existing schemas and define them in a language that type ORM understands.

00:37.220 --> 00:39.220
So let's go ahead and get started.

00:39.230 --> 00:40.730
Now a quick note.

00:40.730 --> 00:47.840
I'm going to be keeping all of this code related to moving over to Type ORM inside of a new GitHub branch

00:47.840 --> 00:49.610
called Type ORM.

00:49.610 --> 00:56.090
So if you want to see the completed code with all of the type ORM integration completed, you can run

00:56.090 --> 01:03.860
Git checkout type ORM to get on this branch and I'm going to be working off of the main branch for this

01:03.860 --> 01:05.300
series of lectures.

01:05.330 --> 01:11.570
The first place we're going to start is our Docker compose file, where right now for our database you

01:11.570 --> 01:14.690
can see we're running Mongo using the mongo image.

01:14.690 --> 01:18.080
I want to swap this out for MySQL instead.

01:18.080 --> 01:27.080
So to do this I'll change mongo to MySQL and for the image we're going to use the official MySQL image

01:27.680 --> 01:34.610
and then we're going to specify an m file that the MySQL image will use to read in credentials for our

01:34.610 --> 01:35.720
database.

01:35.720 --> 01:38.540
So let's go ahead and specify this m file.

01:38.540 --> 01:44.540
We'll create a new dot m at the root of our project where we will have environment variables specific

01:44.540 --> 01:47.540
to just these extra images like MySQL.

01:47.540 --> 01:50.330
So let's go ahead and create this dot env.

01:50.540 --> 01:57.620
Now lastly, the database will be listening on port 3306 by default for incoming connections.

01:57.890 --> 02:05.520
So let's go ahead and define a new ports list that will expose this port to our host machine by running

02:05.530 --> 02:14.760
3306 colon 3306 which will map the target port to our machine's local port of 3306.

02:14.760 --> 02:21.570
Now let's go to our Env and define some environment variables for this MySQL image.

02:21.570 --> 02:29.010
Now just like we've done in all of the other M files in this project, this file would normally be git

02:29.010 --> 02:31.950
ignored using the dot git ignore.

02:31.980 --> 02:38.160
We'd add this to the git ignore so that we don't commit any sensitive information in this dot env as

02:38.160 --> 02:39.390
I've done in the past.

02:39.390 --> 02:43.350
I'm going to commit this dot m to make it easier for you in this project.

02:43.350 --> 02:46.110
However, we wouldn't want to do this in production.

02:46.140 --> 02:52.320
Let's go ahead and set up the MySQL database which is going to be the database or the default database

02:52.320 --> 02:54.870
that MySQL will automatically create.

02:54.870 --> 02:56.100
When it starts up.

02:56.130 --> 03:01.980
We'll go ahead and call this sleeper, which is going to be our database name of choice next, we'll

03:01.980 --> 03:08.010
specify a MySQL root password, which is going to be the root password used to connect to the database

03:08.010 --> 03:08.640
server.

03:08.640 --> 03:10.680
You can make this anything you'd like.

03:10.680 --> 03:13.470
I'll just call it random root password.

03:14.610 --> 03:20.460
So now at this point with Docker desktop running on my system, we can actually run Docker, compose

03:20.460 --> 03:26.400
up and then specify this MySQL image to actually start up MySQL.

03:27.060 --> 03:32.430
We can even see that we are listening on port 3306 for incoming connections.

03:32.730 --> 03:38.850
Okay, so I've opened this application called MySQL Workbench, which is just a UI tool to allow us

03:38.850 --> 03:40.740
to interact with our database.

03:40.740 --> 03:46.080
You can easily download this by googling MySQL workbench and following along the installation instructions

03:46.080 --> 03:47.250
for your system.

03:47.790 --> 03:52.800
Once you've installed it, we can click on the plus sign here to add a new connections.

03:52.800 --> 03:58.920
You can call this whatever you'd like, I'll call mine local Docker to and then we specify the default

03:58.950 --> 04:01.860
host name here, which is our local loopback address.

04:01.860 --> 04:05.250
And specify that default port of 3306.

04:05.280 --> 04:11.970
The username is going to be root by default and for the password we'll click on store in Keychain where

04:11.970 --> 04:14.220
we can then enter in our password.

04:14.220 --> 04:21.760
So in our case it's random root password that we defined in that dot m file or whatever password you

04:21.760 --> 04:22.740
were using.

04:22.950 --> 04:26.430
Finally, we'll go ahead and click okay to add this connection.

04:26.430 --> 04:32.070
So after adding this connection, I'll double click on it and you can see now we are brought to this

04:32.070 --> 04:37.850
schema section here where we have our default database created, which we specified in the dot env.

04:37.890 --> 04:43.440
We don't have any tables defined yet, but we can see we can connect to our Docker image and see this

04:43.440 --> 04:44.730
default schema.

04:44.760 --> 04:48.300
All right, so we're ready to start integrating Typekit into our app.

04:48.300 --> 04:56.550
Let's go to the command line where I'll stop our server right now and then we need to run some dependencies.

04:57.240 --> 05:00.030
And now we need to install some dependencies.

05:00.060 --> 05:08.910
I'll go ahead and run npm install nestjs slash type ORM type ORM itself and then my too.

05:08.940 --> 05:15.660
So now that we have the dependencies we need, let's go into our libs common folder and in our database

05:15.660 --> 05:23.220
directory we'll open the database module back up and here we will refactor our database module to work

05:23.220 --> 05:25.860
with type ORM instead of mongoose.

05:26.010 --> 05:33.840
So now instead of using the Mongoose module for root async, I want to be running the type ORM module

05:33.840 --> 05:42.150
dot for root async where we still have access to a use factory and here we will inject the config service.

05:42.150 --> 05:48.780
But now we're going to expose a different set of environment variables to the type ORM module.

05:48.810 --> 05:53.920
The first one we'll provide is a type here where we specify the type of database we're using.

05:53.940 --> 05:55.800
We will use MySQL.

05:56.070 --> 05:58.360
Then we're going to provide the host.

05:58.390 --> 06:05.290
Now, like we did in MongoDB, we'll pull this off of the config service and I want to show you a different

06:05.290 --> 06:08.410
way of also validating environment variables.

06:08.410 --> 06:12.570
So in the past we've used joy to validate our environment variables.

06:12.580 --> 06:18.250
However, instead we can use, get or throw, which will throw an error if the environment variable

06:18.250 --> 06:20.510
doesn't exist in this call.

06:20.530 --> 06:23.350
So this is the approach that we will take here.

06:23.380 --> 06:29.050
We are going to get or throw the MySQL host environment variable.

06:29.230 --> 06:32.050
We're going to go ahead and do the same thing for the port.

06:32.080 --> 06:37.330
We need to call, get or throw and this will be the MySQL port.

06:37.960 --> 06:42.100
Next we have the actual database name that we're connecting to.

06:42.100 --> 06:49.450
So we will call, get or throw MySQL database, which we know is going to be sleeper as we've already

06:49.450 --> 06:50.470
defined.

06:50.740 --> 06:57.580
And finally, for authentication, we'll provide our username and password from the config service.

06:57.580 --> 07:01.990
So this will be MySQL username.

07:03.970 --> 07:06.910
And the password will be getter.

07:06.910 --> 07:10.090
Throw my SQL password.

07:10.600 --> 07:16.600
So another option that we're going to enable is called synchronize, which is going to automatically

07:16.600 --> 07:20.260
create our database schema every time our app is launched.

07:20.260 --> 07:21.840
If it doesn't match.

07:21.850 --> 07:28.690
So importantly, we can see here that this should not be used in production, otherwise you could potentially

07:28.690 --> 07:29.770
lose data.

07:30.130 --> 07:36.100
So it's important and helpful to enable this when we're developing locally but not in production.

07:36.100 --> 07:43.990
So we'll use an environment variable to pick this option up as well and call config service dot getter

07:43.990 --> 07:48.940
throw MySQL synchronize.

07:51.090 --> 07:57.930
Finally, the last property we'll enable is auto load entities and this is going to automatically load

07:57.930 --> 08:04.860
all of the different entities, which is the same thing as schemas in mongoose in our application so

08:04.860 --> 08:09.420
that we don't have to come back to here and define them every time we create them.

08:09.420 --> 08:15.690
Nestjs will automatically load them using the metadata we provide on those classes, which is super

08:15.690 --> 08:16.390
helpful.

08:16.410 --> 08:19.860
Now let's come back to the database module for feature call.

08:19.860 --> 08:24.240
We're currently we're taking in these models of type model definition.

08:24.240 --> 08:29.550
Let's change this to entity class or schema from type ORM.

08:29.550 --> 08:33.300
And you can see this auto import that occurred up top.

08:33.300 --> 08:42.780
And instead of calling Mongoose module, we will return the type ORM module dot for feature and then

08:42.780 --> 08:44.190
pass in these models.

08:44.190 --> 08:49.470
So essentially we're doing the same thing we did in Mongoose when we had to register our schemas.

08:49.470 --> 08:55.300
We're going to do the same thing here in the database module with type ORM and the entities is what

08:55.300 --> 08:56.170
we call them.

08:56.290 --> 09:02.410
So now we can go ahead and remove these imports up top that reference mongoose since we're no longer

09:02.410 --> 09:03.100
using them.
