Skip to main content

Pydantic Models for Redis

Project description

Pydantic Models for Redis

This repository allows you to create Pydantic models representing Redis objects, allowing your models to follow a schema with validation and serialization.

The Redis models are async and have ORM-like operations.

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 typed_redis import Store
from redis.asyncio import Redis

redis = Redis(...)

class User(Store(redis)):
    """User model."""

    id: int
    name: str

    @property
    def redis_key(self) -> str:
        return f"user:{self.id}"


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

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

# Later:
user = await user.get("user:1")
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, pass it into your Pydantic classes by inheritting from it. Add a redis_key property to return the string that should be used as the Redis key.

user.py

from .store import Store

class User(Store):
    """User model."""

    id: int
    name: str

    @property
    def redis_key(self) -> str:
        return f"user:{self.id}"

Use Your Model

Now you can use your model:

from .user import User

# Get existing user
user = await user.get("user: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 Underlying Redis Notes
Create await instance.create(**kwargs) await user.create(ex=60) SET key value [EX seconds] [PX milliseconds] [NX] Serializes with model_dump_json() and stores at redis_key. Optional ex, px, nx are forwarded.
Upsert (call) await instance() await user() SET key value Same as create() with default options.
Update await instance.update(**changes) await user.update(name="Charlie Brown") SET key value Validates via Pydantic model_copy(update=...), then persists. Returns the updated model.
Get await Model.get(key) user = await User.get("user:1") GET key Parses JSON using model_validate_json(...) into your model.
Delete await instance.delete() await user.delete() DEL key Removes the key at redis_key.

Notes

  • Your model must implement a redis_key property returning the key string.
  • Bind a Redis client via Store(redis_client) and inherit from it; otherwise, operations raise a RuntimeError.

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.4.tar.gz (3.8 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.4-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typed_redis-0.0.4.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.10.11 Darwin/24.5.0

File hashes

Hashes for typed_redis-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8f9f0545babaaa8c1f7730634404a846e9c49cb48a34edf072049a1558bf1b22
MD5 9d65aa463c3b60463c7ff57a9789c0a1
BLAKE2b-256 44278b9271c0eb2f906f8a701fc7b3a95345a1634410f5036d6f43edfb12e687

See more details on using hashes here.

File details

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

File metadata

  • Download URL: typed_redis-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.10.11 Darwin/24.5.0

File hashes

Hashes for typed_redis-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 97ee8600ea9d1b77f54eefafe3d254d9f02a92f966abfc3d7bf7dabe08a82200
MD5 7bfc9bb37d6e8272f161f8fc0bdaf6fc
BLAKE2b-256 0b8d7e75a61d7c406c27387a259d9e47ff2b1da19641d846e6a0c4f8706b4a86

See more details on using hashes here.

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