This package provides a simple ORM for redis using pydantic-like models.
Project description
pydantic-redis
A simple declarative ORM for Redis
Main Dependencies
Getting Started
-
Install the package
pip install pydantic-redis
-
Import the
Store
, theRedisConfig
and theModel
classes and use accordinglyfrom pydantic_redis import RedisConfig, Model, Store # Create models as you would create pydantic models i.e. using typings class Book(Model): _primary_key_field: str = 'title' title: str author: str published_on: date in_stock: bool = True # Do note that there is no concept of relationships here class Library(Model): # the _primary_key_field is mandatory _primary_key_field: str = 'name' name: str address: str # Create the store and register your models 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) # Sample books. You can create as many as you wish anywhere in the code 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)), ] # Some library objects libraries = [ Library(name="The Grand Library", address="Kinogozi, Hoima, Uganda"), Library(name="Christian Library", address="Buhimba, Hoima, Uganda") ] # Insert them into redis Book.insert(books) Library.insert(libraries) # Select all books to view them. A list of Model instances will be returned all_books = Book.select() print(all_books) # Will print [Book(title="Oliver Twist", author="Charles Dickens", published_on=date(year=1215, month=4, day=4), in_stock=False), Book(...] # Or select some of the books some_books = Book.select(ids=["Oliver Twist", "Jane Eyre"]) print(some_books) # Will return only those two books # Or select some of the columns. THIS RETURNS DICTIONARIES not MODEL Instances # The Dictionaries have values in string form so you might need to do some extra work books_with_few_fields = Book.select(columns=["author", "in_stock"]) print(books_with_few_fields) # Will print [{"author": "'Charles Dickens", "in_stock": "True"},...] # Update any book or library Book.update(_id="Oliver Twist", data={"author": "John Doe"}) # Delete any number of items Library.delete(ids=["The Grand Library"])
How to test
-
Clone the repo and enter its root folder
git clone https://github.com/sopherapps/pydantic-redis.git && cd pydantic-redis
-
Ensure you have redis server installed and running at port 6379 on your development machine. Follow the quick start guide from redis.
-
Create a virtual environment and activate it
virtualenv -p /usr/bin/python3.6 env && source env/bin/activate
-
Install the dependencies
pip install -r requirements.txt
-
Run the test command
python -m unittest
ToDo
- Add parsed filtering e.g. title < r
- Add pubsub such that for each table, there is a channel for each mutation e.g. table_nameinsert, table_nameupdate, table_name__delete such that code can just subscribe to an given table's mutation and be updated each time a mutation occurs
License
Copyright (c) 2020 Martin Ahindura Licensed under the MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pydantic-redis-0.0.5.tar.gz
(6.7 kB
view hashes)
Built Distribution
Close
Hashes for pydantic_redis-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03005bb678b0ea1b9d01849cbeaf9047068ca2f0878a0904a8326cb64924da58 |
|
MD5 | 0afb9c3f6185c826f8b743e408018ad2 |
|
BLAKE2b-256 | 033c4ab35eb3a8521edb59a7e7633c91105b5d80b7bf4a6a8170513df00b1a8c |