# erd-codegen/ts-node-knex architecture

> Architecture diagram of the GitHub repository erd-codegen/ts-node-knex, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/erd-codegen/ts-node-knex
- Repository: https://github.com/erd-codegen/ts-node-knex

## Overview

##### Main Components

a. Client Layer:
- External API consumers
- Swagger/OpenAPI documentation interface (visible from the /api-doc endpoint)

b. API Gateway Layer:
- AWS API Gateway (as mentioned in the serverless deployment)
- Lambda Functions integration points

c. Application Services Layer (Microservices):
- User Service (/apps/user)
- Article Service (/apps/article)
- App Service (/apps/app) - Documentation and health checks

d. Core Layer (/packages/core):
- Authentication Service
- User Service
- Article Service
- Repository Layer
- Database Access Layer

e. Database Layer:
- MySQL Database (using Knex.js as ORM)
- Database Tables:
- Users
- Articles
- Comments
- Tags
- User Follows
- Article Favorites

##### Architectural Patterns to Highlight

a. Microservices Architecture:
- Clear boundaries between user, article, and app services
- Independent deployment capabilities

b. Clean Architecture:
- Controllers
- Services
- Repositories
- Database Access
- DTOs for data transformation

c. Infrastructure Components:
- AWS Lambda functions
- API Gateway configuration
- CDK deployment stack

##### Database Layer

Use these visual elements:
- Blue: External interfaces
- Green: Application services
- Yellow: Core services
- Red: Database layer
- Gray: Infrastructure components

- Rectangles: Services and components
- Hexagons: Lambda functions

##### Key Relationships

- Authentication flow between services
- Article creation and management flow
- User management and following relationships
- Comment and favorite functionality
- Tag management system

##### Additional Elements

- Error handling flow
- Middleware pipeline
- Configuration management
- Logging system
- Testing infrastructure

##### Infrastructure Details

- AWS deployment architecture
- Docker container relationships
- Development vs Production environments

Remember to highlight the serverless nature of the deployment and how it integrates with AWS services, as this is a key architectural feature of the system.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Clients
        API[API Consumers]:::client
        SwaggerUI[Swagger UI]:::client
    end

    subgraph Gateway
        APIGW[API Gateway]:::gateway
        click APIGW "https://github.com/ERD-Codegen/ts-node-knex/blob/master/infra/stacks/ApiGatewayStack/ApiGatewayStack.ts"
        Lambda[Lambda Functions]:::gateway
        click Lambda "https://github.com/ERD-Codegen/ts-node-knex/blob/master/infra/stacks/LambdaStack/LambdaStack.ts"
    end

    subgraph Microservices
        UserMS[User Service]:::microservice
        click UserMS "https://github.com/ERD-Codegen/ts-node-knex/tree/master/apps/user"
        ArticleMS[Article Service]:::microservice
        click ArticleMS "https://github.com/ERD-Codegen/ts-node-knex/tree/master/apps/article"
        AppMS[App Service]:::microservice
        click AppMS "https://github.com/ERD-Codegen/ts-node-knex/tree/master/apps/app"
    end

    subgraph CoreServices
        AuthService[Authentication Service]:::core
        click AuthService "https://github.com/ERD-Codegen/ts-node-knex/tree/master/packages/core/service/auth"
        UserService[User Service]:::core
        click UserService "https://github.com/ERD-Codegen/ts-node-knex/tree/master/packages/core/service/user"
        ArticleService[Article Service]:::core
        click ArticleService "https://github.com/ERD-Codegen/ts-node-knex/tree/master/packages/core/service/article"
    end

    subgraph Middleware
        AuthMW[Auth Middleware]:::middleware
        click AuthMW "https://github.com/ERD-Codegen/ts-node-knex/blob/master/packages/middleware/auth.ts"
        ErrorHandler[Global Exception Handler]:::middleware
        click ErrorHandler "https://github.com/ERD-Codegen/ts-node-knex/tree/master/packages/middleware/configureGlobalExceptionHandler"
        LambdaConfig[Lambda Configuration]:::middleware
        click LambdaConfig "https://github.com/ERD-Codegen/ts-node-knex/tree/master/packages/middleware/configureLambda"
    end

    subgraph DataLayer
        Repository[Repository Layer]:::data
        click Repository "https://github.com/ERD-Codegen/ts-node-knex/tree/master/packages/core/repository"
        DBAccess[Database Access Layer]:::data
        click DBAccess "https://github.com/ERD-Codegen/ts-node-knex/tree/master/packages/core/database"
    end

    subgraph Database
        MySQL[(MySQL Database)]:::db
        Users[(Users)]:::db
        click Users "https://github.com/ERD-Codegen/ts-node-knex/tree/master/packages/core/database/DbUser"
        Articles[(Articles)]:::db
        click Articles "https://github.com/ERD-Codegen/ts-node-knex/tree/master/packages/core/database/DbArticle"
        Comments[(Comments)]:::db
        Tags[(Tags)]:::db
        Follows[(User Follows)]:::db
        Favorites[(Article Favorites)]:::db
    end

    API --> APIGW
    SwaggerUI --> APIGW
    APIGW --> Lambda
    Lambda --> UserMS
    Lambda --> ArticleMS
    Lambda --> AppMS

    UserMS --> AuthMW
    ArticleMS --> AuthMW
    AppMS --> AuthMW

    AuthMW --> AuthService
    AuthMW --> ErrorHandler

    UserMS --> UserService
    ArticleMS --> ArticleService
    AppMS --> UserService
    AppMS --> ArticleService

    UserService --> Repository
    ArticleService --> Repository
    AuthService --> Repository

    Repository --> DBAccess
    DBAccess --> MySQL

    MySQL --> Users
    MySQL --> Articles
    MySQL --> Comments
    MySQL --> Tags
    MySQL --> Follows
    MySQL --> Favorites

    classDef client fill:#2374AB
    classDef gateway fill:#FF8C42
    classDef microservice fill:#4CAF50
    classDef core fill:#FFC107
    classDef middleware fill:#9C27B0
    classDef data fill:#FF5722
    classDef db fill:#F44336

    %% Legend
    subgraph Legend
        Client[Client Layer]:::client
        GW[Gateway Layer]:::gateway
        MS[Microservices]:::microservice
        Core[Core Services]:::core
        MW[Middleware]:::middleware
        Data[Data Layer]:::data
        DB[Database]:::db
    end
```
