{"id":18918045,"url":"https://github.com/piccolo-orm/piccolo_cursor_pagination","last_synced_at":"2025-04-15T09:32:18.509Z","repository":{"id":39676490,"uuid":"479398568","full_name":"piccolo-orm/piccolo_cursor_pagination","owner":"piccolo-orm","description":"Cursor pagination for Piccolo ORM","archived":false,"fork":false,"pushed_at":"2022-05-27T19:03:32.000Z","size":28,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-28T19:44:48.709Z","etag":null,"topics":["cursor-pagination","fastapi","piccolo","starlette"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/piccolo-orm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-08T13:19:36.000Z","updated_at":"2024-03-16T12:48:36.000Z","dependencies_parsed_at":"2022-09-20T07:50:41.653Z","dependency_job_id":null,"html_url":"https://github.com/piccolo-orm/piccolo_cursor_pagination","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piccolo-orm%2Fpiccolo_cursor_pagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piccolo-orm%2Fpiccolo_cursor_pagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piccolo-orm%2Fpiccolo_cursor_pagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piccolo-orm%2Fpiccolo_cursor_pagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piccolo-orm","download_url":"https://codeload.github.com/piccolo-orm/piccolo_cursor_pagination/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248657858,"owners_count":21140843,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cursor-pagination","fastapi","piccolo","starlette"],"created_at":"2024-11-08T10:29:24.924Z","updated_at":"2025-04-15T09:32:18.052Z","avatar_url":"https://github.com/piccolo-orm.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Cursor pagination for Piccolo ORM\n\n[Piccolo](https://github.com/piccolo-orm) is an great ecosystem that helps you create [ASGI](https://asgi.readthedocs.io/en/latest/) apps faster and easier. [LimitOffset](https://piccolo-api.readthedocs.io/en/latest/crud/piccolo_crud.html#pagination) is the default Piccolo pagination used by Piccolo Admin and Piccolo API. This package contains usage of cursor pagination which is suitable for large data sets and has better performance than ``LimitOffset`` pagination,\nbut it is **strictly optional** because it does not work with the Piccolo Admin and Piccolo API.\n\n# Installation \n\n```bash\npip install piccolo_cursor_pagination\n```\n# Usage\n\nExample usage of ``CursorPagination``:\n\n```python\nimport typing as t\n\nfrom fastapi import FastAPI, Request\nfrom fastapi.responses import JSONResponse\nfrom piccolo_api.crud.serializers import create_pydantic_model\nfrom piccolo_cursor_pagination.pagination import CursorPagination\n\nfrom home.tables import Task\n\napp = FastAPI()\n\nTaskModelOut: t.Any = create_pydantic_model(\n    table=Task, include_default_columns=True, model_name=\"TaskModelOut\"\n)\n\n@app.get(\"/tasks/\", response_model=t.List[TaskModelOut])\nasync def tasks(\n    request: Request,\n    __cursor: t.Optional[str] = None,\n    __previous: t.Optional[str] = None,\n):\n    try:\n        previous = request.query_params[\"__previous\"]\n        paginator = CursorPagination(cursor=__cursor)\n        rows_result, headers_result = await paginator.get_cursor_rows(\n            Task, request\n        )\n        rows = await rows_result.run()\n        headers = headers_result\n        response = JSONResponse(\n            {\"rows\": rows[::-1]},\n            headers={\n                \"next_cursor\": headers[\"cursor\"],\n            },\n        )\n    except KeyError:\n        paginator = CursorPagination(cursor=__cursor)\n        rows_result, headers_result = await paginator.get_cursor_rows(\n            Task, request\n        )\n        rows = await rows_result.run()\n        headers = headers_result\n        response = JSONResponse(\n            {\"rows\": rows},\n            headers={\n                \"next_cursor\": headers[\"cursor\"],\n            },\n        )\n    return response\n\n@app.on_event(\"startup\")\nasync def open_database_connection_pool():\n    try:\n        engine = engine_finder()\n        await engine.start_connection_pool()\n    except Exception:\n        print(\"Unable to connect to the database\")\n\n\n@app.on_event(\"shutdown\")\nasync def close_database_connection_pool():\n    try:\n        engine = engine_finder()\n        await engine.close_connection_pool()\n    except Exception:\n        print(\"Unable to connect to the database\")\n```\nThe ``CursorPagination`` stores the value of ``next_cursor`` in the response headers. \nWe can then use the ``next_cursor`` value to get new set of results by passing \n``next_cursor`` to ``__cursor`` query parameter.\n\nFull Piccolo ASGI app is in **example** folder or you can check [another example](https://github.com/sinisaos/piccolo-cursor-vue) with Vue frontend.\n\n# Customization\n\nThe ``CursorPagination`` class has a default value of ``page_size`` and ``order_by``, \nbut we can overide this value in constructor to adjust the way the results are displayed.\n\nExample of displaying results in ascending order and page size is 10:\n\n```python\npaginator = CursorPagination(cursor=__cursor, page_size=10, order_by=\"id\")\n```\n\n# Directions\n\nThe ``CursorPagination`` has the ability to move forward and backward. \nTo go backward we have to pass ``__previous=yes`` in the query parameters.\n\nExample usage of direction:\n\n```\nGET http://localhost:8000/tasks/?__cursor=NA== (forward)\nGET http://localhost:8000/tasks/?__cursor=NA==\u0026__previous=yes (backward)\n```\n\n# Limitations\n\nYou need to be aware that cursor pagination has several trade offs. The cursor must be based on a unique and sequential column in the table and the client can't go to a specific page because there is no concept of the total number of pages or results.\n\n\u003e **WARNING**: ``CursorPagination`` use Piccolo ORM ``id`` (PK type **integer**) column as unique and sequential column for pagination.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiccolo-orm%2Fpiccolo_cursor_pagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiccolo-orm%2Fpiccolo_cursor_pagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiccolo-orm%2Fpiccolo_cursor_pagination/lists"}