You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FastAPI-Async-SQLAlchemy provides a simple integration between FastAPI_ and SQLAlchemy_ in async way. It gives access to useful helpers to facilitate the completion of common tasks.
13
-
Based on FastAPI-SQLAlchemy
10
+
FastAPI-Async-SQLAlchemy provides a simple integration between FastAPI_ and SQLAlchemy_ in async way. It gives access to useful helpers to facilitate the completion of common tasks. Based on FastAPI-SQLAlchemy
14
11
15
-
Installing
16
-
----------
12
+
### Install
17
13
18
-
Install and update using pip_:
14
+
```bash
15
+
pip install fastapi-async-sqlalchemy
16
+
```
19
17
20
-
.. code-block:: text
18
+
### Usage
21
19
22
-
$ pip install fastapi-async-sqlalchemy
20
+
FastAPI-Async-SQLAlchemy provides a simple integration between FastAPI_ and SQLAlchemy_ in async way. It gives access to useful helpers to facilitate the completion of common tasks.
21
+
Based on FastAPI-SQLAlchemy
23
22
23
+
### Examples
24
24
25
-
Examples
26
-
--------
27
25
28
26
Usage inside of a route
29
-
^^^^^^^^^^^^^^^^^^^^^^^
30
27
31
-
.. code-block:: python
28
+
29
+
```python
32
30
33
31
from fastapi import FastAPI
34
32
from fastapi_async_sqlalchemy import SQLAlchemyMiddleware # middleware helper
@@ -48,16 +46,15 @@ Usage inside of a route
48
46
users = db.session.query(User).all()
49
47
50
48
return users
49
+
```
51
50
52
51
Note that the session object provided by ``db.session`` is based on the Python3.7+ ``ContextVar``. This means that
53
52
each session is linked to the individual request context in which it was created.
54
53
55
-
Usage outside of a route
56
-
^^^^^^^^^^^^^^^^^^^^^^^^
57
-
54
+
### Usage outside of a route
58
55
Sometimes it is useful to be able to access the database outside the context of a request, such as in scheduled tasks which run in the background:
59
56
60
-
.. code-block:: python
57
+
```python
61
58
62
59
import pytz
63
60
from apscheduler.schedulers.asyncio import AsyncIOScheduler # other schedulers are available
@@ -94,8 +91,4 @@ Sometimes it is useful to be able to access the database outside the context of
94
91
# no longer able to access a database session once the db() context manager has ended
0 commit comments