|
| 1 | +# Fluent Query API Proposal |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +A type-safe, fluent query builder API inspired by JOOQ that provides an intuitive, SQL-like interface for constructing Arkiv queries. The API follows the builder pattern and allows for method chaining to construct complex queries. |
| 6 | + |
| 7 | +## Design Goals |
| 8 | + |
| 9 | +1. **Simplicity**: WHERE clauses are plain SQL-like strings - no magic, no operator overloading |
| 10 | +2. **SQL Familiarity**: Follow SQL query patterns (SELECT, WHERE, ORDER BY, etc.) |
| 11 | +3. **Type Safety**: Use `Attribute()` objects for ORDER BY to specify type and direction |
| 12 | +4. **Readability**: Clear, self-documenting code that matches SQL structure |
| 13 | +5. **Backward Compatibility**: Coexist with existing string-based query API |
| 14 | +6. **Transparency**: Query strings are passed directly to Arkiv node |
| 15 | + |
| 16 | +## Core API Design |
| 17 | + |
| 18 | +Parts and descriptions |
| 19 | +- `.select(...)` feeds into "fields" bitmask of QueryOptions of query_entities |
| 20 | +- `.where(...)` feeds into "query" parameter of query_entities |
| 21 | +- `.order_by(...)` (optional) feeds into "order_by" field of QueryOptions of query_entities |
| 22 | +- `.at_block(...)` (optional) feeds into "at_block" field of QueryOptions of query_entities |
| 23 | +- `.fetch()` returns the QueryIterator from query_entities |
| 24 | +- `.count()` optimized to retrieve only entity keys, count them, and return an int |
| 25 | + |
| 26 | +### Field Selection |
| 27 | + |
| 28 | +Arkiv supports selection of entity field groups (not individual user-defined attributes): |
| 29 | +- **Metadata fields**: `KEY`, `OWNER`, `CREATED_AT`, `LAST_MODIFIED_AT`, `EXPIRES_AT`, `TX_INDEX_IN_BLOCK`, `OP_INDEX_IN_TX` |
| 30 | +- **Content fields**: `PAYLOAD`, `CONTENT_TYPE` |
| 31 | +- **Attributes**: `ATTRIBUTES` (all user-defined attributes - cannot select individual ones) |
| 32 | + |
| 33 | +The `.select()` method accepts a list of these field constants: |
| 34 | + |
| 35 | +### Basic Query Structure |
| 36 | + |
| 37 | +```python |
| 38 | +from arkiv import Arkiv |
| 39 | +from arkiv.query import IntAttribute, StrAttribute |
| 40 | +from arkiv.types import KEY, OWNER, ATTRIBUTES, PAYLOAD, CONTENT_TYPE |
| 41 | + |
| 42 | +client = Arkiv() |
| 43 | + |
| 44 | +# Simple query - WHERE clause is a plain SQL-like string |
| 45 | +# Select specific field groups (no brackets needed) |
| 46 | +results = client.arkiv \ |
| 47 | + .select(KEY, ATTRIBUTES) \ |
| 48 | + .where('type = "user"') \ |
| 49 | + .fetch() |
| 50 | + |
| 51 | +# Select all fields ( .select() defaults to all fields) |
| 52 | +results = client.arkiv \ |
| 53 | + .select() \ |
| 54 | + .where('type = "user" AND age >= 18') \ |
| 55 | + .fetch() |
| 56 | + |
| 57 | +# Select multiple field groups |
| 58 | +results = client.arkiv \ |
| 59 | + .select(KEY, OWNER, ATTRIBUTES, PAYLOAD, CONTENT_TYPE) \ |
| 60 | + .where('status = "active"') \ |
| 61 | + .fetch() |
| 62 | + |
| 63 | +# Select single field |
| 64 | +results = client.arkiv \ |
| 65 | + .select(KEY) \ |
| 66 | + .where('type = "user"') \ |
| 67 | + .fetch() |
| 68 | + |
| 69 | +# Complex conditions with OR and parentheses |
| 70 | +results = client.arkiv \ |
| 71 | + .where('(type = "user" OR type = "admin") AND status != "banned"') \ |
| 72 | + .fetch() |
| 73 | + |
| 74 | +# Count matching entities (ignores .select()) |
| 75 | +count = client.arkiv \ |
| 76 | + .where('type = "user"') \ |
| 77 | + .count() |
| 78 | +``` |
| 79 | + |
| 80 | +**Key Points**: |
| 81 | +- `.where()` takes a plain string with SQL-like syntax that is passed directly to the Arkiv node |
| 82 | +- `.select()` accepts field group constants as arguments (not individual attribute names) |
| 83 | +- Arkiv returns all user-defined attributes or none - cannot select specific attributes like `type` or `age` |
| 84 | +- Field groups: `KEY`, `OWNER`, `ATTRIBUTES`, `PAYLOAD`, `CONTENT_TYPE`, etc. |
| 85 | +- For sorting: use `IntAttribute` for numeric fields, `StrAttribute` for string fields |
| 86 | + |
| 87 | +### Sorting |
| 88 | + |
| 89 | +Sorting uses type-specific attribute classes (`IntAttribute` for numeric, `StrAttribute` for string): |
| 90 | + |
| 91 | +```python |
| 92 | +from arkiv.query import IntAttribute, StrAttribute |
| 93 | +from arkiv import ASC, DESC |
| 94 | + |
| 95 | +# Single field sorting |
| 96 | +results = client.arkiv \ |
| 97 | + .select() \ |
| 98 | + .where('type = "user"') \ |
| 99 | + .order_by(IntAttribute('age', DESC)) \ |
| 100 | + .fetch() |
| 101 | + |
| 102 | +# Multiple field sorting - no brackets needed |
| 103 | +results = client.arkiv \ |
| 104 | + .select() \ |
| 105 | + .where('type = "user"') \ |
| 106 | + .order_by( |
| 107 | + StrAttribute('status'), # String, ascending (default) |
| 108 | + IntAttribute('age', DESC) # Numeric, descending |
| 109 | + ) \ |
| 110 | + .fetch() |
| 111 | + |
| 112 | +# Ascending is default, so direction can be omitted |
| 113 | +results = client.arkiv \ |
| 114 | + .select() \ |
| 115 | + .where('status = "active"') \ |
| 116 | + .order_by( |
| 117 | + IntAttribute('priority', DESC), # Descending - explicit |
| 118 | + StrAttribute('name') # Ascending - default |
| 119 | + ) \ |
| 120 | + .fetch() |
| 121 | + |
| 122 | +# Alternative: Method chaining for direction |
| 123 | +results = client.arkiv \ |
| 124 | + .select() \ |
| 125 | + .where('type = "user"') \ |
| 126 | + .order_by( |
| 127 | + StrAttribute('status').asc(), |
| 128 | + IntAttribute('age').desc() |
| 129 | + ) \ |
| 130 | + .fetch() |
| 131 | +``` |
| 132 | + |
| 133 | +**Why type-specific classes are valuable:** |
| 134 | +- **Explicit type**: `IntAttribute` vs `StrAttribute` - immediately clear from class name |
| 135 | +- **Required for sorting**: Arkiv needs to know if attribute is string or numeric |
| 136 | +- **IDE support**: Type system knows what's available for each class |
| 137 | +- **Prevents errors**: Can't accidentally use wrong type |
| 138 | +- **Default direction**: ASC is default, only specify DESC when needed |
| 139 | + |
| 140 | +## Implementation Architecture |
| 141 | + |
| 142 | +### Core Classes |
| 143 | + |
| 144 | + |
| 145 | +## Migration Strategy |
| 146 | + |
| 147 | +The fluent API would coexist with the existing string-based API: |
| 148 | + |
| 149 | +```python |
| 150 | +from arkiv.types import KEY, ATTRIBUTES |
| 151 | + |
| 152 | +# Existing API - still supported |
| 153 | +results = list(client.arkiv.query_entities('type = "user" AND age >= 18')) |
| 154 | + |
| 155 | +# New fluent API - same query string, cleaner interface |
| 156 | +results = client.arkiv \ |
| 157 | + .where('type = "user" AND age >= 18') \ |
| 158 | + .fetch() |
| 159 | + |
| 160 | +# With field selection |
| 161 | +results = client.arkiv \ |
| 162 | + .select(KEY, ATTRIBUTES) \ |
| 163 | + .where('type = "user"') \ |
| 164 | + .fetch() |
| 165 | + |
| 166 | +# With sorting (new capability made easy) |
| 167 | +from arkiv.query import IntAttribute, StrAttribute |
| 168 | + |
| 169 | +results = client.arkiv \ |
| 170 | + .select(KEY, ATTRIBUTES) \ |
| 171 | + .where('type = "user" AND age >= 18') \ |
| 172 | + .order_by( |
| 173 | + StrAttribute('status'), |
| 174 | + IntAttribute('age', DESC) |
| 175 | + ) \ |
| 176 | + .fetch() |
| 177 | + |
| 178 | +# With block pinning |
| 179 | +results = client.arkiv \ |
| 180 | + .select(KEY, ATTRIBUTES) \ |
| 181 | + .where('status = "active"') \ |
| 182 | + .at_block(12345) \ |
| 183 | + .fetch() |
| 184 | + |
| 185 | +# Quick count |
| 186 | +count = client.arkiv \ |
| 187 | + .where('type = "user"') \ |
| 188 | + .count() |
| 189 | +``` |
| 190 | + |
| 191 | +**Key differences**: The fluent API provides a cleaner interface for: |
| 192 | +- **Field selection**: `.select(KEY, ATTRIBUTES)` instead of bitmask `KEY | ATTRIBUTES` |
| 193 | +- **Sorting**: `.order_by()` with type-specific classes `IntAttribute`, `StrAttribute` |
| 194 | +- **Block pinning**: `.at_block()` for historical queries |
| 195 | +- **Counting**: `.count()` convenience method |
| 196 | +- **Iteration**: Returns same iterator, but building the query is more readable |
| 197 | + |
| 198 | +While keeping the WHERE clause simple and familiar (plain SQL-like strings). |
| 199 | + |
| 200 | +**Note**: Both `.select()` and `.order_by()` use Python's `*args` so no brackets needed: |
| 201 | +- `.select(KEY, ATTRIBUTES)` not `.select([KEY, ATTRIBUTES])` |
| 202 | +- `.order_by(StrAttribute('name'), IntAttribute('age', DESC))` not `.order_by([...])` |
| 203 | + |
| 204 | +## Benefits |
| 205 | + |
| 206 | +1. **Simplicity**: WHERE clauses use familiar SQL syntax - no learning curve |
| 207 | +2. **Readability**: Self-documenting code that reads like SQL |
| 208 | +3. **Type Safety**: `Attribute()` for ORDER BY provides IDE autocomplete and type checking |
| 209 | +4. **Composability**: Build queries programmatically with method chaining |
| 210 | +5. **Transparency**: Query strings are passed directly to node - what you see is what you get |
| 211 | +6. **Flexibility**: List-based field selection is clearer than bitmask operations |
| 212 | +7. **Testability**: Easy to test query building separately from execution |
| 213 | + |
| 214 | +## Open Questions |
| 215 | + |
| 216 | +1. **Error Messages**: How to provide clear error messages when query construction fails? |
| 217 | + |
| 218 | +2. **Performance**: Overhead of building query objects vs. direct string construction? |
0 commit comments