NestJS boilerplate. Auth, TypeORM, Mongoose, Postgres, MongoDB, Mailing, I18N, Docker.
# Architecture
---
## Table of Contents <!-- omit in toc -->
- [Hexagonal Architecture](#hexagonal-architecture)
- [Motivation](#motivation)
- [Description of the module structure](#description-of-the-module-structure)
- [Recommendations](#recommendations)
- [Repository](#repository)
- [FAQ](#faq)
- [Is there a way to generate a new resource (controller, service, DTOs, etc) with Hexagonal Architecture?](#is-there-a-way-to-generate-a-new-resource-controller-service-dtos-etc-with-hexagonal-architecture)
- [Links](#links)
---
## Hexagonal Architecture
NestJS Boilerplate is based on [Hexagonal Architecture](https://en.wikipedia.org/wiki/Hexagonal_architecture_(software)). This architecture is also known as Ports and Adapters.

## Motivation
The main reason for using Hexagonal Architecture is to separate the business logic from the infrastructure. This separation allows us to easily change the database, the way of uploading files, or any other infrastructure without changing the business logic.
## Description of the module structure
```txt
.
โโโ domain
โ โโโ [DOMAIN_ENTITY].ts
โโโ dto
โ โโโ create.dto.ts
โ โโโ find-all.dto.ts
โ โโโ update.dto.ts
โโโ infrastructure
โ โโโ persistence
โ โโโ document
โ โ โโโ document-persistence.module.ts
โ โ โโโ entities
โ โ โ โโโ [SCHEMA].ts
โ โ โโโ mappers
โ โ โ โโโ [MAPPER].ts
โ โ โโโ repositories
โ โ โโโ [ADAPTER].repository.ts
โ โโโ relational
โ โ โโโ entities
โ โ โ โโโ [ENTITY].ts
โ โ โโโ mappers
โ โ โ โโโ [MAPPER].ts
โ โ โโโ relational-persistence.module.ts
โ โ โโโ repositories
โ โ โโโ [ADAPTER].repository.ts
โ โโโ [PORT].repository.ts
โโโ controller.ts
โโโ module.ts
โโโ service.ts
```
`[DOMAIN ENTITY].ts` represents an entity used in the business logic. Domain entity has no dependencies on the database or any other infrastructure.
`[SCHEMA].ts` represents the **database structure**. It is used in the document-oriented database (MongoDB).
`[ENTITY].ts` represents the **database structure**. It is used in the relational database (PostgreSQL).
`[MAPPER].ts` is a mapper that converts **database entity** to **domain entity** and vice versa.
`[PORT].repository.ts` is a repository **port** that defines the methods for interacting with the database.
`[ADAPTER].repository.ts` is a repository that implements the `[PORT].repository.ts`. It is used to interact with the database.
`infrastructure` folder - contains all the infrastructure-related components such as `persistence`, `uploader`, `senders`, etc.
Each component has `port` and `adapters`. `Port` is interface that define the methods for interacting with the infrastructure. `Adapters` are implementations of the `port`.
## Recommendations
### Repository
Don't try to create universal methods in the repository because they are difficult to extend during the project's life. Instead of this create methods with a single responsibility.
```typescript
// โ
export class UsersRelationalRepository implements UserRepository {
async find(condition: UniversalConditionInterface): Promise<User> {
// ...
}
}
// โ
export class UsersRelationalRepository implements UserRepository {
async findByEmail(email: string): Promise<User> {
// ...
}
async findByRoles(roles: string[]): Promise<User> {
// ...
}
async findByIds(ids: string[]): Promise<User> {
// ...
}
}
```
---
## FAQ
### Is there a way to generate a new resource (controller, service, DTOs, etc) with Hexagonal Architecture?
Yes, you can use the [CLI](cli.md) to generate a new resource with Hexagonal Architecture.
---
## Links
- [Dependency Inversion Principle](https://trilon.io/blog/dependency-inversion-principle) with NestJS.
---
Previous: [Installing and Running](installing-and-running.md)
Next: [Command Line Interface](cli.md)