Skip to main content

Middleware for integrating Falcon applications with SQLAlchemy.

Project description

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, 'r')

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.

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

falcon-sqla-0.4.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

falcon_sqla-0.4.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file falcon-sqla-0.4.0.tar.gz.

File metadata

  • Download URL: falcon-sqla-0.4.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for falcon-sqla-0.4.0.tar.gz
Algorithm Hash digest
SHA256 b83be0b7b1a066d89ceae4b96445a601d2b290eaddf449970dc2bf9399585276
MD5 bf0abfaa43f1f4d9893c6381ab42dc63
BLAKE2b-256 09c9a8955f875857a91bb1d09bfbb8c5dd3cc8509a45535f6432a7ac508beb03

See more details on using hashes here.

File details

Details for the file falcon_sqla-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: falcon_sqla-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for falcon_sqla-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3aa4c87ffcb7ce88137679ac9efa4d16b2e2cee9c11f43216b403001252cc3c
MD5 2e3f9fbb8d1a4f23a424b1f15cab4fcd
BLAKE2b-256 eeea255ddb9c3f49bc435adbebe1c91ac0011319700df6e2b5644a013c417950

See more details on using hashes here.

Supported by

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