@@ -24,7 +24,7 @@ class CursorPagination:
24
24
page_size : int = 15
25
25
order_by : str = "-id"
26
26
27
- def get_cursor_rows (self , table : t .Type [Table ], request : Request ):
27
+ async def get_cursor_rows (self , table : t .Type [Table ], request : Request ):
28
28
# headers to adding next_cursor
29
29
headers : t .Dict [str , str ] = {}
30
30
@@ -37,7 +37,7 @@ def get_cursor_rows(self, table: t.Type[Table], request: Request):
37
37
query = query .limit (self .page_size + 1 )
38
38
39
39
# decoded query params cursor
40
- decoded_cursor = self .decode_cursor (self .cursor , table )
40
+ decoded_cursor = await self .decode_cursor (self .cursor , table )
41
41
42
42
try :
43
43
# preform operation if previous in query params
@@ -50,15 +50,15 @@ def get_cursor_rows(self, table: t.Type[Table], request: Request):
50
50
.order_by (table ._meta .primary_key , ascending = False )
51
51
.limit (self .page_size )
52
52
)
53
- rows = query .run_sync ()
53
+ rows = await query .run ()
54
54
try :
55
55
# set new value to next_cursor
56
56
next_cursor = self .encode_cursor (str (rows [- 1 ]["id" ]))
57
57
headers ["cursor" ] = next_cursor
58
58
# return empty cursor if no more results
59
- last_row = table .select ().limit (1 ).first ().run_sync ()
59
+ last_row = await table .select ().limit (1 ).first ().run ()
60
60
if (
61
- self .decode_cursor (next_cursor , table )
61
+ await self .decode_cursor (next_cursor , table )
62
62
== last_row ["id" ]
63
63
):
64
64
headers ["cursor" ] = ""
@@ -69,7 +69,7 @@ def get_cursor_rows(self, table: t.Type[Table], request: Request):
69
69
query = query .where (
70
70
table ._meta .primary_key >= int (decoded_cursor )
71
71
).limit (self .page_size + 1 )
72
- rows = query .run_sync ()
72
+ rows = await query .run ()
73
73
# set new value to next_cursor
74
74
next_cursor = self .encode_cursor (str (rows [- 1 ]["id" ]))
75
75
headers ["cursor" ] = next_cursor
@@ -83,7 +83,7 @@ def get_cursor_rows(self, table: t.Type[Table], request: Request):
83
83
query = query .limit (self .page_size + 1 )
84
84
85
85
# decoded query params cursor
86
- decoded_cursor = self .decode_cursor (self .cursor , table )
86
+ decoded_cursor = await self .decode_cursor (self .cursor , table )
87
87
88
88
try :
89
89
# preform operation if previous in query params
@@ -96,21 +96,21 @@ def get_cursor_rows(self, table: t.Type[Table], request: Request):
96
96
.order_by (table ._meta .primary_key , ascending = True )
97
97
.limit (self .page_size )
98
98
)
99
- rows = query .run_sync ()
99
+ rows = await query .run ()
100
100
try :
101
101
# set new value to next_cursor
102
102
next_cursor = self .encode_cursor (str (rows [- 1 ]["id" ]))
103
103
headers ["cursor" ] = next_cursor
104
104
# return empty cursor if no more results
105
- last_row = (
105
+ last_row = await (
106
106
table .select ()
107
107
.limit (1 )
108
108
.order_by (table ._meta .primary_key , ascending = False )
109
109
.first ()
110
- .run_sync ()
110
+ .run ()
111
111
)
112
112
if (
113
- self .decode_cursor (next_cursor , table )
113
+ await self .decode_cursor (next_cursor , table )
114
114
== last_row ["id" ]
115
115
):
116
116
headers ["cursor" ] = ""
@@ -121,7 +121,7 @@ def get_cursor_rows(self, table: t.Type[Table], request: Request):
121
121
query = query .where (
122
122
table ._meta .primary_key <= int (decoded_cursor )
123
123
).limit (self .page_size + 1 )
124
- rows = query .run_sync ()
124
+ rows = await query .run ()
125
125
# set new value to next_cursor
126
126
next_cursor = self .encode_cursor (str (rows [- 1 ]["id" ]))
127
127
headers ["cursor" ] = next_cursor
@@ -134,17 +134,17 @@ def encode_cursor(self, cursor: str) -> str:
134
134
base64_bytes = base64 .b64encode (cursor_bytes )
135
135
return base64_bytes .decode ("ascii" )
136
136
137
- def decode_cursor (self , cursor : str , table : t .Type [Table ]) -> int :
137
+ async def decode_cursor (self , cursor : str , table : t .Type [Table ]) -> int :
138
+ if cursor is None :
139
+ cursor = ""
138
140
base64_bytes = cursor .encode ("ascii" )
139
141
cursor_bytes = base64 .b64decode (base64_bytes )
140
- # we provide initial cursor like this because
141
- # we cannot pass empty string to FastAPI openapi
142
- initial_cursor = (
142
+ initial_cursor = await (
143
143
table .select ()
144
144
.order_by (table ._meta .primary_key , ascending = False )
145
145
.limit (1 )
146
146
.first ()
147
- .run_sync ()
147
+ .run ()
148
148
)
149
149
return (
150
150
int (cursor_bytes .decode ("ascii" ) or 0 )
0 commit comments