Query
query() maps to DynamoDB's Query. Requires the partition key. Returns a chainable builder.
Basic Query
# Basic query — returns all orders for a user
results = orders.query(user_id="usr_123")
Chain Conditions
results = (
orders.query(user_id="usr_123")
.where(gte="ord_100") # sort key condition (KeyConditionExpression)
.filter(total__gt=100) # filter condition (FilterExpression)
.select("total", "status") # projection (reduces RCU)
.limit(20) # max items per page
.scan_forward(False) # descending order (newest first)
.consistent() # strongly consistent read
)
All builder methods return self, so you can chain in any order.
Auto-execute
Query builders auto-execute on first access. No need to call .execute() explicitly:
results = orders.query(user_id="usr_123")
# Any of these triggers execution:
for order in results: # iteration
print(order)
first = results[0] # indexing
n = len(results) # length
if results: # truthiness
print("has orders")
key = results.last_key # pagination key
count = results.scanned_count # items scanned before filtering
You can still call .execute() explicitly if you prefer. It returns a QueryResult with .items, .last_key, .count, and .scanned_count attributes.