Skip to content

Commit

Permalink
Merge pull request #235 from amos402/cursor
Browse files Browse the repository at this point in the history
Avoid creating redundant query object
  • Loading branch information
roman-right authored Apr 11, 2022
2 parents cdabb3c + 56caa6d commit 4fdcd7d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion beanie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from beanie.odm.utils.general import init_beanie
from beanie.odm.documents import Document

__version__ = "1.10.4"
__version__ = "1.10.5"
__all__ = [
# ODM
"Document",
Expand Down
14 changes: 8 additions & 6 deletions beanie/odm/queries/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BaseCursorQuery(Generic[CursorResultType]):
BaseCursorQuery class. Wrapper over AsyncIOMotorCursor,
which parse result with model
"""
cursor = None

@abstractmethod
def get_projection_model(self) -> Optional[Type[BaseModel]]:
Expand All @@ -36,13 +37,13 @@ def _cursor_params(self):
...

def __aiter__(self):
if self.cursor is None:
self.cursor = self.motor_cursor
return self

async def __anext__(self) -> CursorResultType:
if self.motor_cursor is None:
raise RuntimeError("self.motor_cursor was not set")
if getattr(self, "cursor", None) is None:
self.cursor = self.motor_cursor
if self.cursor is None:
raise RuntimeError("cursor was not set")
next_item = await self.cursor.__anext__()
projection = self.get_projection_model()
if projection is None:
Expand All @@ -64,11 +65,12 @@ async def to_list(
:param length: Optional[int] - length of the list
:return: Union[List[BaseModel], List[Dict[str, Any]]]
"""
if self.motor_cursor is None:
cursor = self.motor_cursor
if cursor is None:
raise RuntimeError("self.motor_cursor was not set")
motor_list: List[Dict[str, Any]] = self._get_cache()
if motor_list is None:
motor_list = await self.motor_cursor.to_list(length)
motor_list = await cursor.to_list(length)
self._set_cache(motor_list)
projection = self.get_projection_model()
if projection is not None:
Expand Down
17 changes: 15 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

Beanie project

## [1.10.4] - 2022-02-24
## [1.10.5] - 2022-04-11

### Improvement

- Avoid creating redundant query object

### Implementation

- Author - [amos402](https://github.com/amos402)
- PR <https://github.com/roman-right/beanie/pull/235>

## [1.10.4] - 2022-03-24

### Improvement

Expand Down Expand Up @@ -775,4 +786,6 @@ how specific type should be presented in the database

[1.10.3]: https://pypi.org/project/beanie/1.10.3

[1.10.4]: https://pypi.org/project/beanie/1.10.4
[1.10.4]: https://pypi.org/project/beanie/1.10.4

[1.10.5]: https://pypi.org/project/beanie/1.10.5
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "beanie"
version = "1.10.4"
version = "1.10.5"
description = "Asynchronous Python ODM for MongoDB"
authors = ["Roman <[email protected]>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_beanie.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "1.10.4"
assert __version__ == "1.10.5"

0 comments on commit 4fdcd7d

Please sign in to comment.