/fastsqla:fastsqla-setupfrom fastsqla
Installs and configures FastSQLA with FastAPI, covering pip installation, async driver selection, environment variable configuration, and programmatic lifespan setup.
Ships in fastsqla. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
FastSQLA is an async SQLAlchemy 2.0+ extension for FastAPI. It provides session management, pagination, and deferred table reflection out of the box.
The entire library is a single module (fastsqla).
pip install FastSQLA
pip install FastSQLA[sqlmodel]
When SQLModel is installed, FastSQLA automatically uses sqlmodel.ext.asyncio.session.AsyncSession
instead of SQLAlchemy's AsyncSession.
You must install an async driver for your database. The driver determines the URL scheme:
| Database | Driver | Install | URL scheme |
|---|---|---|---|
| PostgreSQL | asyncpg | pip install asyncpg | postgresql+asyncpg:// |
| SQLite | aiosqlite | pip install aiosqlite | sqlite+aiosqlite:/// |
| MySQL | aiomysql | pip install aiomysql | mysql+aiomysql:// |
There are two ways to configure FastSQLA: environment variables or programmatic.
Use fastsqla.lifespan for 12-factor app style configuration. All configuration is
read from environment variables at startup.
from fastapi import FastAPI
from fastsqla import lifespan
app = FastAPI(lifespan=lifespan)
Set environment variables:
export SQLALCHEMY_URL=postgresql+asyncpg://user:pass@localhost/mydb
export SQLALCHEMY_POOL_SIZE=20
export SQLALCHEMY_MAX_OVERFLOW=10
export SQLALCHEMY_ECHO=true
How it works: All environment variables prefixed with SQLALCHEMY_ are collected
(case-insensitive) and passed to SQLAlchemy's async_engine_from_config() with prefix
sqlalchemy_. This means any create_async_engine parameter can be set via env var
using the SQLALCHEMY_ prefix.
Required: SQLALCHEMY_URL must be set. If missing, startup raises:
Missing sqlalchemy_url in environ.
Warning: Stray SQLALCHEMY_* environment variables (e.g. from another app or a
typo) will be passed to the engine factory and can cause unexpected errors. Keep your
environment clean.
Use new_lifespan() when you want to pass configuration directly in code. It accepts
the same arguments as SQLAlchemy's create_async_engine().
from fastapi import FastAPI
from fastsqla import new_lifespan
lifespan = new_lifespan(
"sqlite+aiosqlite:///app/db.sqlite",
connect_args={"autocommit": False},
)
app = FastAPI(lifespan=lifespan)
When using new_lifespan() with arguments, environment variables are not read.
new_lifespan() called with no arguments returns the same env-var-based lifespan as
fastsqla.lifespan.
If your app has multiple lifespan contexts (e.g. FastSQLA + another library), compose
them with AsyncExitStack:
from collections.abc import AsyncGenerator
from contextlib import AsyncExitStack, asynccontextmanager
from fastapi import FastAPI
from fastsqla import lifespan as fastsqla_lifespan
from other_library import lifespan as other_lifespan
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[dict, None]:
async with AsyncExitStack() as stack:
state1 = await stack.enter_async_context(fastsqla_lifespan(app))
state2 = await stack.enter_async_context(other_lifespan(app))
yield {**state1, **state2}
app = FastAPI(lifespan=lifespan)
Each lifespan returns a state dict. FastSQLA's lifespan returns
{"fastsqla_engine": <AsyncEngine>}. Merge them so FastAPI's request.state has all
keys.
On startup:
AsyncEngine via async_engine_from_config() — with prefix
sqlalchemy_ for the env var path, or with no prefix for the programmatic path.Base.prepare() inside a connection — this triggers SQLAlchemy's
DeferredReflection, reflecting table metadata from the database for any model
inheriting from fastsqla.Base.SessionFactory (async_sessionmaker).On shutdown:
SessionFactory (sets bind=None).What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub hadrien/fastsqla --plugin fastsqla3 skills in this plugin
/fastsqla-paginationPaginate SQLAlchemy select queries in FastAPI endpoints using FastSQLA. Covers fastsqla.Paginate (offset/limit) and fastsqla.cursor.Paginate (forward-only cursor/limit), Page/Item/Collection response models, and the new_pagination() factory for custom page sizes, count queries, and result processing.
/fastsqla-sessionManages async SQLAlchemy sessions in FastSQLA endpoints and background tasks. Covers the Session dependency (auto-commit/rollback lifecycle), flush vs commit rules, IntegrityError handling, and the open_session() context manager. Use when writing FastAPI endpoints or background tasks that interact with a database through FastSQLA.
Other plugins' skills for the same job