Google Cloud Datastore ODM¶
A modern, fully-typed, Object Document Mapper for Google Cloud Datastore.
Built for modern Python, this ODM bridges the gap between the raw google-cloud-datastore client and the developer-friendly ergonomics of the legacy App Engine NDB library.
It features a declarative property system, intuitive AST-based query building, lightning-fast server-side aggregations, and intelligent ACID transactions with automatic concurrency retries.
A Taste of the ODM¶
Stop wrestling with raw dictionaries. Define strict schemas and query them using pure, Pythonic syntax:
from google_cloud_datastore_odm import Model, StringProperty, IntegerProperty, Sum
class Player(Model):
name = StringProperty(required=True)
team = StringProperty(choices=["red", "blue"])
score = IntegerProperty(default=0)
# Create and save natively
player = Player(name="Alice", team="red", score=150)
player.put()
# Query using standard Python operators
top_red_players = Player.query().filter(Player.team == "red", Player.score > 100).fetch()
# Perform lightning-fast server-side aggregations
stats = Player.query().aggregate(total_score=Sum(Player.score))
print(f"Total points scored: {stats['total_score']}")
Key Features¶
🏗️ Advanced Data Modeling¶
- NDB Muscle Memory: Familiar API including
.put(),.get(),.query(), and_pre_puthooks. - Model and fields Validation: Built-in type checking, property-level constraints (
required=True,choices), and custom@field_validatorand@model_validatordecorators. - Complex Data Types: Natively embed full models using
StructuredProperty, or store dynamic data withJsonPropertyandPickleProperty. - Cost Optimization: Automatically bypass Datastore's 1500-byte index limits and compress massive text or binary payloads using
compressed=True.
🔍 Powerful Querying¶
- Pythonic AST Queries: Chain filters using standard operators (
==,>=,!=) and bitwise logic (&,|). - Server-Side Aggregations: Delegate heavy math to Google's backend using
Count,Sum, andAvg. - Cost-Effective Fetching: Utilize
.projection(),.distinct_on(), and.keys_only()to minimize read costs. - Cursor Pagination: Easily build paginated APIs using native
.fetch_page().
🛡️ Enterprise Grade¶
- Smart Transactions: Execute ACID-compliant operations using the
with transaction():context manager, or use the@transactional(retries=5)decorator to automatically handle optimistic concurrency collisions. - Multi-Tenancy & Routing: Route data dynamically across different GCP Projects, Databases, and Namespaces using the
Metaclass or ad-hoc instance kwargs. - Zero-Overhead Batching: Native support for
put_multi,get_multi, anddelete_multito maximize network efficiency.