Skip to content

h0rn3t/fastapi-async-sqlalchemy

Repository files navigation

FastAPI Async SQLAlchemy middleware

ci codecov License: MIT pip Downloads

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)

About

FastAPI Async SQLAlchemy middleware

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages