Skip to content

Commit d501981

Browse files
author
Rodrigo Martins de Oliveira
authored
Support for aiohttp (tortoise#473)
1 parent 7e5a384 commit d501981

14 files changed

Lines changed: 194 additions & 4 deletions

File tree

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ omit =
99
tortoise/contrib/sanic/*
1010
tortoise/contrib/starlette/*
1111
tortoise/contrib/fastapi/*
12+
tortoise/contrib/aiohttp/*
1213
[report]
1314
show_missing = True

CONTRIBUTORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Contributors
3535
* Seo Jiyeon ``jiyeonseo``
3636
* Blake Watters ``blakewatters``
3737
* Alexey Tylindus ``@mirrorrim``
38+
* Rodrigo Oliveira ``@allrod5``
3839

3940
Special Thanks
4041
==============

docs/contrib.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Contrib
1111
contrib/quart
1212
contrib/sanic
1313
contrib/starlette
14+
contrib/aiohttp
1415

1516

1617

docs/contrib/aiohttp.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. _contrib_aiohttp:
2+
3+
================================
4+
Tortoise-ORM aiohttp integration
5+
================================
6+
7+
We have a lightweight integration util ``tortoise.contrib.aiohttp`` which has a single function ``register_tortoise`` which sets up Tortoise-ORM on startup and cleans up on teardown.
8+
9+
See the :ref:`example_aiohttp`
10+
11+
Reference
12+
=========
13+
14+
.. automodule:: tortoise.contrib.aiohttp
15+
:members:
16+
:undoc-members:
17+
:show-inheritance:

docs/examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Examples
1313
examples/quart
1414
examples/sanic
1515
examples/starlette
16+
examples/aiohttp

docs/examples/aiohttp.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. _example_aiohttp:
2+
3+
===============
4+
AIOHTTP Example
5+
===============
6+
7+
This is an example of the :ref:`contrib_aiohttp`
8+
9+
**Usage:**
10+
11+
.. code-block:: sh
12+
13+
python3 main.py
14+
15+
16+
models.py
17+
=========
18+
.. literalinclude:: ../../examples/aiohttp/models.py
19+
20+
main.py
21+
=======
22+
.. literalinclude:: ../../examples/aiohttp/main.py
23+
24+

examples/aiohttp/README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Tortoise-ORM aiohttp example
2+
============================
3+
4+
We have a lightweight integration util ``tortoise.contrib.aiohttp`` which has a single function ``register_tortoise`` which sets up Tortoise-ORM on startup and cleans up on teardown.
5+
6+
Usage
7+
-----
8+
9+
.. code-block:: sh
10+
11+
python3 main.py

examples/aiohttp/__init__.py

Whitespace-only changes.

examples/aiohttp/main.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# pylint: disable=E0401,E0611
2+
import logging
3+
4+
from aiohttp import web
5+
6+
from models import Users
7+
from tortoise.contrib.aiohttp import register_tortoise
8+
9+
logging.basicConfig(level=logging.DEBUG)
10+
11+
12+
async def list_all(request):
13+
users = await Users.all()
14+
return web.json_response({"users": [str(user) for user in users]})
15+
16+
17+
async def add_user(request):
18+
user = await Users.create(name="New User")
19+
return web.json_response({"user": str(user)})
20+
21+
22+
app = web.Application()
23+
app.add_routes([web.get("/", list_all), web.post("/user", add_user)])
24+
register_tortoise(
25+
app, db_url="sqlite://:memory:", modules={"models": ["models"]}, generate_schemas=True
26+
)
27+
28+
29+
if __name__ == "__main__":
30+
web.run_app(app, port=5000)

examples/aiohttp/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from tortoise import Model, fields
2+
3+
4+
class Users(Model):
5+
id = fields.IntField(pk=True)
6+
name = fields.CharField(50)
7+
8+
def __str__(self):
9+
return f"User {self.id}: {self.name}"

0 commit comments

Comments
 (0)