Skip to content

fastapi-fsp

Add fsp: FSPManager = Depends(FSPManager) to an endpoint, pass it a SQLModel or SQLAlchemy select(), and the endpoint accepts filters, sorting, pagination and multi-column search from the query string. No filter schema to declare per model.

GET /heroes/?search=spider+man&search_fields=name,secret_name&field=age&operator=gte&value=18&sort_by=name&page=2
{
  "data": [{"id": 7, "name": "Spider-Man", "secret_name": "Peter Parker", "age": 18}],
  "meta": {"pagination": {"total_items": 11, "per_page": 10, "current_page": 2, "total_pages": 2}},
  "links": {"self": "...", "first": "...", "next": null, "prev": "...", "last": "..."}
}

It is built for the backend of a data table: the search box, column filters, sortable headers and the pager all map to query parameters on one endpoint. See the React + TanStack Table example.

A TanStack Table searching, filtering, sorting and paging through a fastapi-fsp endpoint

Why fastapi-fsp?

fastapi-pagination and fastapi-filter are good libraries that each cover part of this. fastapi-fsp covers all of it in one dependency, for SQL databases:

fastapi-fsp fastapi-pagination fastapi-filter
Pagination ✅ page / per_page, total count, HATEOAS links ✅ page, limit-offset and cursor ➖ pair it with fastapi-pagination
Filtering ✅ 18 operators on any column ➖ ✅ via a Filter class per model
Sorting ✅ ➖ ✅
Search one term across several columns (OR) ✅ search + search_fields, per-word matching ➖ ✅ over the fields you declare
Indexed PostgreSQL search (tsvector, pg_trgm) ✅ ➖ ➖
Setup per endpoint one Depends(FSPManager) one Depends + paginate() a Filter class per model
Backends SQLModel / SQLAlchemy, sync and async SQLAlchemy, SQLModel, MongoDB, and more SQLAlchemy, MongoEngine

Choose fastapi-pagination if you need cursor pagination or a non-SQL backend. Choose fastapi-filter if you want every filter declared as a typed field that shows up in the OpenAPI schema. Choose fastapi-fsp if you want one dependency that turns a select() into a full list endpoint, with fast search on PostgreSQL. It also filters and sorts on computed hybrid_property fields.

Full documentation: fromej-dev.github.io/fastapi-fsp