Boilerplate to build Rest API servers having Clean Architecture with NestJS and ORM.
The approach is using in a few real projects.
However the boilerplate is not completed yet:
- Domain, application and infrastructure layers are still meshed now.
- Layered tests are not implemented yet.
- Layered error handling are not implemented yet.
- Docs are not completed yet.
To quickly and simple run the boilerplate you will need:
- NodeJS
- NestJS
- MySQL
- MikroORM
create database nestjs_clean_arch character set utf8;
cp src/mikro-orm.config.ts.example src/mikro-orm.config.ts
With npm:
npm install
With yarn:
yarn install
nest start
npx mikro-orm migration:create --initial
npx mikro-orm migrations:up
Open url https://localhost:3000/docs
in your browser.
To understand the principles and structure just see the famous Uncle Bob's Clean Architecture diagram:
-
Use NestJS modules to decouple whole application functionality by features. Internal module structure follow the Clean Architecture principles. Use the main Clean Architecture diagram as orientir to build internal module architecture and define layers and relations between them.
-
Folder structure corresponds layers from the main Clean Architecture diagram.
-
Layered tests
-
Layered error handling
I like principles of Clean Architecture. And I like NestJS because of it modular architecture, quality, support and community, rich functionality and documentation.
During development a startup project with NestJS I tried to follow Clean Architecture principles and terminology. As result it has given yet another way to build application with NestJS.
It is working fine for me and I decide to build ready to use boilerplate.
The approach provides the following characteristics for you application:
- Modular and layered simple clean architecture
- Data layer (ORM/DB/Query Builder) agnostic
- All other 3rd party services agnostic
- Platform agnostic (in theory)
- Add some real-world functional to demonstrate the princeples
- Add layered tests
- Add layered error handling
- Add configuration management
- Improve docs:
- Add diagrams to demonostrate data and execution flows
- Add folders structure description
- Demonstrate ability to use different ORMs (QB and even DBs) and switch them
- Add ability to run in Docker container
- Add some minimal cli to generate common code
- Add more examples of typical applications and situations