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.10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ee2933c917d7e46187b8cc0c34dad50796afca0ce7576546acdf03671e4ea95 |
|
MD5 | e27da9bf1c6953f001d1961601b347dc |
|
BLAKE2b-256 | 1d9b4b0cd56398eb793efe2c5d07fa6482c58299fd6d088483c2368dd5fcb4e1 |
Close
Hashes for fastapi_async_sqlalchemy-0.3.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2037df4e4734e16709db5f3130ee619b1b896e7512d31403e4003fb61bacc839 |
|
MD5 | b4fafbd3cf6f29e8869e04f5052ca521 |
|
BLAKE2b-256 | c54b695391c46c9051c1d94fb721d8b9fc04dce9559481a53fca4674defda300 |