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 # middleware helper
from fastapi_async_sqlalchemy import db # an object to provide global 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.dev3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a074b34858f7914ed3bf1f7891dedc4eaf1a62486d702019b1a173732dbe833 |
|
MD5 | 58aeb0f7f6bac77c3aa36f72747ae47e |
|
BLAKE2b-256 | 5bb441bc667102499dcd7c25af55d35d36365730231df9125ce0300e05fd709d |
Close
Hashes for fastapi_async_sqlalchemy-0.3.1.dev3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b939db36db3916a10e7a2a8c97880354df1f505ebb229cd14ce24f044b89673 |
|
MD5 | ca97a8adb2cee9841f26d04e980b441b |
|
BLAKE2b-256 | 714136271adc071878b75c3dd7f60f5f5436a52e2a90ea1caf3c92ad3e5c78a1 |