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.12.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fe666a2dffa78a3ca811497f06725107534ce77e381e112683520e60f2ef40c |
|
MD5 | 5e798f48fb2fbd9345f735313b26301f |
|
BLAKE2b-256 | ada08b90a1c352e409d6d35e29f4879c77ae99a9c7fd0a71db0acb334b15461d |
Close
Hashes for fastapi_async_sqlalchemy-0.3.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13c4d492c464c3442c097fe4db9017bddbb155bc5024a1087c808eaf3c1efea5 |
|
MD5 | 87269bd4db842639adccc1fc3bccfed9 |
|
BLAKE2b-256 | e8d1fc08c373588d1091e6f99a1b47449119b8b6c80e33beeb02338aca4a4c85 |