Skip to main content

A lightweight proxy for SQLAlchemy sessions that seamlessly supports both synchronous (`Session`) and asynchronous (`AsyncSession`) usage. This library provides a unified interface to interact with SQLAlchemy sessions, making it easy to write code that works in both sync and async environments.

Project description

sqlalchemy-session-proxy

A lightweight proxy for SQLAlchemy sessions that seamlessly supports both synchronous (Session) and asynchronous (AsyncSession) usage. This library provides a unified interface to interact with SQLAlchemy sessions, making it easy to write code that works in both sync and async environments.

Features

  • Unified API: Use the same methods for both sync and async SQLAlchemy sessions.
  • Automatic Async Detection: Automatically detects if the session is async and dispatches calls accordingly.
  • Proxy Pattern: Forwards all attribute and method calls to the underlying session.
  • Supports Core Session Methods: Includes common session operations like add, commit, rollback, execute, scalars, get, merge, and more.
  • Type Hints: Fully type-annotated for better IDE support and static analysis.

Installation

pip install sqlalchemy-session-proxy

Usage

from sqlalchemy.orm import Session
from sqlalchemy.ext.asyncio import AsyncSession
from session_proxy import SqlalchemySessionProxy

# For synchronous session
sync_session = Session(...)
proxy = SqlalchemySessionProxy(sync_session)

# For asynchronous session
async_session = AsyncSession(...)
proxy = SqlalchemySessionProxy(async_session)

# Use proxy in your code
data = proxy.execute(statement)
# or, in async context:
data = await proxy.execute(statement)

Example: Unified Session Usage

from sqlalchemy.orm import Session
from sqlalchemy.ext.asyncio import AsyncSession
from session_proxy import SqlalchemySessionProxy

# Synchronous usage
session = Session(...)
proxy = SqlalchemySessionProxy(session)
proxy.add(obj)
proxy.commit()

# Asynchronous usage
async def main():
    async_session = AsyncSession(...)
    proxy = SqlalchemySessionProxy(async_session)
    await proxy.add(obj)
    await proxy.commit()

API Overview

  • SqlalchemySessionProxy(session) — Initialize with a Session or AsyncSession.
  • .is_async — Returns True if using AsyncSession.
  • .session — Access the underlying session object.
  • Methods: add, add_all, commit, rollback, close, flush, merge, delete, get, get_one, execute, scalars, refresh, expire, expire_all, expunge, expunge_all, is_modified, in_transaction, in_nested_transaction, and more.

All methods are automatically dispatched to the correct sync/async implementation.

API Summary

Method Sync Async Description
add ✔️ ✔️ Add an object to the session
add_all ✔️ ✔️ Add multiple objects
commit ✔️ ✔️ Commit the transaction
rollback ✔️ ✔️ Rollback the transaction
close ✔️ ✔️ Close the session
flush ✔️ ✔️ Flush changes to the database
merge ✔️ ✔️ Merge an object
delete ✔️ ✔️ Mark an object as deleted
get ✔️ ✔️ Get object by primary key
get_one ✔️ ✔️ Get one object or raise if not found
execute ✔️ ✔️ Execute a statement
scalars ✔️ ✔️ Execute and return scalar results
refresh ✔️ ✔️ Refresh an object from the database
expire ✔️ ✔️ Expire attributes on an object
expire_all ✔️ ✔️ Expire all objects in the session
expunge ✔️ ✔️ Remove an object from the session
expunge_all ✔️ ✔️ Remove all objects from the session
is_modified ✔️ ✔️ Check if object is modified
in_transaction ✔️ ✔️ Check if a transaction is in progress
in_nested_transaction ✔️ ✔️ Check if a nested transaction is in progress

See the source code for the full list and details.

License

Apache-2.0

Author

Tercel (tercel.yi@gmail.com)

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

sqlalchemy_session_proxy-0.1.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_session_proxy-0.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_session_proxy-0.1.0.tar.gz.

File metadata

  • Download URL: sqlalchemy_session_proxy-0.1.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for sqlalchemy_session_proxy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9cc77ed8b028d785ae134baac225fed414a583dd7818d3c86d9a99887029df2d
MD5 4af20fe7de2ada7b5bd318bc91234473
BLAKE2b-256 f435f29626aee693409bf54e42fb6c3629c93d8d6882e722098d32407cfd6a84

See more details on using hashes here.

File details

Details for the file sqlalchemy_session_proxy-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_session_proxy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbf44dbb62956832f3ee9a4303dbcafae13fe9df29392136d69b9351f938d621
MD5 f971341a98ed9a03defd373bf1404cc4
BLAKE2b-256 2d430769c04a3c40b1cfe3a30f99f25293ae88a5e4d62dd7245f9cfb52f390c8

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