Code Monkey home page Code Monkey logo

paginate-any's Introduction

python version formatter black Ruff

PaginateAny

Pagination primitives for any ORM and web-frameworks.

Known issues

  • Decode and Encode of a cursor values hardcoded with msgspec.msgpack module. You can't use timezone-naive datetime format, because it converts value to the str type. See spec and issue about supported types.

Code example

from typing import Annotated
from datetime import datetime, timezone

from fastapi import FastAPI, Depends
from paginate_any.cursor_pagination import InMemoryCursorPaginator
from paginate_any.ext.fastapi import (
    FastApiCursorPagination,
    PaginationDependProtocol,
    init_paginate_any_fastapi_app,
)
from paginate_any.rest_api import JsonPaginationResp
from pydantic import BaseModel

app = FastAPI()
init_paginate_any_fastapi_app(app)


class Car(BaseModel):
    id: int
    name: str
    factory_start: datetime


paginator = InMemoryCursorPaginator[Car](
    unq_field='id',
    sort_fields={
      'id': 'id',
      'factory_start': 'factory_start',
    },
    default_size=2,
)
store = [
    Car(id=1, name='BMW', factory_start=datetime(1916, 3, 7, tzinfo=timezone.utc)),
    Car(id=2, name='Audi', factory_start=datetime(1909, 7, 16, tzinfo=timezone.utc)),
    Car(id=3, name='Mercedes-Benz', factory_start=datetime(1926, 6, 28, tzinfo=timezone.utc)),
    Car(id=4, name='Volkswagen', factory_start=datetime(1937, 5, 28, tzinfo=timezone.utc)),
    Car(id=5, name='Porsche', factory_start=datetime(1931, 4, 25, tzinfo=timezone.utc)),
]


@app.get('/cars', response_model=JsonPaginationResp[Car])
async def cars(
    p: Annotated[
        PaginationDependProtocol[list[Car], Car],
        Depends(FastApiCursorPagination.depend(paginator)),
    ],
):
    result = await p.paginate(store)
    return result.json_resp()

Request:

curl -X GET "http://127.0.0.1:8000/cars?ordering=factory_start" -H  "accept: application/json"

Response:

{
  "pagination": {
    "size": 2,
    "after": "kscM_wAAAAD_____msOxAAE="
  },
  "links": {
    "next": "http://127.0.0.1:8000/cars?after=kscM_wAAAAD_____msOxAAE%3D&ordering=factory_start"
  },
  "data": [
    {
      "id": 2,
      "name": "Audi",
      "factory_start": "1909-07-16T00:00:00Z"
    },
    {
      "id": 1,
      "name": "BMW",
      "factory_start": "1916-03-07T00:00:00Z"
    }
  ]
}

paginate-any's People

Contributors

gromsterus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.