Skip to main content

Build Status PyPi Documentation codecov.io

Falcon Middleware: SQLAlchemy Integration

The falcon-sqla package provides a middleware component for managing SQLAlchemy sessions. The manager component can also serve as a base building block or a recipe for more complex use cases, such as applications leveraging multiple database binds.

Installation

$ pip install falcon-sqla

Usage

The falcon_sqla session Manager can be used in two ways:

Configuration

  • Create a SQLAlchemy engine.

  • Pass the engine to the Manager() initializer as its first parameter.

  • If using the manager as a middleware component, pass its middleware property to a falcon.App's middleware list:

engine = create_engine('dialect+driver://my/database')
manager = falcon_sqla.Manager(engine)

app = falcon.App(middleware=[manager.middleware])

# The database session will be available as req.context.session

Context Manager

A falcon_sqla.Manager can also explicitly provide a database session using the session_scope() context manager:

# Somewhere inside a responder
with self.manager.session_scope(req, resp) as session:
    # Use the session
    # <...>

session_scope() can also be used as a standalone session context outside of the request-response cycle:

with self.manager.session_scope() as session:
    # Use the session
    # <...>

Custom Vertical Partitioning

Simple random selection of read- and write- database replicas is supported out of the box. Use the add_engine() method to instruct the Manager to include the provided engines in the runtime bind selection logic:

manager = falcon_sqla.Manager(engine)

read_replica = create_engine('dialect+driver://my/database.replica')
manager.add_engine(read_replica, falcon_sqla.EngineRole.READ)

The Manager.get_bind() method can be overridden to implement custom engine selection logic for more complex use cases.

See also this SQLAlchemy recipe: Custom Vertical Partitioning.

About Falcon

Falcon is the minimalist REST API and microservices framework for Python developers, with a focus on reliability, correctness, and performance at scale.

About SQLAlchemy

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.

Release files for falcon-sqla 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for falcon-sqla 0.5.0
File Size Uploaded
falcon_sqla-0.5.0.tar.gz 17.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for falcon-sqla 0.5.0
File Interpreter ABI Platform
falcon_sqla-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.8 kB

Release files / falcon_sqla-0.5.0.tar.gz

Download URL falcon_sqla-0.5.0.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
fc2fe44b756c96ba25d59ac5ce92c5705b29265845dad35c6c3eebb3369a4e37
BLAKE2b-256 checksum
How to use checksums
2673ac1356dc97d868549cbdbd5f91c0d7fe41753243f77d63d055d5f76ebe3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Release files / falcon_sqla-0.5.0-py3-none-any.whl

Download URL falcon_sqla-0.5.0-py3-none-any.whl
Size 16.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e7d3fc7037ba5725b582a647c148776bbc6febb6602e86a389a676e926b9a33b
BLAKE2b-256 checksum
How to use checksums
4b9014f09bb227a9af7c3ecc6dd21a4ae19c48a24ac0a2381e296e6d788ead38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page