# インフラ構成図

## 1. システム全体構成図

```mermaid
graph TB
    subgraph Client["クライアント層"]
        Browser["Webブラウザ"]
        API_Client["APIクライアント"]
    end

    subgraph Edge["エッジ/CDN層"]
        CDN["CDN<br/>(Vercel/Cloudflare)"]
    end

    subgraph Application["アプリケーション層"]
        Next["Next.js + Payload CMS<br/>(Node.js 18.20.2+ / 20.9.0+)"]
    end

    subgraph Database["データベース層"]
        MongoDB["MongoDB 8.2<br/>(Community Server)"]
        PostgreSQL["PostgreSQL<br/>(PostGIS + pgvector)"]
        SQLite["SQLite<br/>(D1/libsql)"]
    end

    subgraph Cache["キャッシュ層"]
        Redis["Redis<br/>(ioredis)"]
    end

    subgraph Storage["ストレージ層"]
        S3["AWS S3"]
        Azure["Azure Blob"]
        GCS["Google Cloud Storage"]
        R2["Cloudflare R2"]
        VercelBlob["Vercel Blob"]
    end

    subgraph Email["メール配信層"]
        Nodemailer["Nodemailer<br/>(SMTP)"]
        Resend["Resend API"]
    end

    Browser --> CDN
    API_Client --> CDN
    CDN --> Next
    Next --> MongoDB
    Next --> PostgreSQL
    Next --> SQLite
    Next --> Redis
    Next --> S3
    Next --> Azure
    Next --> GCS
    Next --> R2
    Next --> VercelBlob
    Next --> Nodemailer
    Next --> Resend
```

## 2. 開発環境構成図（Docker Compose）

```mermaid
graph TB
    subgraph Host["ホストマシン"]
        Dev["開発者"]
    end

    subgraph Docker["Docker環境"]
        subgraph Payload_Container["Payload コンテナ<br/>(node:18/20-alpine)"]
            NextApp["Next.js App<br/>:3000"]
        end

        subgraph DB_Containers["データベースコンテナ"]
            Mongo["MongoDB<br/>:27017/27018"]
            Mongot["mongot<br/>(Vector Search)"]
            Postgres["PostgreSQL<br/>:5432/5433"]
        end

        subgraph Storage_Emulators["ストレージエミュレータ"]
            LocalStack["LocalStack<br/>(S3)<br/>:4563-4599"]
            Azurite["Azurite<br/>(Azure Blob)<br/>:10000-10002"]
            FakeGCS["fake-gcs-server<br/>:4443"]
        end
    end

    subgraph Volumes["永続化ボリューム"]
        NodeModules["node_modules"]
        MongoData["mongodb_data"]
        PostgresData["postgres_data"]
    end

    Dev -->|HTTP :3000| NextApp
    NextApp -->|MongoDB Protocol| Mongo
    NextApp -->|PostgreSQL Protocol| Postgres
    Mongo --> Mongot
    NextApp -->|S3 API| LocalStack
    NextApp -->|Azure API| Azurite
    NextApp -->|GCS API| FakeGCS
    Mongo --> MongoData
    Postgres --> PostgresData
    NextApp --> NodeModules
```

## 3. 本番環境構成図（クラウドデプロイ）

```mermaid
graph TB
    subgraph Internet["インターネット"]
        Users["ユーザー"]
    end

    subgraph CDN_Layer["CDN / エッジレイヤー"]
        Vercel["Vercel Edge"]
        Cloudflare["Cloudflare"]
    end

    subgraph Compute["コンピュートレイヤー"]
        VercelFn["Vercel Functions"]
        CloudflareW["Cloudflare Workers"]
        Docker["Docker / K8s"]
    end

    subgraph Database_Layer["データベースレイヤー"]
        subgraph Managed_DB["マネージドDB"]
            MongoAtlas["MongoDB Atlas"]
            VercelPG["Vercel Postgres"]
            CloudflareD1["Cloudflare D1"]
            Supabase["Supabase"]
        end
        subgraph Self_Hosted["セルフホスト"]
            MongoSelf["MongoDB<br/>(Self-hosted)"]
            PostgresSelf["PostgreSQL<br/>(Self-hosted)"]
        end
    end

    subgraph Storage_Layer["ストレージレイヤー"]
        AWS_S3["AWS S3"]
        Azure_Blob["Azure Blob Storage"]
        GCS_Storage["Google Cloud Storage"]
        CF_R2["Cloudflare R2"]
        V_Blob["Vercel Blob"]
        Uploadthing["Uploadthing"]
    end

    subgraph Auth_Email["認証/メール"]
        Cognito["AWS Cognito<br/>(Payload Cloud)"]
        ResendSvc["Resend"]
        SMTP["SMTP Server"]
    end

    subgraph Monitoring["監視"]
        Sentry["Sentry"]
    end

    Users --> Vercel
    Users --> Cloudflare
    Vercel --> VercelFn
    Cloudflare --> CloudflareW
    Vercel --> Docker
    Cloudflare --> Docker
    VercelFn --> MongoAtlas
    VercelFn --> VercelPG
    CloudflareW --> CloudflareD1
    Docker --> MongoSelf
    Docker --> PostgresSelf
    Docker --> MongoAtlas
    Docker --> VercelPG
    Docker --> Supabase
    VercelFn --> AWS_S3
    VercelFn --> V_Blob
    CloudflareW --> CF_R2
    Docker --> AWS_S3
    Docker --> Azure_Blob
    Docker --> GCS_Storage
    Docker --> Uploadthing
    VercelFn --> Cognito
    Docker --> ResendSvc
    Docker --> SMTP
    VercelFn --> Sentry
    Docker --> Sentry
```

## 4. データベースアダプター構成

```mermaid
graph LR
    subgraph Payload["Payload CMS"]
        Core["Payload Core"]
    end

    subgraph DB_Adapters["データベースアダプター"]
        subgraph MongoDB_Family["MongoDB系"]
            db_mongodb["@payloadcms/db-mongodb"]
        end
        subgraph Drizzle_Family["Drizzle ORM系"]
            db_postgres["@payloadcms/db-postgres"]
            db_vercel_postgres["@payloadcms/db-vercel-postgres"]
            db_sqlite["@payloadcms/db-sqlite"]
            db_d1["@payloadcms/db-d1-sqlite"]
        end
    end

    subgraph ORM_Layer["ORM/ODM層"]
        Mongoose["Mongoose 8.15.1"]
        Drizzle["@payloadcms/drizzle<br/>(Drizzle ORM 0.44.7)"]
    end

    subgraph Databases["データベース"]
        MongoDB["MongoDB"]
        PostgreSQL["PostgreSQL"]
        SQLite["SQLite"]
        D1["Cloudflare D1"]
        VercelPG["Vercel Postgres"]
    end

    Core --> db_mongodb
    Core --> db_postgres
    Core --> db_vercel_postgres
    Core --> db_sqlite
    Core --> db_d1

    db_mongodb --> Mongoose
    db_postgres --> Drizzle
    db_vercel_postgres --> Drizzle
    db_sqlite --> Drizzle
    db_d1 --> Drizzle

    Mongoose --> MongoDB
    Drizzle --> PostgreSQL
    Drizzle --> SQLite
    Drizzle --> D1
    Drizzle --> VercelPG
```

## 5. ストレージアダプター構成

```mermaid
graph LR
    subgraph Payload["Payload CMS"]
        Core["Payload Core"]
        CloudStorage["@payloadcms/plugin-cloud-storage"]
    end

    subgraph Storage_Adapters["ストレージアダプター"]
        storage_s3["@payloadcms/storage-s3"]
        storage_azure["@payloadcms/storage-azure"]
        storage_gcs["@payloadcms/storage-gcs"]
        storage_r2["@payloadcms/storage-r2"]
        storage_vercel["@payloadcms/storage-vercel-blob"]
        storage_uploadthing["@payloadcms/storage-uploadthing"]
    end

    subgraph Storage_Services["ストレージサービス"]
        AWS_S3["AWS S3"]
        Azure_Blob["Azure Blob Storage"]
        GCS["Google Cloud Storage"]
        CF_R2["Cloudflare R2"]
        V_Blob["Vercel Blob"]
        UT["Uploadthing"]
    end

    Core --> CloudStorage
    CloudStorage --> storage_s3
    CloudStorage --> storage_azure
    CloudStorage --> storage_gcs
    CloudStorage --> storage_r2
    CloudStorage --> storage_vercel
    CloudStorage --> storage_uploadthing

    storage_s3 --> AWS_S3
    storage_azure --> Azure_Blob
    storage_gcs --> GCS
    storage_r2 --> CF_R2
    storage_vercel --> V_Blob
    storage_uploadthing --> UT
```

## 6. Dockerコンテナビルドフロー

```mermaid
graph TB
    subgraph Build_Process["Docker マルチステージビルド"]
        subgraph Stage1["Stage 1: base"]
            Base["node:22.17.0-alpine"]
        end

        subgraph Stage2["Stage 2: deps"]
            Deps["依存関係インストール"]
            YarnLock["yarn.lock"]
            NPMLock["package-lock.json"]
            PNPMLock["pnpm-lock.yaml"]
        end

        subgraph Stage3["Stage 3: builder"]
            Builder["アプリケーションビルド"]
            NextBuild["next build"]
            Standalone["standalone output"]
        end

        subgraph Stage4["Stage 4: runner"]
            Runner["本番実行環境"]
            NonRoot["非rootユーザー<br/>(nextjs:nodejs)"]
            Port3000["EXPOSE 3000"]
        end
    end

    Base --> Deps
    YarnLock --> Deps
    NPMLock --> Deps
    PNPMLock --> Deps
    Deps --> Builder
    NextBuild --> Builder
    Builder --> Standalone
    Standalone --> Runner
    NonRoot --> Runner
    Port3000 --> Runner
```

## 7. ネットワークポート一覧

```mermaid
graph LR
    subgraph Ports["ポート構成"]
        subgraph Application_Ports["アプリケーション"]
            P3000["3000: Next.js"]
        end

        subgraph Database_Ports["データベース"]
            P27017["27017: MongoDB"]
            P27018["27018: MongoDB (Test)"]
            P5432["5432: PostgreSQL"]
            P5433["5433: PostgreSQL (Test)"]
        end

        subgraph Storage_Ports["ストレージエミュレータ"]
            P4563["4563-4599: LocalStack"]
            P8055["8055: LocalStack Admin"]
            P10000["10000-10002: Azurite"]
            P4443["4443: fake-gcs-server"]
        end
    end
```
