FastSQLA
Async SQLAlchemy 2.0+ for FastAPI — boilerplate, pagination, and seamless session management.

Documentation: https://hadrien.github.io/FastSQLA/
Github Repo: https://github.com/hadrien/fastsqla
FastSQLA is an async SQLAlchemy 2.0+
extension for FastAPI with built-in pagination,
SQLModel support and more.
It streamlines the configuration and asynchronous connection to relational databases by
providing boilerplate and intuitive helpers. Additionally, it offers built-in
customizable pagination and automatically manages the SQLAlchemy session lifecycle
following SQLAlchemy's best practices.
Features
-
Easy setup at app startup using
FastAPI Lifespan:
from fastapi import FastAPI
from fastsqla import lifespan
app = FastAPI(lifespan=lifespan)
-
SQLAlchemy async session dependency:
...
from fastsqla import Session
from sqlalchemy import select
...
@app.get("/heros")
async def get_heros(session:Session):
stmt = select(...)
result = await session.execute(stmt)
...
-
SQLAlchemy async session with an async context manager:
from fastsqla import open_session
async def background_job():
async with open_session() as session:
stmt = select(...)
result = await session.execute(stmt)
...
-
Built-in pagination with offset/limit:
...
from fastsqla import Page, Paginate
from sqlalchemy import select
...
@app.get("/heros", response_model=Page[HeroModel])
async def get_heros(paginate:Paginate):
return await paginate(select(Hero))
👇 /heros?offset=10&limit=10 👇
{
"data": [
{
"name": "The Flash",
"secret_identity": "Barry Allen",
"id": 11
},
{
"name": "Green Lantern",
"secret_identity": "Hal Jordan",
"id": 12
}
],
"meta": {
"offset": 10,
"total_items": 12,
"total_pages": 2,
"page_number": 2
}
}
-
Forward cursor pagination for "Load more" lists:
from fastsqla.cursor import Page, Paginate
@app.get("/heros")
async def get_heros(paginate: Paginate[Hero]) -> Page[HeroModel]:
return await paginate(select(Hero).order_by(Hero.id))
👇 /heros?limit=2&next_cursor=eyJ2IjoxLCJvcmRlciI6W1siaGVybyIsImlkIixmYWxzZSwiaW50Il1dLCJ2YWx1ZXMiOlsyXX0 👇