https://github.com/abhiaagarwal/asgi-sqlalchemy
ASGI Middleware that manages the lifespan of a database request
https://github.com/abhiaagarwal/asgi-sqlalchemy
asgi fastapi python sqlalchemy
Last synced: about 2 months ago
JSON representation
ASGI Middleware that manages the lifespan of a database request
- Host: GitHub
- URL: https://github.com/abhiaagarwal/asgi-sqlalchemy
- Owner: abhiaagarwal
- License: mit
- Created: 2025-01-02T04:24:48.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-01-02T16:08:08.000Z (4 months ago)
- Last Synced: 2025-02-08T08:48:21.344Z (3 months ago)
- Topics: asgi, fastapi, python, sqlalchemy
- Language: Python
- Homepage: https://pypi.org/project/asgi-sqlalchemy/
- Size: 42 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# asgi-sqlalchemy
ASGI Middleware that manages the lifespan of a database engine and a corresponding session.
## Usage:
### FastAPI:
```python
from contextlib import AsyncContextManager
from collections.abc import AsyncGenerator
from typing_extensions import TypedDictfrom fastapi import FastAPI
from asgi_sqlalchemy import DatabaseContext, SessionMiddleware
from asgi_sqlalchemy.fastapi import SessionDependencyclass AppState(TypedDict):
db: DatabaseContextasync def lifespan() -> AsyncGenerator[AppState]:
async with DatabaseContext(...) as db:
yield {"db": db}app = FastAPI()
app.add_middleware(SessionMiddleware)@app.get("/db")
async def handler(session: SessionDependency) -> str:
# do something with your async session!
```