@@ -82,6 +82,7 @@ def _filter_select(self, stmt: Select, search_params: Mapping[str, Any]) -> Sele
8282 :param stmt: a Select statement
8383 :type stmt: Select
8484 :param search_params: Any keyword argument to be used as equality filter
85+ :type search_params: Mapping[str, Any]
8586 :return: The filtered query
8687 """
8788 # TODO: Add support for relationship eager load
@@ -110,6 +111,7 @@ def _filter_order_by(
110111 :param stmt: a Select statement
111112 :type stmt: Select
112113 :param order_by: a list of columns, or tuples (column, direction)
114+ :type order_by: Iterable[Union[str, Tuple[str, SortDirection]]]
113115 :return: The filtered query
114116 """
115117 for value in order_by :
@@ -127,6 +129,24 @@ def _find_query(
127129 search_params : Union [None , Mapping [str , Any ]] = None ,
128130 order_by : Union [None , Iterable [Union [str , Tuple [str , SortDirection ]]]] = None ,
129131 ) -> Select :
132+ """Build a query with column filters and orders.
133+
134+ E.g.
135+ q = _find_query(search_params={"name":"John"})
136+ finds all models with name = John
137+
138+ q = _find_query(order_by=["name"])
139+ finds all models ordered by `name` column
140+
141+ q = _find_query(order_by=[("name", SortDirection.DESC)])
142+ finds all models with reversed order by `name` column
143+
144+ :param search_params: Any keyword argument to be used as equality filter
145+ :type search_params: Mapping[str, Any]
146+ :param order_by: a list of columns, or tuples (column, direction)
147+ :type order_by: Iterable[Union[str, Tuple[str, SortDirection]]]
148+ :return: The filtered query
149+ """
130150 stmt = select (self ._model )
131151
132152 if search_params :
@@ -177,19 +197,22 @@ def _cursor_paginated_query(
177197 is_before_cursor : bool = False ,
178198 items_per_page : int = _max_query_limit ,
179199 ) -> Select :
180- """Build the query offset and limit clauses from submitted parameters.
200+ """Adds the clauses to retrieve the requested slice of models, after
201+ or before the cursor value, plus a model before the slice and one after
202+ the slice, to identify if previous or next results are available.
181203
182204 :param stmt: a Select statement
183205 :type stmt: Select
184- :param before: Identifier of the last node to skip
185- :type before: Union[int, str]
186- :param after: Identifier of the last node to skip
187- :type after: Union[int, str]
206+ :param cursor_reference: A cursor reference containing ordering column
207+ and threshold value
208+ :type cursor_reference: Union[CursorReference, None]
209+ :param is_before_cursor: If True it will return items before the cursor,
210+ otherwise items after
211+ :type is_before_cursor: bool
188212 :param items_per_page: Number of models to retrieve
189213 :type items_per_page: int
190214 :return: The filtered query
191215 """
192-
193216 forward_limit = self ._sanitised_query_limit (items_per_page ) + 1
194217
195218 if not cursor_reference :
@@ -220,9 +243,24 @@ def _cursor_pagination_slice_query(
220243 self ,
221244 stmt : Select ,
222245 cursor_reference : CursorReference ,
223- forward_limit : int ,
246+ limit : int ,
224247 is_before_cursor : bool ,
225248 ):
249+ """Adds the clauses to retrieve a requested slice of models,
250+ after or before the cursor value (excluding the cursor itself)
251+
252+ :param stmt: a Select statement
253+ :type stmt: Select
254+ :param cursor_reference: A cursor reference containing ordering column
255+ and threshold value
256+ :type cursor_reference: Union[CursorReference, None]
257+ :param is_before_cursor: If True it will return items before the cursor,
258+ otherwise items after
259+ :type is_before_cursor: bool
260+ :param limit: Number of models to retrieve
261+ :type limit: int
262+ :return: The filtered query
263+ """
226264 if not is_before_cursor :
227265 page_query = stmt .where (
228266 getattr (self ._model , cursor_reference .column ) > cursor_reference .value
@@ -237,11 +275,24 @@ def _cursor_pagination_slice_query(
237275 page_query = self ._filter_order_by (
238276 page_query , [(cursor_reference .column , SortDirection .DESC )]
239277 )
240- return page_query .limit (forward_limit )
278+ return page_query .limit (limit )
241279
242280 def _cursor_pagination_previous_item_query (
243281 self , stmt : Select , cursor_reference : CursorReference , is_before_cursor : bool
244282 ) -> Select :
283+ """Adds the clauses to retrieve a single model, after or before
284+ the cursor value (including the cursor itself).
285+
286+ :param stmt: a Select statement
287+ :type stmt: Select
288+ :param cursor_reference: A cursor reference containing ordering column
289+ and threshold value
290+ :type cursor_reference: Union[CursorReference, None]
291+ :param is_before_cursor: If True it will return items before the cursor,
292+ otherwise items after
293+ :type is_before_cursor: bool
294+ :return: The filtered query
295+ """
245296 if not is_before_cursor :
246297 previous_query = stmt .where (
247298 getattr (self ._model , cursor_reference .column ) <= cursor_reference .value
@@ -263,6 +314,11 @@ def _sanitised_query_limit(self, limit):
263314 return max (min (limit , self ._max_query_limit ), 0 )
264315
265316 def _model_pk (self ) -> str :
317+ """
318+ Retrieves the primary key name from the repository model class.
319+
320+ :return:
321+ """
266322 primary_keys = inspect (self ._model ).primary_key # type: ignore
267323 if len (primary_keys ) > 1 :
268324 raise NotImplementedError ("Composite primary keys are not supported." )
0 commit comments