r/FastAPI • u/One_Fuel_4147 • Aug 24 '24
Question I need some advice on my new FastAPI project using ContextVar
Hello everyone,
I've recently bootstrapped a new project using FastAPI and wanted to share my approach, especially how I'm using ContextVar with SQLAlchemy and asyncpg for managing asynchronous database sessions. Below is a quick overview of my project structure and some code snippets. I would appreciate any feedback or advice!
Project structure:
/app
├── __init__.py
├── main.py
├── contexts.py
├── depends.py
├── config.py
├── ...
├── modules/
│ ├── __init__.py
│ ├── post/
│ │ ├── __init__.py
│ │ ├── models.py
│ │ ├── repository.py
│ │ ├── exceptions.py
│ │ ├── service.py
│ │ ├── schemas.py
│ │ └── api.py1. Defining a Generic ContextWrapper
To manage the database session within a ContextVar, I created a ContextWrapper class in contexts.py. This wrapper makes it easier to set, reset, and retrieve the context value.
# app/contexts.py
from contextvars import ContextVar, Token
from typing import Generic, TypeVar
from sqlalchemy.ext.asyncio import AsyncSession
T = TypeVar("T")
class ContextWrapper(Generic[T]):
def __init__(self, value: ContextVar[T]):
self.__value: ContextVar[T] = value
def set(self, value: T) -> Token[T]:
return self.__value.set(value)
def reset(self, token: Token[T]) -> None:
self.__value.reset(token)
@property
def value(self) -> T:
return self.__value.get()
db_ctx = ContextWrapper[AsyncSession](ContextVar("db", default=None))2. Creating Dependency
In depends.py, I created a dependency to manage the lifecycle of the database session. This will ensure that the session is properly committed or rolled back and reset in the ContextVar after each request.
# app/depends.py
from fastapi import Depends
from app.contexts import db_ctx
from app.database.engine import AsyncSessionFactory
async def get_db():
async with AsyncSessionFactory() as session:
token = db_ctx.set(session)
try:
yield
await session.commit()
except:
await session.rollback()
raise
finally:
db_ctx.reset(token)
DependDB = Depends(get_db)3. Repository
In repository.py, I defined the data access methods. The db_ctx value is used to execute queries within the current context.
# modules/post/repository.py
from sqlalchemy import select
from uuid import UUID
from .models import Post
from app.contexts import db_ctx
async def find_by_id(post_id: UUID) -> Post | None:
stmt = select(Post).where(Post.id == post_id)
result = await db_ctx.value.execute(stmt)
return result.scalar_one_or_none()
async def save(post: Post) -> Post:
db_ctx.value.add(post)
await db_ctx.value.flush()
return post4. Schemas
The schemas.py file defines the request and response schemas for the Post module.
# modules/post/schemas.py
from pydantic import Field
from app.schemas import BaseResponse, BaseRequest
from uuid import UUID
from datetime import datetime
class CreatePostRequest(BaseRequest):
title: str = Field(..., min_length=1, max_length=255)
content: str = Field(..., min_length=1)
class PostResponse(BaseResponse):
id: uuid.UUID
title: str content: str
created_at: datetime
updated_at: datetime5. Service layer
In service.py, I encapsulate the business logic. The service functions return the appropriate response schemas and raise exceptions when needed. Exception is inherit from another that contains status, message and catch global by FastAPI.
# modules/post/service.py
from uuid import UUID
from . import repository as post_repository
from .schemas import CreatePostRequest, PostResponse
from .exceptions import PostNotFoundException
async def create(*, request: CreatePostRequest) -> PostResponse:
post = Post(title=request.title, content=request.content)
created_post = await post_repository.save(post)
return PostResponse.model_validate(created_post)
async def get_by_id(*, post_id: UUID) -> PostResponse:
post = await post_repository.find_by_id(post_id)
if not post:
raise PostNotFoundException()
return PostResponse.model_validate(post)6. API Routes
Finally, in api.py, I define the API endpoints and use the service functions to handle the logic. I'm using msgspec for faster JSON serialization.
# modules/post/api.py
from fastapi import APIRouter, Body
from uuid import UUID
from . import service as post_service
from .schemas import CreatePostRequest, PostResponse
from app.depends import DependDB
router = APIRouter()
@router.post(
"",
status_code=201,
summary="Create new post",
responses={201: {"model": PostResponse}},
dependencies = [DependDB], # Ensure the database context is available for this endpoint
)
async def create_post(*, request: CreatePostRequest = Body(...)):
response = await post_service.create(request=request)
return JSONResponse(content=response)Conclusion
This approach allows me to keep the database session context within the request scope, making it easier to manage transactions. I've also found that this structure helps keep the code organized and modular.
I'm curious to hear your thoughts on this approach and if there are any areas where I could improve or streamline things further. Thanks in advance!
1
u/Smok3dSalmon Aug 24 '24
To preface, the code looks good to me. But if I had a gun to my head and had to find something wrong then, in service.py create what is the purpose of the first line?
Are you relying on get_user_by_id to throw an exception if the id doesn’t exist. If you are, then this line of code’s value is not obvious or explicit… It’s certainly not self documenting unless you’re familiar with the entire project.
I could see someone deleting it because you’re not using the variable.
You could potentially raise a unique exception here because someone may be attempting to create a new post with a new author id.
It would have to be a lot of mistakes that could cause this edge case. Maybe it could occur if someone is writing test cases or running tests without bootstrapping or populating the db.
Someone could also infer this edge case occurred by reading their stack trace.
AuthorNotFoundException caught in Post.create
1
u/suuhreddit Aug 24 '24
thanks for sharing this. got somewhere similar by following https://github.com/zhanymkanov/fastapi-best-practices
question about the session. do you have longer running endpoints? if yes, when you use db session as dependency, won't that hold onto for the db transaction for unnecessary amount of time?
3
u/coldflame563 Aug 25 '24
I would rename post to something more descriptive. Given it’s an api based app, it’s a tad confusing just from overview.