|
12 | 12 | Mapping, |
13 | 13 | Tuple, |
14 | 14 | Type, |
15 | | - TypeVar, |
16 | 15 | Union, |
17 | 16 | ) |
18 | 17 |
|
19 | | -from pydantic import BaseModel |
20 | | -from pydantic.generics import GenericModel |
21 | 18 | from sqlalchemy import asc, desc, func, inspect, select |
22 | 19 | from sqlalchemy.orm import Mapper, aliased, class_mapper, lazyload |
23 | 20 | from sqlalchemy.orm.exc import UnmappedClassError |
24 | 21 | from sqlalchemy.sql import Select |
25 | 22 |
|
26 | 23 | from sqlalchemy_bind_manager.exceptions import InvalidModel, UnmappedProperty |
27 | 24 |
|
28 | | -MODEL = TypeVar("MODEL") |
29 | | -PRIMARY_KEY = Union[str, int, tuple, dict] |
| 25 | +from .common import ( |
| 26 | + MODEL, |
| 27 | + Cursor, |
| 28 | + CursorPageInfo, |
| 29 | + CursorPaginatedResult, |
| 30 | + PageInfo, |
| 31 | + PaginatedResult, |
| 32 | +) |
30 | 33 |
|
31 | 34 |
|
32 | 35 | class SortDirection(Enum): |
33 | 36 | ASC = partial(asc) |
34 | 37 | DESC = partial(desc) |
35 | 38 |
|
36 | 39 |
|
37 | | -class PageInfo(BaseModel): |
38 | | - page: int |
39 | | - items_per_page: int |
40 | | - total_pages: int |
41 | | - total_items: int |
42 | | - has_next_page: bool |
43 | | - has_previous_page: bool |
44 | | - |
45 | | - |
46 | | -class PaginatedResult(GenericModel, Generic[MODEL]): |
47 | | - items: List[MODEL] |
48 | | - page_info: PageInfo |
49 | | - |
50 | | - |
51 | | -class CursorPageInfo(BaseModel): |
52 | | - items_per_page: int |
53 | | - total_items: int |
54 | | - has_next_page: bool = False |
55 | | - has_previous_page: bool = False |
56 | | - start_cursor: Union[str, None] = None |
57 | | - end_cursor: Union[str, None] = None |
58 | | - |
59 | | - |
60 | | -class CursorPaginatedResult(GenericModel, Generic[MODEL]): |
61 | | - items: List[MODEL] |
62 | | - page_info: CursorPageInfo |
63 | | - |
64 | | - |
65 | | -class Cursor(BaseModel): |
66 | | - column: str |
67 | | - value: str |
68 | | - |
69 | | - |
70 | 40 | class BaseRepository(Generic[MODEL], ABC): |
71 | 41 | _max_query_limit: int = 50 |
72 | 42 | _model: Type[MODEL] |
@@ -435,7 +405,8 @@ def _build_cursor_paginated_result( |
435 | 405 | return result_structure |
436 | 406 |
|
437 | 407 | def encode_cursor(self, cursor: Cursor) -> str: |
438 | | - return b64encode(cursor.json().encode()).decode() |
| 408 | + serialised_cursor = cursor.json() |
| 409 | + return b64encode(serialised_cursor.encode()).decode() |
439 | 410 |
|
440 | 411 | def decode_cursor(self, cursor: str) -> Cursor: |
441 | 412 | return Cursor.parse_raw(b64decode(cursor)) |
|
0 commit comments