Skip to content

SimformSolutionsPvtLtd/nestjs-mean-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

40 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NestJS Boilerplate πŸš€

NestJS TypeScript Prisma PostgreSQL Swagger

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.

Table of Contents

Quick Start

Perfect for developers who want to get up and running fast!

Prerequisites Check βœ…

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.

1. Clone & Install πŸ“¦

git clone <repository-url>
cd nestjs-boilerplate
npm install

2. Setup Environment πŸ”§

# Copy the example environment file
cp .env.example .env

Update .env with your database credentials and JWT secrets.

3. Database Setup πŸ—„οΈ

# Generate Prisma client
npm run prisma:generate

# Run database migrations
npm run prisma:migrate

# Optional: Seed with test data
npm run db:seed

4. Start Development πŸš€

npm run dev

πŸŽ‰ You're Ready!

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

Requirements

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

Features

πŸ—οΈ Core Framework

  • NestJS - Progressive Node.js framework with TypeScript
  • Express.js - Fast, unopinionated web framework
  • TypeScript - Type-safe development experience
  • Internationalization - Multi-language support with i18n

πŸ—„οΈ Database & ORM

  • PostgreSQL - Robust relational database
  • Prisma ORM - Type-safe database client with migrations
  • Database Seeding - Pre-populated test data for development

πŸ” Authentication & Security

  • 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

πŸ“š API Documentation

  • Swagger/OpenAPI - Interactive API documentation
  • Auto-generated Docs - Documentation from decorators
  • API Versioning - Built-in API version management

πŸ§ͺ Testing & Quality

  • 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

πŸ“Š Monitoring & Logging

  • 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

πŸ› οΈ Developer Experience

  • Hot Reload - Instant development feedback
  • Environment Configuration - Flexible env management with validation
  • Database Studio - Visual database explorer
  • Docker Support - Containerized development and deployment

Project Structure

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

Database Setup

Prisma Commands πŸ”§

# 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

Verify Setup βœ…

Check if the application is running correctly by visiting the health endpoint at /api/v1/health.

Available Database Commands πŸ“Š

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 ⚠️ Reset database (deletes all data) Development reset
npm run db:seed Seed with test data Development setup

Working with Migrations πŸ”„

Creating New Migrations
  1. Update schema: Edit src/database/schema.prisma
  2. Generate migration:
    npm run prisma:migrate
    # Follow the prompts to name your migration
  3. Review migration: Check the generated SQL in src/database/migrations/

Production Database πŸš€

For production deployments:

# Deploy migrations without prompts
npm run prisma:deploy

# Generate client for production
npm run prisma:generate

Development

Start Development Server πŸš€

# Start with hot reload
npm run dev

The application will start on http://localhost:3000 (or your configured PORT).

Available Scripts πŸ“œ

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.

Testing

Quick Test Commands πŸš€

# 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

Writing Tests ✍️

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.

Testing Best Practices βœ…

  1. Test Structure: Follow AAA pattern (Arrange, Act, Assert)
  2. Mocking: Mock external dependencies and services
  3. Data: Use factory functions for test data
  4. Isolation: Each test should be independent
  5. Coverage: Aim for meaningful test coverage, not just numbers

Test Configuration βš™οΈ

The project uses Jest with TypeScript support and comprehensive coverage thresholds for unit testing.

Test Database πŸ—„οΈ

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(),
  },
};

Docker Setup

Development with Docker πŸ› οΈ

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)

Production Docker Image πŸš€

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

Docker Commands 🐳

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

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •