AI Skill Report Card
Generated Skill
Building NestJS Applications
Quick Start
TypeScript// app.module.ts import { Module } from '@nestjs/common'; import { UsersModule } from './users/users.module'; @Module({ imports: [UsersModule], }) export class AppModule {} // users/users.controller.ts import { Controller, Get, Post, Body, Param } from '@nestjs/common'; import { UsersService } from './users.service'; @Controller('users') export class UsersController { constructor(private readonly usersService: UsersService) {} @Get() findAll() { return this.usersService.findAll(); } @Post() create(@Body() createUserDto: any) { return this.usersService.create(createUserDto); } }
Recommendation▾
Consider adding more specific examples
Workflow
-
Project Setup
Bashnpm i -g @nestjs/cli nest new project-name cd project-name -
Generate Resources
Bashnest generate resource users --no-spec -
Structure Implementation Progress:
- Create DTOs for request/response validation
- Implement service layer with business logic
- Configure controller endpoints
- Set up module with providers and imports
- Add database integration if needed
- Configure middleware and guards
-
Testing Setup
Bashnpm run test npm run test:e2e
Recommendation▾
Include edge cases
Examples
Example 1: Complete CRUD Module Input: User management with validation Output:
TypeScript// create-user.dto.ts import { IsEmail, IsNotEmpty } from 'class-validator'; export class CreateUserDto { @IsNotEmpty() name: string; @IsEmail() email: string; } // users.service.ts import { Injectable } from '@nestjs/common'; @Injectable() export class UsersService { private users = []; create(createUserDto: CreateUserDto) { const user = { id: Date.now(), ...createUserDto }; this.users.push(user); return user; } findAll() { return this.users; } } // users.module.ts import { Module } from '@nestjs/common'; import { UsersController } from './users.controller'; import { UsersService } from './users.service'; @Module({ controllers: [UsersController], providers: [UsersService], exports: [UsersService], }) export class UsersModule {}
Example 2: Database Integration Input: TypeORM with PostgreSQL Output:
TypeScript// app.module.ts import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'postgres', host: 'localhost', port: 5432, username: 'user', password: 'password', database: 'nestjs_db', entities: [User], synchronize: true, }), UsersModule, ], }) export class AppModule {} // user.entity.ts import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column() name: string; @Column({ unique: true }) email: string; }
Best Practices
- Module Organization: One feature per module, keep related functionality together
- Dependency Injection: Use constructor injection, avoid circular dependencies
- DTOs: Always validate input with class-validator and class-transformer
- Error Handling: Use built-in HTTP exceptions (
BadRequestException,NotFoundException) - Configuration: Use
@nestjs/configfor environment variables - Testing: Write unit tests for services, integration tests for controllers
- Guards: Implement authentication/authorization with guards
- Interceptors: Use for logging, transformation, caching
- Pipes: Validate and transform data at endpoint level
Common Pitfalls
- Circular Dependencies: Avoid importing modules that import each other
- Missing Providers: Always register services in module providers array
- Synchronous Operations: Use async/await for database operations
- Global Scope: Don't make everything global; use proper module imports
- Direct Database Access: Always use repositories/services, not direct entity access in controllers
- Missing Validation: Always validate DTOs with decorators
- Hardcoded Values: Use configuration service instead of environment variables directly
- Missing Error Handling: Don't let unhandled promises crash the application