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.1.0.tar.gz (30.7 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.1.0-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_sqlalchemy_querykit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7c490af5a479dcd0716aea97a524329ab048cc436ec8b965e71f6c9a2651777b
MD5 60aace4106019f1113c28533627591cd
BLAKE2b-256 d9f188e86f5ab8c5e6964ffbaac165a33361c90ca22f1297d4255137452b2b0e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for fastapi_sqlalchemy_querykit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43666ebc047650a9686369fabdcd4238313d460d7a7792c7934b9278521e63ea
MD5 a1ce84f744a0d83d8312355853e5cbfc
BLAKE2b-256 09d07bdd40f6a88492ae8ad54eb1897568b3ad0c340b0b89d4721f9732edac40

See more details on using hashes here.

Provenance

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