A developer sets up a persistence layer in a NestJS application using MongoDB and Mongoose. The process includes installing dependencies, abstracting configuration and database modules, utilizing environment variables with validation, and verifying the connection via terminal commands.
Length 13:56
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #nestjs #mongodb #mongoose #typescript #microservices #backend #database #environment variables #coding #software engineering
0:00 Okay, so we're ready to get started building out 0:02 our common module, and 0:04 the first part of our common module is going to be our persistence 0:07 layer. So working with a database, uh, and working with an 0:11 ORM so that we can interact with the database and save entities in our 0:15 microservices. 0:17 So we're gonna use MongoDB for our database 0:21 layer, and we're gonna build it in a very 0:23 abstract way so that we could potentially 0:25 swap the database out with a SQL database using something like 0:29 TypeORM. So we're gonna build this in a very 0:31 generic way, a very 0:32 abstract way, follow best practices. So let's jump right in. 0:36 On the command line, we're gonna need to add a few 0:38 dependencies. 0:39 So I'm gonna run pnpm install nestjs- 0:43 mongoose and then mongoose itself here. 0:46 So go ahead and install these dependencies. 0:49 So in addition to adding our database module here, we're also going to add 0:54 our configuration module, which is gonna allow us to read in 0:57 environment variables using a .env file using the 1:01 dotenv package, is what NestJS uses under the hood. 1:04 But this is gonna make it so that we read in environment variables, and 1:08 we can change these variables depending on where we're running. 1:11 If we're running locally or in production, we can change that out for 1:15 different values like our MongoDB URI and other 1:19 configuration variables we will need later on as we build out our 1:22 microservices. So to add that, we'll run pnpm 1:26 install nestjs/config. 1:31 Okay, so now we have all the dependencies we 1:32 actually need. 1:33 We can use the Nest CLI again to actually generate our 1:37 common module. So we'll use nest generate module 1:41 and then do -p where we specify the project we wanna pass 1:45 in. Of course, if we go to our nestcli.json,
The rest of the transcript is for members.
Sign in