gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
give me code for crud in nest js with typerom
- User
give me code for crud in nest js with typerom
- Assistant
give me code for crud in nest js with typerom
Sure! Here's an example code for a CRUD operation in Nest.js with TypeORM:
Install required packages:
npm install @nestjs/typeorm typeorm reflect-metadata
Create a User entity using TypeORM decorators:
// src/user/user.entity.ts
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
email: string;
@Column()
age: number;
}
Create a UserDto for data transfer:
// src/user/dto/user.dto.ts
export class UserDto {
id: number;
name: string;
email: string;
age: number;
}
Create a UserController with CRUD endpoints:
// src/user/user.controller.ts
import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common';
import { UserService } from './user.service';
import { UserDto } from './dto/user.dto';
@Controller('users')
export class UserController {
constructor(private readonly userService: UserService) {}
@Get()
findAll(): Promise<UserDto[]> {
return this.userService.findAll();
}
@Get(':id')
findOne(@Param('id') id: string): Promise<UserDto> {
return this.userService.findOne(Number(id));
}
@Post()
create(@Body() userDto: UserDto): Promise<UserDto> {
return this.userService.create(userDto);
}
@Put(':id')
update(@Param('id') id: string, @Body() userDto: UserDto): Promise<UserDto> {
return this.userService.update(Number(id), userDto);
}
@Delete(':id')
delete(@Param('id') id: string): Promise<void> {
return this.userService.delete(Number(id));
}
}
Create a UserService to handle CRUD operations using TypeORM:
// src/user/user.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';
import { UserDto } from './dto/user.dto';
@Injectable()
export class UserService {
constructor(@InjectRepository(User) private readonly userRepository: Repository<User>) {}
async findAll(): Promise<UserDto[]> {
const users = await this.userRepository.find();
return users.map(user => this.mapToDto(user));
}
async findOne(id: number): Promise<UserDto> {
const user = await this.userRepository.findOne(id);
return this.mapToDto(user);
}
async create(userDto: UserDto): Promise<UserDto> {
const user = this.userRepository.create(userDto);
await this.userRepository.save(user);
return this.mapToDto(user);
}
async update(id: number, userDto: UserDto): Promise<UserDto> {
const user = await this.userRepository.findOne(id);
this.userRepository.merge(user, userDto);
await this.userRepository.save(user);
return this.mapToDto(user);
}
async delete(id: number): Promise<void> {
await this.userRepository.delete(id);
}
private mapToDto(user: User): UserDto {
const { id, name, email, age } = user;
return { id, name, email, age };
}
}
Import the required modules and configure TypeORM in the main application file:
// src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Connection } from 'typeorm';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const connection = app.get(Connection);
await connection.runMigrations();
await app.listen(3000);
}
bootstrap();
Finally, create an AppModule file and configure the TypeORM entity and controller:
// src/app.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user/user.entity';
import { UserController } from './user/user.controller';
import { UserService } from './user/user.service';
@Module({
imports: [TypeOrmModule.forRoot(), TypeOrmModule.forFeature([User])],
controllers: [UserController],
providers: [UserService],
})
export class AppModule {}
That's it! You now have a basic CRUD operation setup with Nest.js and TypeORM. You can test the API using tools like Postman or cURL.