WEBVTT

00:00.740 --> 00:01.070
All right.

00:01.100 --> 00:07.970
Next up, let's go ahead and modify our abstract schema, which right now is representing the abstract

00:07.970 --> 00:10.040
document in the mongoose world.

00:10.070 --> 00:12.440
Let's go ahead and start off by renaming this.

00:12.440 --> 00:19.850
So instead of abstract schema, we're going to create an abstract entity, which is the same thing as

00:19.850 --> 00:29.440
a document or schema in Mongoose so we can remove the schema decorator and the ID property.

00:29.450 --> 00:36.320
Let's change the name to abstract entity and remove these imports to mongoose.

00:36.350 --> 00:42.590
Now I want to add just one property called ID of type number, which is going to represent the primary

00:42.590 --> 00:45.680
key of all our entities in our application.

00:45.680 --> 00:51.770
So since this is a primary key, this is going to be a unique value that automatically increments each

00:51.770 --> 00:55.220
time a new entity is created in that given table.

00:55.220 --> 01:01.670
So let's go ahead and mark this as the primary generated column to indicate this.

01:01.710 --> 01:08.190
So in type ORM, to actually save these entities to the database, we're going to actually be instantiating

01:08.190 --> 01:14.700
new instances of this class, the abstract entity which we didn't have to do in mongoose.

01:14.700 --> 01:21.720
So in order to make this easy, we'll go ahead and supply a common constructor that we can use when

01:21.720 --> 01:27.480
we want to create a new entity out of a subset of properties that exist on that entity.

01:27.480 --> 01:33.600
So in the constructor we'll take in the partial entity which will be of type partial, and then we'll

01:33.600 --> 01:35.250
take in a T here.

01:35.250 --> 01:41.970
So the abstract entity also takes in that T, and then inside the constructor we'll call object, dot

01:41.970 --> 01:46.140
assign and reference this and then the entity.

01:46.410 --> 01:53.760
So what this is going to do is it's going to take all of the properties on this partial entity being

01:53.760 --> 02:00.720
provided to the constructor and assign them to the abstract entity, which just means that all the properties

02:00.720 --> 02:06.780
that get passed in the constructor will be copied over to the newly created instance of this class,

02:06.780 --> 02:10.080
making it very easy to create new entities.

02:10.740 --> 02:15.210
We will see how this works in practice soon when we start saving entities.

02:15.420 --> 02:21.450
So now that we've defined the abstract entity, let's go back to our abstract repository where of course

02:21.450 --> 02:26.430
we can see it's having an issue because the abstract document no longer exists.

02:26.460 --> 02:33.090
Let's refactor this abstract repository to move away from mongoose and instead work with type ORM.

02:33.090 --> 02:38.430
So all of the repositories in our microservices will get this functionality.

02:38.580 --> 02:43.050
Okay, so to start, let's remove this import to the abstract document.

02:43.050 --> 02:50.880
And now in the abstract repository, instead of extending the abstract document here, let's change

02:50.880 --> 03:02.480
this to be t extends abstract entity which we've just created and then pass in the t to this abstract

03:02.480 --> 03:03.290
entity.

03:07.600 --> 03:11.500
So this T here is going to be the entity that the.

03:13.180 --> 03:20.230
Subclass of the abstract repository will be passing in, which will be extending the abstract entity.

03:20.560 --> 03:22.930
We're going to keep the logger the same.

03:22.930 --> 03:28.210
And now in the constructor, let's change this out to no longer take in a model.

03:28.420 --> 03:35.410
Instead, we're going to go ahead and inject two separate dependencies to allow us to interact with

03:35.410 --> 03:36.850
the SQL database.

03:37.090 --> 03:45.130
The first is going to be private read only entity repository, and this is going to be of type repository

03:45.130 --> 03:50.500
from type ORM, and we can pass in the T to get some type information around it.

03:50.500 --> 03:58.030
So this entity repository is what we're going to be using to find different entities in the database

03:58.030 --> 04:03.550
and get some type information around our queries, which is very similar to the model that we worked

04:03.550 --> 04:05.140
with with Mongoose.

04:05.320 --> 04:12.830
So additionally, we're also going to inject a private read only entity manager of type entity manager,

04:12.830 --> 04:20.960
and the entity manager is going to allow us to save entities in our database using instances of the

04:20.960 --> 04:22.460
entities that we've created.

04:22.460 --> 04:26.780
So let's go ahead and get started with the async create method.

04:27.080 --> 04:35.180
So now instead of taking in a document, we're going to be taking in an entity of type T, and we're

04:35.180 --> 04:37.700
going to return a promise of type T.

04:37.730 --> 04:43.750
Now we can remove everything in the create method and all we're going to do is simply return this dot

04:43.790 --> 04:46.640
entity, manager, dot save.

04:46.910 --> 04:51.290
And you can see here that this will save all of the given entities in the database.

04:51.320 --> 04:55.760
If they don't exist, it will insert, otherwise it will update these entities.

04:55.760 --> 05:00.770
So in a sense this will be doing an upsert for all the entities that are passed in.

05:00.770 --> 05:08.560
So we'll pass in the entity and now we can create new entities in our database.

05:08.570 --> 05:11.100
Let's move on to the find one method.

05:11.120 --> 05:14.690
So let's get rid of the filter query as a parameter.

05:14.690 --> 05:20.120
And our first parameter is going to be called where and this is going to be our where clause or our

05:20.120 --> 05:20.830
query.

05:20.840 --> 05:28.460
So this is going to be of type find options where from type ORM and we'll pass in the T as well.

05:28.460 --> 05:37.490
So let's replace this line here and instead of getting a document we'll call const entity is equal to

05:37.490 --> 05:42.560
await this dot entity repository dot find one.

05:42.920 --> 05:49.700
And here we pass in an options object where we pass in that where clause which is going to allow us

05:49.700 --> 05:54.050
to match a simple condition that's applied to match entities.

05:54.230 --> 06:00.590
So now that we have this entity pulled back from the database, let's go ahead and check to see if it

06:00.590 --> 06:01.430
exists.

06:01.430 --> 06:10.220
So if the entity doesn't exist, we will say entity not found with where, and then we can provide the

06:10.220 --> 06:11.360
where clause.

06:11.540 --> 06:18.940
Let's also go ahead and change the promise Ms. of t document out with just a promise of type T And then

06:18.940 --> 06:23.380
lastly, we're going to go ahead and return the entity instead of the document.

06:23.510 --> 06:28.000
Also go ahead and change the not found exception to be an entity not found.

06:28.210 --> 06:30.430
So now we have a find one.

06:30.430 --> 06:33.550
Let's move on to find one and update.

06:34.030 --> 06:35.980
This is going to look quite similar.

06:35.980 --> 06:41.980
We'll take in the where clause which is find options where.

06:43.170 --> 06:49.590
And then for the update parameter, we'll change this to partial entity, which is exactly what it's

06:49.590 --> 06:50.010
going to be.

06:50.010 --> 06:54.420
It's going to be a partial subset of our entity, which we want to update.

06:54.420 --> 07:02.540
So this type is going to be called query deep partial entity and pass in our type of T.

07:03.840 --> 07:09.510
So essentially this is going to be a subset of properties that exist on our entity that we want to be

07:09.510 --> 07:10.470
updating.

07:10.980 --> 07:18.900
And so we'll go ahead and change our update expression to a new const called update result.

07:19.740 --> 07:27.390
And we'll set this equal to await this dot entity repository dot update.

07:28.080 --> 07:34.950
And here we'll pass in our where clause as the criteria to find the entity to update and then we'll

07:34.950 --> 07:36.780
pass in the partial entity.

07:37.080 --> 07:42.780
So next, let's check to see if the update result Dot affected, which is going to be the number of

07:42.780 --> 07:47.010
affected documents by the update statement is zero.

07:47.010 --> 07:55.170
So if this is zero, which is what this expression will check for, then we want to warn that the entity

07:55.200 --> 08:00.300
was not found with where and pass in our where clause.

08:00.330 --> 08:04.070
We'll also change the exception to entity not found.

08:04.090 --> 08:11.080
Then in order to get the most up to date entity, we'll return this dot find one which is the method

08:11.080 --> 08:16.780
in this abstract repository and we'll pass in the same where clause to get the most up to date version

08:16.780 --> 08:19.790
of this entity after the update was applied.

08:19.810 --> 08:25.840
So lastly, these two remaining methods find and find one and delete are going to be very simple.

08:26.020 --> 08:33.550
Async find is going to take in a different parameter here that we'll call where as we've done before,

08:33.580 --> 08:42.880
this will be find options where as we are used to, we pass in the type of T and then we're simply going

08:42.880 --> 08:52.960
to return this dot entity repository dot find by and pass in the where clause which is going to allow

08:52.960 --> 08:57.730
us to find multiple entities that all match this where clause.

08:59.010 --> 09:06.630
And finally, for find one and delete, we will have the where clause which is going to be find options

09:06.630 --> 09:16.170
where of type T and instead of returning the deleted result we are going to call await this dot entity

09:16.170 --> 09:21.060
repository, dot delete and pass in the where clause.
