Skip to content

Commit 6247ec5

Browse files
committed
add optional initial cursor
1 parent 6cb8084 commit 6247ec5

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ TaskModelOut: t.Any = create_pydantic_model(
3131
@app.get("/tasks/", response_model=t.List[TaskModelOut])
3232
async def tasks(
3333
request: Request,
34-
__cursor: str,
34+
__cursor: t.Optional[str] = None,
3535
__previous: t.Optional[str] = None,
3636
):
3737
try:
@@ -50,7 +50,9 @@ async def tasks(
5050
)
5151
except KeyError:
5252
paginator = CursorPagination(cursor=__cursor)
53-
rows_result, headers_result = await paginator.get_cursor_rows(Task, request)
53+
rows_result, headers_result = await paginator.get_cursor_rows(
54+
Task, request
55+
)
5456
rows = await rows_result.run()
5557
headers = headers_result
5658
response = JSONResponse(

example/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
@app.get("/tasks/", response_model=t.List[TaskModelOut])
4242
async def tasks(
4343
request: Request,
44-
__cursor: str,
44+
__cursor: t.Optional[str] = None,
4545
__previous: t.Optional[str] = None,
4646
):
4747
try:

piccolo_cursor_pagination/pagination.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ def encode_cursor(self, cursor: str) -> str:
135135
return base64_bytes.decode("ascii")
136136

137137
async def decode_cursor(self, cursor: str, table: t.Type[Table]) -> int:
138+
if cursor is None:
139+
cursor = ""
138140
base64_bytes = cursor.encode("ascii")
139141
cursor_bytes = base64.b64decode(base64_bytes)
140-
# we provide initial cursor like this because
141-
# we cannot pass empty string to FastAPI openapi
142142
initial_cursor = await (
143143
table.select()
144144
.order_by(table._meta.primary_key, ascending=False)

0 commit comments

Comments
 (0)