-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add sessions to all db methods
- Loading branch information
Showing
1 changed file
with
7 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |