File tree Expand file tree Collapse file tree 2 files changed +10
-9
lines changed
Expand file tree Collapse file tree 2 files changed +10
-9
lines changed Original file line number Diff line number Diff line change 1- from sqlalchemy .ext .asyncio import AsyncSession , create_async_engine
2- from sqlalchemy .orm import declarative_base , sessionmaker
1+ """Set up the database connection and session.""" ""
2+ from sqlalchemy .ext .asyncio import async_sessionmaker , create_async_engine
3+ from sqlalchemy .orm import declarative_base
34
45DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost/postgres"
56
67engine = create_async_engine (DATABASE_URL , echo = False )
78Base = declarative_base ()
8-
9-
10- async_session = sessionmaker (
11- engine , class_ = AsyncSession , expire_on_commit = False # type: ignore
12- )
9+ async_session = async_sessionmaker (engine , expire_on_commit = False )
Original file line number Diff line number Diff line change 1+ """An example of using FastAPI with Async SQLAlchemy 2."""
12import subprocess
23from contextlib import asynccontextmanager
34
@@ -29,8 +30,11 @@ async def lifespan(app: FastAPI):
2930
3031
3132async def get_db ():
32- """Get a database session."""
33- async with async_session () as session : # type: ignore
33+ """Get a database session.
34+
35+ To be used for dependency injection.
36+ """
37+ async with async_session () as session :
3438 async with session .begin ():
3539 yield session
3640
You can’t perform that action at this time.
0 commit comments