SQLAlchemy decorators for an optional connection/session dependency injection.
Project description
sa_decor
SQLAlchemy decorators for an optional connection/session dependency injection.
usage with SQLAlchemy Core
You can set a global engine if you're using only one in your application.
import sa_decor
sa_decor.set_global_engine(my_engine)
Annotate a function and add a named parameter connection.
from sa_decor import with_connection
@with_connection()
def foo(a, b, *, connection):
...
connection.execute(stmt)
...
If you're using multiple engines, you can provide them to each function separately.
@with_connection(engine=my_other_engine)
def bar(a, b, *, connection):
...
connection.execute(stmt)
...
Then either call the function without the parameter to let the decorator connect for you, or supply an existing connection.
foo(1, 2)
foo(1, 2, connection=my_connection)
usage with SQLAlchemy ORM sessions
There is a similar functionality available for ORM sessions, but you need to provide a sessionmaker object instead of an engine and add a session parameter. You also need to choose whether the session should be committed when the function finishes or even when the function raises an exception.
sa_decor.set_global_sessionmaker(my_sessionmaker)
@with_session(commit=False)
def function_that_only_queries_data(*, session):
...
session.execute(select_stmt)
...
@with_session(commit=True)
def function_that_updates_the_db(*, session):
...
session.execute(insert_stmt)
...
raise Exception # the insert WILL NOT be commited
@with_session(force_commit=True)
def function_that_commits_no_matter_what(*, session):
...
session.execute(insert_stmt)
...
raise Exception # the insert WILL be commited
The commit behaviour is ignored when the function is called with an existing session.
function_that_commits_no_matter_what(session=my_session)
# not commited automatically!
my_session.commit()
notes
This is the most useful when you have a bunch of functions that all rely on a connection/session but are sometimes called independently and sometimes from within a different function that also needs a connection.
@with_connection()
def get_stuff(*, connection):
...
@with_connection()
def do_stuff(*, connection):
...
get_stuff(connection=connection) # nested calls can use the same connection
...
# somewhere else in the code, e.g. an API controller
def get():
...
stuff = get_stuff()
...
def post():
...
result = do_stuff()
...
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 sa_decor-1.0.1.tar.gz.
File metadata
- Download URL: sa_decor-1.0.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b55e71cdb235db7964f6dbe0b6dfc76870c57f2b265d2b5bb09449701b38b1a0
|
|
| MD5 |
5a788f7b5aa0d21486b9e1c94309a692
|
|
| BLAKE2b-256 |
0f9bf830cb0a6b3fa6ba8bf2ce22c1a29dfdbbb08e559ecd84bb7dea95d413bd
|
File details
Details for the file sa_decor-1.0.1-py3-none-any.whl.
File metadata
- Download URL: sa_decor-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d7e6bfea76fa1098e43bc90c5f945dd66ddf9c3d41d06e75bad7cf52a16b386
|
|
| MD5 |
7d65b01415347d8bd7e3b839f1f47bb2
|
|
| BLAKE2b-256 |
1a5f6a7086d6796682342c5705e435ce74e4a44dda08934b2f8d259b8a9c7d27
|