Metadata-Version: 2.4
Name: approck-sqlalchemy-query-builder
Version: 1.0.4
Summary: Build SQLAlchemy 2 WHERE clauses from nested JSON-friendly filter rules validated with Pydantic.
Project-URL: Homepage, https://github.com/adalekin/approck-sqlalchemy-query-builder
Project-URL: Repository, https://github.com/adalekin/approck-sqlalchemy-query-builder
Project-URL: Changelog, https://github.com/adalekin/approck-sqlalchemy-query-builder/releases
Project-URL: Issues, https://github.com/adalekin/approck-sqlalchemy-query-builder/issues
Author-email: Aleksey Dalekin <adalekin@gmail.com>
License: MIT License
        
        Copyright (c) 2024 Aleksey Dalekin
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: filter,pydantic,query-builder,sqlalchemy,sqlalchemy-2,where
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: pydantic<3,>=2.4.1
Requires-Dist: python-dateutil>=2.8.2
Requires-Dist: sqlalchemy[mypy]>=2.0.0
Description-Content-Type: text/markdown

# approck-sqlalchemy-query-builder

Take a **filter tree as JSON** (from a REST body, WebSocket message, or stored config), validate it with [Pydantic](https://docs.pydantic.dev/), 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

```bash
pip install approck-sqlalchemy-query-builder
```

Or with [uv](https://docs.astral.sh/uv/):

```bash
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`:

```json
{
  "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):

```python
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:

```bash
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](CONTRIBUTING.md) for pull request guidelines.

## License

MIT — see [LICENSE](LICENSE).
