Skip to main content

Pydantic Models for Redis

Project description

Pydantic Models for Redis

Coverage

Typed Redis provides strongly typed, Pydantic-based models for Redis with built-in validation and serialization.

It offers an async, ORM-like API for creating, retrieving, updating, and deleting data stored in Redis.

Installation

Install with pip

pip install typed_redis

Features

  • Add a schema to Redis models with validation and serialization
  • Async support
  • ORM-like syntax

Example

from typing import Annotated
from typed_redis import Store, PrimaryRedisKey
from redis.asyncio import Redis

redis = Redis(...)

class User(Store(redis), model_name="user"):
    """User model."""

    id: Annotated[int, PrimaryRedisKey]
    name: str


user = User(id=1, name="Charlie")

await user.create()  # Store user object in Redis

# Later:
user = await User.get(1)  # Look up by primary key value
print(user.name)  # "Charlie"

Documentation

Create Store

The Store function takes in your Redis instance and returns back a base class with the ORM operations.

Create a Store:

store.py

from redis.asyncio import Redis
from typed_redis import Store as _Store

redis = Redis(...)

Store = _Store(redis)

Create Model

Using your Store object created earlier, inherit from it and set a model_name class argument to prefix your Redis keys. Annotate one field as the primary key using PrimaryRedisKey. The Redis key is derived using the model name and field value.

user.py

from .store import Store

class User(Store, model_name="user"):
    """User model."""

    id: Annotated[int, PrimaryRedisKey]
    name: str

Use Your Model

Now you can use your model:

from .user import User

# Get existing user by primary key value
user = await User.get(1)
print(user.name)

# Create new user (idempotent)
new_user = User(id=2, name="Bob")
await new_user() # Same as calling await user.create(...)

print(user.name)

# Update user:
await new_user.update(name="Bob Smith")
print(user.name)

Supported Operations

Operation Method Example Notes
Create await instance.create(**kwargs) or await instance(**kwargs) await user.create(ex=60) or await user(ex=60) Serializes with model_dump_json() and stores in Redis. Optional kwargs are passed to Redis.
Update await instance.update(**changes) await user.update(name="Charlie Brown") Validates via Pydantic then persists to Redis.
Get await Model.get(primary_key) user = await User.get(1) Key is derived as <model_name>:<pk>. Parses JSON using model_validate_json(...) and returns the model.
Delete await instance.delete() await user.delete() Removes the model from Redis. No further operations are allowed after this is called.

Notes

  • Annotate exactly one field with PrimaryRedisKey.
  • Bind a Redis client via Store(redis_client) and inherit from it; otherwise, operations raise a RuntimeError.
  • Set the model name using the model_name class argument, e.g., class User(Store, model_name="user"):. This determines the Redis key prefix.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typed_redis-0.0.5.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typed_redis-0.0.5-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file typed_redis-0.0.5.tar.gz.

File metadata

  • Download URL: typed_redis-0.0.5.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for typed_redis-0.0.5.tar.gz
Algorithm Hash digest
SHA256 37f360d13e888e257b39d2fc19123323b4775e416c0caff29f4c4f988fb2511c
MD5 94ca1ab0e31feaaeb2b82c7cbe24e342
BLAKE2b-256 2396f88888e1737175f082047698a64dc3b9bb2b4cb6f8d0393ebd582f3830cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for typed_redis-0.0.5.tar.gz:

Publisher: publish-to-pypi.yml on krystal-compute/typed-redis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file typed_redis-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: typed_redis-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for typed_redis-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4cd04c08ba4a19a9baa81935ecd0119fa435a070c43a7f9118008a9cdde956c5
MD5 705298ce8ec661509a107f1235418bd2
BLAKE2b-256 5659abce1b770d06ef2ff708160a1a1d85e1fec391ce1c04c3aba7befcd52f4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for typed_redis-0.0.5-py3-none-any.whl:

Publisher: publish-to-pypi.yml on krystal-compute/typed-redis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page