Selecto.Retarget (Selecto v0.4.3)
Retarget functionality for retargeting joined tables as primary query focus.
The Retarget feature allows you to shift the perspective of a Selecto query from the source table to any joined table, while preserving existing filters through subqueries.
examples
Examples
# Basic retarget - shift from events to orders
selecto
|> Selecto.filter([{"event_id", 123}])
|> Selecto.retarget(:orders)
|> Selecto.select(["product_name", "quantity"])
# This generates SQL like:
# SELECT o.product_name, o.quantity
# FROM orders o
# WHERE o.attendee_id IN (
# SELECT a.attendee_id FROM events e
# JOIN attendees a ON e.event_id = a.event_id
# WHERE e.event_id = 123
# )
configuration-options
Configuration Options
:preserve_filters- Whether to preserve existing filters in subquery (default: true):subquery_strategy- How to generate the subquery (:in,:exists,:join)
Link to this section Summary
Functions
Calculate the join path from the source table to the target table.
Get the retarget configuration from a Selecto query.
Check if a Selecto query has retarget configuration applied.
Reset/remove retarget configuration from a Selecto query.
Retarget the query to focus on a different table while preserving existing context.
Validate that a retarget path exists and is traversable.
Link to this section Functions
calculate_join_path(selecto, target_schema)
@spec calculate_join_path(Selecto.Types.t(), atom()) :: {:ok, [atom()]} | {:error, String.t()}
Calculate the join path from the source table to the target table.
This function analyzes the domain configuration to find the shortest path of associations from the current source to the target schema.
get_retarget_config(selecto)
@spec get_retarget_config(Selecto.Types.t()) :: Selecto.Types.retarget_config() | nil
Get the retarget configuration from a Selecto query.
has_retarget?(selecto)
@spec has_retarget?(Selecto.Types.t()) :: boolean()
Check if a Selecto query has retarget configuration applied.
reset_retarget(selecto)
@spec reset_retarget(Selecto.Types.t()) :: Selecto.Types.t()
Reset/remove retarget configuration from a Selecto query.
retarget(selecto, target_schema, opts \\ [])
@spec retarget(Selecto.Types.t(), atom(), keyword()) :: Selecto.Types.t()
Retarget the query to focus on a different table while preserving existing context.
parameters
Parameters
selecto- The Selecto struct to retargettarget_schema- Atom representing the target table to retarget toopts- Optional configuration (see module docs)
returns
Returns
Updated Selecto struct with retarget configuration applied.
examples
Examples
selecto
|> Selecto.filter([{"event_id", 123}])
|> Selecto.retarget(:orders)
|> Selecto.select(["product_name"])
validate_retarget_path(selecto, join_path)
@spec validate_retarget_path(Selecto.Types.t(), [atom()]) :: :ok | {:error, String.t()}
Validate that a retarget path exists and is traversable.