def __await__( self, ) -> Generator[Coroutine, Any, Optional[FindQueryResultType]]: """ Run the query :return: BaseModel """ # projection = get_projection(self.projection_model) if (self.document_model.get_settings().use_cache and self.ignore_cache is False): cache_key = LRUCache.create_key( "FindOne", self.get_filter_query(), self.projection_model, self.session, self.fetch_links, ) document: Dict[ str, Any] = self.document_model._cache.get( # type: ignore cache_key) if document is None: document = yield from self._find_one().__await__( ) # type: ignore self.document_model._cache.set( # type: ignore cache_key, document) else: document = yield from self._find_one().__await__() # type: ignore if document is None: return None return cast(FindQueryResultType, parse_obj(self.projection_model, document))
async def __anext__(self) -> CursorResultType: 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: return next_item return parse_obj(projection, next_item) # type: ignore
def __await__( self, ) -> Generator[Coroutine, Any, Optional[FindQueryResultType]]: """ Run the query :return: BaseModel """ projection = get_projection(self.projection_model) document: Dict[str, Any] = ( yield from self.document_model.get_motor_collection().find_one( filter=self.get_filter_query(), projection=projection, session=self.session, )) if document is None: return None return cast(FindQueryResultType, parse_obj(self.projection_model, document))
async def to_list( self, length: Optional[int] = None) -> List[CursorResultType]: # noqa """ Get list of documents :param length: Optional[int] - length of the list :return: Union[List[BaseModel], List[Dict[str, Any]]] """ if self.motor_cursor is None: raise RuntimeError("self.motor_cursor was not set") motor_list: List[Dict[str, Any]] = await self.motor_cursor.to_list(length) projection = self.get_projection_model() if projection is not None: return cast( List[CursorResultType], [parse_obj(projection, i) for i in motor_list], ) return cast(List[CursorResultType], motor_list)