You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The expression builder generates SQL-like query strings under the hood, providing a type-safe Python API for constructing filter conditions. The `.where()` method accepts either an `Expr` object from the expression builder or a raw SQL-like query string (see [Query Language](#query-language) below).
196
+
197
+
For dynamic query building with runtime type checking, use the expression builder:
198
+
199
+
```python
200
+
from arkiv import Arkiv, IntAttr, StrAttr, IntSort, DESC
age =="18"# TypeError: IntAttr 'age' requires int, got str
227
+
status ==1# TypeError: StrAttr 'status' requires str, got int
228
+
```
229
+
230
+
**Expression Operators:**
231
+
-`&` - AND
232
+
-`|` - OR
233
+
-`~` - NOT
234
+
235
+
**Note:** Always use parentheses around comparisons when combining with `&`, `|`, or `~` due to Python operator precedence.
236
+
237
+
#### Sorting with IntSort/StrSort
238
+
239
+
Use type-specific sort classes for ORDER BY clauses:
240
+
241
+
```python
242
+
from arkiv import Arkiv, IntSort, StrSort, StrAttr, DESC
243
+
244
+
client = Arkiv()
245
+
246
+
# Define typed attributes
247
+
entity_type = StrAttr("type")
248
+
status = StrAttr("status")
249
+
250
+
# Define sorting_criteria
251
+
status_asc = StrSort("status")
252
+
age_desc = IntSort("age", DESC)
253
+
254
+
# Sort by age descending
255
+
results = client.arkiv.select() \
256
+
.where(entity_type =="user") \
257
+
.order_by(age_desc) \
258
+
.fetch()
259
+
260
+
# Multi-field sorting: status ascending, then age descending
261
+
results = client.arkiv.select() \
262
+
.where(status =="active") \
263
+
.order_by(status_asc, age_desc) \
264
+
.fetch()
265
+
```
266
+
164
267
### Query Iterator
165
268
166
-
The `query_entities` method returns an iterator that automatically handles pagination, making it easy to work with large result sets:
269
+
The `query_entities` method returns an iterator that automatically handles pagination, making it easy to work with large result sets. This is the lower-level API that the fluent query builder wraps:
167
270
168
271
```python
169
272
from arkiv import Arkiv
@@ -254,7 +357,34 @@ Note that the GLOB operator might be replace by a SQL standard LIKE operator in
254
357
255
358
Query results can be sorted by one or more attribute fields in ascending or descending order. Sorting supports both string and numeric attributes, with multi-field sorting following priority order (first field has highest priority).
256
359
257
-
#### Basic Sorting
360
+
#### Using the Query Builder (Recommended)
361
+
362
+
The query builder provides a cleaner API for sorting with `IntSort` and `StrSort`:
363
+
364
+
```python
365
+
from arkiv import Arkiv, IntSort, StrSort, StrAttr, DESC
0 commit comments