--- id: fastapi-sqlalchemy version: "0.2.1" license: MIT license_treatment: permissive maintenance: dormant --- # FastAPI-SQLAlchemy — Adds simple SQLAlchemy support to FastAPI License: permissive · Maintenance: dormant · Downloads: 109.4K/mo ## What it is and what it does FastAPI-SQLAlchemy is a thin integration layer that attaches SQLAlchemy session management to FastAPI applications via middleware. It provides a global `db` object that gives routes automatic access to a request-scoped database session, and also supports out-of-request database access (e.g., in background tasks) by using the same `db` object as a context manager. The package relies on Python 3.7+ ContextVar to isolate sessions per request, ensuring thread-safe and async-safe behavior. The package is designed for developers who want to use SQLAlchemy's ORM with FastAPI without manually managing session lifecycles in each route. It abstracts away the boilerplate of creating, binding, and cleaning up sessions, letting you focus on query logic. However, because the package entered dormancy after 2020, it may not reflect current best practices or compatibility with recent FastAPI and SQLAlchemy releases. Use it for: - Add SQLAlchemy ORM support to a new FastAPI REST API without writing custom session management code. - Query a database from FastAPI route handlers using the global `db.session` object. - Access the database in background tasks or scheduled jobs using `db()` as a context manager. - Migrate a Flask-SQLAlchemy application to FastAPI while keeping similar session patterns. ## Worth the install? AI-flagged interpretation of the facts above — verify before relying. Provides middleware and helpers to integrate SQLAlchemy database sessions with FastAPI applications, enabling both request-scoped and background task database access. Yes, if you are starting a new FastAPI project with SQLAlchemy and are comfortable with a dormant package. The integration is straightforward and low-friction. No, if you require active maintenance, up-to-date compatibility with the latest FastAPI and SQLAlchemy versions, or ongoing support—consider alternatives or manage sessions manually. The lack of updates since 2020 is a significant concern for long-term projects. ## Install pip install fastapi-sqlalchemy uv add fastapi-sqlalchemy poetry add fastapi-sqlalchemy ## Installing FastAPI-SQLAlchemy Before you install: Low friction install with just two runtime dependencies (starlette and SQLAlchemy). However, the package is dormant—last release was 2020-09-25 and last commit 2024-04-09—so expect no active maintenance or support for newer FastAPI or SQLAlchemy versions. License in practice: MIT license (permissive) means you can use, modify, and distribute this package freely in both open-source and commercial projects with minimal restrictions. Quickstart: pip install fastapi-sqlalchemy from fastapi import FastAPI from fastapi_sqlalchemy import DBSessionMiddleware, db app = FastAPI() app.add_middleware(DBSessionMiddleware, db_url="sqlite://") @app.get("/users") def get_users(): users = db.session.query(User).all() return users Requires Python 3.7 or later; session access relies on ContextVar, which is built into Python 3.7+. Verify before relying: - Compatibility with FastAPI versions released after 2020-09-25 is unclear given the dormant maintenance status. - Whether the package works with current SQLAlchemy 2.x API changes is not documented in the fact sheet. ## Package facts - License: MIT (permissive) - Python support: supports_current - Install friction: low - Maintenance: dormant - Downloads: 109.4K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags fastapi sqlalchemy integration, fastapi database session middleware, sqlalchemy fastapi orm, fastapi async database, fastapi db session management, fastapi, sqlalchemy, orm [View on SkillFed](https://skillfed.io/packages/fastapi-sqlalchemy) · [View on PyPI](https://pypi.org/project/fastapi-sqlalchemy/)