# uptrace/bun architecture

> Architecture diagram of the GitHub repository uptrace/bun, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/uptrace/bun
- Repository: https://github.com/uptrace/bun

## Overview

##### Core Components (Center
- Query Builder
- Model Mapper
- Schema Manager

##### Database Dialects Layer (Bottom)
- PostgreSQL
- MySQL
- MSSQL
- SQLite
- Oracle

##### Feature Modules (Top)
- Features in separate modules:
- Migration System (/migrate)
- Fixtures (/dbfixture)
- Schema Management (/schema)
- Query Building (/query_*)

##### Integration Layer (Right Side)
- External integrations:
- OpenTelemetry Support
- Logging Systems
- Monitoring Tools

##### Driver Layer (Left Side)
- Database drivers:
- pgdriver
- sqliteshim

##### Relationships and Data Flow
- Arrowheads to show data flow direction

##### Component Details
- Core:
- SQL Query Building
- Model Mapping
- Relationship Management
- Hook System

- Dialect Layer:
- SQL Translation
- Type Mapping
- Feature Detection

- Migration:
- Version Control
- Auto Migration
- Schema Diffing

##### Additional Elements
- Version information
- Supported Go versions
- Third-party dependencies
- Experimental features

- The SQL-first approach
- Multi-database support
- Extensibility points
- Integration capabilities
- Clear separation of concerns

Remember to include notes about:
- Transaction handling
- Connection pooling
- Query optimization
- Error handling paths
- Hook system integration points

This will provide a comprehensive view of Bun's architecture while highlighting its key features and integration capabilities.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Core
        QB["Query Builder"]:::core
        MM["Model Mapper"]:::core
        SM["Schema Manager"]:::core
        HS["Hook System"]:::core
        CDB["Core DB"]:::core
        
        QB --> MM
        MM --> SM
        HS --> CDB
        QB --> CDB
        MM --> CDB
    end

    subgraph "Feature Modules"
        MS["Migration System"]:::feature
        FX["Fixtures"]:::feature
        SH["Schema Management"]:::feature
        QB_M["Query Building"]:::feature
    end

    subgraph "Integration Layer"
        OT["OpenTelemetry"]:::integration
        LS["Logging Systems"]:::integration
        MT["Monitoring Tools"]:::integration
    end

    subgraph "Driver Layer"
        PGD["pgdriver"]:::driver
        SLS["sqliteshim"]:::driver
    end

    subgraph "Database Dialects"
        PG["PostgreSQL"]:::db
        MY["MySQL"]:::db
        MS["MSSQL"]:::db
        SL["SQLite"]:::db
        OR["Oracle"]:::db
    end

    %% Core Connections
    MS --> SM
    FX --> MM
    SH --> SM
    QB_M --> QB

    %% Integration Connections
    OT -.-> CDB
    LS -.-> CDB
    MT -.-> CDB

    %% Driver Connections
    PGD --> PG
    SLS --> SL

    %% Dialect Connections
    CDB <--> PG
    CDB <--> MY
    CDB <--> MS
    CDB <--> SL
    CDB <--> OR

    %% Click Events
    click QB "https://github.com/uptrace/bun/blob/master//query_*.go"
    click MM "https://github.com/uptrace/bun/blob/master//model.go"
    click SM "https://github.com/uptrace/bun/tree/master//schema"
    click HS "https://github.com/uptrace/bun/blob/master//hook.go"
    click CDB "https://github.com/uptrace/bun/blob/master//db.go"
    click MS "https://github.com/uptrace/bun/tree/master//migrate"
    click FX "https://github.com/uptrace/bun/tree/master//dbfixture"
    click SH "https://github.com/uptrace/bun/tree/master//schema"
    click QB_M "https://github.com/uptrace/bun/blob/master//query_*.go"
    click OT "https://github.com/uptrace/bun/tree/master//extra/bunotel"
    click LS "https://github.com/uptrace/bun/tree/master//extra/bunzerolog"
    click PGD "https://github.com/uptrace/bun/tree/master//driver/pgdriver"
    click SLS "https://github.com/uptrace/bun/tree/master//driver/sqliteshim"
    click PG "https://github.com/uptrace/bun/tree/master//dialect/pgdialect"
    click MY "https://github.com/uptrace/bun/tree/master//dialect/mysqldialect"
    click MS "https://github.com/uptrace/bun/tree/master//dialect/mssqldialect"
    click SL "https://github.com/uptrace/bun/tree/master//dialect/sqlitedialect"
    click OR "https://github.com/uptrace/bun/tree/master//dialect/oracledialect"

    classDef core fill:#3498db,stroke:#2980b9,color:white
    classDef feature fill:#e67e22,stroke:#d35400,color:white
    classDef integration fill:#9b59b6,stroke:#8e44ad,color:white
    classDef driver fill:#f1c40f,stroke:#f39c12,color:black
    classDef db fill:#2ecc71,stroke:#27ae60,color:white

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Feature Modules"]:::feature
        L3["Integrations"]:::integration
        L4["Drivers"]:::driver
        L5["Databases"]:::db
    end
```
