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.1.dev5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ab569798e4ae059dd237d9f6c90ac2f53ece8f68568c2844f5df444a87ccfd2 |
|
MD5 | 5069a0deaf8353c44ff1188d1d9d1d2a |
|
BLAKE2b-256 | e0146f27db543e23aff0f4802254806b889fcf56f9e2cdcd35acca5049e7460a |
Close
Hashes for fastapi_async_sqlalchemy-0.3.1.dev5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ba1bbbe2349786032368b603a03dad7d4b7d4bc101284ee131fcf1bc2009f09 |
|
MD5 | 178f58ca3c0607683ea0d468f46f93e8 |
|
BLAKE2b-256 | 8f6eac44378910a610f3d8c593d6fd03250eb9d7434f67856b8edc7023e55157 |