# maheshbabugorantla/dbtodrf architecture

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

- Interactive diagram: https://gitdiagram.com/maheshbabugorantla/dbtodrf
- Repository: https://github.com/maheshbabugorantla/dbtodrf

## Overview

##### Project type and purpose
- This is an open-source CLI tool (“drf-auto-generator”) whose sole purpose is to read a relational database schema and emit a fully configured Django + Django REST Framework project (models, serializers, views, URLs, admin, OpenAPI docs, tests).

##### File structure
- Top-level:
- drf_auto_generator/ – the Python package;
- examples/ – sample database + config to demonstrate usage;
- tests/ – unit & integration tests;
- config.sample.yaml, Makefile, pyproject.toml – build & configuration scaffolding.
- drf_auto_generator submodules:
- cli.py & ast_codegen_main.py – entrypoints;
- config_manager.py & config_validation.py – parse & validate YAML;
- introspection_django.py – read DB schema via Django;
- domain/ – in-memory representation of tables, fields, relationships;
- ast_codegen/ – classes to construct Python AST nodes for models, serializers, views, urls, admin, tests, project files;
- templates/ – Jinja2 templates for project boilerplate;
- codegen_utils.py, mapper.py, openapi_gen.py – helpers for mapping types, generating OpenAPI specs;
- colored_logging.py – CLI logging;
- exceptions.py, constants.py, validators.py – support.

##### Insights from the README
- Architecture is layered:
1. CLI reads config
2. Database introspection → domain model
3. AST-based code generator + template renderer → Django project
4. Write files to disk, run tests
- Supports extension points: custom field mapping, relation styles, test generation.

##### Final recommendation
- Be exhaustive in breaking out each submodule (e.g., show astro_codegen/admin.py, serializers.py as subcomponents of “AST Codegen”)
- With technologies & data formats (YAML, SQL, Python AST, Jinja2)
- The result will clearly reflect how a user’s config + DB drives code generation and file emission.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI Frontend
    subgraph "CLI Frontend"
        direction TB
        CLI["CLI (drf-generate)"]:::frontend
        Main["AST Codegen Main"]:::frontend
    end

    %% Configuration Manager & Validator
    subgraph "Configuration Layer"
        direction TB
        ConfigManager["Config Manager"]:::processing
        ConfigValidator["Config Validator"]:::processing
    end

    %% Database Introspection
    subgraph "Introspection Layer"
        direction TB
        Introspect["Database Introspection Module"]:::processing
    end

    %% Domain Model Layer
    subgraph "Domain Model Layer"
        direction TB
        DMModels["models.py"]:::processing
        DMRelationships["relationships.py"]:::processing
        DMConstraints["constraints.py"]:::processing
        DMFieldMapping["field_mapping.py"]:::processing
        DMNaming["naming.py"]:::processing
    end

    %% AST Code Generator
    subgraph "AST Code Generator"
        direction TB
        ASTBase["base.py"]:::processing
        ASTCodeGen["code_generator.py"]:::processing
        ASTModels["models.py"]:::processing
        ASTSerializers["serializers.py"]:::processing
        ASTViews["views.py"]:::processing
        ASTUrls["urls.py"]:::processing
        ASTAdmin["admin.py"]:::processing
        ASTProjectFiles["project_files.py"]:::processing
        ASTSchemathesis["schemathesis_tests.py"]:::processing
    end

    %% Template Renderer
    subgraph "Template Renderer"
        direction TB
        TEnv[".env.j2"]:::rendering
        TGitignore[".gitignore.j2"]:::rendering
        TInit["__init__.py.j2"]:::rendering
        TRequirements["requirements.txt.j2"]:::rendering
    end

    %% File-System Writer / Orchestration
    subgraph "Output Layer"
        direction TB
        Writer["codegen.py"]:::rendering
        TestOrch["generate_tests_using_ast.py"]:::rendering
        Output["Generated Django Project"]:::rendering
    end

    %% External Systems
    RelDB["Relational Database"]:::external
    Django["Django ORM"]:::external
    Jinja2["Jinja2 Templates"]:::external
    DRF["Django REST Framework"]:::external
    Schemathesis["Schemathesis"]:::external

    %% Flow
    CLI -->|"invoke"| ConfigManager
    CLI -->|"invoke"| Main
    Main -->|"load config"| ConfigValidator
    ConfigValidator --> ConfigManager
    ConfigManager -->|"config object"| Introspect
    Introspect -->|"schema"| DMModels
    DMModels --> DMRelationships
    DMRelationships --> DMConstraints
    DMConstraints --> DMFieldMapping
    DMFieldMapping --> DMNaming
    DMNaming --> ASTBase
    ASTBase --> ASTCodeGen
    ASTCodeGen --> ASTModels
    ASTCodeGen --> ASTSerializers
    ASTCodeGen --> ASTViews
    ASTCodeGen --> ASTUrls
    ASTCodeGen --> ASTAdmin
    ASTCodeGen --> ASTProjectFiles
    ASTCodeGen --> ASTSchemathesis
    ASTProjectFiles --> Writer
    TEnv --> Writer
    TGitignore --> Writer
    TInit --> Writer
    TRequirements --> Writer
    Writer --> Output
    TestOrch --> Output

    %% External integrations
    RelDB --> Introspect
    Django --> Introspect
    ASTCodeGen --> DRF
    TEnv --> Jinja2
    TGitignore --> Jinja2
    TInit --> Jinja2
    TRequirements --> Jinja2
    ASTSchemathesis --> Schemathesis
    TestOrch --> Schemathesis

    %% Click Events
    click CLI "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/cli.py"
    click Main "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/ast_codegen_main.py"
    click ConfigManager "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/config_manager.py"
    click ConfigValidator "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/config_validation.py"
    click Introspect "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/introspection_django.py"
    click DMModels "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/domain/models.py"
    click DMRelationships "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/domain/relationships.py"
    click DMConstraints "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/domain/constraints.py"
    click DMFieldMapping "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/domain/field_mapping.py"
    click DMNaming "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/domain/naming.py"
    click ASTBase "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/ast_codegen/base.py"
    click ASTCodeGen "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/ast_codegen/code_generator.py"
    click ASTModels "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/ast_codegen/models.py"
    click ASTSerializers "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/ast_codegen/serializers.py"
    click ASTViews "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/ast_codegen/views.py"
    click ASTUrls "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/ast_codegen/urls.py"
    click ASTAdmin "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/ast_codegen/admin.py"
    click ASTProjectFiles "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/ast_codegen/project_files.py"
    click ASTSchemathesis "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/ast_codegen/schemathesis_tests.py"
    click TEnv "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/templates/.env.j2"
    click TGitignore "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/templates/.gitignore.j2"
    click TInit "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/templates/__init__.py.j2"
    click TRequirements "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/templates/requirements.txt.j2"
    click Writer "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/codegen.py"
    click TestOrch "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/generate_tests_using_ast.py"
    click codegen_utils "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/codegen_utils.py"
    click mapper "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/mapper.py"
    click openapi_gen "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/openapi_gen.py"
    click logging "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/colored_logging.py"
    click exceptions "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/exceptions.py"
    click constants "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/constants.py"
    click validators "https://github.com/maheshbabugorantla/dbtodrf/blob/main/drf_auto_generator/validators.py"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef processing fill:#90EE90,stroke:#333,stroke-width:1px
    classDef rendering fill:#FFA500,stroke:#333,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px
```
