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.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6190b186ec3987ff8ba5fcceab95cbbb2b1e0059abbe3a9556ba2b6c37e2e1d1 |
|
MD5 | 8c0d4bdef7d205e4834bfef21eaf4321 |
|
BLAKE2b-256 | 4180ca963ffa7a9c3e99956b03a4c75cc6cc9eefb6f59ec1dc71bc890d7b1c35 |
Close
Hashes for fastapi_async_sqlalchemy-0.3.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bac2d298e93e8fe004eeadec7f9c92ae479dc8b5625653e80238b8b59a5fc720 |
|
MD5 | 3f69d3829915eef699da8bd69b0288c3 |
|
BLAKE2b-256 | cef26a4c292984f1b7e609342f047d894af580bf8b36c9ed86da986ac092e6e5 |