Skip to main content

A convenient way to configure and interact with a async sqlalchemy session through context in asynchronous applications

Project description

context-async-sqlalchemy

PyPI

DOCUMENTATION

Provides a super convenient way to work with sqlalchemy in asynchronous applications. It takes care of the issues of managing the lifecycle of engine, session, and transactions without being a wrapper.

The main task is to get quick and easy access to the session and not worry about when to open and when to close it.

The key features are:

  • Super easy to use
  • Automatically manages the lifecycle of engine, session, and transaction (autocommit/autorollback)
  • It doesn't interfere with manually opening and closing sessions and transactions when needed.
  • Does not depend on the web framework
  • It is not a wrapper over sqlalchemy
  • It is convenient to test
  • Host switching in runtime
  • It can manage multiple databases and multiple sessions to a single database
  • Provides tools for concurrent sql queries
  • Lazy initialization is everywhere

What does usage look like?

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  # just some model for example

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

    # On the first request, a connection and transaction were opened
    await session.execute(stmt)
    
    # If you call db_session again, it will return the same session
    # even in child coroutines.
    session = await db_session(connection)
    
    # The second request will use the same connection and the same transaction
    await session.execute(stmt)

    # The commit and closing of the session will occur automatically

How it works

Here is a very simplified diagram of how everything works:

basic schema.png

  1. Before executing your code, the middleware will prepare a container in which the sessions required by your code will be stored. The container is saved in contextvars
  2. Your code accesses the library to create new sessions and retrieve existing ones
  3. After your code, middleware will automatically commit or roll back open transactions. Closes open sessions and clears the context.

The library also provides the ability to commit, rollback, and close at any time, without waiting for the end of the request, without any problems.

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.0.1.tar.gz (3.4 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.0.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for context_async_sqlalchemy-2.0.1.tar.gz
Algorithm Hash digest
SHA256 79be1ec0f0fadf9c4035c2a3efa5436ed236fc44f4beef3873176c5ab0dd385e
MD5 011738c99fa6441310652b6af429eacb
BLAKE2b-256 647d140e7ba72bbea9aee5b4ce5ddb29fa47c78c957abcb9721c3b43ead9862a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for context_async_sqlalchemy-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 086ee560a097057dec6982ec359afa5c38182cf0dc5afff0ba149a87ec8bd39a
MD5 d9dd726e6e5588cf6b3403e7fafe7003
BLAKE2b-256 dbc28276daa3da88790a7887242678f0939abd2f8629ffe4f136a2ecda005e1a

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