Skip to main content

A convenient way to configure and work with an async SQLAlchemy session through context in asynchronous applications

Project description

context-async-sqlalchemy

PyPI PyPI Downloads Tests (coverage > 90%)

DOCUMENTATION

Provides a super convenient way to work with SQLAlchemy in asynchronous applications. It handles the lifecycle management of the engine, sessions, and transactions.

The main goal is to provide quick and easy access to a session, without worrying about opening or closing it when it's not necessary.

Key features:

  • Automatically manages the lifecycle of the engine, sessions, and transactions
  • Allows for user autonomy when manually opening or closing sessions and transactions
  • Framework-agnostic
  • Not a wrapper around SQLAlchemy
  • Convenient for testing
  • Runtime host switching
  • Supports multiple databases and sessions per database
  • Provides tools for running concurrent SQL queries
  • Fully lazy initialization

Example of a typical session

from context_async_sqlalchemy import db_session
from sqlalchemy import insert

from database import connection  # your configured connection to the database
from models import ExampleTable  # a model for example

async def some_func() -> None:
    # Creates a session with no connection to the database yet
    session = await db_session(connection)
    
    stmt = insert(ExampleTable).values(text="example_with_db_session")

    # A connection and transaction open in the first request.
    await session.execute(stmt)
    
    # If you call db_session again, it returns the same session
    # even in child coroutines.
    session = await db_session(connection)
    
    # The second request uses the same connection and the same transaction
    await session.execute(stmt)

    # The commit and closing of the session occurs automatically

How it works

basic schema.png

  1. Before executing your code, the middleware prepares a container in which the sessions required by your code are stored. The container is saved in contextvars.
  2. Your code accesses the library to create new sessions and retrieve existing ones.
  3. The middleware automatically commits or rolls back open transactions. It also closes open sessions and clears the context.

The library provides the ability to commit, roll back, and close at any

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

context_async_sqlalchemy-2.1.9.tar.gz (3.5 MB view details)

Uploaded Source

Built Distribution

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

context_async_sqlalchemy-2.1.9-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file context_async_sqlalchemy-2.1.9.tar.gz.

File metadata

File hashes

Hashes for context_async_sqlalchemy-2.1.9.tar.gz
Algorithm Hash digest
SHA256 67efc9070f70c375067a4df1db3afcab3c96f659dbef3d357f96109bb3719889
MD5 4e3907fa31221cba783ea96258fdc078
BLAKE2b-256 0dcb6407034a21ecc19a5d94687a937d9a8e513fe5b223d2493b2f2e62eaea0e

See more details on using hashes here.

File details

Details for the file context_async_sqlalchemy-2.1.9-py3-none-any.whl.

File metadata

File hashes

Hashes for context_async_sqlalchemy-2.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 27a3338be54e04f1cf15163bfc6028154bd65e914e5892ced8c4cdb89d6ac4b5
MD5 86325b9e7a84636c520f299e1a862e0e
BLAKE2b-256 46e68ecafc44d9ea2aab0ff397aedd1ad9f947c67ad153b85679cd99e74c0782

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