The onion architecture, introduced by Jeffrey Palermo in 2008. The main idea of this architecture is that the Domain Layer (Entities and Validation Rules that are common to the business case) is the Core of entire Application. In this project, I implement a variant of Onion Architecture with NestJs.
- Install PostgreSQL
- Create new database in your local with whatever name you want.
- Go to .env file and change the database connection config based on the database create in step 2.
- Install all dependencies via
npm install
- Start project in development watch mode via
npm run start:dev
Domain:
- This layer contains entities, abstract of repository and constants.
Infrastructure:
- This layer contains all things which are infrastructure of this project such as: connecting database (Typeorm config, Migration), Environment Config, Authentication Service...
- We implement repository for each entity in this layer. Typeorm already has its own repository but we define wrapper repository classes so it's easy to change other library if we want.
Application:
- Application layer will do the following tasks:
1. Validate User Input.
2. Handle business logic.
3. Return data transfer objects (DTO) as a result of business logic.
API:
- Contains controllers of this apllication.
- It receives the requests made to the server and send it to services in Application Layer to execute some business logic.