2021-12-31 10:00:00+00:00

In schema-less databases like Cloud Datastore, database structures are defined inside Python code models (using GAE's NDB library). As code bases grow, understanding relationships and attributes across entities becomes difficult. Creating visual UML class diagrams manually is error-prone. A robust solution uses static analysis tools that parse Python source files and generate Graphviz class diagrams automatically.

By writing Python tools that inspect NDB property objects, we can generate dynamic database schemas for documentation.


1. Programmatic Inspection of NDB Classes

We write a script that imports our database models, parses their property attributes, and outputs Graphviz DOT syntax:

# generate_schema_uml.py
from google.cloud import ndb

def export_ndb_to_dot(model_classes):
    dot_lines = ["digraph NDB_Schema {", "  node [shape=record, fontname=Helvetica];"]
    
    for cls in model_classes:
        name = cls.__name__
        properties = []
        
        # Loop through NDB properties
        for prop_name, prop in cls._properties.items():
            prop_type = type(prop).__name__
            properties.append(f"{prop_name}: {prop_type}")
            
        fields_str = "\n".join(properties)
        dot_lines.append(f'  {name} [label="{{{name}|{fields_str}}}"];')
        
    dot_lines.append("}")
    return "\n".join(dot_lines)

2. Executing Schema Compilation in CI/CD

The compiled DOT strings are written to text files and processed using Graphviz CLI commands (dot -Tpng schema.dot -o schema.png), keeping repository documentation updated with every schema commit.

Automated Entity-Relationship Diagram Generation from NDB & Django Models

Introspect Python ORM model ASTs in CI to keep architecture documentation and schema diagrams continuously synchronized with production code.


About the Author — Hire Freelance Programmer Aldian Fazrihady

Aldian Fazrihady is a Top-Rated Senior Freelance Programmer, Freelance Full Stack Developer, and AI Application Engineer with 15+ years of production software engineering experience and Google Cloud Professional Cloud Architect & Data Engineer certifications. He specializes in building custom LLM & autonomous AI agents, RAG pipelines, Next.js/React PWAs, Python (Django/FastAPI), Go microservices, and serverless cloud infrastructure on Google Cloud Run and AWS.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD via Polar) | View Live AI Portfolio | Portfolio Directory | Send a Direct Message

Related Engineering Articles

Freelance Programmer Specializations & Category Hubs