Query Builder¶
Query
¶
Bases: Generic[T]
Build and execute fluent ORM queries.
Attributes:
| Name | Type | Description |
|---|---|---|
model_cls |
Model class used to hydrate results. |
|
where_clause |
list[QueryNode]
|
Accumulated filter nodes for the query. |
order_by_clause |
list[dict[str, str]]
|
Sort definitions sent to the Rust core. |
Attributes¶
model_cls = model_cls
instance-attribute
¶
where_clause = []
instance-attribute
¶
order_by_clause = []
instance-attribute
¶
Functions¶
__init__(model_cls)
¶
where(node)
¶
Add a filter condition to the query
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
QueryNode
|
A QueryNode representing the condition (e.g., User.id == 1). |
required |
Returns:
| Type | Description |
|---|---|
Query[T]
|
The current Query instance for chaining. |
Examples:
order_by(field, direction='asc')
¶
Add an ordering clause to the query
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Any
|
The field to order by (e.g., User.username). |
required |
direction
|
str
|
The direction of the sort ("asc" or "desc"). |
'asc'
|
Returns:
| Type | Description |
|---|---|
Query[T]
|
The current Query instance for chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If direction is not "asc" or "desc". |
Examples:
limit(value)
¶
Limit the number of records returned
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
The maximum number of records to return. |
required |
Returns:
| Type | Description |
|---|---|
Query[T]
|
The current Query instance for chaining. |
Examples:
offset(value)
¶
Skip a specific number of records
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
The number of records to skip. |
required |
Returns:
| Type | Description |
|---|---|
Query[T]
|
The current Query instance for chaining. |
Examples:
all()
async
¶
count()
async
¶
update(**fields)
async
¶
Update all records matching the current query
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**fields
|
Field names and values to update. |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
The number of records updated. |
Examples:
first()
async
¶
delete()
async
¶
exists()
async
¶
add(*instances)
async
¶
Add links to a many-to-many relationship
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*instances
|
Any
|
Target model instances that provide an |
()
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the query is not bound to a many-to-many context. |
Examples:
remove(*instances)
async
¶
Remove links from a many-to-many relationship
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*instances
|
Any
|
Target model instances that provide an |
()
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the query is not bound to a many-to-many context. |
Examples:
clear()
async
¶
__repr__()
¶
Return a developer-friendly representation of the query
BackRelationship
¶
Bases: Query[T]
Represent reverse relationship queries with Query typing support
Examples:
>>> class User(Model):
... id: Annotated[int, FerroField(primary_key=True)]
... name: str
... posts: BackRelationship[list["Post"]] = None