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.2.0.tar.gz (38.4 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.2.0-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_sqlalchemy_querykit-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8877309d29f022fbd0f689de5ac3ab469a7b7263860a5e72b059a984f9e11129
MD5 5211ab7667e6936eca6cc2fdd5f3c8e1
BLAKE2b-256 d8940c47c2d3c6e4cd9f20db0f568ba3f513e9997f08eb62649a277779891229

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_sqlalchemy_querykit-0.2.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.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_sqlalchemy_querykit-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cfefbc5b83670565dc776fde15dee0ca5e9fd89062812605591204811c6bf8fc
MD5 4580dbd6cff0202fa5a4652215237e1e
BLAKE2b-256 9f2d476e8cdb386339166a559cd343860433eaaa980a5564b32d69cfc2598d2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_sqlalchemy_querykit-0.2.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