Selecto.Pivot (Selecto v0.3.8)

Pivot functionality for retargeting joined tables as primary query focus.

The Pivot 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 pivot - shift from events to orders
selecto
|> Selecto.filter([{"event_id", 123}])
|> Selecto.pivot(: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 pivot configuration from a Selecto query.

Check if a Selecto query has pivot configuration applied.

Pivot the query to focus on a different table while preserving existing context.

Reset/remove pivot configuration from a Selecto query.

Validate that a pivot 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_pivot_config(selecto)

@spec get_pivot_config(Selecto.Types.t()) :: Selecto.Types.pivot_config() | nil

Get the pivot configuration from a Selecto query.

Link to this function

has_pivot?(selecto)

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

Check if a Selecto query has pivot configuration applied.

Link to this function

pivot(selecto, target_schema, opts \\ [])

@spec pivot(Selecto.Types.t(), atom(), keyword()) :: Selecto.Types.t()

Pivot the query to focus on a different table while preserving existing context.

parameters

Parameters

  • selecto - The Selecto struct to pivot
  • target_schema - Atom representing the target table to pivot to
  • opts - Optional configuration (see module docs)

returns

Returns

Updated Selecto struct with pivot configuration applied.

examples

Examples

selecto
|> Selecto.filter([{"event_id", 123}])
|> Selecto.pivot(:orders)
|> Selecto.select(["product_name"])
Link to this function

reset_pivot(selecto)

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

Reset/remove pivot configuration from a Selecto query.

Link to this function

validate_pivot_path(selecto, join_path)

@spec validate_pivot_path(Selecto.Types.t(), [atom()]) :: :ok | {:error, String.t()}

Validate that a pivot path exists and is traversable.