pydantic-aioredis
Pydantic-aioredis is designed to provide an efficient way of integrating Redis databases with Python-based applications. Built on top of Pydantic, pydantic-aioredis allows you to define data models and validate input data before it is stored in Redis. Data is validated before storing and after retrieval from Redis. The library also provides an easy-to-use API for querying, updating, and deleting data stored in Redis.
Inspired by
pydantic-redis by
Martin Ahindura
Main Dependencies
Getting Started
Examples
Examples are in the examples/ directory of this repo.
Installation
Install the package
pip install pydantic-aioredis
Quick Usage
Import the Store
, the RedisConfig
and the Model
classes and use accordingly
import asyncio
from datetime import date
from pydantic_aioredis import RedisConfig, Model, Store
class Book(Model):
_primary_key_field: str = 'title'
title: str
author: str
published_on: date
in_stock: bool = True
class Library(Model):
_primary_key_field: str = 'name'
name: str
address: str
store = Store(name='some_name', redis_config=RedisConfig(db=5, host='localhost', port=6379), life_span_in_seconds=3600)
store.register_model(Book)
store.register_model(Library)
books = [
Book(title="Oliver Twist", author='Charles Dickens', published_on=date(year=1215, month=4, day=4),
in_stock=False),
Book(title="Great Expectations", author='Charles Dickens', published_on=date(year=1220, month=4, day=4)),
Book(title="Jane Eyre", author='Charles Dickens', published_on=date(year=1225, month=6, day=4), in_stock=False),
Book(title="Wuthering Heights", author='Jane Austen', published_on=date(year=1600, month=4, day=4)),
]
libraries = [
Library(name="The Grand Library", address="Kinogozi, Hoima, Uganda"),
Library(name="Christian Library", address="Buhimba, Hoima, Uganda")
]
async def work_with_orm():
await Book.insert(books)
await Library.insert(libraries)
all_books = await Book.select()
print(all_books)
some_books = await Book.select(ids=["Oliver Twist", "Jane Eyre"])
print(some_books)
books_with_few_fields = await Book.select(columns=["author", "in_stock"])
print(books_with_few_fields)
this_book = Book(title="Moby Dick", author='Herman Melvill', published_on=date(year=1851, month=10, day=17))
await Book.insert(this_book)
async with this_book.update():
this_book.author = "Herman Melville"
this_book.published_on=date(year=1851, month=10, day=18)
this_book_from_redis = await Book.select(ids=["Moby Dick"])
assert this_book_from_redis[0].author == "Herman Melville"
assert this_book_from_redis[0].published_on == date(year=1851, month=10, day=18)
await Library.delete(ids=["The Grand Library"])
loop = asyncio.get_event_loop()
loop.run_until_complete(work_with_orm())
Custom Fields in Model
_primary_key_field | Yes | None | The field of your model that is the primary key |
_redis_prefix | No | None | If set, will be added to the beginning of the keys we store in redis |
_redis_separator | No | : | Defaults to :, used to separate prefix, table_name, and primary_key |
_table_name | No | cls.name | Defaults to the model's name, can set a custom name in redis |
_auto_save | No | False | Defaults to false. If true, will save to redis on instantiation |
_auto_sync | No | False | Defaults to false. If true, will save to redis on attr update |
License
Licensed under the MIT License
Contributing
Contributions are very welcome.
To learn more, see the Contributor Guide
Contributors
Thanks go to these wonderful people (emoji key):