-
Notifications
You must be signed in to change notification settings - Fork 31
Asynced the Database #659 and #649 #660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,53 +18,54 @@ class AbstractWrapper(ABC, Generic[T, PK]): | |
|
|
||
| model: type[T] | ||
|
|
||
| def get_all(self) -> list[T]: | ||
| async def get_all(self) -> list[T]: | ||
| """ | ||
| Get all data wrapper for the unspecified model | ||
|
|
||
| :return: a list of all model instances | ||
| """ | ||
| with get_db_session() as session: | ||
| return list(session.exec(select(self.model)).all()) | ||
| async with get_db_session() as session: | ||
| result = await session.exec(select(self.model)) | ||
| return list(result.all()) | ||
|
|
||
| def get_by_id(self, obj_id: PK) -> T: | ||
| async def get_by_id(self, obj_id: PK) -> T: | ||
| """ | ||
| Retrieve data wrapper for the unspecified model | ||
|
|
||
| :param obj_id: PK of the model instance to be retrieved | ||
| :return: the retrieved instance | ||
| """ | ||
| with get_db_session() as session: | ||
| obj = session.get(self.model, obj_id) | ||
| async with get_db_session() as session: | ||
| obj = await session.get(self.model, obj_id) | ||
| if not obj: | ||
| raise ValueError(f"{self.model.__name__} with ID {obj_id} not found.") | ||
| return obj | ||
|
|
||
| def create(self, data: dict[str, Any]) -> T: | ||
| async def create(self, data: dict[str, Any]) -> T: | ||
| """ | ||
| Post data wrapper for the unspecified model | ||
|
|
||
| :param data: the JSON object of the model instance to be created | ||
| :return: the newly created instance | ||
| """ | ||
| with get_db_session() as session: | ||
| async with get_db_session() as session: | ||
| obj = self.model(**data) | ||
| session.add(obj) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i mean since we're going nuts and awaiting everything, does this need await as well?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, though looking at it, we could just add session: AsyncSession = Depends(get_db_session) into the function args of the abstract wrapper |
||
| session.commit() | ||
| session.refresh(obj) | ||
| await session.commit() | ||
| await session.refresh(obj) | ||
| return obj | ||
|
|
||
| def delete_by_id(self, obj_id: PK) -> T: | ||
| async def delete_by_id(self, obj_id: PK) -> T: | ||
| """ | ||
| Delete data wrapper for the unspecified model | ||
|
|
||
| :param obj_id: PK of the model instance to be deleted | ||
| :return: the deleted instance | ||
| """ | ||
| with get_db_session() as session: | ||
| obj = session.get(self.model, obj_id) | ||
| async with get_db_session() as session: | ||
| obj = await session.get(self.model, obj_id) | ||
| if not obj: | ||
| raise ValueError(f"{self.model.__name__} with ID {obj_id} not found.") | ||
| session.delete(obj) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert this change to return obj, not make a obj copy |
||
| session.commit() | ||
| await session.commit() | ||
| return obj | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this allow for the db to be asynchronous? i thought it already was asynchronous
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the new one is the async driver for psql connection