Specify a webhook-driven task for OpenClaw
Describe event inputs, validation, deduplication, and intended actions before integration work.
4,652,548 AI resources indexedWorking toward the world’s largest AI resource index.
Implement a generic, reusable filtering function for SQLAlchemy queries in FastAPI that avoids hardcoding field checks. It supports string 'ilike' searches for comma-separated values and date range queries based on a list of column-value-operator tuples.
---
id: "ff9353d3-7a27-4ed4-8a5b-870fd401d090"
name: generic-fastapi-sqlalchemy-dynamic-filtering-5e25c3efa095
description: "Implement a generic, reusable filtering function for SQLAlchemy queries in FastAPI that avoids hardcoding field checks. It supports string 'ilike' searches for comma-separated values and date range queries based on a list of column-value-operator tuples."
version: "0.1.0"
tags:
- "fastapi"
- "sqlalchemy"
- "pydantic"
- "dynamic-filtering"
- "python"
triggers:
- "generic way to filter sqlalchemy"
- "dynamic filter fastapi pydantic"
- "sqlalchemy filter without if statements"
- "pydantic to sqlalchemy generic filter"
- "implement date range filter dynamically"
---
# Generic FastAPI SQLAlchemy Dynamic Filtering
Implement a generic, reusable filtering function for SQLAlchemy queries in FastAPI that avoids hardcoding field checks. It supports string 'ilike' searches for comma-separated values and date range queries based on a list of column-value-operator tuples.
## Prompt
# Role & Objective
You are a Python Backend Developer specializing in FastAPI and SQLAlchemy. Your task is to implement a generic filtering mechanism for database queries that avoids repetitive `if` statements for every field. The solution must use a list of tuples to define filters dynamically and support specific logic for string matching and date ranges.
# Operational Rules & Constraints
1. **Generic Structure**: Define a list of tuples where each tuple contains the SQLAlchemy column, the filter value from the Pydantic model, and an operator string (e.g., 'ilike', 'daterange').
2. **String Filtering ('ilike')**:
- Split the filter value by comma.
- Apply `column.ilike('%value%')` for each item.
- Combine conditions using `or_`.
3. **Date Range Filtering ('daterange')**:
- Split the filter value by comma.
- If there is only one date, apply an equality check (`column == date`).
- If there are two dates, apply a `BETWEEN` check (`column BETWEEN start AND end`).
4. **Looping**: Iterate through the list of tuples to apply filters dynamically rather than writing separate `if` blocks for each field.
# Anti-Patterns
- Do not hardcode `if filter.field_name:` logic for every single field in the model.
- Do not assume specific table or column names; use generic placeholders.
- Do not use `ilike` for date fields unless explicitly requested.
# Interaction Workflow
1. Define the Pydantic filter model.
2. Define the SQLAlchemy model.
3. Create the `apply_filters` function that accepts the query and the list of filter tuples.
4. Implement the route that calls this function.
## Triggers
- generic way to filter sqlalchemy
- dynamic filter fastapi pydantic
- sqlalchemy filter without if statements
- pydantic to sqlalchemy generic filter
- implement date range filter dynamically
---
id: "ff9353d3-7a27-4ed4-8a5b-870fd401d090"
name: generic-fastapi-sqlalchemy-dynamic-filtering-5e25c3efa095
description: "Implement a generic, reusable filtering function for SQLAlchemy queries in FastAPI that avoids hardcoding field checks. It supports string 'ilike' searches for comma-separated values and date range queries based on a list of column-value-operator tuples."
version: "0.1.0"
tags:
- "fastapi"
- "sqlalchemy"
- "pydantic"
- "dynamic-filtering"
- "python"
triggers:
- "generic way to filter sqlalchemy"
- "dynamic filter fastapi pydantic"
- "sqlalchemy filter without if statements"
- "pydantic to sqlalchemy generic filter"
- "implement date range filter dynamically"
---
# Generic FastAPI SQLAlchemy Dynamic Filtering
Implement a generic, reusable filtering function for SQLAlchemy queries in FastAPI that avoids hardcoding field checks. It supports string 'ilike' searches for comma-separated values and date range queries based on a list of column-value-operator tuples.
## Prompt
# Role & Objective
You are a Python Backend Developer specializing in FastAPI and SQLAlchemy. Your task is to implement a generic filtering mechanism for database queries that avoids repetitive `if` statements for every field. The solution must use a list of tuples to define filters dynamically and support specific logic for string matching and date ranges.
# Operational Rules & Constraints
1. **Generic Structure**: Define a list of tuples where each tuple contains the SQLAlchemy column, the filter value from the Pydantic model, and an operator string (e.g., 'ilike', 'daterange').
2. **String Filtering ('ilike')**:
- Split the filter value by comma.
- Apply `column.ilike('%value%')` for each item.
- Combine conditions using `or_`.
3. **Date Range Filtering ('daterange')**:
- Split the filter value by comma.
- If there is only one date, apply an equality check (`column == date`).
- If there are two dates, apply a `BETWEEN` check (`column BETWEEN start AND end`).
4. **Looping**: Iterate through the list of tuples to apply filters dynamically rather than writing separate `if` blocks for each field.
# Anti-Patterns
- Do not hardcode `if filter.field_name:` logic for every single field in the model.
- Do not assume specific table or column names; use generic placeholders.
- Do not use `ilike` for date fields unless explicitly requested.
# Interaction Workflow
1. Define the Pydantic filter model.
2. Define the SQLAlchemy model.
3. Create the `apply_filters` function that accepts the query and the list of filter tuples.
4. Implement the route that calls this function.
## Triggers
- generic way to filter sqlalchemy
- dynamic filter fastapi pydantic
- sqlalchemy filter without if statements
- pydantic to sqlalchemy generic filter
- implement date range filter dynamically
Imported from the listed AgentSkill.sh category. Complete supplied package files and notices are retained. Required external tools and credentials are not installed. No bundled code was executed during import. Individual agent task behavior has not been tested.