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.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e90b0cb7e7627c865287ff40a0812e15dfc1a9855e0f571a37446ee033b5fdbe |
|
MD5 | e25946e8df45cd45fe0660b535b1975d |
|
BLAKE2b-256 | 5ea691a12370da276493ee21f03123649ae994eff085cdb44d43475c820f2d48 |
Close
Hashes for fastapi_async_sqlalchemy-0.3.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 594793e28c69c69338093e4611dff3d953493877721732f61a95d21bcf635954 |
|
MD5 | e2a630c846e30b67a534c0c4af7641da |
|
BLAKE2b-256 | 4db45b9912ed1614f0a830b6c4371444be45f53174be3fa34037f0b221989242 |