SQLAlchemy middleware for FastAPI
Project description
FastAPI Async SQLAlchemy middleware
Description
FastAPI-Async-SQLAlchemy provides middleware for FastAPI and SQLAlchemy using async AsyncSession and async engine. Based on FastAPI-SQLAlchemy
Install
pip install fastapi-async-sqlalchemy
Examples
Note that the session object provided by db.session
is based on the Python3.7+ ContextVar
. This means that
each session is linked to the individual request context in which it was created.
from fastapi import FastAPI
from fastapi_async_sqlalchemy import SQLAlchemyMiddleware
from fastapi_async_sqlalchemy import db # provide access to a database session
from sqlalchemy import column
from sqlalchemy import table
app = FastAPI()
app.add_middleware(
SQLAlchemyMiddleware,
db_url="postgresql+asyncpg://user:user@192.168.88.200:5432/primary_db"
)
foo = table("ms_files", column("id"))
@app.get("/")
async def get_files():
result = await db.session.execute(foo.select())
return result.fetchall()
@app.get("/db_context")
async def db_context():
async with db():
result = await db.session.execute(foo.select())
return result.fetchall()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8002)
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
Close
Hashes for fastapi-async-sqlalchemy-0.3.9.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 308e5b95b9196018597103c9a13177e43c5835b136eda933f0d37f4446e1c807 |
|
MD5 | ae313c9044334a4945fde98528af06be |
|
BLAKE2b-256 | 70b98d6a63564d88780305081daeee335a50d40bd3dde6e9793a36e5026682a1 |
Close
Hashes for fastapi_async_sqlalchemy-0.3.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94388f1998443241ce82969648186bc7690759d79b22eb2e13945a722877b752 |
|
MD5 | a3164627786a8e4467fe4eb709cc7925 |
|
BLAKE2b-256 | 2ef54415225d70e0dad3dc6f8b050501a8409cf26e0cea9f91a6df0416cb6e96 |