2.0 KiB
2.0 KiB
Backend Architecture
Layer Responsibilities
Domain
- Encapsulates business rules and invariants.
- Contains aggregates, entities, value objects, domain events, and repository interfaces.
- Has no knowledge of HTTP, databases, or frameworks.
Application
- Implements use cases through command and query handlers.
- Coordinates domain models, authorization, transactional boundaries, and DTO shaping.
- Depends on domain contracts and abstract infrastructure ports.
Infrastructure
- Implements technical details such as TypeORM repositories, database connections, JWT, logging, caching, messaging, and external services.
- Maps ORM entities to domain aggregates.
- Can be swapped without changing use-case or domain logic.
Interfaces
- Adapts delivery mechanisms such as HTTP, queues, or scheduled jobs to application use cases.
- TSOA controllers parse requests and delegate to application handlers.
- Middleware resolves request-scoped concerns like tenant context, authentication, tracing, and error formatting.
Interaction Flow
- HTTP request reaches Express.
- Middleware enriches the request with
RequestContextandTenantContext. - TSOA controller transforms the request into a command/query DTO.
- Application handler invokes domain behavior using repository contracts.
- Infrastructure repository loads/persists aggregates through TypeORM entities and mappers.
- Controller returns a response DTO.
- Error middleware converts exceptions into consistent API responses.
Multi-Tenant Notes
The recommended progression is:
- Shared database / shared schema with
tenant_idenforcement in repositories. - Schema-per-tenant for higher isolation.
- Database-per-tenant for top-tier compliance or noisy-neighbor isolation.
Keep tenancy decisions at the edge and in infrastructure:
- Controllers should not manually enforce tenant filters.
- Application handlers should accept tenant context only when the use case is tenant-specific.
- Infrastructure repositories must apply tenant scope consistently.