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

Link to this function

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.

Link to this function

get_retarget_config(selecto)

@spec get_retarget_config(Selecto.Types.t()) :: Selecto.Types.retarget_config() | nil

Get the retarget configuration from a Selecto query.

Link to this function

has_retarget?(selecto)

@spec has_retarget?(Selecto.Types.t()) :: boolean()

Check if a Selecto query has retarget configuration applied.

Link to this function

reset_retarget(selecto)

@spec reset_retarget(Selecto.Types.t()) :: Selecto.Types.t()

Reset/remove retarget configuration from a Selecto query.

Link to this function

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 retarget
  • target_schema - Atom representing the target table to retarget to
  • opts - 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"])
Link to this function

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.