Clean Arquitecture Node.js

February 4, 2023 · View on GitHub

Backend using clean arquitecture

This project is a back end portfolio using Clean Arquitecture and repository pattern, this will allow us to work with diferent databases such as non sql and sql.

Branch distribution

The repository has a branch for each database implementation ie. my-sql-implementation.

Software layer overview

This picture show the layer distribution of a clean arquitecture.

clean-arquitecure-diagram

Image by Robert C. Martin


In this project the layer names will be changed as Clean Architecture with .NET Core: Getting Started indicates.

clean-arquitecture-jason

Image By Clean Architecture with .NET Core: Getting Started


Structure Folder

Based on The Clean Architecture — Beginner’s Guide and Clean Architecture with .NET Core: Getting Started i decided to use this distribution.


1. Domain (Entities)

Enterprise Business Rules, this layer holds the entities and enumerables definition.


2. Application (Use Cases)

Application Business Rules, this layer has Commands and Queries like CQRS pattern indicates.


3. Gateways.

Interface of all CRUD aperations in the application layer, implemented by DB. To do this operations the Repository pattern are used.


4. Infrastructure (DB)

Database Implementations and identity, here are the Repository Database Implementations.


5. Presentation

Web Api User Interface, calls the Application layer operations.


The Dependency Rule

All outer layers know about inner layers but not viceversa.

To achive this rule is implemented Dependency Inversion Principle with Brand DI Container.

Folder structure.

└── stories-api/
    ├── brandDI
    ├── src/
    │   ├── 1-domain/
    │   │   ├── entities.ts
    │   │   └── statusCodes.ts
    │   ├── 2-application/
    │   │   ├── common/
    │   │   │   ├── errorResponseViewModel.ts
    │   │   │   └── responseViewModel.ts
    │   │   └── users/
    │   │       ├── dtos/
    │   │       │   ├── createUserDto.ts
    │   │       │   └── updateUserDto.ts
    │   │       ├── viewModels/
    │   │       │   ├── userCreatedViewModel.ts
    │   │       │   └── userViewModel.ts  
    │   │       ├── userCommands.ts
    │   │       └── userQueries.ts
    │   ├── 3-gateways/
    │   │   └── repositories/
    │   │       └── database/
    │   │           ├── base/
    │   │           │   └── baseRepository.ts
    │   │           └── userRepository.ts
    │   ├── 4-infrastructure/
    │   │   ├── db/
    │   │   │   ├── mongoDB/
    │   │   │   │   ├── repositories/
│   │   │   │   │   |   ├── base/
│   │   │   │   │   |   │   └── baseRepository.ts
│   │   │   │   │   |   └── userRepository
    │   │   │   │   ├── models/
    │   │   │   │   │   └── user.ts
    │   │   │   │   └── connection.ts
    │   │   │   ├── ...
    │   │   │   ├── another database implementations
    │   │   │   └── ...
    │   │   └── identity/
    │   │       ├── middleware/
    │   │       │   └── check-auth.ts
    │   │       └── JWTManager.ts
    │   └── 5.presentation/
    │       ├── routes/
    │       │   ├── index.ts (all routes)
    │       │   └── user.ts ( user routes implementations)
    │       └── index.tx ( express app)
    └── tests/
        └── user.spec.ts