CRUD operations let you create, read, update, and delete data in your app. They are the basic actions to manage information.
0
0
CRUD operations in NestJS
Introduction
Building a user management system where you add, view, edit, or remove users.
Creating a blog where you write, read, update, or delete posts.
Managing products in an online store with add, list, update, and delete features.
Syntax
NestJS
import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common'; @Controller('items') export class ItemsController { @Get() findAll() {} @Get(':id') findOne(@Param('id') id: string) {} @Post() create(@Body() createItemDto) {} @Put(':id') update(@Param('id') id: string, @Body() updateItemDto) {} @Delete(':id') remove(@Param('id') id: string) {} }
Use decorators like @Get, @Post, @Put, and @Delete to define HTTP methods.
@Param gets data from the URL, @Body gets data from the request body.
Examples
Gets all items when you visit /items with a GET request.
NestJS
@Get()
findAll() {
return 'Return all items';
}Creates a new item using data sent in the request body.
NestJS
@Post()
create(@Body() createItemDto) {
return `Create item with name: ${createItemDto.name}`;
}Updates an existing item by its ID with new data.
NestJS
@Put(':id') update(@Param('id') id: string, @Body() updateItemDto) { return `Update item ${id} with name: ${updateItemDto.name}`; }
Deletes an item by its ID.
NestJS
@Delete(':id') remove(@Param('id') id: string) { return `Delete item with id ${id}`; }
Sample Program
This controller manages a list of items in memory. You can add, get, update, and delete items by their ID.
NestJS
import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common'; interface Item { id: number; name: string; } @Controller('items') export class ItemsController { private items: Item[] = []; private idCounter = 1; @Get() findAll(): Item[] { return this.items; } @Get(':id') findOne(@Param('id') id: string): Item | string { const item = this.items.find(i => i.id === +id); return item || 'Item not found'; } @Post() create(@Body() createItemDto: { name: string }): Item { const newItem = { id: this.idCounter++, name: createItemDto.name }; this.items.push(newItem); return newItem; } @Put(':id') update(@Param('id') id: string, @Body() updateItemDto: { name: string }): Item | string { const item = this.items.find(i => i.id === +id); if (!item) return 'Item not found'; item.name = updateItemDto.name; return item; } @Delete(':id') remove(@Param('id') id: string): string { const index = this.items.findIndex(i => i.id === +id); if (index === -1) return 'Item not found'; this.items.splice(index, 1); return `Deleted item with id ${id}`; } }
OutputSuccess
Important Notes
In real apps, data usually comes from a database, not memory.
Use DTOs (Data Transfer Objects) to validate and type-check data.
Each HTTP method matches a CRUD action: GET=read, POST=create, PUT=update, DELETE=delete.
Summary
CRUD operations let you manage data by creating, reading, updating, and deleting.
In NestJS, use controllers with decorators like @Get, @Post, @Put, and @Delete.
Use @Param to get URL data and @Body to get data sent by the client.