r/FastAPI • u/LordPeter_s • Sep 04 '25
feedback request I built a library to handle complex SQLAlchemy queries with a clean architecture, inspired by shadcn/ui.
Hey everyone,
A while back, I shared the first version of a library I was working on. After a lot of great feedback and more development, I'm back with a much more refined version of fastapi-query-builder.
My goal was to solve a problem I'm sure many of you have faced: your FastAPI routes get cluttered with repetitive logic for filtering, sorting, pagination, and searching SQLAlchemy models, especially across relationships.
To fix this, I designed a library that not only provides powerful query features but also encourages a clean, reusable architecture. The most unique part is its installation, inspired by shadcn/ui. You run query-builder init, and it copies the source code directly into your project. You own the code, so you can customize and extend it freely.
GitHub Repo: https://github.com/Pedroffda/fastapi-query-builder
The Core Idea: A Clean Architecture for Your Endpoints
The library is built around a three-layer pattern (UseCase, Service, Mapper) that integrates perfectly with FastAPI's dependency injection.
BaseService: This is your data layer. It uses the core QueryBuilder to talk to the database. It only knows about SQLAlchemy models.
BaseMapper: Your presentation layer. It's a master at converting SQLAlchemy models to Pydantic schemas, handling nested relationships and dynamic field selection (select_fields) without breaking a sweat.
BaseUseCase: Your business logic layer. It coordinates the service and mapper. This is what your endpoint depends on, keeping your route logic minimal and clean.
See it in Action: From Complex Logic to a Single Dependency
Here’s a quick example of setting up a Post model that has a relationship to a User.
First, the one-time setup:
# --- In your project, after running 'query-builder init' ---
# Import from your new local 'query_builder/' directory
from query_builder import BaseService, BaseMapper, BaseUseCase, get_dynamic_relations_map
from your_models import User, Post
from your_schemas import UserView, PostView
# 1. Define Mappers to convert DB models to Pydantic schemas
user_mapper = BaseMapper(model_class=User, view_class=UserView, ...)
post_mapper = BaseMapper(
model_class=Post, view_class=PostView,
relationship_map={'user': {'mapper': user_mapper.map_to_view, ...}}
)
# 2. Define the Service to handle all DB logic
post_service = BaseService(
model_class=Post,
relationship_map=get_dynamic_relations_map(Post),
searchable_fields=["title", "user.name"] # Search across relationships!
)
# 3. Define the UseCase to orchestrate everything
post_use_case = BaseUseCase(
service=post_service,
map_to_view=post_mapper.map_to_view,
map_list_to_view=post_mapper.map_list_to_view
)Now, look how clean your FastAPI endpoint becomes:
from query_builder import QueryBuilder
query_builder = QueryBuilder()
.get("/posts", response_model=...)
async def get_posts(
db: Session = Depends(get_db),
query_params: QueryParams = Depends(), # Captures all filter[...][...] params
# Your standard pagination and sorting params...
skip: int = Query(0),
limit: int = Query(100),
search: Optional[str] = Query(None),
sort_by: Optional[str] = Query(None),
select_fields: Optional[str] = Query(None, description="Ex: id,title,user.id,user.name")
):
filter_params = query_builder.parse_filters(query_params)
# Just call the use case. That's it.
return await post_use_case.get_all(
db=db,
filter_params=filter_params,
skip=skip, limit=limit, search=search, sort_by=sort_by,
select_fields=select_fields
)This single, clean endpoint now supports incredibly powerful queries out-of-the-box:
Filter on a nested relationship: .../posts?filter[user.name][ilike]=%pedro%
Sort by a related field: .../posts?sort_by=user.name
Dynamically select fields to prevent over-fetching and create custom views: .../posts?select_fields=id,title,user.id,user.name
Key Features for FastAPI Developers:
Clean, Three-Layer Architecture: A production-ready pattern for structuring your business logic.
shadcn/ui-style init Command: No more black-box dependencies. You get the source and full control.
Powerful Filtering & Sorting: Supports 13+ operators (ilike, in, gte, etc.) on nested models.
Dynamic Field Selection (select_fields): Easily build GraphQL-like flexibility into your REST APIs.
Built-in Soft Delete & Multi-Tenancy Support: Common real-world requirements are handled for you.
Looking for Your Feedback!
As FastAPI developers, what are your thoughts?
Is this architectural pattern something you'd find useful for your projects?
What do you think of the init command and owning the code vs. a traditional package?
What's the most critical feature I might have missed?
The library is on TestPyPI, and I'm looking to do a full release after incorporating feedback from the community that uses FastAPI every day.
TestPyPI Link: https://test.pypi.org/project/fastapi-query-builder/
Thanks for taking a look