A developer refactors a codebase by migrating from Mongoose to TypeORM. The process involves creating an abstract entity, updating repository logic, and implementing database operations like create, find, update, and delete using TypeORM's entity manager and repository patterns.
Length 9:22
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #typescript #typeorm #refactoring #mongoose #database #repository #entity #programming #backend #software
0:00 All right, next up, let's go ahead and modify our abstract 0:04 schema, which right now is representing the abstract 0:08 document in the Mongoose world. Let's go ahead and start off by 0:11 renaming this. So instead of abstract schema, 0:14 we're gonna create 0:15 an abstract entity, which is the same thing 0:19 as a document or schema in Mongoose. 0:23 So we can remove the schema decorator and 0:27 the ID property. Let's change the name to 0:31 abstract entity and remove these imports 0:35 to Mongoose. Now I wanna add just one property called 0:39 ID of type number, which is gonna represent the primary 0:42 key of all our entities in our application. 0:46 So since this is a primary key, this is gonna be a unique value that 0:49 automatically increments each time a new entity is 0:53 created in that given table. So let's go ahead and mark this 0:57 as the primary generated column 0:59 to indicate this. So in TypeORM, to actually save 1:03 these entities to the database, we're gonna actually be 1:07 instantiating new instances of this class, 1:11 the abstract entity, which we didn't have to do in Mongoose. 1:14 So in order to make this easy, we'll go ahead and supply a 1:18 common constructor that we can use when we want to create a 1:22 new entity out of a subset of properties that exist on 1:26 that entity. So in the constructor, we'll take in the partial 1:30 entity, which will be of type partial, and then we'll take in a T 1:34 here. So the abstract entity also takes in 1:37 that T. 1:38 And then inside the constructor, we'll call object.assign 1:43 and reference this and then the entity. 1:46 So what this is going to do is it's gonna take all of the 1:50 properties on this partial entity being provided to the 1:54 constructor and assign them to the abstract entity, 1:59 which just means that all the properties
The rest of the transcript is for members.
Sign in