A production-ready, enterprise-grade NestJS boilerplate with PostgreSQL, Prisma ORM, JWT authentication, and comprehensive API documentation. This project provides a solid foundation for building modern, scalable backend applications with TypeScript.
- Quick Start
- Requirements
- Features
- Project Structure
- Database Setup
- Development
- Testing
- Docker Setup
- License
Perfect for developers who want to get up and running fast!
Ensure you have these installed:
Tool | Minimum Version | Download Link |
---|---|---|
Node.js | v22+ |
Download Node.js |
PostgreSQL | v15+ |
Download PostgreSQL |
npm | v10.9.2+ |
Comes with Node.js |
π‘ Pro Tip: Use Volta for Node.js version management to automatically switch between Node versions in different projects.
git clone <repository-url>
cd nestjs-boilerplate
npm install
# Copy the example environment file
cp .env.example .env
Update .env
with your database credentials and JWT secrets.
# Generate Prisma client
npm run prisma:generate
# Run database migrations
npm run prisma:migrate
# Optional: Seed with test data
npm run db:seed
npm run dev
Your application is now running! Here are the key endpoints:
Service | URL | Description |
---|---|---|
API Server | http://localhost:3000 | Main application |
API Docs | http://localhost:3000/docs | Interactive Swagger UI |
Health Check | http://localhost:3000/api/v1/health | System status |
Database Studio | npm run prisma:studio |
Visual DB explorer |
Essential Requirements:
Tool | Version | Purpose | Installation |
---|---|---|---|
Node.js | v22.15.0+ | JavaScript runtime | Download |
PostgreSQL | v15+ | Database | Download |
npm | v10.9.2+ | Package manager | Comes with Node.js |
Recommended Tools:
Tool | Purpose | Why We Recommend |
---|---|---|
Volta | Node.js version management | Automatically uses correct Node/npm versions |
Docker | Containerization | Easy deployment and consistent environments |
- NestJS - Progressive Node.js framework with TypeScript
- Express.js - Fast, unopinionated web framework
- TypeScript - Type-safe development experience
- Internationalization - Multi-language support with i18n
- PostgreSQL - Robust relational database
- Prisma ORM - Type-safe database client with migrations
- Database Seeding - Pre-populated test data for development
- JWT Authentication - Secure token-based auth with refresh tokens
- Cookie-based Auth - HttpOnly, secure cookie storage
- Password Hashing - PBKDF2 with salt for secure password storage
- Rate Limiting - Built-in request throttling
- CORS & Helmet - Security headers and cross-origin protection
- Swagger/OpenAPI - Interactive API documentation
- Auto-generated Docs - Documentation from decorators
- API Versioning - Built-in API version management
- Jest Testing - Unit tests with comprehensive coverage
- Test Coverage - Coverage reports and thresholds
- ESLint & Prettier - Code formatting and linting
- Husky & lint-staged - Pre-commit hooks
- Health Checks - Application and database health monitoring
- Request Tracing - Unique trace IDs for each request
- Structured Logging - Consistent log formatting
- Sentry Integration - Error tracking and monitoring
- Hot Reload - Instant development feedback
- Environment Configuration - Flexible env management with validation
- Database Studio - Visual database explorer
- Docker Support - Containerized development and deployment
nestjs-boilerplate/
βββ π README.md # You are here!
βββ π package.json # Dependencies and scripts
βββ π .env.example # Environment variables template
βββ π docker-compose.yml # Docker setup for local development
β
βββ π src/ # π― Main application source code
β βββ π app/ # Application module (entry point)
β β βββ app.controller.ts # Health check endpoints
β β βββ app.module.ts # Root application module
β β βββ app.controller.spec.ts # Controller tests
β β
β βββ π common/ # π§ Shared utilities and components
β β βββ π constants/ # Application constants
β β βββ π dtos/ # Data Transfer Objects
β β βββ π interfaces/ # TypeScript interfaces
β β βββ π messages/ # Error/success messages
β β βββ π services/ # Shared services (health, logger)
β β βββ π utils/ # Utility functions (crypto, common)
β β
β βββ π config/ # βοΈ Configuration modules
β β βββ app.config.ts # App-level configuration
β β βββ database.config.ts # Database configuration
β β βββ jwt.config.ts # JWT configuration
β β
β βββ π core/ # ποΈ Core application features
β β βββ π class/ # Base classes (ResponseResult)
β β βββ π decorators/ # Custom decorators (@CurrentUser, @Public)
β β βββ π guards/ # Route guards (AuthGuard, ThrottlerGuard)
β β βββ π interceptors/ # Request/response interceptors
β β βββ π interfaces/ # Core interfaces
β β βββ π middleware/ # Custom middleware (TraceMiddleware)
β β
β βββ π database/ # ποΈ Database configuration
β β βββ π migrations/ # Prisma migration files
β β βββ schema.prisma # Database schema definition
β β βββ prisma.service.ts # Prisma service provider
β β βββ seed.ts # Database seeding script
β β
β βββ π i18n/ # π Internationalization
β β βββ π en/ # English translations
β β βββ error.json # error messages
β β
β βββ π modules/ # π§© Feature modules
β β βββ π feature/ # Feature module
β β βββ π dtos/ # Feature-specific DTOs
β β βββ π interfaces/ # Feature interfaces
β β βββ π messages/ # Feature messages
β β βββ feature.controller.ts # Feature endpoints
β β βββ feature.controller.spec.ts # Feature endpoints tests
β β βββ feature.service.ts # Feature business logic
β β βββ feature.service.spec.ts # Feature business logic tests
β β βββ feature.repository.ts # Feature specifice repository
β β βββ feature.repository.spec.ts # Feature specifice repository tests
β β βββ feature.module.ts # Feature module
β β βββ feature.constant.ts # Feature-specific constants
β β
β βββ main.ts # π Application entry point
β
βββ π test/ # π§ͺ Test configuration and utilities
β βββ app.e2e-spec.ts # End-to-end tests
β βββ helpers.ts # Test helper functions
β βββ jest-e2e.json # E2E test configuration
β βββ setup.ts # Test setup and global mocks
β
βββ π .eslintrc.js # ESLint configuration
βββ π .prettierrc # Prettier configuration
βββ π commitlint.config.js # Commit message linting
βββ π jest.config.js # Jest testing configuration
βββ π tsconfig.json # TypeScript configuration
βββ π tsconfig.build.json # Build-specific TypeScript config
βββ π nest-cli.json # NestJS CLI configuration
βββ π Dockerfile # Production Docker image
βββ π Dockerfile.dev # Development Docker image
βββ π LICENSE # License information
# Generate Prisma client from schema
npm run prisma:generate
# Run database migrations (creates tables)
npm run prisma:migrate
# Optional: Seed database with test data
npm run db:seed
# Open Prisma Studio (visual database browser)
npm run prisma:studio
Check if the application is running correctly by visiting the health endpoint at /api/v1/health
.
Command | Description | Use Case |
---|---|---|
npm run prisma:generate |
Generate Prisma client | After schema changes |
npm run prisma:migrate |
Create and apply migration | Schema updates |
npm run prisma:deploy |
Deploy migrations (production) | Production deployment |
npm run prisma:studio |
Open database GUI | Data exploration |
npm run db:reset |
Development reset | |
npm run db:seed |
Seed with test data | Development setup |
Creating New Migrations
- Update schema: Edit
src/database/schema.prisma
- Generate migration:
npm run prisma:migrate # Follow the prompts to name your migration
- Review migration: Check the generated SQL in
src/database/migrations/
For production deployments:
# Deploy migrations without prompts
npm run prisma:deploy
# Generate client for production
npm run prisma:generate
# Start with hot reload
npm run dev
The application will start on http://localhost:3000
(or your configured PORT).
Script | Description | Use Case |
---|---|---|
npm run dev |
π₯ Development with hot reload | Daily development |
npm run build |
ποΈ Build for production | Pre-deployment |
npm run start |
π Start production server | Production |
npm run lint |
π§Ή Fix ESLint issues | Code cleanup |
npm run lint:check |
π Check lint issues | CI/CD |
npm run prettier |
π Format code with Prettier | Code formatting |
npm run prettier:check |
π Check code formatting | CI/CD |
Tip: For database commands, see the Database Setup section above.
# Run all unit tests
npm run test
# Run tests with coverage
npm run test:cov
# Run tests in watch mode (for development)
npm run test:watch
The project follows standard Jest testing patterns with proper TypeScript support and mocking. All tests are unit tests that focus on testing individual components in isolation.
- Test Structure: Follow AAA pattern (Arrange, Act, Assert)
- Mocking: Mock external dependencies and services
- Data: Use factory functions for test data
- Isolation: Each test should be independent
- Coverage: Aim for meaningful test coverage, not just numbers
The project uses Jest with TypeScript support and comprehensive coverage thresholds for unit testing.
For unit tests, the project uses mocked database services to ensure tests run quickly and don't require a real database connection.
// Example: Mocking Prisma service in unit tests
const mockPrismaService = {
user: {
create: jest.fn(),
findUnique: jest.fn(),
findMany: jest.fn(),
},
};
Run the entire stack (app + database) with Docker Compose:
# Build and start all services
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down
What's included:
- β PostgreSQL database (with persistent volume)
- β NestJS application (with hot reload)
- β Network configuration (services can communicate)
Build optimized production image:
# Build production image
docker build -t nestjs-app .
# Run production container
docker run -p 3000:3000 \
-e DATABASE_URL="your_production_db_url" \
-e JWT_ACCESS_SECRET_KEY="your_secret" \
nestjs-app
Command | Description |
---|---|
docker-compose up |
Start all services |
docker-compose up -d |
Start services in background |
docker-compose down |
Stop and remove containers |
docker-compose logs backend |
View app logs |
docker-compose exec backend bash |
Access app container |
docker-compose exec postgres psql -U postgres |
Access database |
This project is licensed under the MIT License - see the LICENSE file for details.