Drops.Relation.Schema.Field (drops_relation v0.1.0)

View Source

Represents a field in a database table/schema.

This struct stores comprehensive information about a database field including its name, type information, and source mapping.

Examples

# Simple field
%Drops.Relation.Schema.Field{
  name: :email,
  type: :string
}

Summary

Functions

Merges two Field structs, with the right field taking precedence.

Creates a new Field struct.

Types

meta()

@type meta() :: %{
  optional(:type) => term(),
  optional(:adapter) => atom(),
  optional(:source) => atom(),
  optional(:nullable) => boolean() | nil,
  optional(:default) => term() | nil,
  optional(:check_constraints) => [String.t()] | nil,
  optional(:primary_key) => boolean() | nil,
  optional(:foreign_key) => boolean() | nil,
  optional(:association) => boolean() | nil
}

t()

@type t() :: %Drops.Relation.Schema.Field{
  meta: meta(),
  name: atom(),
  source: term(),
  type: term()
}

Functions

merge(left, right)

@spec merge(t(), t()) :: t()

Merges two Field structs, with the right field taking precedence.

This function is useful for combining inferred fields with custom fields, where custom fields should override inferred properties while preserving metadata from both sources.

Parameters

  • left - The base field (typically inferred)
  • right - The field to merge (typically custom, takes precedence)

Examples

iex> inferred = Drops.Relation.Schema.Field.new(:email, :string, :string, :email, %{nullable: true})
iex> custom = Drops.Relation.Schema.Field.new(:email, :string, {:parameterized, {Ecto.Enum, %{values: [:active, :inactive]}}}, :email)
iex> merged = Drops.Relation.Schema.Field.merge(inferred, custom)
iex> merged.type
{:parameterized, {Ecto.Enum, %{values: [:active, :inactive]}}}
iex> merged.meta.nullable
true

new(name, type, meta \\ %{})

@spec new(atom(), any(), meta()) :: t()

Creates a new Field struct.

Parameters

  • name - The field name as an atom
  • type - The normalized type (e.g., :string, :integer)
  • type - The original Ecto type
  • source - The source column name in the database
  • meta - Optional metadata map with nullable, default, check_constraints

Examples

iex> Drops.Relation.Schema.Field.new(:email, :string, :string, :email)
%Drops.Relation.Schema.Field{
  name: :email,
  type: :string,
  type: :string,
  source: :email,
  meta: %{}
}

iex> meta = %{nullable: false, default: "active"}
iex> Drops.Relation.Schema.Field.new(:status, :string, :string, :status, meta)
%Drops.Relation.Schema.Field{
  name: :status,
  type: :string,
  type: :string,
  source: :status,
  meta: %{nullable: false, default: "active"}
}

new(name, db_type, ecto_type, source, meta \\ %{})

@spec new(atom(), any(), any(), atom(), meta()) :: t()