Skip to content

Fastapi pagination with meta endpoint links(first, last, next, previous)

License

Notifications You must be signed in to change notification settings

nazmulnnb/fastapi-paginate

Repository files navigation

FastAPI Pagination

License codecov PYPI PYPI

fastapi-paginate is an extended work of fastapi-pagination. fastapi-paginate returns following extra meta information:

  • next: endpoint of the next page.
  • previous: endpoint of the previous page.
  • first: endpoint of the first page.
  • last: endpoint of the last page.

All of these meta keeps all the filter parameters passed by the client and returns as it is. If any of these meta is not available, it will return null.

example: OpenAPI

Installation

# Basic version
pip install fastapi-paginate

# All available integrations
pip install fastapi-paginate[all]

Available integrations:

Example

from fastapi import FastAPI
from pydantic import BaseModel

from fastapi_paginate import Page, add_pagination, paginate

app = FastAPI()


class User(BaseModel):
    name: str
    surname: str


users = [
    User(name='Yurii', surname='Karabas'),
    # ...
]


@app.get('/users', response_model=Page[User])
async def get_users():
    return paginate(users)


add_pagination(app)

sqlalchemy example

from fastapi import FastAPI, Depends
from pydantic import BaseModel

from fastapi_paginate import Page, add_pagination
from fastapi_paginate.ext.sqlalchemy import paginate

from sqlalchemy.orm import Session

app = FastAPI()

class UserModel(Base):
    name = Column(String)
    surname = Column(String)
    age = Column(Integer)

class User(BaseModel):
    name: str
    surname: str
    age: int

@app.get('/users', response_model=Page[User])
async def get_users(db_session: Session = Depends(get_db_session)):
    stmt = db_session.query(UserModel)
    
    # add filters 
    stmt = stmt.filter(UserModel.age < 30)
    
    # sort
    stmt = stmt.order_by(asc(UserModel.age))
    
    return paginate(stmt)


add_pagination(app)

This repo is forked from fastapi-pagination. Although original repository is already good enough, but I modified it according to my needs and published thinking it might be helpful for some.