# clemo97/pv-site-datamodel architecture

> Architecture diagram of the GitHub repository clemo97/pv-site-datamodel, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/clemo97/pv-site-datamodel
- Repository: https://github.com/clemo97/pv-site-datamodel

## Overview

##### Main Components
- Database Connection: The module (pvsite_datamodel/connection.py) encapsulates how clients connect to the database.
- Read Module: Found in pvsite_datamodel/read, this package provides functions to query and retrieve data from the database (e.g., get_user_by_email, get_latest_forecast_values_by_site) using the defined models.
- Write Module: Located in pvsite_datamodel/write, this package offers functions to insert and update records in the database (e.g., insert_forecast_values, create_site).
- Database Migrations: The alembic directory (and associated configuration and migration scripts) handles the evolution of the database schema over time.
- Scripts and Tests: The scripts directory includes utilities to run specific operations (like adding sites or downloading data), and the tests directory confirms the functioning of both read and write operations. These ensure consistency and reliability across database transactions.

##### Relationships and Interactions
- The main data flow starts with establishing a connection (via DatabaseConnection) from the pvsite_datamodel core.
- Both the Read and Write modules use this connection and the SQLAlchemy model definitions to communicate with the physical database.
- Alembic migrations serve as a bridge between code changes (updates in the model definitions) and actual changes in the database schema.
- Utility scripts often act as entry points that call into these read/write functions to perform specific tasks like seeding data or updating records.
- The test suite validates the interactions between these components, ensuring robustness.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The codebase clearly delineates responsibilities between components (e.g., one package for reading, another for writing, and a dedicated module for database connection).
- Repository/DAO Pattern: The Read and Write modules act as Data Access Objects, abstracting the underlying database details from application logic.
- Migration Management: Alembic is used effectively to manage versioning of the database schema.
- Use of ORM: SQLAlchemy simplifies complex SQL interactions by mapping Python classes to database tables.

##### Relevant Technologies, Frameworks, and Libraries
- Python (as the primary development language).
- SQLAlchemy (for ORM-based interaction with the database).
- Alembic (for handling database migrations).
- Pydantic (as indicated by the presence of a pydantic_models.py, likely for data validation).
- Poetry (for dependency management).
- Makefile scripts for common developer tasks like formatting, linting, and testing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Developer Tools (Top Layer)
    subgraph "Developer Tools"
        E["Alembic Migrations"]:::tools
        F["Scripts"]:::tools
        G["Tests"]:::tools
    end

    %% Core Logic (Middle Layer)
    subgraph "Core Logic"
        B["Database Connection"]:::core
        A["Database Schema (SQLAlchemy Models)"]:::core
        C["Read Module"]:::core
        D["Write Module"]:::core
    end

    %% Physical Database (Bottom Layer)
    subgraph "Physical Database"
        H[(Physical Database)]:::db
    end

    %% Relationships within Core Logic and to Physical Database
    B -->|"uses"| A
    C -->|"queries"| A
    D -->|"updates"| A

    B -->|"connectsTo"| H
    A -->|"mapsTo"| H
    C -->|"readsFrom"| H
    D -->|"writesTo"| H

    %% Relationships from Developer Tools to Core Logic
    E -->|"migrates"| A
    F -->|"triggersRead"| C
    F -->|"triggersWrite"| D
    G -->|"testsConnection"| B
    G -->|"testsRead"| C
    G -->|"testsWrite"| D
    G -->|"testsModels"| A

    %% Click Events
    click A "https://github.com/clemo97/pv-site-datamodel/blob/main/pvsite_datamodel/sqlmodels.py"
    click B "https://github.com/clemo97/pv-site-datamodel/blob/main/pvsite_datamodel/connection.py"
    click C "https://github.com/clemo97/pv-site-datamodel/tree/main/pvsite_datamodel/read/"
    click D "https://github.com/clemo97/pv-site-datamodel/tree/main/pvsite_datamodel/write/"
    click E "https://github.com/clemo97/pv-site-datamodel/tree/main/alembic/"
    click F "https://github.com/clemo97/pv-site-datamodel/tree/main/scripts/"
    click G "https://github.com/clemo97/pv-site-datamodel/tree/main/tests/"

    %% Styles
    classDef core fill:#FFDDCC,stroke:#333,stroke-width:2px;
    classDef tools fill:#CCFFCC,stroke:#333,stroke-width:2px;
    classDef db fill:#CCCCFF,stroke:#333,stroke-width:2px;
```
