Global prefix
This commit is contained in:
parent
ccad3de32d
commit
7f0fb15f19
4 changed files with 336 additions and 89 deletions
200
README.md
200
README.md
|
@ -14,119 +14,167 @@ redis-developer-python includes an ORM/ODM.
|
||||||
```pyhon
|
```pyhon
|
||||||
import decimal
|
import decimal
|
||||||
import datetime
|
import datetime
|
||||||
from typing import Optional
|
from typing import Optional, List
|
||||||
|
|
||||||
from redis import Redis
|
import redis
|
||||||
|
from pydantic import ValidationError
|
||||||
|
|
||||||
from redis_developer.orm import (
|
from redis_developer.orm import (
|
||||||
RedisModel,
|
RedisModel,
|
||||||
Field,
|
Field,
|
||||||
Relationship
|
Relationship,
|
||||||
)
|
)
|
||||||
|
|
||||||
db = Redis()
|
|
||||||
|
|
||||||
|
|
||||||
# Declarative model classes
|
# Declarative model classes
|
||||||
class BaseModel(RedisModel):
|
|
||||||
config:
|
class BaseRedisModel(RedisModel):
|
||||||
database = db
|
class Meta:
|
||||||
|
database = redis.Redis(password="my-password", decode_responses=True)
|
||||||
|
model_key_prefix = "redis-developer:"
|
||||||
|
|
||||||
|
|
||||||
class Address(BaseModel):
|
class Address(BaseRedisModel):
|
||||||
address_line_1: str
|
address_line_1: str
|
||||||
address_line_2: str
|
address_line_2: Optional[str]
|
||||||
city: str
|
city: str
|
||||||
country: str
|
country: str
|
||||||
postal_code: str
|
postal_code: str
|
||||||
|
|
||||||
|
|
||||||
class Order(BaseModel):
|
class Order(BaseRedisModel):
|
||||||
total: decimal.Decimal
|
total: decimal.Decimal
|
||||||
currency: str
|
currency: str
|
||||||
created_on: datetime.datetime
|
created_on: datetime.datetime
|
||||||
|
|
||||||
|
|
||||||
class Member(BaseModel):
|
class Member(BaseRedisModel):
|
||||||
# An auto-incrementing primary key is added by default if no primary key
|
first_name: str
|
||||||
# is specified.
|
last_name: str
|
||||||
id: Optional[int] = Field(default=None, primary_key=True)
|
email: str = Field(unique=True, index=True)
|
||||||
first_name: str
|
join_date: datetime.date
|
||||||
last_name: str
|
|
||||||
email: str = Field(unique=True, index=True)
|
|
||||||
zipcode: Optional[int]
|
|
||||||
join_date: datetime.date
|
|
||||||
|
|
||||||
# Creates an embedded document: stored as hash fields or JSON document.
|
# Creates an embedded document: stored as hash fields or JSON document.
|
||||||
address: Address
|
address: Address
|
||||||
|
|
||||||
# Creates a relationship to data in separate Hash or JSON documents.
|
# Creates a relationship to data in separate Hash or JSON documents.
|
||||||
orders: Relationship(Order, backref='recommended',
|
orders: Optional[List[Order]] = Relationship(back_populates='member')
|
||||||
field_name='recommended_by')
|
|
||||||
|
|
||||||
# Creates a self-relationship.
|
# Creates a self-relationship.
|
||||||
recommended_by: Relationship('Member', backref='recommended',
|
recommended_by: Optional['Member'] = Relationship(back_populates='recommended')
|
||||||
field_name='recommended_by')
|
|
||||||
|
|
||||||
class Meta:
|
class Meta(BaseRedisModel.Meta):
|
||||||
key_pattern = "member:{id}"
|
model_key_prefix = "member"
|
||||||
|
primary_key_pattern = ""
|
||||||
|
|
||||||
|
|
||||||
# Validation
|
# Validation
|
||||||
|
|
||||||
# Raises ValidationError: last_name is required
|
address = Address(
|
||||||
Member(
|
address_line_1="1 Main St.",
|
||||||
first_name="Andrew",
|
city="Happy Town",
|
||||||
zipcode="97086",
|
state="WY",
|
||||||
join_date=datetime.date.today()
|
postal_code=11111,
|
||||||
|
country="USA"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Raises ValidationError: last_name, address are required
|
||||||
|
try:
|
||||||
|
Member(
|
||||||
|
first_name="Andrew",
|
||||||
|
zipcode="97086",
|
||||||
|
join_date=datetime.date.today()
|
||||||
|
)
|
||||||
|
except ValidationError as e:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# Raises ValidationError: join_date is not a date
|
||||||
|
try:
|
||||||
|
Member(
|
||||||
|
first_name="Andrew",
|
||||||
|
last_name="Brookins",
|
||||||
|
join_date="yesterday"
|
||||||
|
)
|
||||||
|
except ValidationError as e:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
# Passes validation
|
# Passes validation
|
||||||
Member(
|
|
||||||
first_name="Andrew",
|
|
||||||
last_name="Brookins",
|
|
||||||
zipcode="97086",
|
|
||||||
join_date=datetime.date.today()
|
|
||||||
)
|
|
||||||
|
|
||||||
# Raises ValidationError: zipcode is not a number
|
|
||||||
Member(
|
|
||||||
first_name="Andrew",
|
|
||||||
last_name="Brookins",
|
|
||||||
zipcode="not a number",
|
|
||||||
join_date=datetime.date.today()
|
|
||||||
)
|
|
||||||
|
|
||||||
# Persist a model instance to Redis
|
|
||||||
member = Member(
|
member = Member(
|
||||||
first_name="Andrew",
|
first_name="Andrew",
|
||||||
last_name="Brookins",
|
last_name="Brookins",
|
||||||
zipcode="97086",
|
email="a@example.com",
|
||||||
join_date=datetime.date.today()
|
address=address,
|
||||||
|
join_date=datetime.date.today()
|
||||||
)
|
)
|
||||||
# Assign the return value to get any auto-fields filled in,
|
|
||||||
# like the primary key (if an auto-incrementing integer).
|
|
||||||
member = member.save()
|
|
||||||
|
|
||||||
# Hydrate a model instance from Redis using the primary key.
|
|
||||||
member = Member.get(d=1)
|
|
||||||
|
|
||||||
# Hydrate a model instance from Redis using a secondary index on a unique field.
|
# Save a model instance to Redis
|
||||||
member = Member.get(email="a.m.brookins@gmail.com")
|
|
||||||
|
|
||||||
# What if the field wasn't unique and there were two "a.m.brookins@gmail.com"
|
address.save()
|
||||||
# entries?
|
|
||||||
# This would raise a MultipleObjectsReturned error:
|
address2 = Address.get(address.pk)
|
||||||
|
assert address2 == address
|
||||||
|
|
||||||
|
|
||||||
|
# Save a model with relationships (TODO!)
|
||||||
|
|
||||||
|
member.save()
|
||||||
|
|
||||||
|
|
||||||
|
# Save many model instances to Redis
|
||||||
|
today = datetime.date.today()
|
||||||
|
members = [
|
||||||
|
Member(
|
||||||
|
first_name="Andrew",
|
||||||
|
last_name="Brookins",
|
||||||
|
email="a@example.com",
|
||||||
|
address=address,
|
||||||
|
join_date=today
|
||||||
|
),
|
||||||
|
Member(
|
||||||
|
first_name="Kim",
|
||||||
|
last_name="Brookins",
|
||||||
|
email="k@example.com",
|
||||||
|
address=address,
|
||||||
|
join_date=today
|
||||||
|
)
|
||||||
|
]
|
||||||
|
Member.add(members)
|
||||||
|
|
||||||
|
# Get a model instance from Redis using the primary key.
|
||||||
|
member = Member.get(1)
|
||||||
|
|
||||||
|
|
||||||
|
# Update a model instance in Redis
|
||||||
|
member.first_name = "Brian"
|
||||||
|
member.last_name = "Sam-Bodden"
|
||||||
|
member.save()
|
||||||
|
|
||||||
|
# Or, with an implicit save:
|
||||||
|
member.update(first_name="Brian", last_name="Sam-Bodden")
|
||||||
|
|
||||||
|
# Or, affecting multiple model instances with an implicit save:
|
||||||
|
Member.filter(Member.last_name == "Brookins").update(last_name="Sam-Bodden")
|
||||||
|
|
||||||
|
|
||||||
|
# Exact-value queries on indexed fields
|
||||||
|
|
||||||
|
# What if the field wasn't unique and there were two "a@example.com"
|
||||||
|
# entries? This would raise a MultipleObjectsReturned error:
|
||||||
member = Member.get(Member.email == "a.m.brookins@gmail.com")
|
member = Member.get(Member.email == "a.m.brookins@gmail.com")
|
||||||
|
|
||||||
# What if you know there might be multiple results? Use filter():
|
# What if you know there might be multiple results? Use filter():
|
||||||
members = Member.filter(Member.last_name == "Brookins")
|
members = Member.filter(Member.last_name == "Brookins")
|
||||||
|
|
||||||
# What if you want to only return values that don't match a query?
|
# What if you want to only return values that don't match a query?
|
||||||
members = Member.exclude(last_name="Brookins")
|
members = Member.exclude(Member.last_name == "Brookins")
|
||||||
|
|
||||||
# You can combine filer() and exclude():
|
# You can combine filer() and exclude():
|
||||||
members = Member.filter(last_name="Brookins").exclude(first_name="Andrew")
|
members = Member.filter(Member.last_name == "Brookins").exclude(
|
||||||
|
Member.first_name == "Andrew")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -277,6 +277,7 @@ class RedisModel(BaseModel, metaclass=RedisModelMetaclass):
|
||||||
if not hasattr(cls.Meta, 'primary_key_pattern'):
|
if not hasattr(cls.Meta, 'primary_key_pattern'):
|
||||||
cls.Meta.primary_key_pattern = f"{cls.Meta.primary_key.name}:{{pk}}"
|
cls.Meta.primary_key_pattern = f"{cls.Meta.primary_key.name}:{{pk}}"
|
||||||
|
|
||||||
|
|
||||||
def __init__(__pydantic_self__, **data: Any) -> None:
|
def __init__(__pydantic_self__, **data: Any) -> None:
|
||||||
# Uses something other than `self` the first arg to allow "self" as a
|
# Uses something other than `self` the first arg to allow "self" as a
|
||||||
# settable attribute
|
# settable attribute
|
||||||
|
@ -323,27 +324,29 @@ class RedisModel(BaseModel, metaclass=RedisModelMetaclass):
|
||||||
raise RedisModelError("You must define only one primary key for a model")
|
raise RedisModelError("You must define only one primary key for a model")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def key(cls, part: str):
|
def make_key(cls, part: str):
|
||||||
global_prefix = getattr(cls.Meta, 'global_key_prefix', '')
|
global_prefix = getattr(cls.Meta, 'global_key_prefix', '')
|
||||||
model_prefix = getattr(cls.Meta, 'model_key_prefix', '')
|
model_prefix = getattr(cls.Meta, 'model_key_prefix', '')
|
||||||
return f"{global_prefix}{model_prefix}{part}"
|
return f"{global_prefix}{model_prefix}{part}"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def make_primary_key(self, pk: Any):
|
||||||
|
"""Return the Redis key for this model."""
|
||||||
|
return self.make_key(self.Meta.primary_key_pattern.format(pk=pk))
|
||||||
|
|
||||||
|
def key(self):
|
||||||
|
"""Return the Redis key for this model."""
|
||||||
|
pk = self.__fields__[self.Meta.primary_key.field.name]
|
||||||
|
return self.make_primary_key(pk)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get(cls, pk: Any):
|
def get(cls, pk: Any):
|
||||||
# TODO: Getting related objects
|
# TODO: Getting related objects
|
||||||
pk_pattern = cls.Meta.primary_key_pattern.format(pk=str(pk))
|
document = cls.db().hgetall(cls.make_primary_key(pk))
|
||||||
print("GET ", cls.key(pk_pattern))
|
|
||||||
document = cls.db().hgetall(cls.key(pk_pattern))
|
|
||||||
if not document:
|
if not document:
|
||||||
raise NotFoundError
|
raise NotFoundError
|
||||||
return cls.parse_obj(document)
|
return cls.parse_obj(document)
|
||||||
|
|
||||||
def delete(self):
|
|
||||||
# TODO: deleting relationships
|
|
||||||
pk = self.__fields__[self.Meta.primary_key.field.name]
|
|
||||||
pk_pattern = self.Meta.primary_key_pattern.format(pk=pk)
|
|
||||||
return self.db().delete(self.key(pk_pattern))
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def db(cls):
|
def db(cls):
|
||||||
return cls.Meta.database
|
return cls.Meta.database
|
||||||
|
@ -369,7 +372,12 @@ class RedisModel(BaseModel, metaclass=RedisModelMetaclass):
|
||||||
"""Return raw values from Redis instead of model instances."""
|
"""Return raw values from Redis instead of model instances."""
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
# TODO: deleting relationships
|
||||||
|
return self.db().delete(self.key())
|
||||||
|
|
||||||
def save(self) -> 'RedisModel':
|
def save(self) -> 'RedisModel':
|
||||||
|
# TODO: Saving related models
|
||||||
pk_field = self.Meta.primary_key.field
|
pk_field = self.Meta.primary_key.field
|
||||||
document = jsonable_encoder(self.dict())
|
document = jsonable_encoder(self.dict())
|
||||||
pk = document[pk_field.name]
|
pk = document[pk_field.name]
|
||||||
|
@ -379,8 +387,7 @@ class RedisModel(BaseModel, metaclass=RedisModelMetaclass):
|
||||||
setattr(self, pk_field.name, pk)
|
setattr(self, pk_field.name, pk)
|
||||||
document[pk_field.name] = pk
|
document[pk_field.name] = pk
|
||||||
|
|
||||||
pk_pattern = self.Meta.primary_key_pattern.format(pk=pk)
|
success = self.db().hset(self.key(), mapping=document)
|
||||||
success = self.db().hset(self.key(pk_pattern), mapping=document)
|
|
||||||
return success
|
return success
|
||||||
|
|
||||||
Meta = DefaultMeta
|
Meta = DefaultMeta
|
||||||
|
|
192
redis_developer/tests/test_model.py
Normal file
192
redis_developer/tests/test_model.py
Normal file
|
@ -0,0 +1,192 @@
|
||||||
|
import decimal
|
||||||
|
import datetime
|
||||||
|
from typing import Optional, List
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import redis
|
||||||
|
from pydantic import ValidationError
|
||||||
|
|
||||||
|
from redis_developer.orm import (
|
||||||
|
RedisModel,
|
||||||
|
Field,
|
||||||
|
Relationship,
|
||||||
|
)
|
||||||
|
|
||||||
|
r = redis.Redis()
|
||||||
|
|
||||||
|
|
||||||
|
class BaseRedisModel(RedisModel):
|
||||||
|
class Meta:
|
||||||
|
database = redis.Redis(password="my-password", decode_responses=True)
|
||||||
|
model_key_prefix = "redis-developer:"
|
||||||
|
|
||||||
|
|
||||||
|
class Address(BaseRedisModel):
|
||||||
|
address_line_1: str
|
||||||
|
address_line_2: Optional[str]
|
||||||
|
city: str
|
||||||
|
country: str
|
||||||
|
postal_code: str
|
||||||
|
|
||||||
|
|
||||||
|
class Order(BaseRedisModel):
|
||||||
|
total: decimal.Decimal
|
||||||
|
currency: str
|
||||||
|
created_on: datetime.datetime
|
||||||
|
|
||||||
|
|
||||||
|
class Member(BaseRedisModel):
|
||||||
|
first_name: str
|
||||||
|
last_name: str
|
||||||
|
email: str = Field(unique=True, index=True)
|
||||||
|
join_date: datetime.date
|
||||||
|
|
||||||
|
# Creates an embedded document: stored as hash fields or JSON document.
|
||||||
|
address: Address
|
||||||
|
|
||||||
|
# Creates a relationship to data in separate Hash or JSON documents.
|
||||||
|
orders: Optional[List[Order]] = Relationship(back_populates='member')
|
||||||
|
|
||||||
|
# Creates a self-relationship.
|
||||||
|
recommended_by: Optional['Member'] = Relationship(back_populates='recommended')
|
||||||
|
|
||||||
|
class Meta(BaseRedisModel.Meta):
|
||||||
|
model_key_prefix = "member"
|
||||||
|
primary_key_pattern = ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_validates_required_fields():
|
||||||
|
address = Address(
|
||||||
|
address_line_1="1 Main St.",
|
||||||
|
city="Happy Town",
|
||||||
|
state="WY",
|
||||||
|
postal_code=11111,
|
||||||
|
country="USA"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Raises ValidationError: last_name, address are required
|
||||||
|
with pytest.raises(ValidationError):
|
||||||
|
Member(
|
||||||
|
first_name="Andrew",
|
||||||
|
zipcode="97086",
|
||||||
|
join_date=datetime.date.today()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_validates_field():
|
||||||
|
|
||||||
|
# Raises ValidationError: join_date is not a date
|
||||||
|
with pytest.raises(ValidationError):
|
||||||
|
Member(
|
||||||
|
first_name="Andrew",
|
||||||
|
last_name="Brookins",
|
||||||
|
join_date="yesterday"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Passes validation
|
||||||
|
def test_validation_passes():
|
||||||
|
address = Address(
|
||||||
|
address_line_1="1 Main St.",
|
||||||
|
city="Happy Town",
|
||||||
|
state="WY",
|
||||||
|
postal_code=11111,
|
||||||
|
country="USA"
|
||||||
|
)
|
||||||
|
member = Member(
|
||||||
|
first_name="Andrew",
|
||||||
|
last_name="Brookins",
|
||||||
|
email="a@example.com",
|
||||||
|
address=address,
|
||||||
|
join_date=datetime.date.today()
|
||||||
|
)
|
||||||
|
assert member.first_name == "Andrew"
|
||||||
|
|
||||||
|
|
||||||
|
def test_saves_model():
|
||||||
|
address = Address(
|
||||||
|
address_line_1="1 Main St.",
|
||||||
|
city="Happy Town",
|
||||||
|
state="WY",
|
||||||
|
postal_code=11111,
|
||||||
|
country="USA"
|
||||||
|
)
|
||||||
|
# Save a model instance to Redis
|
||||||
|
address.save()
|
||||||
|
|
||||||
|
address2 = Address.get(address.pk)
|
||||||
|
assert address2 == address
|
||||||
|
|
||||||
|
|
||||||
|
# Saves a model with relationships (TODO!)
|
||||||
|
@pytest.skip("Not implemented yet")
|
||||||
|
def test_saves_with_relationships():
|
||||||
|
address = Address(
|
||||||
|
address_line_1="1 Main St.",
|
||||||
|
city="Happy Town",
|
||||||
|
state="WY",
|
||||||
|
postal_code=11111,
|
||||||
|
country="USA"
|
||||||
|
)
|
||||||
|
member = Member(
|
||||||
|
first_name="Andrew",
|
||||||
|
last_name="Brookins",
|
||||||
|
email="a@example.com",
|
||||||
|
address=address,
|
||||||
|
join_date=datetime.date.today()
|
||||||
|
)
|
||||||
|
member.save()
|
||||||
|
|
||||||
|
|
||||||
|
# Save many model instances to Redis
|
||||||
|
@pytest.skip("Not implemented yet")
|
||||||
|
def test_saves_many():
|
||||||
|
today = datetime.date.today()
|
||||||
|
members = [
|
||||||
|
Member(
|
||||||
|
first_name="Andrew",
|
||||||
|
last_name="Brookins",
|
||||||
|
email="a@example.com",
|
||||||
|
address=address,
|
||||||
|
join_date=today
|
||||||
|
),
|
||||||
|
Member(
|
||||||
|
first_name="Kim",
|
||||||
|
last_name="Brookins",
|
||||||
|
email="k@example.com",
|
||||||
|
address=address,
|
||||||
|
join_date=today
|
||||||
|
)
|
||||||
|
]
|
||||||
|
Member.add(members)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.skip("No implemented yet")
|
||||||
|
def test_updates_a_model():
|
||||||
|
# Update a model instance in Redis
|
||||||
|
member.first_name = "Brian"
|
||||||
|
member.last_name = "Sam-Bodden"
|
||||||
|
member.save()
|
||||||
|
|
||||||
|
# Or, with an implicit save:
|
||||||
|
member.update(first_name="Brian", last_name="Sam-Bodden")
|
||||||
|
|
||||||
|
# Or, affecting multiple model instances with an implicit save:
|
||||||
|
Member.filter(Member.last_name == "Brookins").update(last_name="Sam-Bodden")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.skip("Not implemented yet")
|
||||||
|
def test_exact_match_queries():
|
||||||
|
# What if the field wasn't unique and there were two "a@example.com"
|
||||||
|
# entries? This would raise a MultipleObjectsReturned error:
|
||||||
|
member = Member.get(Member.email == "a.m.brookins@gmail.com")
|
||||||
|
|
||||||
|
# What if you know there might be multiple results? Use filter():
|
||||||
|
members = Member.filter(Member.last_name == "Brookins")
|
||||||
|
|
||||||
|
# What if you want to only return values that don't match a query?
|
||||||
|
members = Member.exclude(Member.last_name == "Brookins")
|
||||||
|
|
||||||
|
# You can combine filer() and exclude():
|
||||||
|
members = Member.filter(Member.last_name == "Brookins").exclude(
|
||||||
|
Member.first_name == "Andrew")
|
2
test.py
2
test.py
|
@ -17,7 +17,7 @@ from redis_developer.orm import (
|
||||||
class BaseRedisModel(RedisModel):
|
class BaseRedisModel(RedisModel):
|
||||||
class Meta:
|
class Meta:
|
||||||
database = redis.Redis(password="my-password", decode_responses=True)
|
database = redis.Redis(password="my-password", decode_responses=True)
|
||||||
model_key_prefix = "redis-developer:"
|
global_key_prefix = "redis-developer:"
|
||||||
|
|
||||||
|
|
||||||
class Address(BaseRedisModel):
|
class Address(BaseRedisModel):
|
||||||
|
|
Loading…
Reference in a new issue