The tutorial demonstrates creating an abstract schema and a generic abstract repository for NestJS microservices. It covers implementing standard CRUD operations—create, find, update, and delete—using Mongoose while optimizing performance with lean queries and adding robust error handling for missing documents.
Length 11:05
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #typescript #mongoose #nestjs #mongodb #crud #abstract-repository #microservices #programming #software-development #coding
0:00 Okay, so back in the database directory, we're gonna create 0:04 an abstract schema, and so this will be the Mongoose 0:08 schema type that we'll call the 0:10 abstract.schema.ts. So this abstract 0:14 schema is what all other schemas in our microservice architecture will 0:18 extend from and will have a single common property of the 0:22 underscore ID, which exists on all MongoDB 0:25 documents by default. So we will decorate this class 0:29 with the schema decorator from NestJS Mongoose and export 0:33 the abstract document class here 0:38 because it is an abstract document, and we will decorate a property here 0:42 with underscore ID. 0:44 We'll say that the type here is of schema types. 0:48 Make sure we import this from mongoose.ObjectId, 0:53 and then we can add the actual property here, which in this case will be types 0:57 from mongoose.ObjectId. So now we have 1:00 this abstract document that we can use in reference inside of our 1:04 abstract repository. So let's go ahead and actually finally create the 1:08 abstract.repository.ts. And 1:12 as I said before, this is going to be the common repository 1:16 that all 1:16 other repositories in our microservices will extend from and have common 1:20 CRUD functionality, including being able to create, find, 1:24 update, and delete entities from our database. 1:28 This abstract repository will make sure that we don't have to duplicate this code 1:32 in all of our microservices. So let's start by exporting an abstract 1:36 class here called AbstractRepository. 1:39 And now we can give a type here, a TypeScript generic. 1:43 In this case, we'll call it TDocument, and this will 1:47 be the document that this repository is 1:51 associated with. So in this case, we know that this document is 1:55 going to extend the abstract document that we 1:59 just created because we know it will be a MongoDB document with that 2:03 underscore ID. So now we can start filling out the
The rest of the transcript is for members.
Sign in