Selecto.Output.Transformers.Structs (Selecto v0.3.8)

Transforms query results into structured data using Elixir structs.

This transformer creates struct instances from database rows, providing:

  • Dynamic struct module support or predefined structs
  • Field name transformations (snake_case, camelCase, PascalCase)
  • Type coercion from database types to Elixir types
  • Validation of required fields and field mapping
  • Memory-efficient processing for large datasets

options

Options

  • :struct_module - Module to create structs from (required)
  • :field_mapping - Map of database field names to struct field names
  • :transform_keys - Key transformation strategy (:snake_case, :camel_case, :pascal_case)
  • :coerce_types - Whether to coerce database types to Elixir types (default: true)
  • :type_strategy - Type coercion strategy (:strict, :lenient, :preserve)
  • :validate_fields - Whether to validate all struct fields are present
  • :default_values - Map of default values for missing fields
  • :enforce_keys - Whether to enforce required struct keys

examples

Examples

# Basic struct transformation
transform(rows, columns, aliases, User, [])

# With field mapping and validation
transform(rows, columns, aliases, User, [
  field_mapping: %{"user_id" => :id, "full_name" => :name},
  validate_fields: true,
  enforce_keys: true
])

# Dynamic struct creation
transform(rows, columns, aliases, nil, [
  struct_module: DynamicRecord,
  transform_keys: :camel_case,
  coerce_types: true
])

Link to this section Summary

Functions

Streams struct transformation for large datasets.

Transforms database result rows into struct instances.

Link to this section Types

Link to this type

struct_option()

@type struct_option() ::
  {:struct_module, module()}
  | {:field_mapping, map()}
  | {:transform_keys, :snake_case | :camel_case | :pascal_case | :none}
  | {:coerce_types, boolean()}
  | {:type_strategy, :strict | :lenient | :preserve}
  | {:validate_fields, boolean()}
  | {:default_values, map()}
  | {:enforce_keys, boolean()}
Link to this type

struct_options()

@type struct_options() :: [struct_option()]

Link to this section Functions

Link to this function

stream_transform(rows, columns, aliases, struct_module, options)

@spec stream_transform(
  rows :: Enumerable.t(),
  columns :: list(),
  aliases :: map(),
  struct_module :: module() | nil,
  options :: struct_options()
) :: {:ok, Enumerable.t()} | {:error, Selecto.Error.t()}

Streams struct transformation for large datasets.

Returns a stream of struct instances to minimize memory usage.

Link to this function

transform(rows, columns, aliases, struct_module, options)

@spec transform(
  rows :: [list()],
  columns :: list(),
  aliases :: map(),
  struct_module :: module() | nil,
  options :: struct_options()
) :: {:ok, [struct()]} | {:error, Selecto.Error.t()}

Transforms database result rows into struct instances.

Returns a list of struct instances with properly typed and named fields.