nestJS Hexagonal Architecture example

What is Hexagonal Architecture

Concepts

Core: The core could be viewed as a “box” capable of resolve all the business logic independently of the infrastructure in which the application is mounted

Drivers (or primary) actors: are those who trigger the communication with the core. .

Driven (or secondary) actors: are those who are expecting the core to be the one who trigger the communication.

Ports: In one hand, we have the ports which are interfaces that define how the communication between an actor and the core has to be done

Adapters: In the other hand, we have the adapters that are responsible of the transformation between a request from the actor to the core, and vice versa

hexagon

Original Article

Project structure

src/internal/core - all the business logic (domain objects, interfaces and use cases)

src/internal/handlers - incoming requests. usually http layer and authentication

src/internal/repositories - adapters used by the core logic, i.e to access a Database

Commands

# From the Makefile

.PHONY: all
.PHONY: build-docker
.PHONY: clean
.PHONY: deps
.PHONY: format
.PHONY: run-dev
.PHONY: run-docker
.PHONY: test-all
.PHONY: test-cov
.PHONY: test-unit

Example request

curl -XGET http://0.0.0.0/videos/1