Skip to content

Implementation of the Hexagonal architecture with Nest.js and MikroORM.

License

Notifications You must be signed in to change notification settings

ericjeker/nestjs-onion-architecture

Repository files navigation

Nest.js - Onion Architecture

Onion vs Hexagonal vs Clean

Onion, Hexagonal, and Clean Architecture are commonly referred to as "Ports and Adapters Architecture", "Layered Architecture with Dependency Inversion", or "Concentric Architecture". While mostly similar, they differ in subtle ways. For the sake of naming, I have used Onion Architecture most of the time as I feel like that specification is easier to grasp.

Key Common Principles

Dependency Rule

  • Dependencies always point inward (we visualize these architctures as a circle[1] with the domain at the center)
  • Inner layers know nothing about outer layers
  • Business/Domain logic is isolated from external concerns

Layer Separation

  • Clear boundaries between different responsibilities
  • Core business/domain logic at the center
  • Infrastructure and UI concerns at the outer layers

Dependency Inversion

  • High-level modules don't depend on low-level modules
  • Both depend on abstractions
  • Abstractions don't depend on details

[1] Yes, Hexagonal Architecture uses an hexagon, but the principle is identical. The hexagon actually made it confusing.

Description

The goal of this project is to demonstrate how to use the Onion (or similar) Architecture with the Nest.js framework. To read more about my explanations on this architecture, head over to my blog post.

This example was bootstrapped from the official Nest.js Advanced Course courses but will deviate over time toward a more "real-life" example. Still, to dive deeper and get more hands-on experience just go there and buy their courses, they are excellent, and they explore many other concepts and architectures like CQRS, Event Sourcing, etc., that are not covered at all in this repository. 💡

Requisites

  • pnpm - The Best Package Manager.
  • Nest.js - The Best Node.js Framework.
  • MikroORM - The Best TypeScript ORM for Node.js.

(yes, this is strongly opinionated 😉)

The project will create a SQLite database in the root folder.

Project setup

$ pnpm install

Compile and run the project

# development
$ pnpm run start

# watch mode
$ pnpm run start:dev

# production mode
$ pnpm run start:prod

Run tests

# unit tests
$ pnpm run test

# e2e tests
$ pnpm run test:e2e

# test coverage
$ pnpm run test:cov

License

MIT licensed

About

Implementation of the Hexagonal architecture with Nest.js and MikroORM.

Resources

License

Stars

Watchers

Forks

Packages

No packages published