sqlatypemodel

Tests Linting PyPi status PyPi Package Version Python versions Downloads MIT License

Typed JSON fields for SQLAlchemy with automatic mutation tracking

sqlatypemodel solves the “immutable JSON” problem in SQLAlchemy. It allows you to use strictly typed Python objects (Pydantic, Dataclasses, Attrs) as database columns while ensuring that every change—no matter how deep—is automatically saved.

Powered by orjson for blazing-fast performance and featuring a State-Based Architecture for universal compatibility.

Key Features

  • State-Based Tracking: Universal compatibility with unhashable objects (Pydantic, Dataclasses).

  • Maximum Performance: 40%+ speedup in hot paths via direct attribute access and dispatch optimization.

  • Lazy Loading: 2.1x faster DB loading and 35% less memory usage.

  • Pickle & Celery Ready: Full support for serialization and caching.

  • High Performance: Powered by orjson with smart caching.

  • Deep Mutation Tracking: Automatically detects nested changes.

The Problem

By default, SQLAlchemy considers JSON columns immutable unless you replace the entire object.

# ❌ NOT persisted by default in SQLAlchemy
user.settings.theme = "dark"
user.settings.tags.append("new")

session.commit() # Nothing happens! Data is lost.

The Solution

With sqlatypemodel, in-place mutations are tracked automatically:

# ✅ Persisted automatically
user.settings.theme = "dark"
user.settings.tags.append("new")

session.commit() # UPDATE "users" SET settings = ...

Resources

License

MIT