lumix.utils.orm.LXTypedQuery¶
- class lumix.utils.orm.LXTypedQuery(session, model)[source]¶
Type-safe query builder with fluent API.
Provides a chainable query interface with full type safety and IDE autocomplete. The generic type parameter ensures that filter predicates and results have proper type information.
The query builder supports filtering via lambda predicates, where the IDE knows the structure of the model being queried.
- Parameters:
session (Any)
model (Type[TModel])
- session¶
The underlying ORM session
- model¶
The ORM model class being queried
- _filters¶
Internal list of filter predicates to apply
- Type Parameters:
TModel: The ORM model type being queried
Examples
Basic querying with type safety:
query = LXTypedQuery(session, Product) products = query.all() # Type: List[Product]
Filtering with lambda predicates:
expensive = ( LXTypedQuery(session, Product) .filter(lambda p: p.price > 100) .filter(lambda p: p.in_stock) .all() )
Using with LXORMContext:
ctx = LXORMContext(session) active_products = ctx.query(Product).filter(lambda p: p.active).all()
Note
The filter predicates are applied in Python after retrieving results from the ORM. For large datasets, consider using ORM-specific filtering before wrapping in LXTypedQuery.
- __init__(session, model)[source]¶
Initialize typed query builder.
- Parameters:
Examples
Create query builder directly:
query = LXTypedQuery(session, Product)
Methods
__init__(session, model)Initialize typed query builder.
all()Execute query and return all matching results.
filter(predicate)Add a type-safe filter predicate to the query.
first()Execute query and return the first matching result.
- __init__(session, model)[source]¶
Initialize typed query builder.
- Parameters:
Examples
Create query builder directly:
query = LXTypedQuery(session, Product)
- filter(predicate)[source]¶
Add a type-safe filter predicate to the query.
The predicate receives model instances with full type information, enabling IDE autocomplete for all model attributes.
- Parameters:
predicate (
Callable[[TypeVar(TModel)],bool]) – A callable that takes a model instance and returns True/False. The IDE will autocomplete model attributes in the lambda.- Return type:
Self- Returns:
Self for method chaining
Examples
Filter by single condition:
query.filter(lambda p: p.price > 100)
Chain multiple filters:
query.filter(lambda p: p.active).filter(lambda p: p.in_stock)
Complex filtering:
query.filter(lambda p: p.price > 50 and p.category == "Electronics")
- all()[source]¶
Execute query and return all matching results.
Retrieves all records from the ORM, then applies the filter predicates in order.
Examples
Get all filtered results:
products = query.filter(lambda p: p.active).all()
Use in LumiX variable:
production = ( LXVariable[Product, float]("production") .from_data(query.filter(lambda p: p.available).all()) .indexed_by(lambda p: p.id) )
- first()[source]¶
Execute query and return the first matching result.
- Return type:
- Returns:
The first model instance matching all filters, or None if no matches
Examples
Get single result:
product = query.filter(lambda p: p.id == 5).first()
Check for existence:
if query.filter(lambda p: p.name == "Widget").first(): print("Widget exists")