Skip to content

Commit 4821342

Browse files
committed
document setup
1 parent a07c41e commit 4821342

File tree

4 files changed

+53
-58
lines changed

4 files changed

+53
-58
lines changed

docs/api.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

docs/index.md

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,49 +33,12 @@ pip install fastsqla
3333

3434
## Setup
3535

36-
### Lifespan
37-
38-
Setup using [FastSQLA lifespan context manager]
39-
40-
```python
41-
from fastapi import FastAPI
42-
from fastsqla import lifespan
43-
44-
45-
app = FastAPI(lifespan=lifespan)
46-
```
47-
48-
If you need to open more than one lifespan context, create an async context manager
49-
function to open as many lifespans as needed and set it as the lifespan of the app.
50-
Use an [AsyncExitStack][contextlib.AsyncExitStack] to open multiple lifespan contexts:
51-
52-
```python
53-
from collections.abc import AsyncGenerator
54-
from contextlib import asynccontextmanager
55-
56-
from fastapi import FastAPI
57-
from fastsqla import lifespan as fastsqla_lifespan
58-
from this_other_library import another_lifespan
59-
60-
61-
@asynccontextmanager
62-
async def lifespan(app:FastAPI) -> AsyncGenerator[dict, None]:
63-
async with AsyncExitStack() as stack:
64-
yield {
65-
**stack.enter_async_context(lifespan(app)),
66-
**stack.enter_async_context(another_lifespan(app)),
67-
}
68-
69-
70-
app = FastAPI(lifespan=lifespan)
71-
```
72-
73-
To know more about lifespan protocol:
74-
75-
* [Lifespan Protocol](https://asgi.readthedocs.io/en/latest/specs/lifespan.html)
76-
* [Use Lifespan State instead of `app.state`](https://github.com/Kludex/fastapi-tips?tab=readme-ov-file#6-use-lifespan-state-instead-of-appstate)
77-
* [FastAPI lifespan documentation](https://fastapi.tiangolo.com/advanced/events/):
36+
### `fastsqla.lifespan`
7837

38+
::: fastsqla.lifespan
39+
options:
40+
heading_level: false
41+
show_source: false
7942

8043
### Get an async SQLAlchemy session
8144

mkdocs.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ nav:
88
- Get Started:
99
- Welcome to FastSQLA: index.md
1010
- Configuration: configuration.md
11-
#- SQLAlchemy Session: session.md
12-
#- Pagination: pagination.md
13-
- API: api.md
1411
- Changelog: changelog.md
1512

1613
theme:

src/fastsqla.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,56 @@ class State(TypedDict):
4545

4646
@asynccontextmanager
4747
async def lifespan(app: FastAPI) -> AsyncGenerator[State, None]:
48-
"""A lifespan context manager that setup `FastSQLA` when opened.
48+
"""Use `fastsqla.lifespan` to set up SQLAlchemy.
4949
50-
This async context manager must be used to setup SQLAlchemy.
50+
In an ASGI application, [lifespan events](https://asgi.readthedocs.io/en/latest/specs/lifespan.html)
51+
are used to communicate startup & shutdown events.
5152
52-
TBD
53+
The [`lifespan`](https://fastapi.tiangolo.com/advanced/events/#lifespan) parameter of
54+
the `FastAPI` app can be set to a context manager. That context manager is
55+
opened when app starts and closed when app stops.
56+
57+
In order for `FastSQLA` to setup `SQLAlchemy` before the app is started, set
58+
`lifespan` parameter to `fastsqla.lifespan`:
59+
60+
```python
61+
from fastapi import FastAPI
62+
from fastsqla import lifespan
63+
64+
65+
app = FastAPI(lifespan=lifespan)
66+
```
67+
68+
If you need to open more than one lifespan context, create an async context manager
69+
function to open as many lifespans as needed and set it as the lifespan of the app.
70+
Use an [AsyncExitStack][contextlib.AsyncExitStack] to open multiple lifespan contexts:
71+
72+
```python
73+
from collections.abc import AsyncGenerator
74+
from contextlib import asynccontextmanager
75+
76+
from fastapi import FastAPI
77+
from fastsqla import lifespan as fastsqla_lifespan
78+
from this_other_library import another_lifespan
79+
80+
81+
@asynccontextmanager
82+
async def lifespan(app:FastAPI) -> AsyncGenerator[dict, None]:
83+
async with AsyncExitStack() as stack:
84+
yield {
85+
**stack.enter_async_context(lifespan(app)),
86+
**stack.enter_async_context(another_lifespan(app)),
87+
}
88+
89+
90+
app = FastAPI(lifespan=lifespan)
91+
```
92+
93+
To know more about lifespan protocol:
94+
95+
* [Lifespan Protocol](https://asgi.readthedocs.io/en/latest/specs/lifespan.html)
96+
* [Use Lifespan State instead of `app.state`](https://github.com/Kludex/fastapi-tips?tab=readme-ov-file#6-use-lifespan-state-instead-of-appstate)
97+
* [FastAPI lifespan documentation](https://fastapi.tiangolo.com/advanced/events/):
5398
"""
5499
prefix = "sqlalchemy_"
55100
sqla_config = {k.lower(): v for k, v in os.environ.items()}

0 commit comments

Comments
 (0)