Skip to content
This repository was archived by the owner on Jun 21, 2025. It is now read-only.

Commit f7614de

Browse files
Merge pull request #12 from andrewthetechie/json-parser
feat: json serializer for complex fields
2 parents acd37a1 + d28e342 commit f7614de

File tree

11 files changed

+406
-275
lines changed

11 files changed

+406
-275
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ repos:
1919
- id: python-no-eval
2020
- id: python-no-log-warn
2121
- id: python-use-type-annotations
22+
- id: rst-backticks
23+
- id: rst-directive-colons
24+
- id: rst-inline-touching-normal
2225
- repo: https://github.com/asottile/reorder_python_imports
2326
rev: v2.4.0
2427
hooks:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Install the package
4040

4141
pip install pydantic-aioredis
4242

43-
### Usage
43+
### Quick Usage
4444
Import the `Store`, the `RedisConfig` and the `Model` classes and use accordingly
4545

4646

docs/development.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Development
2+
===========
3+
4+
The `Makefile <./makefile>`_ has useful targets to help setup your
5+
development encironment. We suggest using pyenv to have access to
6+
multiple python versions easily.
7+
8+
Environment Setup
9+
^^^^^^^^^^^^^^^^^
10+
11+
12+
*
13+
Clone the repo and enter its root folder
14+
15+
.. code-block::
16+
17+
git clone https://github.com/sopherapps/pydantic-redis.git && cd pydantic-redis
18+
19+
*
20+
Create a python 3.9 virtual environment and activate it. We suggest
21+
using `pyenv <https://github.com/pyenv/pyenv>`_ to easily setup
22+
multiple python environments on multiple versions.
23+
24+
.. code-block::
25+
26+
# We use the extra python version (3.6, 3.7, 3.8) for tox testing
27+
pyenv install 3.9.6 3.6.9 3.7.11 3.8.11
28+
pyenv virtualenv 3.9.6 python-aioredis
29+
pyenv local python-aioredis 3.6.9 3.7.11 3.8.11
30+
31+
*
32+
Install the dependencies
33+
34+
.. code-block::
35+
36+
make setup
37+
38+
How to Run Tests
39+
^^^^^^^^^^^^^^^^
40+
41+
42+
*
43+
Run the test command to run tests on only python 3.9
44+
45+
.. code-block::
46+
47+
pytest
48+
49+
*
50+
Run the tox command to run all python version tests
51+
52+
.. code-block::
53+
54+
tox
55+
56+
Test Requirements
57+
^^^^^^^^^^^^^^^^^
58+
59+
Prs should always have tests to cover the change being made. Code
60+
coverage goals for this project are 100% coverage.
61+
62+
Code Linting
63+
^^^^^^^^^^^^
64+
65+
All code should pass Flake8 and be blackened. If you install and setup
66+
pre-commit (done automatically by environment setup), pre-commit will
67+
lint your code for you.
68+
69+
You can run the linting manually with make
70+
71+
.. code-block::
72+
73+
make lint
74+
75+
CI
76+
--
77+
78+
CI is run via Github Actions on all PRs and pushes to the main branch.
79+
80+
Releases are automatically released by Github Actions to Pypi.

docs/index.rst

Lines changed: 9 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,27 @@
11
pydantic-aioredis
22
=============================================
3-
A simple declarative ORM for Redis, using aioredis. Use your Pydantic
4-
models like an ORM, storing data in Redis!
3+
A declarative ORM for Redis, using aioredis. Use your Pydantic
4+
models like an ORM, storing data in Redis.
55

66
Inspired by
77
`pydantic-redis <https://github.com/sopherapps/pydantic-redis>`_ by
88
`Martin Ahindura <https://github.com/Tinitto>`_
99

10-
ain Dependencies
10+
Dependencies
1111
-----------------
1212

13-
1413
* `Python +3.6 <https://www.python.org>`_
1514
* `aioredis 2.0 <https://aioredis.readthedocs.io/en/latest/>`_
1615
* `pydantic <https://github.com/samuelcolvin/pydantic/>`_
1716

18-
Getting Started
19-
---------------
20-
21-
Examples
22-
^^^^^^^^
23-
24-
Examples are in the `examples/ <./examples>`_ directory of this repo.
25-
26-
Installation
27-
^^^^^^^^^^^^
28-
29-
Install the package
30-
31-
.. code-block::
32-
33-
pip install pydantic-aioredis
34-
35-
36-
Usage
37-
^^^^^
38-
39-
Import the ``Store``\ , the ``RedisConfig`` and the ``Model`` classes and use accordingly
40-
41-
.. code-block::
42-
43-
from pydantic_aioredis import RedisConfig, Model, Store
44-
45-
# Create models as you would create pydantic models i.e. using typings
46-
class Book(Model):
47-
_primary_key_field: str = 'title'
48-
title: str
49-
author: str
50-
published_on: date
51-
in_stock: bool = True
52-
53-
# Do note that there is no concept of relationships here
54-
class Library(Model):
55-
# the _primary_key_field is mandatory
56-
_primary_key_field: str = 'name'
57-
name: str
58-
address: str
59-
60-
# Create the store and register your models
61-
store = Store(name='some_name', redis_config=RedisConfig(db=5, host='localhost', port=6379),life_span_in_seconds=3600)
62-
store.register_model(Book)
63-
store.register_model(Library)
64-
65-
# Sample books. You can create as many as you wish anywhere in the code
66-
books = [
67-
Book(title="Oliver Twist", author='Charles Dickens', published_on=date(year=1215, month=4, day=4),
68-
in_stock=False),
69-
Book(title="Great Expectations", author='Charles Dickens', published_on=date(year=1220, month=4, day=4)),
70-
Book(title="Jane Eyre", author='Charles Dickens', published_on=date(year=1225, month=6, day=4), in_stock=False),
71-
Book(title="Wuthering Heights", author='Jane Austen', published_on=date(year=1600, month=4, day=4)),
72-
]
73-
# Some library objects
74-
libraries = [
75-
Library(name="The Grand Library", address="Kinogozi, Hoima, Uganda"),
76-
Library(name="Christian Library", address="Buhimba, Hoima, Uganda")
77-
]
78-
79-
async def work_with_orm():
80-
# Insert them into redis
81-
await Book.insert(books)
82-
await Library.insert(libraries)
83-
84-
# Select all books to view them. A list of Model instances will be returned
85-
all_books = await Book.select()
86-
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(...]
87-
88-
# Or select some of the books
89-
some_books = await Book.select(ids=["Oliver Twist", "Jane Eyre"])
90-
print(some_books) # Will return only those two books
91-
92-
# Or select some of the columns. THIS RETURNS DICTIONARIES not MODEL Instances
93-
# The Dictionaries have values in string form so you might need to do some extra work
94-
books_with_few_fields = await Book.select(columns=["author", "in_stock"])
95-
print(books_with_few_fields) # Will print [{"author": "'Charles Dickens", "in_stock": "True"},...]
96-
97-
# Update any book or library
98-
await Book.update(_id="Oliver Twist", data={"author": "John Doe"})
99-
100-
# Delete any number of items
101-
await Library.delete(ids=["The Grand Library"])
102-
103-
104-
105-
Development
106-
-----------
107-
108-
The `Makefile <./makefile>`_ has useful targets to help setup your
109-
development encironment. We suggest using pyenv to have access to
110-
multiple python versions easily.
111-
112-
Environment Setup
113-
^^^^^^^^^^^^^^^^^
114-
115-
116-
*
117-
Clone the repo and enter its root folder
118-
119-
.. code-block::
120-
121-
git clone https://github.com/sopherapps/pydantic-redis.git && cd pydantic-redis
122-
123-
*
124-
Create a python 3.9 virtual environment and activate it. We suggest
125-
using `pyenv <https://github.com/pyenv/pyenv>`_ to easily setup
126-
multiple python environments on multiple versions.
127-
128-
.. code-block::
129-
130-
# We use the extra python version (3.6, 3.7, 3.8) for tox testing
131-
pyenv install 3.9.6 3.6.9 3.7.11 3.8.11
132-
pyenv virtualenv 3.9.6 python-aioredis
133-
pyenv local python-aioredis 3.6.9 3.7.11 3.8.11
134-
135-
*
136-
Install the dependencies
137-
138-
.. code-block::
139-
140-
make setup
141-
142-
How to Run Tests
143-
^^^^^^^^^^^^^^^^
144-
145-
146-
*
147-
Run the test command to run tests on only python 3.9
148-
149-
.. code-block::
150-
151-
pytest
152-
153-
*
154-
Run the tox command to run all python version tests
155-
156-
.. code-block::
157-
158-
tox
159-
160-
Test Requirements
161-
^^^^^^^^^^^^^^^^^
162-
163-
Prs should always have tests to cover the change being made. Code
164-
coverage goals for this project are 100% coverage.
165-
166-
Code Linting
167-
^^^^^^^^^^^^
168-
169-
All code should pass Flake8 and be blackened. If you install and setup
170-
pre-commit (done automatically by environment setup), pre-commit will
171-
lint your code for you.
172-
173-
You can run the linting manually with make
174-
175-
.. code-block::
176-
177-
make lint
178-
179-
CI
180-
--
181-
182-
CI is run via Github Actions on all PRs and pushes to the main branch.
183-
184-
Releases are automatically released by Github Actions to Pypi.
185-
186-
License
187-
-------
18817

189-
Licensed under the `MIT License <./LICENSE>`_
18+
.. toctree::
19+
:maxdepth: 2
19020

21+
quickstart
22+
serialization
23+
development
24+
module
19125

19226
Indices and tables
19327
==================

docs/quickstart.rst

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Quick Start
2+
===========
3+
4+
Examples
5+
^^^^^^^^
6+
7+
Examples are in the `examples/ <https://github.com/andrewthetechie/pydantic-aioredis/tree/main/examples>`_ directory of this repo.
8+
9+
Installation
10+
^^^^^^^^^^^^
11+
12+
Install the package
13+
14+
.. code-block::
15+
16+
pip install pydantic-aioredis
17+
18+
19+
Quick Usage
20+
^^^^^^^^^^^
21+
22+
Import the ``Store``\ , the ``RedisConfig`` and the ``Model`` classes.
23+
24+
Store and RedisConfig let you configure and customize the connection to your redis instance. Model is the base class for your ORM models.
25+
26+
.. code-block::
27+
28+
from pydantic_aioredis import RedisConfig, Model, Store
29+
30+
# Create models as you would create pydantic models i.e. using typings
31+
class Book(Model):
32+
_primary_key_field: str = 'title'
33+
title: str
34+
author: str
35+
published_on: date
36+
in_stock: bool = True
37+
38+
# Do note that there is no concept of relationships here
39+
class Library(Model):
40+
# the _primary_key_field is mandatory
41+
_primary_key_field: str = 'name'
42+
name: str
43+
address: str
44+
45+
# Create the store and register your models
46+
store = Store(name='some_name', redis_config=RedisConfig(db=5, host='localhost', port=6379),life_span_in_seconds=3600)
47+
store.register_model(Book)
48+
store.register_model(Library)
49+
50+
# Sample books. You can create as many as you wish anywhere in the code
51+
books = [
52+
Book(title="Oliver Twist", author='Charles Dickens', published_on=date(year=1215, month=4, day=4),
53+
in_stock=False),
54+
Book(title="Great Expectations", author='Charles Dickens', published_on=date(year=1220, month=4, day=4)),
55+
Book(title="Jane Eyre", author='Charles Dickens', published_on=date(year=1225, month=6, day=4), in_stock=False),
56+
Book(title="Wuthering Heights", author='Jane Austen', published_on=date(year=1600, month=4, day=4)),
57+
]
58+
# Some library objects
59+
libraries = [
60+
Library(name="The Grand Library", address="Kinogozi, Hoima, Uganda"),
61+
Library(name="Christian Library", address="Buhimba, Hoima, Uganda")
62+
]
63+
64+
async def work_with_orm():
65+
# Insert them into redis
66+
await Book.insert(books)
67+
await Library.insert(libraries)
68+
69+
# Select all books to view them. A list of Model instances will be returned
70+
all_books = await Book.select()
71+
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(...]
72+
73+
# Or select some of the books
74+
some_books = await Book.select(ids=["Oliver Twist", "Jane Eyre"])
75+
print(some_books) # Will return only those two books
76+
77+
# Or select some of the columns. THIS RETURNS DICTIONARIES not MODEL Instances
78+
# The Dictionaries have values in string form so you might need to do some extra work
79+
books_with_few_fields = await Book.select(columns=["author", "in_stock"])
80+
print(books_with_few_fields) # Will print [{"author": "'Charles Dickens", "in_stock": "True"},...]
81+
82+
# Update any book or library
83+
await Book.update(_id="Oliver Twist", data={"author": "John Doe"})
84+
85+
# Delete any number of items
86+
await Library.delete(ids=["The Grand Library"])

0 commit comments

Comments
 (0)