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",
engine_args={ # engine arguments example
"echo": True, # print all SQL statements
"pool_pre_ping": True, # feature will normally emit SQL equivalent to “SELECT 1” each time a connection is checked out from the pool
"pool_size": 5, # number of connections to keep open at a time
"max_overflow": 10, # number of connections to allow to be opened above pool_size
"connect_args": {
"prepared_statement_cache_size": 0, # disable prepared statement cache
"statement_cache_size": 0, # disable statement cache
},
},
)
# once the middleware is applied, any route can then access the database session
# from the global ``db``
foo = table("ms_files", column("id"))
# Usage inside of a route
@app.get("/")
async def get_files():
result = await db.session.execute(foo.select())
return result.fetchall()
async def get_db_fetch():
# It uses the same ``db`` object and use it as a context manager:
async with db():
result = await db.session.execute(foo.select())
return result.fetchall()
# Usage inside of a route using a db context
@app.get("/db_context")
async def db_context():
return await get_db_fetch()
# Usage outside of a route using a db context
@app.on_event("startup")
async def on_startup():
# We are outside of a request context, therefore we cannot rely on ``SQLAlchemyMiddleware``
# to create a database session for us.
result = await get_db_fetch()
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.14.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f714bf5803a75e83fdefdd5f1672b2930a1fe94c12fee3cf621dbe0f49915fe |
|
MD5 | ae4b047b21195c8d5407f39c5691a96a |
|
BLAKE2b-256 | 8fcc6d5702e0c047c3495afbcd6b8f329e46fd3b3ae7b09b4393c098a036c570 |
Close
Hashes for fastapi_async_sqlalchemy-0.3.14-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6fa6b413dfa2b24f477639f7cf55806b492f7a55848a52bc3e91e74b4b626276 |
|
MD5 | 2a8c3cdd7891a179edd86240e74061ae |
|
BLAKE2b-256 | d2fe9679c807aba710c1fe907b8542fa6da898f8968b791fb9cfb1d55eb6f4f8 |