# psawn/nestjs-sample architecture

> Architecture diagram of the GitHub repository psawn/nestjs-sample, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/psawn/nestjs-sample
- Repository: https://github.com/psawn/nestjs-sample

## Overview

1. This is a NestJS TypeScript backend organized as a modular monolith, not a single flat API. The top-level `AppModule` wires together feature modules for auth, user, gateway, and shared infrastructure.

2. The main request boundary is the controller layer. Each feature module has its own controller/service pair:
   - `auth` handles login/register concerns.
   - `user` handles user/profile creation and related operations.
   - `gateway` is a separate facade-style module that likely exposes higher-level entrypoints over the internal services.

3. The auth subsystem is centered on credential management and access control. It includes DTOs for login/register, an `AuthCredential` entity, a repository interface plus concrete repository, and Nest guards/decorators for JWT and role-based protection. `Public` and `Roles` decorators define route-level access metadata.

4. The user subsystem models user profile data and user lifecycle events. It has a `UserProfile` entity, repository abstraction, service, controller, and `create-user` DTO. The presence of `user-events.ts` plus shared event contracts suggests this module emits domain events when users are created, updated, or deleted.

5. Persistence is intentionally abstracted behind repository interfaces. Both auth and user modules depend on repository contracts in their feature folders, while concrete repository implementations live alongside them. That keeps the business layer insulated from the underlying database technology.

6. There is a dedicated infrastructure layer under `src/infrastructure`. It is split into:
   - `database` for bootstrapping persistence,
   - `kafka` for broker connectivity,
   - `outbox` for event durability and publishing.
   This structure indicates the app treats messaging as infrastructure, not as part of core domain logic.

7. The outbox subsystem is a key architectural boundary. It contains an `Outbox` entity, repository interface/implementation, and an `OutboxPublisherService`. That strongly suggests the app persists events to a database table first, then publishes them asynchronously to Kafka to avoid losing events when handling writes.

8. Kafka is the external integration backbone for event propagation. The repo includes a Kafka module/service and a `kafka/docker-compose.yml`, which implies local broker support and a message-driven path for cross-module or downstream communication.

9. Event contracts are split between shared interfaces and concrete event classes. `src/common/interfaces/events` defines the shape of user-related events, while `src/common/events` contains base and specific event classes. This separation is useful for keeping producers and consumers aligned on payload structure.

10. `src/common` contains cross-cutting application concerns:
   - DI tokens for wiring abstractions to implementations,
   - event constants,
   - guards,
   - custom decorators.
   This is the main shared boundary used by all feature modules.

11. The likely end-to-end flow is: controller receives an HTTP request, service applies business logic, repository persists state, and if a domain event is produced it is stored in the outbox and later published to Kafka. Authorization is enforced before the controller runs via JWT/role guards.

12. The repo is structured for testability rather than direct framework coupling. There are unit test scaffolds for the app controller/service and an e2e test setup under `test/`, which matches the NestJS module-driven design.

13. Operationally, the repo expects environment-based configuration (`.env.example`) and uses standard Nest tooling plus lint/format config. The architecture diagram should show the app runtime, the database, and Kafka as the main external systems, with the outbox as the bridge between transactional writes and event publication.

Main entry points: [`src/app.module.ts`](https://github.com/psawn/nestjs-sample/blob/main/src/app.module.ts) (App module), [`src/infrastructure/outbox/outbox.module.ts`](https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/outbox/outbox.module.ts) (Outbox), [`src/user/user.service.ts`](https://github.com/psawn/nestjs-sample/blob/main/src/user/user.service.ts) (User service), [`src/auth/auth.controller.ts`](https://github.com/psawn/nestjs-sample/blob/main/src/auth/auth.controller.ts) (Auth API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_app["App Shell"]
  node_app_main(("Bootstrap<br/>[main.ts]"))
  node_app_module["App module<br/>root module<br/>[app.module.ts]"]
end

subgraph group_group_auth["Auth"]
  node_auth_controller["Auth API<br/>controller<br/>[auth.controller.ts]"]
  node_auth_service["Auth service<br/>[auth.service.ts]"]
  node_auth_repo["Auth repo"]
  node_auth_entity["Credentials<br/>entity"]
  node_auth_dtos["Auth DTOs<br/>contracts"]
end

subgraph group_group_user["User"]
  node_user_controller["User API<br/>controller<br/>[user.controller.ts]"]
  node_user_service["User service<br/>[user.service.ts]"]
  node_user_repo["User repo"]
  node_user_entity["Profiles<br/>entity"]
  node_user_dto["Create user<br/>contract<br/>[create-user.dto.ts]"]
end

subgraph group_group_gateway["Gateway"]
  node_gateway_controller["Gateway API<br/>facade controller"]
  node_gateway_service["Gateway service<br/>facade service<br/>[gateway.service.ts]"]
end

subgraph group_group_shared["Shared"]
  node_shared_security{{"Guards<br/>security"}}
  node_shared_decorators{{"Decorators<br/>metadata"}}
  node_shared_events["Event contracts"]
end

subgraph group_group_infra["Infrastructure"]
  node_infra_db["Database<br/>persistence module<br/>[database.module.ts]"]
  node_infra_kafka["Kafka<br/>broker module<br/>[kafka.module.ts]"]
  node_infra_outbox["Outbox<br/>durability module<br/>[outbox.module.ts]"]
  node_outbox_entity["Outbox row<br/>entity<br/>[outbox.entity.ts]"]
  node_outbox_repo["Outbox repo"]
  node_outbox_publisher["Publisher"]
end

subgraph group_group_external["External Systems"]
  node_kafka_runtime[("Kafka broker<br/>external broker<br/>[docker-compose.yml]")]
end

node_app_main -->|"boots"| node_app_module
node_app_module -->|"wires"| node_auth_controller
node_app_module -->|"wires"| node_user_controller
node_app_module -->|"wires"| node_gateway_controller
node_app_module -->|"imports"| node_infra_db
node_app_module -->|"imports"| node_infra_kafka
node_app_module -->|"imports"| node_infra_outbox
node_auth_controller -->|"delegates"| node_auth_service
node_auth_controller -.->|"protects"| node_shared_security
node_auth_controller -.->|"annotates"| node_shared_decorators
node_auth_service -->|"persists"| node_auth_repo
node_auth_repo -->|"stores"| node_auth_entity
node_auth_service -.->|"emits"| node_shared_events
node_user_controller -->|"delegates"| node_user_service
node_user_service -->|"persists"| node_user_repo
node_user_repo -->|"stores"| node_user_entity
node_user_service -.->|"accepts"| node_user_dto
node_user_service -.->|"emits"| node_shared_events
node_gateway_controller -->|"delegates"| node_gateway_service
node_gateway_service -->|"uses"| node_auth_service
node_gateway_service -->|"uses"| node_user_service
node_shared_events -.->|"feeds"| node_infra_outbox
node_infra_outbox -->|"stores"| node_outbox_entity
node_infra_outbox -->|"implements"| node_outbox_repo
node_infra_outbox -->|"runs"| node_outbox_publisher
node_outbox_publisher -->|"publishes"| node_infra_kafka
node_infra_kafka -->|"connects"| node_kafka_runtime
node_infra_db -.->|"backing store"| node_infra_outbox

click node_app_main "https://github.com/psawn/nestjs-sample/blob/main/src/main.ts"
click node_app_module "https://github.com/psawn/nestjs-sample/blob/main/src/app.module.ts"
click node_auth_controller "https://github.com/psawn/nestjs-sample/blob/main/src/auth/auth.controller.ts"
click node_auth_service "https://github.com/psawn/nestjs-sample/blob/main/src/auth/auth.service.ts"
click node_auth_repo "https://github.com/psawn/nestjs-sample/blob/main/src/auth/repositories/auth-credential.repository.ts"
click node_auth_entity "https://github.com/psawn/nestjs-sample/blob/main/src/auth/entities/auth-credential.entity.ts"
click node_auth_dtos "https://github.com/psawn/nestjs-sample/tree/main/src/auth/dto"
click node_user_controller "https://github.com/psawn/nestjs-sample/blob/main/src/user/user.controller.ts"
click node_user_service "https://github.com/psawn/nestjs-sample/blob/main/src/user/user.service.ts"
click node_user_repo "https://github.com/psawn/nestjs-sample/blob/main/src/user/repositories/user-profile.repository.ts"
click node_user_entity "https://github.com/psawn/nestjs-sample/blob/main/src/user/entities/user-profile.entity.ts"
click node_user_dto "https://github.com/psawn/nestjs-sample/blob/main/src/user/dto/create-user.dto.ts"
click node_gateway_controller "https://github.com/psawn/nestjs-sample/blob/main/src/gateway/gateway.controller.ts"
click node_gateway_service "https://github.com/psawn/nestjs-sample/blob/main/src/gateway/gateway.service.ts"
click node_shared_security "https://github.com/psawn/nestjs-sample/tree/main/src/common/guards"
click node_shared_decorators "https://github.com/psawn/nestjs-sample/tree/main/src/common/decorators"
click node_shared_events "https://github.com/psawn/nestjs-sample/tree/main/src/common/interfaces/events"
click node_infra_db "https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/database/database.module.ts"
click node_infra_kafka "https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/kafka/kafka.module.ts"
click node_infra_outbox "https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/outbox/outbox.module.ts"
click node_outbox_entity "https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/outbox/entities/outbox.entity.ts"
click node_outbox_repo "https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/outbox/repositories/outbox.repository.ts"
click node_outbox_publisher "https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/outbox/services/outbox-publisher.service.ts"
click node_kafka_runtime "https://github.com/psawn/nestjs-sample/blob/main/kafka/docker-compose.yml"

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_app_main,node_app_module,node_kafka_runtime toneBlue
class node_auth_controller,node_auth_service,node_auth_repo,node_auth_entity,node_auth_dtos toneAmber
class node_user_controller,node_user_service,node_user_repo,node_user_entity,node_user_dto toneMint
class node_gateway_controller,node_gateway_service toneRose
class node_shared_security,node_shared_decorators,node_shared_events toneIndigo
class node_infra_db,node_infra_kafka,node_infra_outbox,node_outbox_entity,node_outbox_repo,node_outbox_publisher toneTeal
```

## Components

### App Shell

NestJS bootstrap and top-level module wiring

- [Bootstrap](https://github.com/psawn/nestjs-sample/blob/main/src/main.ts): Nest bootstrap · Application entrypoint that starts the Nest runtime · `src/main.ts`
- [App module](https://github.com/psawn/nestjs-sample/blob/main/src/app.module.ts): root module · Composes the application feature and infrastructure modules · `src/app.module.ts`

### Auth

Credential and access-control subsystem

- [Auth API](https://github.com/psawn/nestjs-sample/blob/main/src/auth/auth.controller.ts): controller · HTTP boundary for login and registration · `src/auth/auth.controller.ts`
- [Auth service](https://github.com/psawn/nestjs-sample/blob/main/src/auth/auth.service.ts): service · Coordinates authentication flows and credential checks · `src/auth/auth.service.ts`
- [Auth repo](https://github.com/psawn/nestjs-sample/blob/main/src/auth/repositories/auth-credential.repository.ts): repository · Concrete credential persistence implementation · `src/auth/repositories/auth-credential.repository.ts`
- [Credentials](https://github.com/psawn/nestjs-sample/blob/main/src/auth/entities/auth-credential.entity.ts): entity · Stored authentication credential model · `src/auth/entities/auth-credential.entity.ts`
- [Auth DTOs](https://github.com/psawn/nestjs-sample/tree/main/src/auth/dto): contracts · Login and registration request shapes · `src/auth/dto`

### User

User profile and lifecycle subsystem

- [User API](https://github.com/psawn/nestjs-sample/blob/main/src/user/user.controller.ts): controller · HTTP boundary for user profile operations · `src/user/user.controller.ts`
- [User service](https://github.com/psawn/nestjs-sample/blob/main/src/user/user.service.ts): service · Coordinates user creation and lifecycle logic · `src/user/user.service.ts`
- [User repo](https://github.com/psawn/nestjs-sample/blob/main/src/user/repositories/user-profile.repository.ts): repository · Concrete user profile persistence implementation · `src/user/repositories/user-profile.repository.ts`
- [Profiles](https://github.com/psawn/nestjs-sample/blob/main/src/user/entities/user-profile.entity.ts): entity · User profile domain model · `src/user/entities/user-profile.entity.ts`
- [Create user](https://github.com/psawn/nestjs-sample/blob/main/src/user/dto/create-user.dto.ts): contract · Request shape for creating users · `src/user/dto/create-user.dto.ts`

### Gateway

Facade entrypoint over internal services

- [Gateway API](https://github.com/psawn/nestjs-sample/blob/main/src/gateway/gateway.controller.ts): facade controller · Higher-level entrypoint that fronts internal services · `src/gateway/gateway.controller.ts`
- [Gateway service](https://github.com/psawn/nestjs-sample/blob/main/src/gateway/gateway.service.ts): facade service · Orchestrates gateway-level application flow · `src/gateway/gateway.service.ts`

### Shared

Cross-cutting decorators, guards, tokens, and event contracts

- [Guards](https://github.com/psawn/nestjs-sample/tree/main/src/common/guards): security · JWT and role-based request protection · `src/common/guards`
- [Decorators](https://github.com/psawn/nestjs-sample/tree/main/src/common/decorators): metadata · Public and roles route metadata helpers · `src/common/decorators`
- [Event contracts](https://github.com/psawn/nestjs-sample/tree/main/src/common/interfaces/events): contracts · Shared event interfaces and base event types · `src/common/interfaces/events`

### Infrastructure

Database, Kafka, and outbox persistence/publishing

- [Database](https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/database/database.module.ts): persistence module · Bootstraps persistence infrastructure · `src/infrastructure/database/database.module.ts`
- [Kafka](https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/kafka/kafka.module.ts): broker module · Messaging integration and broker access · `src/infrastructure/kafka/kafka.module.ts`
- [Outbox](https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/outbox/outbox.module.ts): durability module · Stores domain events before asynchronous publication · `src/infrastructure/outbox/outbox.module.ts`
- [Outbox row](https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/outbox/entities/outbox.entity.ts): entity · Persisted event record awaiting publication · `src/infrastructure/outbox/entities/outbox.entity.ts`
- [Outbox repo](https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/outbox/repositories/outbox.repository.ts): repository · Concrete outbox persistence implementation · `src/infrastructure/outbox/repositories/outbox.repository.ts`
- [Publisher](https://github.com/psawn/nestjs-sample/blob/main/src/infrastructure/outbox/services/outbox-publisher.service.ts): service · Publishes stored events to Kafka · `src/infrastructure/outbox/services/outbox-publisher.service.ts`

### External Systems

Runtime dependencies outside the application

- [Kafka broker](https://github.com/psawn/nestjs-sample/blob/main/kafka/docker-compose.yml): external broker · External message broker used for event propagation · `kafka/docker-compose.yml`

## Connections

- Bootstrap → App module: boots · Application bootstrap loads the root module
- App module → Auth API: wires · Registers the auth feature module and controller
- App module → User API: wires · Registers the user feature module and controller
- App module → Gateway API: wires · Registers the gateway facade module
- App module → Database: imports · Connects application wiring to persistence bootstrapping
- App module → Kafka: imports · Connects application wiring to broker integration
- App module → Outbox: imports · Connects application wiring to outbox infrastructure
- Auth API → Auth service: delegates · Controller forwards requests to auth business logic
- Auth API → Guards: protects · Route access is enforced by guards
- Auth API → Decorators: annotates · Route metadata is provided by decorators
- Auth service → Auth repo: persists · Auth logic uses repository abstraction and implementation
- Auth repo → Credentials: stores · Credential repository persists credential entities
- Auth service → Event contracts: emits · Auth flows can publish shared event contracts
- User API → User service: delegates · Controller forwards requests to user business logic
- User service → User repo: persists · User logic uses repository abstraction and implementation
- User repo → Profiles: stores · Profile repository persists user profile entities
- User service → Create user: accepts · Service is driven by create-user input shape
- User service → Event contracts: emits · User lifecycle changes are described by shared contracts
- Gateway API → Gateway service: delegates · Gateway controller forwards to facade logic
- Gateway service → Auth service: uses · Gateway can orchestrate auth operations
- Gateway service → User service: uses · Gateway can orchestrate user operations
- Event contracts → Outbox: feeds · Event contracts are durable through the outbox boundary
- Outbox → Outbox row: stores · Outbox module persists pending event rows
- Outbox → Outbox repo: implements · Outbox module provides repository implementation
- Outbox → Publisher: runs · Outbox module schedules publication service
- Publisher → Kafka: publishes · Publisher sends durable events to Kafka
- Kafka → Kafka broker: connects · Kafka module talks to the external broker
- Database → Outbox: backing store · Outbox durability relies on the database layer
