Selecto.EctoAdapter (Selecto v0.4.5)

Copy Markdown

Ecto integration for Selecto query builder.

This module provides functionality to automatically configure Selecto from Ecto schemas and repositories, making it easy to integrate with Phoenix applications.

Usage

# Configure from Ecto repo and schema
selecto = Selecto.EctoAdapter.configure(MyApp.Repo, MyApp.User)

# With options
selecto = Selecto.EctoAdapter.configure(MyApp.Repo, MyApp.User, 
  joins: [:posts, :comments],
  redact_fields: [:password_hash]
)

# Generate domain from schema
domain = Selecto.EctoAdapter.schema_to_domain(MyApp.User)

Summary

Functions

Configure Selecto from an Ecto repository and schema.

Get available associations from an Ecto schema.

Get field information from an Ecto schema.

Generate a Selecto domain configuration from an Ecto schema.

Functions

configure(repo, schema, opts \\ [])

Configure Selecto from an Ecto repository and schema.

Parameters

  • repo - The Ecto repository module (e.g., MyApp.Repo)
  • schema - The Ecto schema module to use as the source table
  • opts - Configuration options

Options

  • :joins - List of associations to include as joins (atoms)
  • :redact_fields - List of fields to exclude from queries (atoms)
  • :custom_columns - Map of custom column definitions
  • :custom_filters - Map of custom filter definitions
  • :extensions - List of Selecto extension specs applied during domain build
  • :validate - Whether to validate domain configuration (boolean)
  • :name - Custom name for the domain (string)

Examples

# Basic usage
selecto = Selecto.EctoAdapter.configure(MyApp.Repo, MyApp.User)

# With joins and redacted fields
selecto = Selecto.EctoAdapter.configure(MyApp.Repo, MyApp.User,
  joins: [:posts, :profile],
  redact_fields: [:password_hash, :email]
)

# With custom columns
selecto = Selecto.EctoAdapter.configure(MyApp.Repo, MyApp.User,
  custom_columns: %{
    "full_name" => %{
      name: "Full Name",
      select: {:concat, ["first_name", {:literal, " "}, "last_name"]}
    }
  }
)

get_associations(schema)

Get available associations from an Ecto schema.

Returns a list of association names that can be used in joins.

get_fields(schema)

Get field information from an Ecto schema.

Returns a map with field names and their types.

schema_to_domain(schema, opts \\ [])

Generate a Selecto domain configuration from an Ecto schema.

This function introspects the Ecto schema and generates a compatible domain map for Selecto configuration.

Parameters

  • schema - The Ecto schema module
  • opts - Configuration options (see configure/3)

Returns

A domain map compatible with Selecto.configure/2