A production-grade async SQLAlchemy database plugin for FastAPI.
Project description
FastAPI Async DB Plugin
A reusable, boilerplate-free async database plugin for FastAPI using SQLAlchemy 2.0 and asyncpg.
Installation
pip install fastapi-async-db-plugin
Setup & Usage
Simply import the plugin functions into your FastAPI app and provide a .env file with DATABASE_URL:
DATABASE_URL=postgresql+asyncpg://user:password@localhost:5432/dbname
Example main.py
from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from fastapi_db_plugin import get_db_session, register_db_events
from fastapi_db_plugin.models_example import User
app = FastAPI(title="Reusable DB App Example")
# Automatically wire up connection pools and dispose them on shutdown.
register_db_events(app, create_all=True)
@app.post("/users/")
async def create_user(username: str, email: str, db: AsyncSession = Depends(get_db_session)):
new_user = User(username=username, email=email)
db.add(new_user)
await db.commit()
await db.refresh(new_user)
return {"id": new_user.id, "username": new_user.username, "email": new_user.email}
@app.get("/users/")
async def list_users(db: AsyncSession = Depends(get_db_session)):
result = await db.execute(select(User))
users = result.scalars().all()
return [{"id": u.id, "username": u.username, "email": u.email} for u in users]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_async_db_plugin-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_async_db_plugin-0.1.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4dae571a272e3a961876feb158b082716ea1d7a4215a2be4352d5e263f3cb713
|
|
| MD5 |
0339332d31436886dea35968caea32a7
|
|
| BLAKE2b-256 |
ee2b2958fc120f80548728f197113e3fa02a61b27c49c7e39bb64249b9ce751e
|
File details
Details for the file fastapi_async_db_plugin-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_async_db_plugin-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38766aaf1ae356a85da887dcb6be14d0ef76862810bce614908e336ceda92886
|
|
| MD5 |
0241a836a3254d5ca49e21651655201e
|
|
| BLAKE2b-256 |
674e82bf91102579cd775204339316e7dd986cb371518000b5eab2967c82be8f
|