# z3r0j/nodejs-clean-architecture architecture

> Architecture diagram of the GitHub repository z3r0j/nodejs-clean-architecture, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/z3r0j/nodejs-clean-architecture
- Repository: https://github.com/z3r0j/nodejs-clean-architecture

## Overview

This repository is a Clean Architecture starter for TypeScript REST APIs, illustrated with a Test resource. The documented request path is HTTP routing through controllers and application services to repositories and a TypeORM database, with DTO validation, Result-based service outcomes, and centralized error responses. Bootstrap initializes the data source and dependency container before configuring Express. The README describes additional capabilities such as API-key protection and transactional Unit of Work; their runtime wiring is not shown in the sampled source, so those links are documented rather than source-verified.

Main entry points: [`src/application/services/TestServices.ts`](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/application/services/TestServices.ts) (Test services), [`src/interfaces/controllers/test.controller.ts`](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/controllers/test.controller.ts) (Test controller), [`src/container/index.ts`](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/container/index.ts) (Dependency container), [`src/config/express.config.ts`](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/config/express.config.ts) (Express configuration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_http["HTTP interface"]
  node_routes["Test routes<br/>[test.route.ts]"]
  node_controller["Test controller<br/>[test.controller.ts]"]
  node_apikey["API-key middleware"]
  node_respond["Result responder<br/>[respond.ts]"]
  node_errorhandler["Error handler"]
end

subgraph group_application["Application logic"]
  node_requestdto["Create DTO<br/>[createTestDTO.ts]"]
  node_responsedto["Response DTO<br/>[testResponseDTO.ts]"]
  node_errors["Domain errors<br/>[CustomError.ts]"]
  node_testservice["Test services<br/>[TestServices.ts]"]
  node_genericservice["Generic services<br/>[GenericServices.ts]"]
  node_testentity["Test entity<br/>[Test.ts]"]
end

subgraph group_persistence["Persistence"]
  node_uow["Unit of Work<br/>[UnitOfWork.ts]"]
  node_testrepo["Test repository<br/>[TestRepository.ts]"]
  node_genericrepo["Generic repository"]
  node_database[("TypeORM database<br/>[typeorm.config.ts]")]
end

subgraph group_runtime["Runtime and wiring"]
  node_express["Express configuration<br/>[express.config.ts]"]
  node_container["Dependency container<br/>[index.ts]"]
  node_bootstrap["Application bootstrap<br/>[index.ts]"]
end

node_client(("API client"))
node_mysql[("MySQL")]

node_client -->|"sends request"| node_routes
node_express -.->|"mounts routes"| node_routes
node_routes -.->|"dispatches"| node_controller
node_controller -->|"validates input"| node_requestdto
node_controller -.->|"calls"| node_testservice
node_controller -->|"formats result"| node_respond
node_respond -->|"returns DTO"| node_responsedto
node_express -->|"protects routes"| node_apikey
node_express -->|"registers handler"| node_errorhandler
node_errorhandler -->|"maps errors"| node_errors
node_testservice -->|"extends"| node_genericservice
node_genericservice -->|"delegates CRUD"| node_testrepo
node_testservice -->|"runs transaction"| node_uow
node_uow -->|"provides repositories"| node_testrepo
node_testservice -->|"creates entity"| node_testentity
node_testrepo -->|"extends"| node_genericrepo
node_genericrepo -->|"queries via ORM"| node_database
node_database -->|"connects to"| node_mysql
node_bootstrap -->|"initializes"| node_database
node_bootstrap -->|"builds bindings"| node_container
node_container -->|"registers service"| node_testservice
node_container -->|"registers repository"| node_testrepo
node_container -->|"registers unit of work"| node_uow
node_bootstrap -->|"starts server"| node_express

click node_routes "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/routes/test.route.ts"
click node_controller "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/controllers/test.controller.ts"
click node_requestdto "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/application/dtos/createTestDTO.ts"
click node_responsedto "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/application/dtos/testResponseDTO.ts"
click node_apikey "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/middlewares/apikey.middleware.ts"
click node_express "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/config/express.config.ts"
click node_respond "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/controllers/helpers/respond.ts"
click node_errorhandler "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/middlewares/error.handler.middleware.ts"
click node_errors "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/error-handling/CustomError.ts"
click node_testservice "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/application/services/TestServices.ts"
click node_genericservice "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/application/services/GenericServices.ts"
click node_testentity "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/domain/entities/Test.ts"
click node_uow "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/infrastructure/UnitOfWork.ts"
click node_testrepo "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/infrastructure/repositories/TestRepository.ts"
click node_genericrepo "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/infrastructure/repositories/GenericRepository.ts"
click node_database "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/infrastructure/typeorm.config.ts"
click node_container "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/container/index.ts"
click node_bootstrap "https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/index.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_routes,node_controller,node_apikey,node_respond,node_errorhandler,node_client toneBlue
class node_requestdto,node_responsedto,node_errors,node_testservice,node_genericservice,node_testentity,node_mysql toneAmber
class node_uow,node_testrepo,node_genericrepo,node_database toneMint
class node_express,node_container,node_bootstrap toneRose
```

## Components

### HTTP interface

- [Test routes](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/routes/test.route.ts): `src/interfaces/routes/test.route.ts`
- [Test controller](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/controllers/test.controller.ts): `src/interfaces/controllers/test.controller.ts`
- [API-key middleware](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/middlewares/apikey.middleware.ts): `src/interfaces/middlewares/apikey.middleware.ts`
- [Result responder](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/controllers/helpers/respond.ts): `src/interfaces/controllers/helpers/respond.ts`
- [Error handler](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/interfaces/middlewares/error.handler.middleware.ts): `src/interfaces/middlewares/error.handler.middleware.ts`

### Application logic

- [Create DTO](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/application/dtos/createTestDTO.ts): `src/application/dtos/createTestDTO.ts`
- [Response DTO](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/application/dtos/testResponseDTO.ts): `src/application/dtos/testResponseDTO.ts`
- [Domain errors](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/error-handling/CustomError.ts): `src/error-handling/CustomError.ts`
- [Test services](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/application/services/TestServices.ts): `src/application/services/TestServices.ts`
- [Generic services](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/application/services/GenericServices.ts): `src/application/services/GenericServices.ts`
- [Test entity](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/domain/entities/Test.ts): `src/domain/entities/Test.ts`

### Persistence

- [Unit of Work](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/infrastructure/UnitOfWork.ts): `src/infrastructure/UnitOfWork.ts`
- [Test repository](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/infrastructure/repositories/TestRepository.ts): `src/infrastructure/repositories/TestRepository.ts`
- [Generic repository](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/infrastructure/repositories/GenericRepository.ts): `src/infrastructure/repositories/GenericRepository.ts`
- [TypeORM database](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/infrastructure/typeorm.config.ts): `src/infrastructure/typeorm.config.ts`

### Runtime and wiring

- [Express configuration](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/config/express.config.ts): `src/config/express.config.ts`
- [Dependency container](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/container/index.ts): `src/container/index.ts`
- [Application bootstrap](https://github.com/z3r0j/nodejs-clean-architecture/blob/main/src/index.ts): `src/index.ts`

### Other components

- **API client**
- **MySQL**

## Connections

- API client → Test routes: sends request (evidence: `README.md`)
- Express configuration → Test routes: mounts routes
- Test routes → Test controller: dispatches
- Test controller → Create DTO: validates input (evidence: `README.md`)
- Test controller → Test services: calls
- Test controller → Result responder: formats result (evidence: `README.md`)
- Result responder → Response DTO: returns DTO (evidence: `README.md`)
- Express configuration → API-key middleware: protects routes (evidence: `README.md`)
- Express configuration → Error handler: registers handler (evidence: `README.md`)
- Error handler → Domain errors: maps errors (evidence: `src/interfaces/middlewares/error.handler.middleware.ts`)
- Test services → Generic services: extends (evidence: `src/application/services/TestServices.ts`)
- Generic services → Test repository: delegates CRUD (evidence: `README.md`)
- Test services → Unit of Work: runs transaction (evidence: `src/application/services/TestServices.ts`)
- Unit of Work → Test repository: provides repositories (evidence: `README.md`)
- Test services → Test entity: creates entity (evidence: `src/application/services/TestServices.ts`)
- Test repository → Generic repository: extends (evidence: `src/infrastructure/repositories/TestRepository.ts`)
- Generic repository → TypeORM database: queries via ORM (evidence: `src/infrastructure/repositories/GenericRepository.ts`)
- TypeORM database → MySQL: connects to (evidence: `README.md`)
- Application bootstrap → TypeORM database: initializes (evidence: `src/index.ts`)
- Application bootstrap → Dependency container: builds bindings (evidence: `src/index.ts`)
- Dependency container → Test services: registers service (evidence: `src/container/index.ts`)
- Dependency container → Test repository: registers repository (evidence: `src/container/index.ts`)
- Dependency container → Unit of Work: registers unit of work (evidence: `src/container/index.ts`)
- Application bootstrap → Express configuration: starts server (evidence: `src/index.ts`)
