A production-ready NestJS gRPC microservice template with comprehensive tooling and best practices for building scalable microservices.
- gRPC Communication: Built with gRPC for high-performance inter-service communication
- NestJS Framework: Leverages NestJS for robust, scalable Node.js applications
- PostgreSQL Database: TypeORM integration with PostgreSQL for data persistence
- Redis Caching: Redis integration for caching and session management
- Elasticsearch: Full-text search capabilities with Elasticsearch
- RabbitMQ: Message queuing for asynchronous processing
- Docker Support: Complete Docker setup for development and production
- TypeScript: Full TypeScript support with strict type checking
- Testing: Jest testing framework with e2e testing support
- Code Quality: ESLint, Prettier, and Husky for code quality enforcement
- Validation: Joi-based configuration validation
- Logging: Structured logging with development/production modes
- Health Checks: Built-in health check endpoints
- Error Handling: Comprehensive error handling and filtering
- Node.js 18+
- Docker and Docker Compose
- PostgreSQL
- Redis
- RabbitMQ
- Elasticsearch
-
Clone the template
git clone <repository-url> cd template
-
Install dependencies
npm install
-
Environment Setup Create a
.envfile in the root directory:# Application NODE_ENV=development PORT=3000 # gRPC GRPC_PORT=50052 GRPC_PACKAGE=user # Database DATABASE_URL=postgresql://username:password@localhost:5432/database_name # Redis REDIS_URL=redis://localhost:6379 # RabbitMQ RABBITMQ_URL=amqp://localhost:5672 # Elasticsearch ELASTICSEARCH_URL=http://localhost:9200 ELASTICSEARCH_USERNAME=elastic ELASTICSEARCH_PASSWORD=changeme
-
Start with Docker Compose
docker-compose up -d
# Development
npm run start:dev # Start in development mode with hot reload
npm run start:debug # Start with debugger attached
npm run start:prod # Start in production mode
# Building
npm run build # Build the application
npm run build:config # Generate query configurations
# Testing
npm run test # Run unit tests
npm run test:e2e # Run end-to-end tests
npm run test:cov # Run tests with coverage
npm run test:watch # Run tests in watch mode
# Code Quality
npm run lint # Run ESLint
npm run lint:fix # Fix ESLint issues
npm run format # Format code with Prettier
npm run check-types # Type checking with TypeScript
# Git Hooks
npm run pre-commit # Run before commit
npm run pre-push # Run before pushsrc/
βββ app.module.ts # Main application module
βββ main.ts # Application bootstrap
βββ app.grpc.controller.ts # gRPC controller
βββ app.service.ts # Application service
βββ modules/
β βββ config/ # Configuration module
β β βββ config.module.ts
β β βββ config.service.ts
β β βββ config.validation.ts
β βββ user/ # User module (example)
β β βββ user.module.ts
β β βββ user.controller.ts
β β βββ user.service.ts
β β βββ dto/
β βββ rabbitmq/ # RabbitMQ module
β βββ rabbitmq.module.ts
β βββ rabbitmq.service.ts
β βββ rabbitmq.handler.ts
βββ proto/ # Protocol Buffer definitions
βββ app.proto
βββ services/
β βββ user.proto
βββ shared/
βββ common.proto
βββ error.proto
The application uses a centralized configuration system with environment variable validation:
| Variable | Description | Default |
|---|---|---|
NODE_ENV |
Application environment | development |
PORT |
HTTP port | 3000 |
GRPC_PORT |
gRPC port | 50051 |
DATABASE_URL |
PostgreSQL connection string | - |
REDIS_URL |
Redis connection string | - |
RABBITMQ_URL |
RabbitMQ connection string | - |
ELASTICSEARCH_URL |
Elasticsearch URL | - |
The ConfigServiceApp provides type-safe access to all configuration values:
@Injectable()
export class ConfigServiceApp {
get isDevelopment(): boolean;
get databaseUrl(): string;
get redisUrl(): string;
get grpcPort(): number;
// ... more getters
}The template includes a complete gRPC service setup with:
The included User service demonstrates CRUD operations:
service UserService {
rpc CreateUser (CreateUserRequest) returns (ApiResponseData) {}
rpc GetUser (GetUserRequest) returns (ApiResponseData) {}
rpc UpdateUser (UpdateUserRequest) returns (ApiResponseData) {}
rpc DeleteUser (DeleteUserRequest) returns (ApiResponseData) {}
rpc ListUsers (ListUsersRequest) returns (ApiPaginatedResponseData) {}
}- Create a new proto file in
src/proto/services/ - Define your service and messages
- Create a new module in
src/modules/ - Add the module to
app.module.ts - Update the gRPC configuration in
main.ts
The template uses TypeORM with PostgreSQL:
OrmModule.forRootAsync({
type: 'postgres',
autoLoadEntities: true,
entities: [...CORE_ENTITIES],
url: configService.databaseUrl,
// ... configuration
});@Entity()
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
firstName: string;
@Column()
lastName: string;
@Column({ unique: true })
email: string;
}The template includes a multi-stage Dockerfile:
# Development stage
FROM node:18-alpine AS dev
# ... development setup
# Production stage
FROM node:18-alpine AS production
# ... production setupservices:
api:
build:
context: .
dockerfile: Dockerfile
target: dev
ports:
- '50052:50052' # gRPC port
- '9230:9229' # Debug port-
Build the production image:
docker build --target production -t your-app . -
Set production environment variables
-
Deploy with your preferred orchestration tool
npm run testnpm run test:e2etest/
βββ jest-e2e.json # E2E test configuration
βββ *.e2e-spec.ts # E2E test files
The application includes built-in health checks for:
- Database connectivity
- Redis connectivity
- RabbitMQ connectivity
- Elasticsearch connectivity
Structured logging with different levels for development and production:
// Development: Detailed request/response logging
// Production: Error-focused logging- Input validation with Joi
- gRPC exception filtering
- Environment variable validation
- Type-safe configuration access
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
The project enforces code quality through:
- ESLint configuration
- Prettier formatting
- Husky git hooks
- Commit message linting
This template is licensed under the [UNLICENSED] license.
For support and questions:
- Create an issue in the repository
- Check the documentation
- Review the example implementations
Built with β€οΈ using NestJS and gRPC