How psawn/nestjs-sample works
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: app.module.ts App module · outbox.module.ts Outbox · user.service.ts User service · auth.controller.ts Auth API
Components 24
App Shell
NestJS bootstrap and top-level module wiring
- Bootstrap · Application entrypoint that starts the Nest runtime
src/main.ts - App module · Composes the application feature and infrastructure modules
src/app.module.ts
Auth
Credential and access-control subsystem
- Auth API · HTTP boundary for login and registration
src/auth/auth.controller.ts - Auth service · Coordinates authentication flows and credential checks
src/auth/auth.service.ts - Auth repo · Concrete credential persistence implementation
src/auth/repositories/auth-credential.repository.ts - Credentials · Stored authentication credential model
src/auth/entities/auth-credential.entity.ts - Auth DTOs · Login and registration request shapes
src/auth/dto
User
User profile and lifecycle subsystem
- User API · HTTP boundary for user profile operations
src/user/user.controller.ts - User service · Coordinates user creation and lifecycle logic
src/user/user.service.ts - User repo · Concrete user profile persistence implementation
src/user/repositories/user-profile.repository.ts - Profiles · User profile domain model
src/user/entities/user-profile.entity.ts - Create user · Request shape for creating users
src/user/dto/create-user.dto.ts
Gateway
Facade entrypoint over internal services
- Gateway API · Higher-level entrypoint that fronts internal services
src/gateway/gateway.controller.ts - Gateway service · Orchestrates gateway-level application flow
src/gateway/gateway.service.ts
Shared
Cross-cutting decorators, guards, tokens, and event contracts
- Guards · JWT and role-based request protection
src/common/guards - Decorators · Public and roles route metadata helpers
src/common/decorators - Event contracts · Shared event interfaces and base event types
src/common/interfaces/events
Infrastructure
Database, Kafka, and outbox persistence/publishing
- Database · Bootstraps persistence infrastructure
src/infrastructure/database/database.module.ts - Kafka · Messaging integration and broker access
src/infrastructure/kafka/kafka.module.ts - Outbox · Stores domain events before asynchronous publication
src/infrastructure/outbox/outbox.module.ts - Outbox row · Persisted event record awaiting publication
src/infrastructure/outbox/entities/outbox.entity.ts - Outbox repo · Concrete outbox persistence implementation
src/infrastructure/outbox/repositories/outbox.repository.ts - Publisher · Publishes stored events to Kafka
src/infrastructure/outbox/services/outbox-publisher.service.ts
External Systems
Runtime dependencies outside the application
- Kafka broker · External message broker used for event propagation
kafka/docker-compose.yml
Connections 28
- 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