Why you should start using NestJS
By: Coding In Depth
Tags: Nestjs, Nestjs Tutorial, Nodejs, API, Architecture

Creating NestJS Product API from Scratch

We will create here a CRUD API for using NestJS. Below is the GitHub link for more control over the project. You can fork it and play around it.

First, install NestJS globally and then create a new project. Check the below command and execute it.

npm i -g @nestjs/cli

$ nest new nest-product-api

Now install dependencies and open the project in visual code.

npm install code .

Now create a product module. The project structure of NestJS is very similar to Angular. We can create modules, controllers in place of components, Services, interceptors, etc. Let's create a product module, controller, and services.

Now add the product module in the root module.

@Module({ imports: [ ProductsModule], controllers: [AppController], providers: [AppService], }) export class AppModule {}

For the demo, we are building the Product API. The product has a product id, product name, product code, and product description. Now create Product data models and a class that implements it. In the class, the constructor accepts product properties and initialize the product.

export class Product implements IProduct { constructor( public prodid: string, public productName: string, public productDesc: string, public prodAddedDate: string, ) {} } export interface IProduct{ prodid?: string | null; productName?: string; productDesc?:string; prodAddedDate?:string }

Import the model in services and create a product service class. Now create an add method that accepts product name, description, added date. The add product method generates product id randomly and returns product id when the product is added successfully.

@Injectable() export class ProductsService { private products: IProduct[] = []; addProduct(productName:string,productDesc:string,productAddedDate:string) { const prodId = Math.random().toString(); let product:IProduct = new Product(prodId,productName,productDesc,productAddedDate); this.products.push(product); return prodId; } }

In the product, the controller adds the following lines of code. NestJS provide inbuilt decorators, Post, and Body.

@Post() addProduct( @Body(‘name’) productName: string, @Body(‘description’) productDesc: string, @Body(‘addedon’) productAddedDate: string, ) { return this.productsService.addProduct(productName,productDesc,productAddedDate) }

Now when we insert some records, we will get record ids. Now time to get all products and a specific product.

For getting all products we can return a piece of products.

getProducts() { return of([…this.products]); }

For getting a specific product we will write a get query. The user will provide id and search it and return the response.

getProductById(productId: string) { console.log(productId); let index = this.products.findIndex(p=>p.prodid===productId); let searchProduct = index == 0 || index >0 ? this.products[index] : {} return of(searchProduct); }

To update the product search by product id and then update the record.

updateProduct(productId: string, productName: string, productDesc: string, prodAddedDate: string) { const index = this.products.findIndex(p=>p.prodid===productId); var product :IProduct; if(index == 0 || index >0) { product = { productName: productName, productDesc: productDesc,prodAddedDate: prodAddedDate} this.products[index]= product; } return of(product); }

For delete check the product by product id and splice from products.

deleteProduct(productId: string) { const index = this.products.findIndex(p=>p.prodid===productId); return of(this.products.splice(index, 1)); }

The complete product controller and services will look like this:

Postman request and response