Skip to content

Instantly share code, notes, and snippets.

@abrookins
Created November 19, 2021 15:51
Show Gist options
  • Save abrookins/7d3a1b007537806ad8ca9727824622cc to your computer and use it in GitHub Desktop.
Save abrookins/7d3a1b007537806ad8ca9727824622cc to your computer and use it in GitHub Desktop.
Declarative Models with Redis OM
import datetime
from typing import Optional
from pydantic import EmailStr
from redis_om import HashModel
class Customer(HashModel):
first_name: str
last_name: str
email: EmailStr
join_date: datetime.date
age: int
bio: Optional[str]
# First, we create a new `Customer` object:
andrew = Customer(
first_name="Andrew",
last_name="Brookins",
email="[email protected]",
join_date=datetime.date.today(),
age=38,
bio="Python developer, works at Redis, Inc."
)
# The model generates a globally unique primary key automatically
# without needing to talk to Redis.
print(andrew.pk)
# > '01FJM6PH661HCNNRC884H6K30C'
# We can save the model to Redis by calling `save()`:
andrew.save()
# To retrieve this customer with its primary key, we use `Customer.get()`:
assert Customer.get(andrew.pk) == andrew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment