Nestjs boilerplate microservice api | Mongodb CRUD - Postgres CRUD | Docker | Husky | Secrets service | HTTP service | Logs service | Authentication | Authoriza
Nestjs boilerplate microservice api | Mongodb CRUD - Postgres CRUD | Docker | Husky | Secrets service | HTTP service | Logs service | Authentication | Authoriza
This repository is more than an application template: it is a complete, production-oriented microservice foundation. It includes the application, databases, caching, observability, AWS infrastructure, Kubernetes platform, GitOps delivery, and operational workflows needed to run a service in a real environment.
The infrastructure is defined as code with Pulumi and is designed to deploy to AWS using Amazon EKS, Karpenter, Argo CD, and Kubernetes. It also includes secure secret integration, health checks, automated database migrations before application rollouts, autoscaling, persistence, and monitoring components.
This provides a strong production baseline rather than a minimal demo. Before using it in a specific production environment, review the AWS account permissions, network topology, security policies, domains, observability retention, backup strategy, sizing, and environment-specific configuration.
For infrastructure setup and deployment instructions, see the Infrastructure as Code README.
This project implements a pragmatic architecture that combines the best ideas from Clean Architecture, Domain-Driven Design (DDD), and Hexagonal Architecture. Rather than strictly following one pattern, it takes a practical approach: powerful enough for enterprise applications, yet simple enough for any developer to understand and maintain.
The architecture is built around one fundamental principle: protect your business logic. Your domain rules should never depend on frameworks, databases, or external services. If you decide to switch from PostgreSQL to MongoDB, or from Redis to Memcached, your core business logic remains untouched.
Clean Architecture organizes code into concentric circles where dependencies point inward. The innermost circle contains business rules, and outer circles contain implementation details.
How we implement it:
src/core/*/entity — pure business objectssrc/core/*/use-cases — application-specific business rulessrc/core/*/repository — contracts for external dependenciessrc/modules and src/infra — NestJS controllers and database implementations
What we simplified:DDD focuses on modeling your business domain. It introduces concepts like Entities, Value Objects, Aggregates, and Repositories.
How we implement it:
UserEntity, RoleEntity)What we simplified:
In traditional DDD, there are two types of services:
In this architecture, both responsibilities are unified into a single component: the UseCase.
UseCase acts as the application service, orchestrating business operations and dependencies.This simplification reduces boilerplate and cognitive overhead, while still maintaining a clear separation between business logic and infrastructure. The result is a leaner, more maintainable service layer without sacrificing the core benefits of DDD.
Hexagonal Architecture separates the application from external concerns through Ports (interfaces) and Adapters (implementations).
How we implement it:
…Ports (Interfaces):
ICatRepository — defines what operations are availableIHttpAdapter — defines HTTP client contractICacheAdapter — defines caching contractAdapters (Implementations):
CatRepository in modules/ — implements ICatRepository with TypeORM/MongooseHttpService in infra/ — implements IHttpAdapter with AxiosRedisService in infra/ — implements ICacheAdapter with Redis⚠️ Important Notes About This Architecture
This section explains some deliberate choices that differ from traditional implementations. Understanding these decisions will help you work with the codebase effectively.
You may notice that some interfaces in this project use the word "Adapter" (e.g., IHttpAdapter, ICacheAdapter). In traditional Hexagonal Architecture:
The academic distinction:
…Our simplified approach:
We use "Adapter" in interface names because, conceptually, both are abstractions. The fundamental principle is the same: decouple your core business logic from implementation details. Whether you call the interface a "Port" or "Adapter" doesn't change how the pattern works.
// Traditional naming
interface IUserRepository {} // Port
class PostgresUserRepository {} // Adapter
// Our naming (simplified)
interface IHttpAdapter {} // Still an abstraction (contract)
class HttpService {} // Still an implementationWhy this simplification?
I*Adapter patternThe key takeaway: if it's an interface, it's a contract. If it's a class implementing that interface, it's the implementation. The names are just labels.
You may notice that repository contracts use abstract class instead of TypeScript interface:
// What we use
export abstract class ICatRepository extends IRepository {
abstract findByBreed(breed: string): Promise
}
// Instead of
export interface ICatRepository extends IRepository {
findByBreed(breed: string): Promise
}Why? This is a NestJS/Node.js limitation.
TypeScript interfaces are erased at runtime — they don't exist in the compiled JavaScript. NestJS dependency injection relies on runtime tokens to resolve providers. If we used interfaces, we would need to pass a string token:
// ❌ With interface — requires string token
@Module({
providers: [
{
provide: 'ICatRepository', // String token (error-prone, no type safety)
useClass: CatRepository,
},
],
})
// ✅ With abstract class — class itself is the token
@Module({
providers: [
{
provide: ICatRepository, // Class reference (type-safe, refactorable)
useClass: CatRepository,
},
],
})Benefits of abstract classes:
The trade-off: Abstract classes can have implementation details (which interfaces cannot). We simply don't use that feature — our abstract classes are pure contracts.
Yes, we know. The src/middlewares/ folder contains:
Why didn't we split them?
Honestly? We couldn't find a better name.
We tried:
http-pipeline/ — too genericrequest-handlers/ — not quite rightcross-cutting/ — sounds like a buzzword bingo winnerstuff-that-runs-before-and-after-your-code/ — accurate but... noSo we stuck with middlewares/ because:
If you have a better name, PRs are welcome! Until then, just accept that middlewares/ is a "creative interpretation" of the term.
This is a fundamental difference from many Clean Architecture implementations.
The traditional approach (Clean Architecture):
Controller → Validates Input → Use Case → Business LogicIn traditional Clean Architecture, input validation happens in the Controller or a dedicated Validation layer before reaching the Use Case. The Use Case assumes it receives valid data.
Our approach:
Controller → Use Case (Validates + Business Logic)We validate inputs inside the Use Case using Zod schemas.
Why we made this choice:
Testability
When you test a Use Case, you should test the complete behavior — including validation. It's unacceptable to have a Use Case that passes tests but fails in production because validation was bypassed.
// Our tests validate the complete use case behavior
it('should throw validation error for invalid email', async () => {
const input = { email: 'invalid-email', name: 'John' }
await expect(useCase.execute(input)).rejects.toThrow(ValidationException)
})Use Case Integrity
A Use Case is a complete unit of business logic. If CreateUserUseCase requires a valid email, that validation IS part of the use case — not something external to it.
Self-Documenting Code
Looking at a Use Case, you immediately see what inputs it expects and how they're validated. No need to hunt through multiple layers.
Reduced Duplication
If multiple controllers call the same Use Case, validations are automatically applied. No risk of one controller forgetting to validate.
Comparison with other approaches:
| Approach | Validation Location | Pros | Cons |
|---|---|---|---|
| Traditional Clean | Controller/Validator layer | Thin use cases | Validation can be bypassed, harder to test |
| DDD | Domain entities (Value Objects) | Rich domain model | Complex, verbose |
| Our Approach | Inside Use Case | Complete testability, self-contained | See trade-offs below |
The trade-off:
If you need to consume a Use Case from multiple entry points with different validation rules, the Use Case validations might be too restrictive.
Solution: For those cases, move specific validations to the Application layer (Control
No open issues yet, or sync has not completed.