How deadislove/nestjs-hexagonal-architecture-template works
Purpose and runtime
A NestJS/TypeScript backend template implementing a hexagonal/onion-style architecture. It exposes HTTP APIs for users and products, while isolating business models and repository ports from TypeORM persistence. Node.js runs the service; TypeORM supports SQLite, PostgreSQL, and MySQL.
Bootstrap and composition
src/main.ts is the process entry point and bootstraps Nest. src/app.module.ts composes application-wide configuration, database infrastructure, and feature modules. src/module-loader.ts supports module/entity discovery rather than requiring a centralized feature registry.
HTTP boundary and cross-cutting policy
Feature controllers are the inbound HTTP adapters: src/users/user.controller.ts and src/products/product.controller.ts. Request DTOs define input contracts, while response DTOs shape outbound module data, e.g. src/users/application/dto/requests/create-user-request.dto.ts and src/products/application/dto/responses/product-response.dto.ts.
Global API behavior is centralized through src/shared/interceptors/response.interceptor.ts for response normalization and src/shared/filters/http-exception.filter.ts for exception translation. Shared result abstractions in src/shared/core/result.ts and src/shared/core/resultAsync.ts provide application-layer success/failure flow control.
User and product application cores
Each feature is independently layered and wired through its Nest module: src/users/user.module.ts and src/products/product.module.ts. Application services coordinate use cases, validation/DTO mapping, and repository-port calls: src/users/application/user.service.ts and src/products/application/product.service.ts.
Their domain entities and repository interfaces form the framework-independent core: src/users/domain/entities/user.entity.ts, src/users/domain/interfaces/user.repository.ts, src/products/domain/entities/product.entity.ts, and src/products/domain/interfaces/product.repository.ts.
Persistence adapters
TypeORM-specific entities and repositories implement the domain persistence boundary. The user adapter is under src/users/infra/typeorm/entities/user.typeorm.entity.ts and src/users/infra/typeorm/repositories/user-typeorm.repository.ts; products follow the same pattern at src/products/infra/typeorm/entities/product.typeorm.entity.ts and src/products/infra/typeorm/repositories/product-typeorm.repository.ts.
Primary control flow is: HTTP controller → application service → domain repository interface → TypeORM repository/entity → selected database; results return through shared response/exception handling.
Database selection and infrastructure
The database module owns TypeORM configuration and database-driver selection: src/infra/typeorm/database/database.module.ts, src/infra/typeorm/database/database.config.factory.ts, and src/infra/typeorm/database/factory/index.ts. Concrete factories target MySQL, PostgreSQL, and SQLite at src/infra/typeorm/database/factory/database.mysql.ts, src/infra/typeorm/database/factory/database.postgresql.ts, and src/infra/typeorm/database/factory/database.sqllite.ts.
Runtime database choice is environment-driven configuration, with sample.env as the contract example. SQLite can use the included database.sqlite; PostgreSQL and SQLite environments are supplied through docker-compose.postgresql.yml and docker-compose.sqlite.yml.
Operational boundaries
Configuration is encapsulated in src/config/config.module.ts and src/config/app.config.ts. Logging is provided as a shared injectable service through src/shared/logger/logger.module.ts and src/shared/logger/logger.service.ts, writing rotated application logs per the README. Container execution is defined by Dockerfile; no external SaaS dependency is required by the documented architecture.
Main entry points: app.module.ts Application composition · database.module.ts Database infrastructure · user.controller.ts Users HTTP API · user.service.ts User application service
Components 20
Runtime & Shared
- Nest bootstrap
src/main.ts - Application composition
src/app.module.ts - Module discovery · Discovers feature modules and entities.
src/module-loader.ts - Configuration
src/config/config.module.ts - API response & errors
src/shared/interceptors/response.interceptor.ts - Result flow
src/shared/core/result.ts - Logging
src/shared/logger/logger.service.ts
Users Feature
- Users module
src/users/user.module.ts - Users HTTP API
src/users/user.controller.ts - User application service
src/users/application/user.service.ts - User domain & port
src/users/domain/interfaces/user.repository.ts - User TypeORM adapter
src/users/infra/typeorm/repositories/user-typeorm.repository.ts
Products Feature
- Products module
src/products/product.module.ts - Products HTTP API
src/products/product.controller.ts - Product application service
src/products/application/product.service.ts - Product domain & port
src/products/domain/interfaces/product.repository.ts - Product TypeORM adapter
src/products/infra/typeorm/repositories/product-typeorm.repository.ts
Persistence
- Database infrastructure
src/infra/typeorm/database/database.module.ts - Driver selection · Selects SQLite, PostgreSQL, or MySQL from environment configuration.
src/infra/typeorm/database/factory/index.ts - Selected database
Connections 26
- Nest bootstrap → Application composition: bootstraps
- Application composition → Module discovery: discovers
- Application composition → Configuration: loads
- Application composition → Database infrastructure: imports
- Application composition → Users module: imports
- Application composition → Products module: imports
- Users module → Users HTTP API: exposes
- Users HTTP API → User application service: requests
- User application service → User domain & port: uses port
- User TypeORM adapter → User domain & port: implements
- User domain & port → User TypeORM adapter: persistence
- Products module → Products HTTP API: exposes
- Products HTTP API → Product application service: requests
- Product application service → Product domain & port: uses port
- Product TypeORM adapter → Product domain & port: implements
- Product domain & port → Product TypeORM adapter: persistence
- User TypeORM adapter → Database infrastructure: uses TypeORM
- Product TypeORM adapter → Database infrastructure: uses TypeORM
- Database infrastructure → Driver selection: configures
- Configuration → Driver selection: environment
- Driver selection → Selected database: selects
- Users HTTP API → API response & errors: global handling
- Products HTTP API → API response & errors: global handling
- User application service → Result flow: returns
- Product application service → Result flow: returns
- Application composition → Logging: provides