Skip to main content

A package to simplify filtering and sorting in FastAPI

Project description

license pypi black

Introduction

fastapi-query-tools is a Python library designed to simplify filtering and sorting in FastAPI applications. It provides a set of utility functions and data models to help you filter, sort, and order your data to be returned to your clients.

fastapi-query-tools is built on top of the popular fastapi library. It is currently designed to work with SQLAlchemy, but support for other databases may be added in the future. It is compatible with Python 3.10 and higher.

Features:

  • Simplifies filtering and sorting in FastAPI applications.
  • Supports a query, a sort by, and an order by system.
  • Works with SQLAlchemy.
  • Compatible with Python 3.10 and higher.

Installation

pip install fastapi-query-tools

Quickstart

All you need to do is to add the Filter dependency to the endpoint params and call filter_and_sort function on data you want to filter and/or sort.

from fastapi import FastAPI, Depends
from sqlalchemy import select

# import all you need from fastapi-query-tools
from fastapi_query_tools import Filter, filter_and_sort

app = FastAPI()  # create FastAPI app


@app.get('/items')
async def get_items(*, db: Session = Depends(get_db), filter: Filter) -> List[ItemOut]:
    stmt = select(Item)
    query = filter_and_sort(stmt, filter)
    return db.execute(query).scalars().all() # filter and sort data and return

Example Request

curl -X 'GET' \
  'http://localhost:8000/api/items?q=item&sort_by=name&order=desc' \
  -H 'accept: application/json' \

Where q is a query, sort_by is a field (column) to sort by, and order (asc, desc) is the order of sorting.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_query_tools-1.0.6.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

fastapi_query_tools-1.0.6-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_query_tools-1.0.6.tar.gz.

File metadata

  • Download URL: fastapi_query_tools-1.0.6.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for fastapi_query_tools-1.0.6.tar.gz
Algorithm Hash digest
SHA256 f21bc0aabd76454460afa2415e98eb57e6cfa12f51d6ef6d34164c3c0381f04a
MD5 9c294f0cb78a53a01a63382d8225beb8
BLAKE2b-256 f6c237233a3945d04782f45d1b23ae585964b2658ad02feeb882888080a0d53b

See more details on using hashes here.

File details

Details for the file fastapi_query_tools-1.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_query_tools-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2b07b40bc60ef4113031a8d5b931cb7652d0cb77973f7e19639a9d2176fb6c88
MD5 f3f780e9c7be1da21052278b09be696c
BLAKE2b-256 218949b8751464ddb542282c9dba0ba2ae5393b68dd5840ade1c1f3ad8fb9a48

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page