Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:APIJSON APIJSON Conditional Querying

From Leeroopedia
Knowledge Sources
Domains ORM, Query_DSL, SQL_Generation
Last Updated 2026-10-11 00:00 GMT

Overview

A query DSL pattern that expresses SQL WHERE clause conditions through key-suffix operators and logical prefixes, allowing clients to specify comparisons, ranges, pattern matching, and boolean combinations declaratively.

Description

Conditional Querying in APIJSON uses key suffixes to encode SQL operators and key prefixes to encode logical connectors. This creates a compact, human-readable DSL that maps directly to SQL WHERE clauses:

Operator suffixes:

  • key: value → key = value (equality)
  • key>: value → key > value (greater than)
  • key<: value → key < value (less than)
  • key>=: value → key >= value
  • key<=: value → key <= value
  • key!=: value → key != value
  • key{}: [1,2,3] → key IN (1,2,3) (IN clause)
  • key}{': [1,2] → key BETWEEN 1 AND 2 (range)
  • key~: "Tom" → key LIKE 'Tom' (pattern match)
  • key$: "a" → key LIKE '%a%' (contains)
  • key?%: "a" → key LIKE 'a%' (starts with)

Logical prefixes:

  • key (no prefix) → default AND
  • &key → explicit AND
  • |key → OR
  • !key → NOT

Complex grouping via @combine:

  • "@combine": "(name~ | age>) & status" → WHERE (name LIKE ... OR age > ...) AND status = ...

The problem solved is allowing clients to express complex query conditions without a separate query language (like GraphQL or OData). The key-suffix approach keeps the JSON format compact while supporting the full range of SQL WHERE operators.

Usage

Use this principle when designing an API that needs flexible querying capabilities beyond simple equality checks. The suffix-based DSL is particularly effective for REST-like APIs that use JSON request bodies.

Theoretical Basis

# Abstract algorithm (NOT real implementation):
def build_where_clause(conditions, combine):
    parts = []
    for key, value in conditions.items():
        # Extract logical prefix
        logic = Logic(key)  # & | ! prefix
        clean_key = logic.key
        
        # Extract operator suffix
        operator = parse_operator_suffix(clean_key)  # >, <, {}, ~, etc.
        column = clean_key_without_suffix
        
        # Generate SQL condition
        if operator == "=":
            parts.append(f"{quote(column)} = {param(value)}")
        elif operator == ">":
            parts.append(f"{quote(column)} > {param(value)}")
        elif operator == "IN":
            parts.append(f"{quote(column)} IN ({param_list(value)})")
        elif operator == "LIKE":
            parts.append(f"{quote(column)} LIKE {param(value)}")
        # ... etc.
    
    if combine:
        return apply_combine_expression(parts, combine)
    else:
        return " AND ".join(parts)

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment