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 indocs/.
Why
- Deny by default. Undeclared fields return a generic 400, never a silent query.
- Declare policy on the model. A
Queryablemixin 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, noDISTINCT, 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
- Operator reference — wire format, every operator, the per-type matrix
- Guide —
Queryable, relationships, search, sort, errors, limits, startup validation - FastAPI integration
- Pagination & query-config — the
Pageenvelope,list_and_paginate,build_query_config
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_sqlalchemy_querykit-0.3.0.tar.gz.
File metadata
- Download URL: fastapi_sqlalchemy_querykit-0.3.0.tar.gz
- Upload date:
- Size: 45.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4016aa77efe8220cf1620b251f14ce21e85a4b0da994aa96a1afbe838e05d088
|
|
| MD5 |
3d40e913a7fe6e458dd51add95c0c3b9
|
|
| BLAKE2b-256 |
6601873f9965f7fffac7ce8c854a0f83efb141da9d3659797314047cf3944cb5
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_sqlalchemy_querykit-0.3.0.tar.gz -
Subject digest:
4016aa77efe8220cf1620b251f14ce21e85a4b0da994aa96a1afbe838e05d088 - Sigstore transparency entry: 1833982309
- Sigstore integration time:
-
Permalink:
Rooler-ai/fastapi-sqlalchemy-querybuilder@218e60918578fcb6133e33bf35ca9f750e2cb62b -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/Rooler-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@218e60918578fcb6133e33bf35ca9f750e2cb62b -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_sqlalchemy_querykit-0.3.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_sqlalchemy_querykit-0.3.0-py3-none-any.whl
- Upload date:
- Size: 29.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ac5701ae885b81989e0b1c09ec121613cc7af645fc8ebc29bcd815eafd68a01
|
|
| MD5 |
996e2b15f938f667e97ac3241c770187
|
|
| BLAKE2b-256 |
21732b471073f713707236e35e20db951f18e835dc95229fe11d32ed602fd1ce
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_sqlalchemy_querykit-0.3.0-py3-none-any.whl -
Subject digest:
1ac5701ae885b81989e0b1c09ec121613cc7af645fc8ebc29bcd815eafd68a01 - Sigstore transparency entry: 1833982393
- Sigstore integration time:
-
Permalink:
Rooler-ai/fastapi-sqlalchemy-querybuilder@218e60918578fcb6133e33bf35ca9f750e2cb62b -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/Rooler-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@218e60918578fcb6133e33bf35ca9f750e2cb62b -
Trigger Event:
push
-
Statement type: