WIP on README
parent
09c91fb756
commit
7c0bea751b
@ -0,0 +1,72 @@
|
||||
import datetime
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import EmailStr
|
||||
|
||||
from redis_om.model import (
|
||||
HashModel
|
||||
)
|
||||
|
||||
|
||||
class Customer(HashModel):
|
||||
first_name: str
|
||||
last_name: str
|
||||
email: EmailStr
|
||||
join_date: datetime.date
|
||||
age: int
|
||||
bio: Optional[str]
|
||||
|
||||
|
||||
# Now we can create new Customer objects:
|
||||
andrew = Customer(
|
||||
first_name="Andrew",
|
||||
last_name="Brookins",
|
||||
email="andrew.brookins@example.com",
|
||||
join_date=datetime.date.today(),
|
||||
age=38,
|
||||
bio="Python developer, works at Redis, Inc."
|
||||
)
|
||||
|
||||
# The model generates a globally unique primary key automatically.
|
||||
print(andrew.pk)
|
||||
# '01FJM6PH661HCNNRC884H6K30C'
|
||||
|
||||
# You can save the model to Redis.
|
||||
andrew.save()
|
||||
|
||||
# Later, you can retrieve this customer with its primary key:
|
||||
other_andrew = Customer.get('01FJM6PH661HCNNRC884H6K30C')
|
||||
|
||||
# The original model and this one pass an equality check.
|
||||
assert other_andrew == andrew
|
||||
|
||||
|
||||
# We'll get a validation error if we try to use an invalid email address!
|
||||
Customer(
|
||||
first_name="Andrew",
|
||||
last_name="Brookins",
|
||||
email="Not an email address!",
|
||||
join_date=datetime.date.today(),
|
||||
age=38,
|
||||
bio="Python developer, works at Redis, Inc."
|
||||
)
|
||||
|
||||
# We'll also get a validation error if we try to save a model
|
||||
# instance with an invalid email.
|
||||
andrew = Customer(
|
||||
first_name="Andrew",
|
||||
last_name="Brookins",
|
||||
email="andrew.brookins@example.com",
|
||||
join_date=datetime.date.today(),
|
||||
age=38,
|
||||
bio="Python developer, works at Redis, Inc."
|
||||
)
|
||||
|
||||
# Sometime later...
|
||||
andrew.email = "Not valid"
|
||||
andrew.save()
|
||||
|
||||
# Traceback:
|
||||
# pydantic.error_wrappers.ValidationError: 1 validation error for Customer
|
||||
# email
|
||||
# value is not a valid email address (type=value_error.email)
|
@ -0,0 +1,30 @@
|
||||
# Redis Modules
|
||||
|
||||
Some advanced features of Redis OM, like rich query expressions and saving data as JSON, rely on core features from two source available Redis modules: **RediSearch** and **RedisJSON**.
|
||||
|
||||
These modules are the "magic" behind the scenes:
|
||||
|
||||
* RediSearch adds querying, indexing, and full-text search to Redis
|
||||
* RedisJSON adds the JSON data type to Redis
|
||||
|
||||
## Why this is important
|
||||
|
||||
Without RediSearch or RedisJSON installed, you can still use Redis OM to create declarative models backed by Redis.
|
||||
|
||||
We'll store your model data in Redis as Hashes, and you can retrieve models using their primary keys. You'll also get all the validation features from Pydantic.
|
||||
|
||||
So, what won't work without these modules?
|
||||
|
||||
1. Without RedisJSON, you won't be able to nest models inside each other, like we did with the example model of a `Customer` model that has an `Address` embedded inside it.
|
||||
2. Without RediSearch, you won't be able to use our expressive queries to find models -- just primary keys.
|
||||
|
||||
## So how do you get RediSearch and RedisJSON?
|
||||
|
||||
You can use RediSearch and RedisJSON with your self-hosted Redis deployment. Just follow the instructions on installing the binary versions of the modules in their Quick Start Guides:
|
||||
|
||||
- [RedisJSON Quick Start - Running Binaries](https://oss.redis.com/redisjson/#download-and-running-binaries)
|
||||
- [RediSearch Quick Start - Running Binaries](https://oss.redis.com/redisearch/Quick_Start/#download_and_running_binaries)
|
||||
|
||||
**NOTE**: Both Quick Start Guides also have instructions on how to run these modules in Redis with Docker.
|
||||
|
||||
Don't want to run Redis yourself? RediSearch and RedisJSON are also available on Redis Cloud. [Get started here.](https://redis.com/try-free/)
|
@ -0,0 +1,50 @@
|
||||
import abc
|
||||
import datetime
|
||||
from collections import namedtuple
|
||||
|
||||
import pytest
|
||||
from pydantic import EmailStr, ValidationError
|
||||
|
||||
from redis_om.model import HashModel, Field
|
||||
from redis_om.model.migrations.migrator import Migrator
|
||||
|
||||
|
||||
today = datetime.date.today()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def m(key_prefix):
|
||||
class BaseHashModel(HashModel, abc.ABC):
|
||||
class Meta:
|
||||
global_key_prefix = key_prefix
|
||||
|
||||
class Member(BaseHashModel):
|
||||
first_name: str
|
||||
last_name: str
|
||||
email: EmailStr = Field(index=True)
|
||||
join_date: datetime.date
|
||||
age: int
|
||||
|
||||
Migrator().run()
|
||||
|
||||
return namedtuple("Models", ["Member"])(Member)
|
||||
|
||||
|
||||
def test_email_str(m):
|
||||
with pytest.raises(ValidationError):
|
||||
m.Member(
|
||||
first_name="Andrew",
|
||||
last_name="Brookins",
|
||||
email="not an email!",
|
||||
age=38,
|
||||
join_date=today,
|
||||
)
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
m.Member(
|
||||
first_name="Andrew",
|
||||
last_name="Brookins",
|
||||
email="andrew@bad-domain",
|
||||
age=38,
|
||||
join_date=today,
|
||||
)
|
Loading…
Reference in New Issue