Skip to content

Commit d0494ea

Browse files
committed
Move the session dependency to settings
1 parent 993bbdb commit d0494ea

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

api/dependencies.py

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

api/settings.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from typing import Annotated, AsyncGenerator
2+
13
import pydantic
2-
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
4+
from fastapi import Depends
5+
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
36

47
from api.utils.settings import CJMSBaseSettings
58

@@ -18,7 +21,17 @@ class Connections:
1821
"""How to connect to other, internal services."""
1922

2023
DB_ENGINE = create_async_engine(ConnectionURLs.DATABASE_URL.get_secret_value())
21-
DB_SESSION = async_sessionmaker(DB_ENGINE)
24+
DB_SESSION_MAKER = async_sessionmaker(DB_ENGINE)
25+
26+
27+
async def _get_db_session() -> AsyncGenerator[AsyncSession, None]:
28+
"""A dependency to pass a database session to every route function."""
29+
async with Connections.DB_SESSION_MAKER() as session:
30+
async with session.begin():
31+
yield session
32+
33+
34+
DBSession = Annotated[AsyncSession, Depends(_get_db_session)]
2235

2336

2437
class _Server(CJMSBaseSettings):

0 commit comments

Comments
 (0)