Package: pydantic-aioredis
pydantic-aioredisPydantic-aioredis is designed to provide an efficient way of integrating Redis databases with Python-based applications. Built on top ofPydantic, 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 bypydantic-redisbyMartin AhinduraMain DependenciesPython +3.7redis-py <4.2.0pydanticGetting StartedExamplesExamples are in theexamples/directory of this repo.InstallationInstall the packagepip install pydantic-aioredisQuick UsageImport theStore, theRedisConfigand theModelclasses and use accordinglyimportasynciofromdatetimeimportdatefrompydantic_aioredisimportRedisConfig,Model,Store# Create models as you would create pydantic models i.e. using typingsclassBook(Model):_primary_key_field:str='title'title:strauthor:strpublished_on:datein_stock:bool=True# Do note that there is no concept of relationships hereclassLibrary(Model):# the _primary_key_field is mandatory_primary_key_field:str='name'name:straddress:str# Create the store and register your modelsstore=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 codebooks=[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 objectslibraries=[Library(name="The Grand Library",address="Kinogozi, Hoima, Uganda"),Library(name="Christian Library",address="Buhimba, Hoima, Uganda")]asyncdefwork_with_orm():# Insert them into redisawaitBook.insert(books)awaitLibrary.insert(libraries)# Select all books to view them. A list of Model instances will be returnedall_books=awaitBook.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 bookssome_books=awaitBook.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 workbooks_with_few_fields=awaitBook.select(columns=["author","in_stock"])print(books_with_few_fields)# Will print [{"author": "'Charles Dickens", "in_stock": "True"},...]this_book=Book(title="Moby Dick",author='Herman Melvill',published_on=date(year=1851,month=10,day=17))awaitBook.insert(this_book)# oops, there was a typo. Fix it# Update is an async context manager and will update redis with all changes in one operationsasyncwiththis_book.update():this_book.author="Herman Melville"this_book.published_on=date(year=1851,month=10,day=18)this_book_from_redis=awaitBook.select(ids=["Moby Dick"])assertthis_book_from_redis[0].author=="Herman Melville"assertthis_book_from_redis[0].author==date(year=1851,month=10,day=18)# Delete any number of itemsawaitLibrary.delete(ids=["The Grand Library"])# Now run these updatesloop=asyncio.get_event_loop()loop.run_until_complete(work_with_orm())Custom Fields in ModelField NameRequiredDefaultDescription_primary_key_fieldYesNoneThe field of your model that is the primary key_redis_prefixNoNoneIf set, will be added to the beginning of the keys we store in redis_redis_separatorNo:Defaults to :, used to separate prefix, table_name, and primary_key_table_nameNocls.nameDefaults to the model's name, can set a custom name in redis_auto_saveNoFalseDefaults to false. If true, will save to redis on instantiation_auto_syncNoFalseDefaults to false. If true, will save to redis on attr updateLicenseLicensed under theMIT LicenseContributingContributions are very welcome.
To learn more, see theContributor GuideContributorsThanks go to these wonderful people (emoji key):Andrew💻📖Martin Ahindura💻🤔david-wahlstedt⚠️📖👀Daniel Wallace⚠️Paco Nathan💡Andreas Brodersen📖