
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
fastapi-pagination
is a Python library designed to simplify pagination in FastAPI applications.
It provides a set of utility functions and data models to help you paginate your database queries
and return paginated responses to your clients.
With fastapi-pagination
, you can easily define pagination parameters in your FastAPI endpoint functions,
and use them to generate paginated responses that include the requested subset of your data.
The library supports a variety of pagination strategies, including cursor-based pagination and page-based pagination.
fastapi-pagination
is built on top of the popular fastapi
library, and it works with a wide range
of SQL and NoSQL databases frameworks. It also supports async/await syntax and is compatible with Python 3.8 and higher.
Features:
SQLAlchemy
, Tortoise ORM
, and PyMongo
.For more information on how to use fastapi-pagination, please refer to the official documentation.
pip install fastapi-pagination
All you need to do is to use Page
class as a return type for your endpoint and call paginate
function
on data you want to paginate.
from fastapi import FastAPI
from pydantic import BaseModel, Field
# import all you need from fastapi-pagination
from fastapi_pagination import Page, add_pagination, paginate
app = FastAPI() # create FastAPI app
add_pagination(app) # important! add pagination to your app
class UserOut(BaseModel): # define your model
name: str = Field(..., example="Steve")
surname: str = Field(..., example="Rogers")
users = [ # create some data
UserOut(name="Steve", surname="Rogers"),
# ...
]
# req: GET /users
@app.get("/users")
async def get_users() -> Page[UserOut]:
# use Page[UserOut] as return type annotation
return paginate(users) # use paginate function to paginate your data
Please, be careful when you work with databases, because default paginate
will require to load all data in memory.
For instance, if you use SQLAlchemy
you can use paginate
from fastapi_pagination.ext.sqlalchemy
module.
from sqlalchemy import select
from fastapi_pagination.ext.sqlalchemy import paginate
@app.get("/users")
def get_users(db: Session = Depends(get_db)) -> Page[UserOut]:
return paginate(db, select(User).order_by(User.created_at))
Code from Quickstart
will generate OpenAPI schema as bellow:
FAQs
FastAPI pagination
We found that fastapi-pagination demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.