Skip to content

Commit

Permalink
Add session_wrapper function decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Dec 18, 2023
1 parent 6a6dc26 commit 0ad1947
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions database/repositories/wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

from sqlalchemy.orm import Session
from functools import wraps

import app

def session_wrapper(func):
@wraps(func)
def wrapper(*args, **kwargs):
if kwargs.get('session'):
# Use existing session
return func(*args, **kwargs)

if args and isinstance(args[-1], Session):
# Use existing session
return func(*args, **kwargs)

with app.session.database.managed_session() as session:
# Get new session for this function
kwargs.update({'session': session})
return func(*args, **kwargs)

return wrapper

0 comments on commit 0ad1947

Please sign in to comment.