# akira393/2021-typescript-ddd architecture

> Architecture diagram of the GitHub repository akira393/2021-typescript-ddd, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/akira393/2021-typescript-ddd
- Repository: https://github.com/akira393/2021-typescript-ddd

## Overview

##### Main Components
- API Gateway / Entry Point: Although not directly seen in the code, the use of SAM to start a local API implies an API gateway (or similar routing mechanism) that receives HTTP requests.
- Lambda Handlers: The handlers located in backend/src/lambda/handlers (e.g., deleteUserLambda.ts, getUserLambda.ts, etc.) act as the entry points to invoke the business logic.
- Application Service Layer: Found under backend/src/lambda/applicationService, this layer (e.g., the Users folder and its various services) contains logic for processing commands and orchestrating the domain operations.
- Command Objects: These are clearly defined in backend/src/lambda/applicationService/Users/command, and their purpose is to encapsulate user operations, which simplifies the application service.
- Domain Layer: Under backend/src/lambda/domains, the domain objects (such as User in model) are present to define business rules and policies. This layer also defines interfaces such as IUserRepository and IUserNotification.
- Infrastructure Layer: The implementation of repository interfaces is visible in backend/src/lambda/infrastructure (e.g., UserRepository.ts), which interacts with DynamoDB (as suggested by the user-table.json and the setup instructions).
- AWS CDK Stack: The file backend/lib/backend-stack.ts defines part of the infrastructure as code, laying out the configuration for AWS resources.
- Tests: The test directories and files indicate a separate test suite for verifying both backend logic and lambda handlers.

- Lambda Handlers invoke the Application Services by passing command objects and data.
- The Application Services call upon Domain Models to perform business logic.
- Domain Models interact with the Repository interface, whose concrete implementation (infrastructure layer) persists data to DynamoDB.
- This shows a clear separation of responsibilities and a unidirectional flow of data.

##### Architectural Patterns and Key Design Principles
- Domain-Driven Design (DDD): Notice the separation with distinct folders for domains, application services, repository interfaces, and command objects.
- Command Pattern: The project leverages command objects to encapsulate user operations.
- Layered Architecture: The code cleanly divides the system into layers (API, application service, domain, infrastructure).
- Infrastructure as Code: The AWS CDK stack is used to provision AWS resources.
- Testing Layer: There is comprehensive testing ensuring each layer functions as expected.

##### Relevant Technologies
- AWS Lambda: for serverless business logic execution.
- AWS CDK and SAM: to orchestrate and deploy the infrastructure.
- DynamoDB: as the persistent data store.
- TypeScript: the language used for development.
- AWS SDK: implied for operations with DynamoDB.
- jest: for testing (based on jest.config.js and test directories).

## Diagram (Mermaid)

```mermaid
graph TD
    %% API Gateway
    API["API Gateway"]:::gateway

    %% Serverless API Layer
    subgraph "Serverless API Layer"
        L1["Lambda: deleteUserLambda"]:::lambda
        L2["Lambda: getUserLambda"]:::lambda
        L3["Lambda: getUsersLambda"]:::lambda
        L4["Lambda: registerUserLambda"]:::lambda
        L5["Lambda: updateUserLambda"]:::lambda
    end

    %% Application Service Layer
    subgraph "Application Service Layer"
        AS1["UserDeleteService"]:::service
        AS2["UserGetService"]:::service
        AS3["UserGetAllService"]:::service
        AS4["UserRegisterService"]:::service
        AS5["UserUpdateService"]:::service
        ADS["UserData"]:::service
        subgraph "Command Objects"
            CMD1["UserDeleteCommand"]:::command
            CMD2["UserUpdateCommand"]:::command
        end
    end

    %% Domain Layer
    subgraph "Domain Layer"
        subgraph "Domain Models"
            DM1["User"]:::domain
            DM2["Base"]:::domain
        end
        subgraph "Domain Interfaces"
            DI1["IUserRepository"]:::interface
            DI2["IUserNotification"]:::interface
        end
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        INF["UserRepository"]:::infra
        DB["DynamoDB"]:::database
    end

    %% AWS CDK Stack
    IAC["AWS CDK Stack"]:::iac

    %% Testing
    TEST["Backend Test Suite"]:::test

    %% Connections
    API -->|"HTTP_Request"| L1
    API -->|"HTTP_Request"| L2
    API -->|"HTTP_Request"| L3
    API -->|"HTTP_Request"| L4
    API -->|"HTTP_Request"| L5

    L1 -->|"invokes"| AS1
    L2 -->|"invokes"| AS2
    L3 -->|"invokes"| AS3
    L4 -->|"invokes"| AS4
    L5 -->|"invokes"| AS5

    AS1 -->|"uses"| CMD1
    AS5 -->|"uses"| CMD2

    AS1 -->|"sends_data"| ADS
    AS2 -->|"sends_data"| ADS
    AS3 -->|"sends_data"| ADS
    AS4 -->|"sends_data"| ADS
    AS5 -->|"sends_data"| ADS

    ADS -->|"processes"| DM1

    DM1 -->|"defines_logic"| DI1

    DI1 -->|"implemented_by"| INF
    INF -->|"persists_to"| DB

    IAC -->|"deploys"| INF
    IAC -->|"deploys"| DB

    TEST -->|"tests"| L2
    TEST -->|"tests"| AS1

    %% Click Events for Lambda Handlers
    click L1 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/handlers/deleteUserLambda.ts"
    click L2 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/handlers/getUserLambda.ts"
    click L3 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/handlers/getUsersLambda.ts"
    click L4 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/handlers/registerUserLambda.ts"
    click L5 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/handlers/updateUserLambda.ts"

    %% Click Events for Application Service Layer
    click AS1 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/applicationService/Users/UserDeleteService.ts"
    click AS2 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/applicationService/Users/UserGetService.ts"
    click AS3 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/applicationService/Users/UserGetAllService.ts"
    click AS4 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/applicationService/Users/UserRegisterService.ts"
    click AS5 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/applicationService/Users/UserUpdateService.ts"
    click ADS "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/applicationService/Users/UserData.ts"

    %% Click Events for Command Objects
    click CMD1 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/applicationService/Users/command/UserDeleteCommand.ts"
    click CMD2 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/applicationService/Users/command/UserUpdateCommand.ts"

    %% Click Events for Domain Models
    click DM1 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/domains/model/User.ts"
    click DM2 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/domains/model/base.ts"

    %% Click Events for Domain Interfaces
    click DI1 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/domains/repositoryInterface/IUserRepository.ts"
    click DI2 "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/domains/notificationInterface/IUserNotification.ts"

    %% Click Event for Infrastructure Layer
    click INF "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/src/lambda/infrastructure/UserRepository.ts"

    %% Click Event for AWS CDK Stack
    click IAC "https://github.com/akira393/2021-typescript-ddd/blob/master/backend/lib/backend-stack.ts"

    %% Click Event for Tests
    click TEST "https://github.com/akira393/2021-typescript-ddd/tree/master/backend/test"

    %% Styles
    classDef gateway fill:#a3c3f5,stroke:#333,stroke-width:2px;
    classDef lambda fill:#f9e79f,stroke:#333,stroke-width:2px;
    classDef service fill:#aed6f1,stroke:#333,stroke-width:2px;
    classDef command fill:#fad7a0,stroke:#333,stroke-width:2px;
    classDef domain fill:#d5f5e3,stroke:#333,stroke-width:2px;
    classDef interface fill:#f5b7b1,stroke:#333,stroke-width:2px;
    classDef infra fill:#f1948a,stroke:#333,stroke-width:2px;
    classDef database fill:#c39bd3,stroke:#333,stroke-width:2px;
    classDef iac fill:#7fb3d5,stroke:#333,stroke-width:2px;
    classDef test fill:#82e0aa,stroke:#333,stroke-width:2px;
```
