Skip to content

Commit

Permalink
chore: add sessions to all db methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Spotika committed Sep 12, 2024
1 parent 74f4127 commit d912297
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/db/methods/users.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
from pymongo.errors import DuplicateKeyError
from pymongo.client_session import ClientSession

from db.types import types
from .collections import users
from .helpers import insert_with_auto_increment_id


def get(user_id: int) -> types.User | None:
if (user := users.find_one({"_id": user_id})) is None:
def get(user_id: int, session: ClientSession | None = None) -> types.User | None:
if (user := users.find_one({"_id": user_id}, session=session)) is None:
return None
return types.User(**user)

def get_by_email(email: str):
if (user := users.find_one({"email": email})) is None:
def get_by_email(email: str, session: ClientSession | None = None):
if (user := users.find_one({"email": email}, session=session)) is None:
return None
return types.User(**user)

def insert_user_with_id(user: types.UserWithoutID) -> int | None:
def insert_user_with_id(user: types.UserWithoutID, session: ClientSession | None = None) -> int | None:
"""
Returns:
Inserted user id or None, if error occurred
"""
try:
return insert_with_auto_increment_id(
users, user.db_dump()
)
return insert_with_auto_increment_id(users, user.db_dump(), session=session)
except DuplicateKeyError:
return None

0 comments on commit d912297

Please sign in to comment.