A modern, production-ready NestJS template with MongoDB, Redis, Zod validation, and Swagger documentation.
- 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
- Node.js (v20.18.0 or later)
- pnpm (v8 or later)
- MongoDB (or MongoDB Atlas account)
- Redis (optional for development if using Docker)
# Clone the repository
git clone https://github.com/yourusername/nestjs-template.git
cd nestjs-template
# Install dependencies
pnpm installCreate 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:
# Development mode
pnpm run dev
# Production mode
pnpm run build
pnpm run start:prodOnce the application is running, you can access the Swagger UI documentation at:
http://localhost:4000/api-docs
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
# Start the application with MongoDB and Redis
docker-compose up -d
# Stop containers
docker-compose down# Build and start for production
docker-compose -f docker-compose.prod.yml up -d --buildThis template includes a GitHub Actions workflow for CI/CD that:
- Builds and tests the application
- Creates a Docker image and pushes it to Docker Hub
- 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 usernameDOCKER_HUB_TOKEN: Your Docker Hub access tokenVPS_HOST: Your server hostname or IPVPS_USERNAME: SSH usernameVPS_SSH_KEY: SSH private keyENV_PRODUCTION: Production environment variables
# Run unit tests
pnpm run test
# Run end-to-end tests
pnpm run test:e2e
# Run tests with coverage
pnpm run test:covnest generate module routes/your-modulenest generate controller routes/your-modulenest generate service routes/your-moduleEdit the MongoDB connection settings in .env file or src/config/database/mongo.config.ts.
Edit the Redis settings in .env file or src/config/cache/redis.config.ts.
Modify the Swagger configuration in src/config/swagger.config.ts.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.