|
1 | 1 | pydantic-aioredis |
2 | 2 | ============================================= |
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. |
5 | 5 |
|
6 | 6 | Inspired by |
7 | 7 | `pydantic-redis <https://github.com/sopherapps/pydantic-redis>`_ by |
8 | 8 | `Martin Ahindura <https://github.com/Tinitto>`_ |
9 | 9 |
|
10 | | -ain Dependencies |
| 10 | +Dependencies |
11 | 11 | ----------------- |
12 | 12 |
|
13 | | - |
14 | 13 | * `Python +3.6 <https://www.python.org>`_ |
15 | 14 | * `aioredis 2.0 <https://aioredis.readthedocs.io/en/latest/>`_ |
16 | 15 | * `pydantic <https://github.com/samuelcolvin/pydantic/>`_ |
17 | 16 |
|
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 | | -------- |
188 | 17 |
|
189 | | -Licensed under the `MIT License <./LICENSE>`_ |
| 18 | +.. toctree:: |
| 19 | + :maxdepth: 2 |
190 | 20 |
|
| 21 | + quickstart |
| 22 | + serialization |
| 23 | + development |
| 24 | + module |
191 | 25 |
|
192 | 26 | Indices and tables |
193 | 27 | ================== |
|
0 commit comments