GCP Engineer: Generating Automated Entity-Relationship Diagrams Dynamically from Google Cloud NDB Schemas
Schemaless NoSQL databases like Google Cloud Datastore and Firestore give engineering teams rapid iteration velocity, but over multi-year lifecycles, undocumented google.cloud.ndb models with implicit KeyProperty relationships and StructuredProperty sub-entities become opaque to new engineers. Manually drawn architecture diagrams drift out of date within weeks. As a Google Cloud Platform (GCP) Engineer and Backend Engineer, I build automated schema introspection tools that inspect live ndb.Model registries in CI/CD and emit always-up-to-date Mermaid ER diagrams (`erDiagram`).
1. Introspecting Cloud NDB Model Properties & Key References Programmatically
Every ndb.Model subclass maintains an internal _properties dictionary mapping datastore attribute names to typed property descriptors (StringProperty, IntegerProperty, KeyProperty(kind=...), and StructuredProperty). We traverse all registered models and extract both scalar columns and foreign-key kind cardinalities:
from google.cloud import ndb
def generate_mermaid_er_from_ndb_models(models: list[type[ndb.Model]]) -> str:
lines = ["erDiagram"]
relationships: list[str] = []
for model_cls in models:
kind_name = model_cls._get_kind()
lines.append(f" {kind_name} {{")
for attr_name, prop in sorted(model_cls._properties.items()):
prop_type = type(prop).__name__.replace("Property", "").lower()
cardinality = "repeated" if prop._repeated else "single"
lines.append(f" {prop_type} {attr_name} "{cardinality}"")
if isinstance(prop, ndb.KeyProperty) and prop._kind:
target_kind = prop._kind
rel_arrow = "}o--||" if not prop._repeated else "}o--o{"
relationships.append(
f" {kind_name} {rel_arrow} {target_kind} : "{attr_name}""
)
elif isinstance(prop, ndb.StructuredProperty):
sub_kind = prop._modelclass._get_kind()
relationships.append(
f" {kind_name} ||--o{{ {sub_kind} : "embeds {attr_name}""
)
lines.append(" }")
lines.extend(sorted(set(relationships)))
return "
".join(lines)
2. Enforcing Index & Schema Governance in CI Pipelines
Beyond generating visual Mermaid ER diagrams for engineering documentation portals, the same introspection pass verifies that large JsonProperty or TextProperty fields explicitly declare indexed=False to prevent Cloud Datastore index bloat—complementing our Zero-Downtime NoSQL Attribute Renaming guide and NoSQL Schema Modeling & Fast Reference Joins.
Need a GCP Engineer to document, audit, or modernize your Google Cloud Datastore and NDB architecture? Book a 1-Day Engineering Sprint.