Skip to main content

SQLAlchemy middleware for FastAPI

Project description

SQLAlchemy FastAPI middleware

ci codecov License: MIT pip Downloads Updates

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-async-sqlalchemy-0.3.10.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_async_sqlalchemy-0.3.10-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-async-sqlalchemy-0.3.10.tar.gz.

File metadata

  • Download URL: fastapi-async-sqlalchemy-0.3.10.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for fastapi-async-sqlalchemy-0.3.10.tar.gz
Algorithm Hash digest
SHA256 7ee2933c917d7e46187b8cc0c34dad50796afca0ce7576546acdf03671e4ea95
MD5 e27da9bf1c6953f001d1961601b347dc
BLAKE2b-256 1d9b4b0cd56398eb793efe2c5d07fa6482c58299fd6d088483c2368dd5fcb4e1

See more details on using hashes here.

File details

Details for the file fastapi_async_sqlalchemy-0.3.10-py3-none-any.whl.

File metadata

  • Download URL: fastapi_async_sqlalchemy-0.3.10-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for fastapi_async_sqlalchemy-0.3.10-py3-none-any.whl
Algorithm Hash digest
SHA256 2037df4e4734e16709db5f3130ee619b1b896e7512d31403e4003fb61bacc839
MD5 b4fafbd3cf6f29e8869e04f5052ca521
BLAKE2b-256 c54b695391c46c9051c1d94fb721d8b9fc04dce9559481a53fca4674defda300

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page