# アーキテクチャ構成図

## システム全体アーキテクチャ

```mermaid
graph TB
    subgraph Client["クライアント"]
        Browser["ブラウザ<br/>(管理UI)"]
        API_Client["API クライアント<br/>(REST/GraphQL)"]
    end

    subgraph Next.js_App["Next.js アプリケーション"]
        subgraph Presentation["プレゼンテーション層"]
            Admin_UI["@payloadcms/ui<br/>管理UIコンポーネント"]
            RSC["React Server Components"]
        end

        subgraph Integration["Next.js統合層"]
            Next_Routes["@payloadcms/next<br/>ルートハンドラー"]
            WithPayload["withPayload<br/>設定統合"]
        end
    end

    subgraph Payload_Core["Payload コア"]
        subgraph API_Layer["API層"]
            REST_API["REST API"]
            GraphQL_API["@payloadcms/graphql<br/>GraphQL API"]
        end

        subgraph Application["アプリケーション層"]
            Collections["コレクション<br/>操作"]
            Globals["グローバル<br/>操作"]
            Auth["認証・認可<br/>JWT/API Key"]
            Hooks["フック<br/>システム"]
            Versioning["バージョン<br/>管理"]
            Jobs["ジョブ<br/>キュー"]
        end

        subgraph Config["設定管理"]
            Payload_Config["Payload Config"]
            Plugins["プラグイン<br/>システム"]
        end
    end

    subgraph Data_Access["データアクセス層"]
        subgraph DB_Adapters["データベースアダプター"]
            DB_MongoDB["@payloadcms/db-mongodb<br/>Mongoose"]
            DB_Postgres["@payloadcms/db-postgres<br/>Drizzle ORM"]
            DB_SQLite["@payloadcms/db-sqlite<br/>Drizzle ORM"]
            Drizzle["@payloadcms/drizzle<br/>共通レイヤー"]
        end

        subgraph Storage_Adapters["ストレージアダプター"]
            Storage_S3["@payloadcms/storage-s3"]
            Storage_Azure["@payloadcms/storage-azure"]
            Storage_GCS["@payloadcms/storage-gcs"]
            Storage_Vercel["@payloadcms/storage-vercel-blob"]
        end

        subgraph Other_Adapters["その他アダプター"]
            Email["Email Adapters<br/>Nodemailer/Resend"]
            KV["@payloadcms/kv-redis"]
        end
    end

    subgraph External["外部サービス"]
        MongoDB[(MongoDB)]
        PostgreSQL[(PostgreSQL)]
        SQLite[(SQLite)]
        S3[(Amazon S3)]
        Azure[(Azure Blob)]
        GCS[(Google Cloud<br/>Storage)]
        Redis[(Redis)]
        SMTP["SMTP Server"]
    end

    Browser --> Admin_UI
    API_Client --> REST_API
    API_Client --> GraphQL_API

    Admin_UI --> RSC
    RSC --> Next_Routes

    Next_Routes --> REST_API
    Next_Routes --> GraphQL_API

    REST_API --> Auth
    GraphQL_API --> Auth
    Auth --> Collections
    Auth --> Globals

    Collections --> Hooks
    Globals --> Hooks
    Hooks --> Versioning
    Hooks --> Jobs

    Collections --> DB_Adapters
    Globals --> DB_Adapters
    Versioning --> DB_Adapters

    Plugins --> Payload_Config
    Payload_Config --> Collections
    Payload_Config --> Globals

    DB_MongoDB --> MongoDB
    DB_Postgres --> Drizzle
    DB_SQLite --> Drizzle
    Drizzle --> PostgreSQL
    Drizzle --> SQLite

    Storage_S3 --> S3
    Storage_Azure --> Azure
    Storage_GCS --> GCS

    KV --> Redis
    Email --> SMTP
```

## パッケージ依存関係図

```mermaid
graph LR
    subgraph Core["コアパッケージ"]
        payload["payload"]
        translations["@payloadcms/translations"]
    end

    subgraph UI_Layer["UI層"]
        ui["@payloadcms/ui"]
        next["@payloadcms/next"]
    end

    subgraph API["API"]
        graphql["@payloadcms/graphql"]
    end

    subgraph Database["データベース"]
        drizzle["@payloadcms/drizzle"]
        db_mongodb["@payloadcms/db-mongodb"]
        db_postgres["@payloadcms/db-postgres"]
        db_sqlite["@payloadcms/db-sqlite"]
    end

    subgraph Storage["ストレージ"]
        cloud_storage["@payloadcms/plugin-cloud-storage"]
        storage_s3["@payloadcms/storage-s3"]
        storage_azure["@payloadcms/storage-azure"]
        storage_gcs["@payloadcms/storage-gcs"]
    end

    subgraph RichText["リッチテキスト"]
        richtext_lexical["@payloadcms/richtext-lexical"]
        richtext_slate["@payloadcms/richtext-slate"]
    end

    subgraph Plugins["プラグイン"]
        plugin_seo["@payloadcms/plugin-seo"]
        plugin_search["@payloadcms/plugin-search"]
        plugin_nested["@payloadcms/plugin-nested-docs"]
        plugin_form["@payloadcms/plugin-form-builder"]
    end

    translations --> payload
    ui --> payload
    ui --> translations
    next --> payload
    next --> ui
    next --> graphql
    graphql --> payload

    drizzle --> payload
    db_mongodb --> payload
    db_postgres --> drizzle
    db_sqlite --> drizzle

    cloud_storage --> payload
    storage_s3 --> cloud_storage
    storage_azure --> cloud_storage
    storage_gcs --> cloud_storage

    richtext_lexical --> payload
    richtext_slate --> payload

    plugin_seo --> payload
    plugin_search --> payload
    plugin_nested --> payload
    plugin_form --> payload
```

## コアパッケージ内部構造

```mermaid
graph TB
    subgraph payload["packages/payload/src"]
        subgraph Entry["エントリーポイント"]
            index["index.ts<br/>Payload クラス"]
        end

        subgraph Config_Module["設定モジュール"]
            config["config/"]
            types["types/"]
        end

        subgraph Auth_Module["認証モジュール"]
            auth["auth/"]
            strategies["strategies/<br/>JWT, API Key"]
            operations_auth["operations/<br/>login, logout, etc."]
        end

        subgraph Collections_Module["コレクションモジュール"]
            collections["collections/"]
            operations["operations/<br/>find, create, update, delete"]
        end

        subgraph Globals_Module["グローバルモジュール"]
            globals["globals/"]
            global_ops["operations/"]
        end

        subgraph DB_Module["データベースモジュール"]
            database["database/"]
            db_types["types.ts<br/>BaseDatabaseAdapter"]
        end

        subgraph Fields_Module["フィールドモジュール"]
            fields["fields/"]
            field_types["config/types.ts"]
        end

        subgraph Utils["ユーティリティ"]
            utilities["utilities/"]
            errors["errors/"]
            email["email/"]
        end

        subgraph Features["機能"]
            versions["versions/"]
            uploads["uploads/"]
            queues["queues/"]
            folders["folders/"]
        end
    end

    index --> Config_Module
    index --> Auth_Module
    index --> Collections_Module
    index --> Globals_Module
    index --> DB_Module

    Auth_Module --> strategies
    Auth_Module --> operations_auth
    Collections_Module --> operations
    Globals_Module --> global_ops

    Collections_Module --> Fields_Module
    Globals_Module --> Fields_Module
    Collections_Module --> Features
```

## リクエスト処理フロー

```mermaid
sequenceDiagram
    participant Client as クライアント
    participant Next as Next.js Router
    participant Auth as 認証モジュール
    participant Core as Payload Core
    participant Hooks as フックシステム
    participant DB as DBアダプター
    participant External as 外部DB

    Client->>Next: HTTP Request
    Next->>Auth: 認証チェック
    Auth->>Auth: JWT/API Key検証
    Auth-->>Next: 認証結果

    alt 認証成功
        Next->>Core: オペレーション実行
        Core->>Hooks: beforeValidate
        Core->>Core: バリデーション
        Core->>Hooks: beforeChange
        Core->>DB: データ操作
        DB->>External: DB Query
        External-->>DB: 結果
        DB-->>Core: データ
        Core->>Hooks: afterChange
        Core-->>Next: 処理結果
        Next-->>Client: Response (200)
    else 認証失敗
        Next-->>Client: Response (401/403)
    end
```

## データベースアダプター構造

```mermaid
graph TB
    subgraph Interface["抽象インターフェース"]
        BaseDatabaseAdapter["BaseDatabaseAdapter<br/>packages/payload/src/database/types.ts"]
    end

    subgraph MongoDB_Adapter["MongoDB アダプター"]
        MongoDB_Index["@payloadcms/db-mongodb"]
        MongoDB_Connect["connect.ts"]
        MongoDB_CRUD["find.ts, create.ts,<br/>update.ts, delete.ts"]
        MongoDB_Models["models/"]
        Mongoose["Mongoose ODM"]
    end

    subgraph Drizzle_Base["Drizzle 共通"]
        Drizzle_Index["@payloadcms/drizzle"]
        Drizzle_CRUD["共通 CRUD 操作"]
        Drizzle_Schema["schema/"]
        Drizzle_Queries["queries/"]
    end

    subgraph Postgres_Adapter["PostgreSQL アダプター"]
        Postgres_Index["@payloadcms/db-postgres"]
        Postgres_Connect["connect.ts"]
        PG_Client["pg クライアント"]
    end

    subgraph SQLite_Adapter["SQLite アダプター"]
        SQLite_Index["@payloadcms/db-sqlite"]
        SQLite_Connect["connect.ts"]
        BetterSQLite["better-sqlite3"]
    end

    BaseDatabaseAdapter --> MongoDB_Adapter
    BaseDatabaseAdapter --> Drizzle_Base

    MongoDB_Index --> MongoDB_Connect
    MongoDB_Index --> MongoDB_CRUD
    MongoDB_CRUD --> MongoDB_Models
    MongoDB_Models --> Mongoose

    Drizzle_Base --> Postgres_Adapter
    Drizzle_Base --> SQLite_Adapter

    Postgres_Index --> Postgres_Connect
    Postgres_Connect --> Drizzle_Base
    Postgres_Connect --> PG_Client

    SQLite_Index --> SQLite_Connect
    SQLite_Connect --> Drizzle_Base
    SQLite_Connect --> BetterSQLite
```

## 認証フロー

```mermaid
sequenceDiagram
    participant User as ユーザー
    participant API as API Endpoint
    participant Strategy as 認証ストラテジー
    participant JWT as JWT Module
    participant DB as データベース

    User->>API: POST /api/users/login
    API->>DB: ユーザー検索
    DB-->>API: ユーザーデータ
    API->>API: パスワード検証

    alt 認証成功
        API->>JWT: トークン生成
        JWT-->>API: JWT Token
        API-->>User: 200 + Token + Cookie
    else 認証失敗
        API-->>User: 401 Unauthorized
    end

    Note over User,DB: 以降のリクエスト

    User->>API: GET /api/posts (Authorization: Bearer token)
    API->>Strategy: トークン検証
    Strategy->>JWT: JWT検証
    JWT-->>Strategy: デコード結果
    Strategy->>DB: ユーザー取得
    DB-->>Strategy: ユーザーデータ
    Strategy-->>API: 認証済みユーザー
    API->>API: アクセス制御チェック
    API-->>User: 200 + Data
```

## プラグインシステム

```mermaid
graph TB
    subgraph User_Config["ユーザー設定"]
        Config["payload.config.ts"]
    end

    subgraph Plugin_System["プラグインシステム"]
        Plugin_Type["Plugin 型<br/>(config) => config"]
        Plugin_Chain["プラグインチェーン"]
    end

    subgraph Official_Plugins["公式プラグイン"]
        SEO["plugin-seo"]
        Search["plugin-search"]
        NestedDocs["plugin-nested-docs"]
        FormBuilder["plugin-form-builder"]
        Redirects["plugin-redirects"]
        MultiTenant["plugin-multi-tenant"]
    end

    subgraph Plugin_Capabilities["プラグイン機能"]
        Add_Collections["コレクション追加"]
        Add_Globals["グローバル追加"]
        Add_Fields["フィールド追加"]
        Add_Hooks["フック追加"]
        Add_Endpoints["エンドポイント追加"]
        Modify_Admin["管理UI変更"]
    end

    Config --> Plugin_System
    Plugin_System --> Plugin_Chain

    Official_Plugins --> Plugin_Type
    Plugin_Type --> Plugin_Chain

    Plugin_Chain --> Add_Collections
    Plugin_Chain --> Add_Globals
    Plugin_Chain --> Add_Fields
    Plugin_Chain --> Add_Hooks
    Plugin_Chain --> Add_Endpoints
    Plugin_Chain --> Modify_Admin
```
