# sidorares/node-mysql2 architecture

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

- Interactive diagram: https://gitdiagram.com/sidorares/node-mysql2
- Repository: https://github.com/sidorares/node-mysql2

## Overview

##### Main Components (Core Architecture)

a) Client Interface Layer
- Connection Management
- Pool Management
- Promise Wrapper Interface
- Query Interface

b) Protocol Layer
- Packet Parser
- Command Handlers
- Authentication Handlers
- Compression Handler

c) Connection Layer
- SSL/TLS Handler
- Binary Protocol
- Text Protocol
- Stream Management

Flow of data through the system:
**Client Request → Connection Pool → Protocol Parser → MySQL Server**
**MySQL Server → Protocol Handler → Result Parser → Client Response**

##### Important Features (Highlight as capabilities)

- Prepared Statements
- Connection Pooling
- Binary Log Protocol
- Compression Support
- SSL/TLS Support
- Promise API
- Streaming Interface

##### Component Relationships

Relationships between components using different line styles:
- Double lines: Data flow

##### Implementation Notes

Notes for important implementation details:
- Native JavaScript implementation
- No binary dependencies
- TypeScript support
- Promise-based API

- The modular architecture
- Clear separation of concerns
- Security features
- Performance optimizations
- Extensibility points

Keep the abstraction level balanced:
- High enough to show the overall architecture
- Detailed enough to understand component interactions
- Clear indication of core features and extensions

This system design should reflect MySQL2's focus on performance, security, and compatibility while showing its advanced features like prepared statements, binary protocol support, and connection pooling.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Client Interface Layer"
        CI[Client Interface]
        CM["Connection Management"]
        PM["Pool Management"]
        PWI["Promise Wrapper Interface"]
        QI["Query Interface"]
    end

    subgraph "Protocol Layer"
        PP["Packet Parser"]
        CH["Command Handlers"]
        AH["Authentication Handlers"]
        CPH["Compression Handler"]
    end

    subgraph "Connection Layer"
        SSL["SSL/TLS Handler"]
        BP["Binary Protocol"]
        TP["Text Protocol"]
        SM["Stream Management"]
    end

    subgraph "Authentication System"
        AP["Auth Plugins"]
        ASM["Auth Switch Mechanism"]
        CSP["Caching SHA2 Password"]
        NPA["Native Password Auth"]
    end

    subgraph "Query Execution System"
        PS["Prepared Statements"]
        TQH["Text Query Handler"]
        BLP["Binary Protocol Parser"]
        RS["Results Streaming"]
    end

    subgraph "Connection Pooling System"
        PLM["Pool Manager"]
        PC["Pool Cluster"]
        CF["Connection Factory"]
        RM["Resource Management"]
    end

    subgraph "External Interfaces"
        MS["MySQL Server"]
        CA["Client Applications"]
        SSLS["SSL/TLS Services"]
        CS["Compression Services"]
    end

    %% Relationships
    CA --> CI
    CI --> PM
    CI --> QI
    PM --> PLM
    PLM --> PC
    PC --> CF
    CF --> CM
    CM --> PP
    PP --> CH
    CH --> AH
    AH --> AP
    AP --> CSP
    AP --> NPA
    CPH --> CS
    SSL --> SSLS
    CM --> MS

    %% Click events for component mapping
    click CM "https://github.com/sidorares/node-mysql2/blob/master/lib/connection.js"
    click PM "https://github.com/sidorares/node-mysql2/blob/master/lib/pool.js"
    click PWI "https://github.com/sidorares/node-mysql2/tree/master/lib/promise/"
    click QI "https://github.com/sidorares/node-mysql2/blob/master/lib/commands/query.js"
    click PP "https://github.com/sidorares/node-mysql2/blob/master/lib/packet_parser.js"
    click CH "https://github.com/sidorares/node-mysql2/tree/master/lib/commands/"
    click AH "https://github.com/sidorares/node-mysql2/blob/master/lib/commands/auth_switch.js"
    click CPH "https://github.com/sidorares/node-mysql2/blob/master/lib/compressed_protocol.js"
    click AP "https://github.com/sidorares/node-mysql2/tree/master/lib/auth_plugins/"
    click CSP "https://github.com/sidorares/node-mysql2/blob/master/lib/auth_plugins/caching_sha2_password.js"
    click NPA "https://github.com/sidorares/node-mysql2/blob/master/lib/auth_plugins/mysql_native_password.js"
    click ASM "https://github.com/sidorares/node-mysql2/blob/master/lib/commands/auth_switch.js"
    click PS "https://github.com/sidorares/node-mysql2/blob/master/lib/commands/prepare.js"
    click TQH "https://github.com/sidorares/node-mysql2/blob/master/lib/commands/query.js"
    click BLP "https://github.com/sidorares/node-mysql2/blob/master/lib/parsers/binary_parser.js"
    click RS "https://github.com/sidorares/node-mysql2/blob/master/lib/results_stream.js"
    click PLM "https://github.com/sidorares/node-mysql2/blob/master/lib/pool.js"
    click PC "https://github.com/sidorares/node-mysql2/blob/master/lib/pool_cluster.js"
    click CF "https://github.com/sidorares/node-mysql2/blob/master/lib/create_pool.js"
    click SSL "https://github.com/sidorares/node-mysql2/blob/master/lib/constants/ssl_profiles.js"
    click SM "https://github.com/sidorares/node-mysql2/blob/master/lib/results_stream.js"

    classDef core fill:#2374ab
    classDef security fill:#57a773
    classDef performance fill:#ff8c42
    classDef external fill:#808080
    classDef interface fill:#4392f1

    class CI,CM,PM,PWI,QI interface
    class PP,CH,AH,CPH core
    class SSL,AP,ASM,CSP,NPA security
    class PS,TQH,BLP,RS,PLM,PC,CF,RM performance
    class MS,CA,SSLS,CS external
```
