A developer refactors application schemas to TypeORM entities within a NestJS microservices environment. The process includes updating models, repositories, and services, transitioning from MongoDB-specific decorators to TypeORM annotations, and configuring environment variables for MySQL database integration.
Length 19:21
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #typescript #typeorm #nestjs #refactoring #microservices #database #entity #coding #programming #backend
0:00 Okay, so now let's work on refactoring our 0:03 schemas in our 0:04 application to entities in TypeORM. 0:08 Let's start off with the reservations microservice. 0:11 So right now, under our models directory, we have our 0:14 reservation.schema. 0:17 I wanna go ahead and rename this as we've done in the 0:21 previous cases. I wanna rename this to reservation.entity 0:25 now. 0:28 And here, we will no longer be importing the 0:31 abstract 0:31 document or using the @schema decorator. 0:35 Instead, we're gonna use the @entity decorator from 0:39 TypeORM, which will mark this as an entity and will 0:42 create a table for us based on this schema. 0:46 Now, let's change this to be a just 0:48 reservation, and it's going to 0:50 extend the abstract entity 0:55 where we pass in the reservation. 1:00 And we can also remove the exported schema from the bottom. 1:05 Now, all we have to do to move this over to a 1:07 TypeORM entity 1:09 is change these prop decorators to @column 1:12 decorators, which represent a column in our 1:16 table in our SQL database. So let's go ahead 1:20 and replace all of these properties with an @column decorator. 1:24 And now let's go into our reservations repository, 1:27 where we're gonna have to refactor this to now work with our 1:31 newly implemented abstract repository. 1:35 So of course, we will remove the reservation 1:39 document and replace it with just a reservation. 1:43 And now we are going to still extend the abstract repository, but 1:47 now we'll pass in the reservation entity. 1:52 And importantly, in the constructor, let's go ahead and remove 1:55 this because now we're going to be using 1:59 @injectRepository from NestJS TypeORM 2:03 to actually get our repository injected for us. 2:06 All we have to do is provide the entity name here. 2:10 So let's provide the entity. 2:13 So we'll call this reservationsRepository 2:16 of type Repository 2:19 and pass in the reservation entity. 2:22 And then, of course, we also need the entityManager of type
The rest of the transcript is for members.
Sign in