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 sqlalchemy_session_proxy.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
# Synchronous usage:
data = proxy.execute(statement)
# Asynchronous usage:
data = await proxy.execute(statement)
Example: Unified Session Usage
from sqlalchemy.orm import Session
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_session_proxy.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 aSessionorAsyncSession..is_async— ReturnsTrueif usingAsyncSession..session— Access the underlying session object.- Methods:
add,add_all,commit,rollback,close,flush,merge,delete,get,get_one,execute,scalars,scalar,refresh,expire,expire_all,expunge,expunge_all,is_modified,in_transaction,in_nested_transaction,query,run_sync, 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 |
scalar |
✔️ | ✔️ | Execute and return a single scalar result |
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 |
query |
✔️ | ✔️ | Create a legacy ORM Query object (sync/async) |
run_sync |
✔️ | Run a sync function in async context (AsyncSession only) |
Additional Methods
-
query: Returns a legacy SQLAlchemy ORM
Queryobject. In sync mode, returns a standardQuery; in async mode, returns an awaitable that yields aQueryobject. Note: TheQueryAPI is considered legacy as of SQLAlchemy 2.0; prefer usingselect()for new code. -
scalar: Executes a statement and returns a single scalar result. Works in both sync and async modes, automatically dispatching to the correct implementation.
-
run_sync: Allows running a synchronous function (that expects a
Session) within an async context. This is only available when using anAsyncSessionand is useful for integrating legacy sync code into async workflows.
See the source code for more details and usage examples.
See the source code for the full list and details. The import path is from sqlalchemy_session_proxy.session_proxy import SqlalchemySessionProxy if installed as a package or used from source.
License
Apache-2.0
Author
Tercel (tercel.yi@gmail.com)
🔗 Links
- GitHub: aipartnerup/sqlalchemy-session-proxy
- PyPI: sqlalchemy-session-proxy
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlalchemy_session_proxy-0.2.0.tar.gz.
File metadata
- Download URL: sqlalchemy_session_proxy-0.2.0.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7b8554bd37a26b60a22a2727bdd8b3fc84e9cac82afffd05399be453fb519d5
|
|
| MD5 |
9a49af216562221e2e45576b075e956e
|
|
| BLAKE2b-256 |
9aae97041f1a03acbc7e7c0565df0e3b47f8f5e62d2c35a17408d4813cd7659e
|
File details
Details for the file sqlalchemy_session_proxy-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_session_proxy-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
139cae83b922a0a8832daaf35cb683823a02c3d211d6216c3bc4b3d3648d605c
|
|
| MD5 |
727774afcb13729148009ebc1e252701
|
|
| BLAKE2b-256 |
053ad6507e0a148aa21e10594d1463efef7bbf359f5e9a354aee3b1af03d31b6
|