Skip to content

Peterxyjz/nestjs-template

Repository files navigation

NestJS Template

Nest Logo

A modern, production-ready NestJS template with MongoDB, Redis, Zod validation, and Swagger documentation.

Features

  • NestJS 11: Latest version with improved performance and features
  • TypeScript: Type-safe development experience
  • MongoDB Integration: Using Mongoose for elegant MongoDB object modeling
  • Redis Caching: Built-in caching system for better performance
  • Zod Validation: Schema validation and serialization using Zod instead of class-validator
  • Swagger Documentation: Automatic API documentation
  • Docker Support: Ready-to-use Docker and Docker Compose configuration
  • CI/CD Pipeline: GitHub Actions workflow for testing, building and deployment
  • Code Quality Tools: ESLint and Prettier pre-configured
  • Git Hooks: Using Husky and lint-staged for pre-commit checks
  • Testing Framework: Jest configured and ready to use

Prerequisites

  • Node.js (v20.18.0 or later)
  • pnpm (v8 or later)
  • MongoDB (or MongoDB Atlas account)
  • Redis (optional for development if using Docker)

Getting Started

Installation

# Clone the repository
git clone https://github.com/yourusername/nestjs-template.git
cd nestjs-template

# Install dependencies
pnpm install

Environment Configuration

Create a .env file in the root directory based on the following template:

# Application
APP_NAME="API Documentation"
APP_URL=http://localhost:4000
APP_PORT=4000
API_PREFIX=api
APP_CORS_ORIGIN=*

# MongoDB
MONGO_URL=mongodb://your-mongodb-connection-string
DB_NAME=your-database-name
DB_OPTIONS=your-db-options

# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
CACHE_TTL=60
CACHE_PREFIX=cache:

Running the Application

# Development mode
pnpm run dev

# Production mode
pnpm run build
pnpm run start:prod

API Documentation

Once the application is running, you can access the Swagger UI documentation at:

http://localhost:4000/api-docs

Project Structure

nestjs-template/
├── src/
│   ├── app.module.ts      # Main application module
│   ├── common/            # Common utilities, filters, pipes, etc.
│   ├── config/            # Configuration files
│   │   ├── cache/         # Redis cache configuration
│   │   ├── database/      # MongoDB connection configuration
│   │   ├── env.config.ts  # Environment variables configuration
│   │   └── swagger.config.ts  # Swagger documentation configuration
│   ├── main.ts            # Application entry point
│   ├── routes/            # Feature modules and controllers
│   └── shared/            # Shared modules and providers
├── test/                  # Test files
├── .env                   # Environment variables
├── .eslintrc.js           # ESLint configuration
├── .prettierrc            # Prettier configuration
├── Dockerfile             # Docker configuration
├── docker-compose.yml     # Docker Compose configuration
├── nest-cli.json          # NestJS CLI configuration
├── package.json           # Project dependencies and scripts
├── tsconfig.json          # TypeScript configuration
└── README.md              # Project documentation

Docker Support

Development with Docker

# Start the application with MongoDB and Redis
docker-compose up -d

# Stop containers
docker-compose down

Production with Docker

# Build and start for production
docker-compose -f docker-compose.prod.yml up -d --build

CI/CD Pipeline

This template includes a GitHub Actions workflow for CI/CD that:

  1. Builds and tests the application
  2. Creates a Docker image and pushes it to Docker Hub
  3. Deploys to a server via SSH

To use this feature, you need to set up the following GitHub secrets:

  • DOCKER_HUB_USERNAME: Your Docker Hub username
  • DOCKER_HUB_TOKEN: Your Docker Hub access token
  • VPS_HOST: Your server hostname or IP
  • VPS_USERNAME: SSH username
  • VPS_SSH_KEY: SSH private key
  • ENV_PRODUCTION: Production environment variables

Testing

# Run unit tests
pnpm run test

# Run end-to-end tests
pnpm run test:e2e

# Run tests with coverage
pnpm run test:cov

Adding New Features

Creating a New Module

nest generate module routes/your-module

Creating a Controller

nest generate controller routes/your-module

Creating a Service

nest generate service routes/your-module

Customizing the Template

Changing MongoDB Connection

Edit the MongoDB connection settings in .env file or src/config/database/mongo.config.ts.

Changing Redis Configuration

Edit the Redis settings in .env file or src/config/cache/redis.config.ts.

Customizing Swagger Documentation

Modify the Swagger configuration in src/config/swagger.config.ts.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published