SQLAlchemy middleware for FastAPI
Project description
SQLAlchemy FastAPI middleware
Description
Provides SQLAlchemy middleware for FastAPI using AsyncSession and async engine.
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.11.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 191acf8651862c1792683e11dd1b4e230d4bdf4e6946aed5bbcbbad035105dac |
|
MD5 | 13fd2fca2a2dc8ecb9cab14588419e94 |
|
BLAKE2b-256 | f2cb0045e780bbb344b01a20f59d1324f93991882a756e35ed290f88408eb045 |
Close
Hashes for fastapi_async_sqlalchemy-0.3.11-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f53b2607836e74edea0406e3e377d2703f38cc493efebac331d45fd7b560de2 |
|
MD5 | 0a470346787bceaebd3251c8357990e4 |
|
BLAKE2b-256 | a5e3e4134abdf72a2212af1a797da0c23cd13ce6c51b349d0e9d900cf89a3eff |