Skip to content

Commit 1eb8abf

Browse files
authored
feat: update docs (#310)
1 parent 81110ec commit 1eb8abf

File tree

6 files changed

+196
-113
lines changed

6 files changed

+196
-113
lines changed

docs/index.rst

+3-52
Original file line numberDiff line numberDiff line change
@@ -24,66 +24,17 @@ The source code is hosted on `GitHub`_.
2424

2525
.. _GitHub: https://github.com/05bit/peewee-async
2626

27-
Quickstart
28-
----------
29-
30-
.. literalinclude:: ./samples/quickstart.py
31-
32-
33-
Install
34-
-------
35-
36-
Install latest version from PyPI.
37-
38-
For PostgreSQL:
39-
40-
.. code-block:: console
41-
42-
pip install peewee-async[postgresql]
43-
44-
For MySQL:
45-
46-
.. code-block:: console
47-
48-
pip install peewee-async[mysql]
49-
50-
Install from sources
51-
++++++++++++++++++++
52-
53-
.. code-block:: console
54-
55-
git clone https://github.com/05bit/peewee-async.git
56-
cd peewee-async
57-
pip install .
58-
59-
Running tests
60-
+++++++++++++
61-
62-
Prepare environment for tests:
63-
64-
* Clone source code from GitHub as shown above
65-
* Run docker environment with PostgreSQL database for testing
66-
67-
Then run tests:
68-
69-
.. code-block:: console
70-
71-
pytest -s -v
72-
73-
Report bugs and discuss
74-
-----------------------
75-
76-
You are welcome to add discussion topics or bug reports to `tracker on GitHub`_!
77-
78-
.. _tracker on GitHub: https://github.com/05bit/peewee-async/issues
7927

8028
Contents
8129
--------
8230

8331
.. toctree::
8432
:maxdepth: 2
8533

34+
peewee_async/installing
35+
peewee_async/quickstart
8636
peewee_async/api
37+
peewee_async/connection
8738
peewee_async/examples
8839

8940
Indices and tables

docs/peewee_async/api.rst

+3-58
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,9 @@
11
API Documentation
22
====================
33

4-
Let's provide an example::
5-
6-
import asyncio
7-
import peewee
8-
import logging
9-
from peewee_async import PostgresqlDatabase
10-
11-
database = PostgresqlDatabase('test')
12-
13-
# Disable sync queries
14-
database.set_allow_sync(False)
15-
16-
# Let's define a simple model:
17-
class PageBlock(peewee_async.AioModel):
18-
key = peewee.CharField(max_length=40, unique=True)
19-
text = peewee.TextField(default='')
20-
21-
class Meta:
22-
database = database
23-
24-
-- as you can see, nothing special in this code, just plain ``peewee_async.AioModel`` definition and disabling sync queries.
25-
26-
Now we need to create a table for model::
27-
28-
with database.allow_sync():
29-
PageBlock.create_table(True)
30-
31-
-- this code is **sync**, and will do **absolutely the same thing** as would do code with regular ``peewee.PostgresqlDatabase``. This is intentional, I believe there's just no need to run database initialization code asynchronously! *Less code, less errors*.
32-
33-
Finally, let's do something async::
34-
35-
async def my_async_func():
36-
# Add new page block
37-
await PageBlock.aio_create(
38-
key='title',
39-
text="Peewee is AWESOME with async!"
40-
)
41-
42-
# Get one by key
43-
title = await PageBlock.aio_get(PageBlock, key='title')
44-
print("Was:", title.text)
45-
46-
# Save with new text using manager
47-
title.text = "Peewee is SUPER awesome with async!"
48-
await title.aio_save()
49-
print("New:", title.text)
50-
51-
loop.run_until_complete(my_async_func())
52-
loop.close()
53-
54-
**That's it!** As you may notice there's no need to connect and re-connect before executing async queries! It's all automatic. But you can run ``AioDatabase.aio_connect()`` or ``AioDatabase.aio_close()`` when you need it.
55-
56-
And you can use methods from from **AioModel** for operations like selecting, deleting etc.
57-
All of them are listed below.
58-
594

605
Databases
61-
---------
6+
++++++++++
627

638
.. autoclass:: peewee_async.databases.AioDatabase
649

@@ -89,7 +34,7 @@ Databases
8934
:members: init
9035

9136
AioModel
92-
--------
37+
++++++++++
9338

9439
.. autoclass:: peewee_async.AioModel
9540

@@ -108,7 +53,7 @@ AioModel
10853
.. autofunction:: peewee_async.aio_prefetch
10954

11055
AioModelSelect
111-
--------------
56+
++++++++++++++
11257

11358
.. autoclass:: peewee_async.aio_model.AioModelSelect
11459

docs/peewee_async/connection.rst

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Working with connections
2+
========================
3+
4+
Every time some query is executed, for example, like this:
5+
6+
.. code-block:: python
7+
8+
await MyModel.aio_get(id=1)
9+
10+
11+
under the hood the execution runs under special async context manager:
12+
13+
.. code-block:: python
14+
15+
async with database.aio_connection() as connection:
16+
await execute_your_query()
17+
18+
which takes a connection from the ``connection_context`` contextvar if it is not ``None`` or acquire from the pool otherwise.
19+
It releases the connection at the exit and set the ``connection_context`` to ``None``.
20+
21+
22+
.. code-block:: python
23+
24+
# acquire a connection and put it to connection_context context variable
25+
await MyModel.aio_get(id=1)
26+
# release the connection and set the connection_context to None
27+
28+
# acquire a connection and put it to connection_context context variable
29+
await MyModel.aio_get(id=2)
30+
# release the connection and set the connection_context to None
31+
32+
# acquire a connection and put it to connection_context context variable
33+
await MyModel.aio_get(id=3)
34+
# release the connection and set the connection_context to None
35+
36+
Connections manual management
37+
++++++++++++++++++++++++++++
38+
39+
If you want to manage connections manually or you want to use one connection for a batch of queries you
40+
can run ``database.aio_connection`` by yourself and run the queries under the context manager.
41+
42+
.. code-block:: python
43+
44+
# acquire a connection and put it to connection_context context variable
45+
async with database.aio_connection() as connection:
46+
47+
# using the connection from the contextvar
48+
await MyModel.aio_get(id=1)
49+
50+
# using the connection from the contextvar
51+
await MyModel.aio_get(id=2)
52+
53+
# using the connection from the contextvar
54+
await MyModel.aio_get(id=3)
55+
# release the connection set connection_context to None at the exit of the async contextmanager
56+
57+
You can even run nested ``aio_connection`` context managers.
58+
In this case you will use one connection for the all managers from the highest context manager of the stack of calls
59+
and it will be closed when the highest manager is exited.

docs/peewee_async/examples.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Examples
33

44

55
Using both sync and async calls
6-
-------------------------------
6+
+++++++++++++++++++++++++++++++
77

88
.. code-block:: python
99
@@ -40,7 +40,7 @@ Using both sync and async calls
4040
4141
4242
Using transactions
43-
------------------
43+
++++++++++++++++++
4444

4545
.. code-block:: python
4646
@@ -66,7 +66,7 @@ Using transactions
6666
loop.run_until_complete(test())
6767
6868
Using async peewee with Tornado
69-
-------------------------------
69+
+++++++++++++++++++++++++++++++
7070

7171
`Tornado`_ is a mature and powerful asynchronous web framework. It provides its own event loop, but there's an option to run Tornado on asyncio event loop. And that's exactly what we need!
7272

docs/peewee_async/installing.rst

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Installing
2+
====================
3+
4+
Install latest version from PyPI.
5+
6+
For PostgreSQL aiopg backend:
7+
8+
.. code-block:: console
9+
10+
pip install peewee-async[postgresql]
11+
12+
For PostgreSQL psycopg3 backend:
13+
14+
.. code-block:: console
15+
16+
pip install peewee-async[psycopg]
17+
18+
For MySQL:
19+
20+
.. code-block:: console
21+
22+
pip install peewee-async[mysql]
23+
24+
Installing and developing
25+
+++++++++++++++++++++++++
26+
27+
Clone source code from GitHub:
28+
29+
.. code-block:: console
30+
31+
git clone https://github.com/05bit/peewee-async.git
32+
cd peewee-async
33+
34+
Install dependencies using pip:
35+
36+
.. code-block:: console
37+
38+
pip install -e .[develop]
39+
40+
41+
Or using `poetry`_:
42+
43+
.. _poetry: https://python-poetry.org/docs/
44+
45+
.. code-block:: console
46+
47+
poetry install -E develop
48+
49+
50+
Running tests
51+
+++++++++++++
52+
53+
* Clone source code from GitHub as shown above
54+
* Run docker environment with PostgreSQL database for testing
55+
56+
.. code-block:: console
57+
58+
docker-compose up -d
59+
60+
Then run tests:
61+
62+
.. code-block:: console
63+
64+
pytest -s -v
65+
66+
Report bugs and discuss
67+
+++++++++++++++++++++++
68+
69+
You are welcome to add discussion topics or bug reports to `tracker on GitHub`_!
70+
71+
.. _tracker on GitHub: https://github.com/05bit/peewee-async/issues

docs/peewee_async/quickstart.rst

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Quickstart
2+
====================
3+
4+
Let's provide an example::
5+
6+
import asyncio
7+
import peewee
8+
import logging
9+
from peewee_async import PooledPostgresqlDatabase
10+
11+
database = PooledPostgresqlDatabase('test')
12+
13+
# Disable sync queries
14+
database.set_allow_sync(False)
15+
16+
# Let's define a simple model:
17+
class PageBlock(peewee_async.AioModel):
18+
key = peewee.CharField(max_length=40, unique=True)
19+
text = peewee.TextField(default='')
20+
21+
class Meta:
22+
database = database
23+
24+
-- as you can see, nothing special in this code, just plain ``peewee_async.AioModel`` definition and disabling sync queries.
25+
26+
Now we need to create a table for model::
27+
28+
with database.allow_sync():
29+
PageBlock.create_table(True)
30+
31+
-- this code is **sync**, and will do **absolutely the same thing** as would do code with regular ``peewee.PostgresqlDatabase``. This is intentional, I believe there's just no need to run database initialization code asynchronously! *Less code, less errors*.
32+
33+
Finally, let's do something async::
34+
35+
async def my_async_func():
36+
# Add new page block
37+
await PageBlock.aio_create(
38+
key='title',
39+
text="Peewee is AWESOME with async!"
40+
)
41+
42+
# Get one by key
43+
title = await PageBlock.aio_get(key='title')
44+
print("Was:", title.text)
45+
46+
# Save with new text using manager
47+
title.text = "Peewee is SUPER awesome with async!"
48+
await title.aio_save()
49+
print("New:", title.text)
50+
51+
loop.run_until_complete(my_async_func())
52+
loop.close()
53+
54+
**That's it!** As you may notice there's no need to connect and re-connect before executing async queries! It's all automatic. But you can run ``AioDatabase.aio_connect()`` or ``AioDatabase.aio_close()`` when you need it.
55+
56+
And you can use methods from from **AioModel** for operations like selecting, deleting etc.
57+
All of them you can find in the next section.

0 commit comments

Comments
 (0)