A production-ready NestJS boilerplate following Clean Architecture principles with advanced standard response formatting, filtering, sorting, pagination, queue processing, and cron jobs.
Author: @howznguyen
Repository: GitHub
- ✅ Clean Architecture: Domain-driven design with clear separation of concerns
- ✅ Advanced Standard Response: Consistent API response format with filtering, sorting & pagination
- ✅ Mapper Pattern: Clean separation between domain and persistence layers
- ✅ Queue Processing: Bull/BullMQ integration with Redis for background jobs
- ✅ Cron Jobs: Scheduled tasks using
@nestjs/schedule - ✅ Database: PostgreSQL with Prisma ORM and relationship handling
- ✅ Docker: Full containerization with environment variables
- ✅ API Documentation: Auto-generated OpenAPI/Swagger with query examples
- ✅ Validation: Request/response validation with class-validator
- ✅ TypeScript: Full TypeScript support
- ✅ Testing: Jest testing framework setup
src/
├── core/ # Core utilities and shared modules
│ ├── entities/ # Base entities and value objects
│ └── modules/
│ └── standard-response/ # Standard response formatting
├── domain/ # Business logic layer
│ └── user-management/ # User domain entities
├── application/ # Use cases layer
│ └── user-management/ # User use cases and ports
└── infra/ # Infrastructure layer
├── http/ # HTTP controllers and DTOs
├── persistence/ # Database repositories
├── queue/ # Background job processing
└── cronjob/ # Scheduled tasks
- Node.js 18+
- Docker & Docker Compose
- PostgreSQL (if running locally)
- Redis (if running locally)
-
Clone the repository
git clone <repository-url> cd nestjs-boilerplate
-
Install dependencies
npm install
-
Setup environment
cp .env.example .env # Edit .env with your configuration -
Start with Docker (Recommended)
# Development mode docker-compose up app-dev # Production mode docker-compose up app
-
Or start locally
# Generate Prisma client npm run prisma:generate # Run migrations npm run prisma:migrate:dev # Start development server npm run start:dev
Once the application is running, visit:
- Swagger UI: http://localhost:3000/api/docs
All API responses follow a consistent format:
{
"success": true,
"message": "Operation completed successfully",
"data": { ... },
"pagination": { ... }, // Only for paginated responses
"timestamp": "2023-01-01T00:00:00.000Z"
}@Controller('users')
export class UserController {
@Get()
@StandardResponse({
message: 'Users retrieved successfully',
type: UserResponseDto,
})
async getUsers() {
return await this.userService.findAll();
}
}The boilerplate includes Bull/BullMQ for background job processing:
// Adding jobs to queue
await this.queueService.addEmailJob({
to: 'user@example.com',
subject: 'Welcome!',
text: 'Welcome to our platform'
});Schedule tasks using decorators:
@Injectable()
export class TasksService {
@Cron(CronExpression.EVERY_HOUR)
handleHourlyTask() {
// Task logic here
}
}@Injectable()
export class CreateUserUseCase {
constructor(private userRepository: UserRepository) {}
async execute(dto: CreateUserDto): Promise<User> {
const user = User.create(dto);
await this.userRepository.save(user);
return user;
}
}# Create migration
npm run prisma:migrate:dev --name add_users_table
# Deploy migrations
npm run prisma:migrate:deploy
# Reset database
npm run prisma:migrate:reset# Unit tests
npm run test
# Integration tests
npm run test:e2e
# Test coverage
npm run test:cov# Start development environment
docker-compose up app-dev
# View logs
docker-compose logs -f app-dev
# Execute commands in container
docker-compose exec app-dev npm run prisma:migrate:dev# Build and start production
docker-compose up --build app
# Scale services
docker-compose up --scale app=3npm run start- Start production servernpm run start:dev- Start development server with hot reloadnpm run start:debug- Start with debugging enablednpm run build- Build for productionnpm run lint- Lint codenpm run test- Run unit testsnpm run test:e2e- Run integration tests
| Variable | Description | Default |
|---|---|---|
NODE_ENV |
Environment mode | development |
PORT |
Application port | 3000 |
DATABASE_URL |
PostgreSQL connection string | Required |
REDIS_HOST |
Redis host | localhost |
REDIS_PORT |
Redis port | 6379 |
CORS_ORIGIN |
CORS origin | true |
- Entities: Base classes for domain entities
- Standard Response: Automatic response formatting and validation
- Entities: Business entities with domain logic
- Value Objects: Immutable objects representing domain concepts
- Use Cases: Application business logic
- Ports: Interfaces for external dependencies
- HTTP: REST API controllers and DTOs
- Persistence: Database implementations
- Queue: Background job processing
- Cronjob: Scheduled task implementations
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the UNLICENSED License.