Skip to main content

Deny-by-default declarative filtering, sorting, and searching for FastAPI + SQLAlchemy 2.0.

Project description

fastapi-sqlalchemy-querykit

Deny-by-default declarative filtering, sorting, and searching for FastAPI + SQLAlchemy 2.0 (async-safe). A field is filterable, sortable, or searchable only if the model explicitly declares it — the opposite of the open-by-default library this is derived from.

pip install fastapi-sqlalchemy-querykit

Distribution: fastapi-sqlalchemy-querykit · import package: querybuilder. Full docs in docs/.

Why

  • Deny by default. Undeclared fields return a generic 400, never a silent query.
  • Declare policy on the model. A Queryable mixin lists the allowed fields once; endpoints may narrow but never widen.
  • Correct relationships. Filters and search across relationships compile to correlated EXISTS (.has() / .any()) — no row fan-out, no DISTINCT, to-many supported; two paths to the same model never collide. Sorting uses path-keyed joins.
  • Type-aware & enum-safe. Operators are validated against the column type; enums match by value (Postgres-safe).
  • Caller-owned base scope. The builder is purely additive — it never adds or inspects your own WHERE (tenant, soft-delete, visibility).

Quick example

from querybuilder import Queryable, QueryParams, build_query

class User(Base, Queryable):
    __filterable__ = frozenset({"status", "username", "age", "organization.name"})
    __sortable__   = frozenset({"created_at", "username"})
    __searchable__ = frozenset({"username", "organization.name"})

# inside your data/query layer
async def list_users(db, params: QueryParams):
    stmt = build_query(User, params)                 # filters + search + sort
    stmt = stmt.where(User.is_deleted.is_(False))     # caller-owned base scope
    return (await db.execute(stmt)).scalars().all()
GET /users?filters={"status":{"$eq":"active"},"organization.name":{"$contains":"acme"}}&sort=created_at:desc&search=jdoe

The filter travels as one JSON object, {field: {operator: value}}, with $and / $or for grouping and a dot for relationship traversal (organization.name).

Operators

$eq $ne $gt $gte $lt $lte $in $nin · $contains $ncontains $startswith $endswith · $isnull $isnotnull · logical $and $or.

String comparisons are case-insensitive by default (case_sensitive=true to opt out). Enum filters resolve the operand to the matching member, so you filter by the enum value regardless of how SQLAlchemy stores it.

Endpoint-level narrowing

The model declares the maximum; an endpoint may expose a subset (never a superset):

stmt = build_query(User, params, allowed_filters={"status", "username"})

Documentation

Build the docs site locally: pip install -e ".[docs]" && mkdocs serve.

Development

python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest

License

MIT. Derivative work of fastapi-querybuilder by bhadri01 — see LICENSE and NOTICE.

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_sqlalchemy_querykit-0.3.0.tar.gz (45.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_sqlalchemy_querykit-0.3.0-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_sqlalchemy_querykit-0.3.0.tar.gz.

File metadata

File hashes

Hashes for fastapi_sqlalchemy_querykit-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4016aa77efe8220cf1620b251f14ce21e85a4b0da994aa96a1afbe838e05d088
MD5 3d40e913a7fe6e458dd51add95c0c3b9
BLAKE2b-256 6601873f9965f7fffac7ce8c854a0f83efb141da9d3659797314047cf3944cb5

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_sqlalchemy_querykit-0.3.0.tar.gz:

Publisher: release.yml on Rooler-ai/fastapi-sqlalchemy-querybuilder

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_sqlalchemy_querykit-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_sqlalchemy_querykit-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ac5701ae885b81989e0b1c09ec121613cc7af645fc8ebc29bcd815eafd68a01
MD5 996e2b15f938f667e97ac3241c770187
BLAKE2b-256 21732b471073f713707236e35e20db951f18e835dc95229fe11d32ed602fd1ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_sqlalchemy_querykit-0.3.0-py3-none-any.whl:

Publisher: release.yml on Rooler-ai/fastapi-sqlalchemy-querybuilder

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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