Crafting a Scalable REST API using NestJS and Mongoose

Crafting a Scalable REST API using NestJS and Mongoose

Building a REST API with NestJS and Mongoose

We are creating an employee crud application using nest js cli and mongoose. We are using GET, POST, PUT, and DELETE methods in this application.

PREREQUISITES

Creating a REST API with NestJS and Mongoose entails harnessing NestJS, a TypeScript-driven framework renowned for crafting resilient and easily maintainable server-side applications. Additionally, it entails leveraging Mongoose, a widely embraced Object-Document Mapping (ODM) library tailor-made for MongoDB.

Before you start building your API, you need to have some prerequisites in place. Here are the requirements:

  • Node.js and npm/yarn :
  • NestJS CLI :
  • You need the NestJS CLI to generate the application. using cli commands.
  • npm install -g @nestjs/cli

# or

  • yarn global add @nestjs/cli
  • MongoDB :
  • The MongoDB server is running and up. You can install MongoDB Compass or you can also use cloud-based MongoDB.
  • MongoDB Install locally.
  • Code Editor :
  • You can use any code editor. but we prefer Visual Studio Code
  • It is to set up and use.
  • VS code has a lot of extensions to enhance your coding speed.
  • TypeScript Knowledge :
  • Nest js cli is building in typescript
  • You must have basic typescript knowledge or understanding.
  • Here is documentation to learn typescript.
  • Basic knowledge or understanding about Restful API’s :
  • Restful API including HTTP methods like, GET, POST, PUT, DELETE.
  • Status codes e.g., 200, 201, 404, 500, etc…

Setup Application

Install or create the application using nest cli commands. the above commands are run then auto-starting creating the application.

        nest new employee-crud

Once the application is created navigate to the application folder and open in code editor.

Then open your code editor terminal or CMD then run this above command.

        npm install mongoose @nestjs/mongoose

This command is installing Mongoose in your application.

Setup Database Connection

We are using MongoDB for storing employee details. Here we are connecting MongoDB connection using Mongoose. At this moment you ensure the MongoDB is running.

Import Mongoose Module and for DB path setup inside the MongoseModule.

Creating Employee Schema

A schema will show the collection representation of an employee. open scr folder create one folder named schema, and create one file named emapoyee.schema.ts. Open this file and write the image code above.

Creating Dto

Dto is a Data Transfer Object. Here we can use Dto to create employees. Managing the fields required and validation. Validation is like required, string, is number, etc…, inside the src folder create one folder named Dto/, and inside the Dto folder creates one file named create-employee.dto.ts.and write code above show image code.

For Validation install the above commands and configuration in the main.ts file.

        npm i --save class-validator class-transformer

Open src/main.ts file and write this above show image code.

Creating Service

A service file handles a business logic of nest js. In the service file, we connect the collection and handle the database operations like insertion, deletion, and Updation.

Create a one folder in the src folder named service, in the service folder, create one file named employee.service.ts and write the code above show.

Creating Controller

The controller is allowed to create routes and perform service login in the controller. create one controller file named employee.controller.ts.

Creating Module

Create a module file named employee.module.ts. Import employee controller and service in the employee module.

And import this employee module in app module

To test the application run this command:

npm run start

Here you can see the application is served successfully now you can test in any API testing tool like Postman or Swagger.

Source code is soon uploading if you have any questions then please contact us.


Post a Comment

Previous Post Next Post