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.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef1320e41a054e4b794c8d0dcd697c79c2d5ad0b12a0840a7d52be12d271e345 |
|
MD5 | c68af56e74858c78998bd29769773105 |
|
BLAKE2b-256 | 17f7b050b20e582fae72f4b858068b7f4e0c96ae4adf90f0a6a8bfae035b3eec |
Close
Hashes for fastapi_async_sqlalchemy-0.3.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4fbbc80ebec091877694b1ab6f3942a94903b243fbdb70a3fa8de1c3dd72334 |
|
MD5 | 988aba6563a8c7724456bbf3a4153562 |
|
BLAKE2b-256 | fd9c472ca6be0d3df15d8a1297100d26820016955d2d4f0a69953260343da275 |