Skip to main content

approck-sqlalchemy-query-builder

Take a filter tree as JSON (from a REST body, WebSocket message, or stored config), validate it with Pydantic, and turn it into a SQLAlchemy 2 WHERE fragment you can attach to your own select(). You keep full control over columns, joins, and ordering; the library only maps allowed fields to real Column objects so arbitrary JSON never touches the database layer directly.

Requirements

  • Python 3.10+
  • SQLAlchemy 2.0+
  • Pydantic 2.x

Install

pip install approck-sqlalchemy-query-builder

Or with uv:

uv add approck-sqlalchemy-query-builder

Usage

1. Define the filter in JSON — nested groups use condition ("AND" / "OR") and rules; each leaf is id (logical name), operator, and value:

{
  "condition": "AND",
  "rules": [
    { "id": "status", "operator": "=", "value": "active" },
    {
      "condition": "OR",
      "rules": [
        { "id": "role", "operator": "=", "value": "admin" },
        { "id": "role", "operator": "=", "value": "moderator" }
      ]
    }
  ]
}

2. Parse and bind to real columns — Query.model_validate_json / Query.model_validate accept the same shape as json.loads. You pass a whitelist map_columns from logical id to SQLAlchemy columns (only those keys can appear in SQL):

import sqlalchemy as sa
from approck_sqlalchemy_query_builder import Query, query_filter

# Dict from your stack: JSON.parse, await request.json(), Pydantic/FastAPI body, etc.
filter_payload = {
    "condition": "AND",
    "rules": [
        {"id": "status", "operator": "=", "value": "active"},
        {
            "condition": "OR",
            "rules": [
                {"id": "role", "operator": "=", "value": "admin"},
                {"id": "role", "operator": "=", "value": "moderator"},
            ],
        },
    ],
}
query = Query.model_validate(filter_payload)

statement = sa.select(MyModel)
statement = query_filter(
    statement,
    map_columns={
        "status": MyModel.status,
        "role": MyModel.role,
    },
    query=query,
)

For a raw JSON string (e.g. straight from the wire), use Query.model_validate_json(body).

Supported operators

=, >, <, >=, <=, !=, in, not in.

Datetime columns

If the column type name contains DATETIME, integer values are treated as Unix timestamps (milliseconds if the value is larger than “now” in seconds). Strings are parsed with python-dateutil and normalized to UTC.

Unknown columns

With skip_unknown_column=True, rules whose id is missing from map_columns are skipped instead of raising ValueError.

Development

Clone the repository and install dependencies with uv:

uv sync --group dev
uv run pytest
uv run ruff check .
uv run ruff format --check .
uv run mypy approck_sqlalchemy_query_builder

See CONTRIBUTING.md for pull request guidelines.

License

MIT — see LICENSE.

Metadata

Release files for approck-sqlalchemy-query-builder 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for approck-sqlalchemy-query-builder 1.0.4
File Size Uploaded Provenance
approck_sqlalchemy_query_builder-1.0.4.tar.gz 5.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for approck-sqlalchemy-query-builder 1.0.4
File Interpreter ABI Platform
approck_sqlalchemy_query_builder-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 11.0 kB

Release files / approck_sqlalchemy_query_builder-1.0.4.tar.gz

Download URL approck_sqlalchemy_query_builder-1.0.4.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
28c7fb9d39471166b9eddb0770e3d782bc4745402747aa2cb553ef0089e53392
BLAKE2b-256 checksum
How to use checksums
5d1481706f553f2485f9f7b2dfa120346249864782aaf25192807b365a321041
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / approck_sqlalchemy_query_builder-1.0.4-py3-none-any.whl

Download URL approck_sqlalchemy_query_builder-1.0.4-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
974ddd9d829d61d0e116f51d22b7e294331f3306f5efdc1cbe4b1681f0db86f4
BLAKE2b-256 checksum
How to use checksums
84fd4aa8340bcc1b47dc15b38ac8f45e2ad2886507fbedd915202ed6b549fb20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for approck-sqlalchemy-query-builder 1.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page