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)

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.3.tar.gz (3.4 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.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typed_redis-0.0.3.tar.gz
  • Upload date:
  • Size: 3.4 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.3.tar.gz
Algorithm Hash digest
SHA256 c69b844f0cea6f94854794a623179b064213275503895a409630707717bfb768
MD5 09050be60b9a7cdd345736f2b8f66096
BLAKE2b-256 50b148050f8e376000a084f14e2dab7a24af69ac674231571a6a110eff5575c8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: typed_redis-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.3 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1a7e3b00dd58ed9541a1c86f69c2d4dbbfb07f8603f7e83bad48d3edbb80ed96
MD5 a02761de2c18a7747b3ad83edd11f1ce
BLAKE2b-256 388b9c54cd5ad5bcb32d76e14f12987f67b465fdae754b60ba1b150f607948df

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