Drops.Relation.Schema (drops_relation v0.1.0)
View SourceRepresents schema metadata for a relation.
This struct stores all extracted metadata about a relation including primary keys, foreign keys, field information, and indices. It serves as a central container for schema information used for automatic Ecto schema generation and query compilation.
Structure
source
- The database table namefields
- List of field definitions with types and metadataprimary_key
- Primary key informationforeign_keys
- List of foreign key relationshipsindices
- List of database indices
Access Behavior
The schema implements the Access behavior, allowing field lookup by name:
schema[:email] # Returns the email field
schema[:id] # Returns the id field
Usage with Relation Modules
Schemas are automatically created when you define a relation module and are
accessible via the schema/0
function:
defmodule MyApp.Users do
use Drops.Relation, repo: MyApp.Repo
schema("users", infer: true)
end
# Access the relation's schema
schema = MyApp.Users.schema()
# Inspect schema metadata
schema.source # => :users
schema.fields # => [%Field{name: :id, type: :integer}, ...]
schema.primary_key # => %PrimaryKey{fields: [:id]}
# Access specific fields
email_field = schema[:email]
id_field = schema[:id]
# Get field information
email_field.type # => :string
email_field.meta # => %{type: :varchar, ...}
Field Access Examples
# Check if a field exists
if schema[:email] do
IO.puts("Email field exists with type: " <> to_string(schema[:email].type))
end
# Get all field names
field_names = Enum.map(schema.fields, & &1.name)
# => [:id, :name, :email, :active, :inserted_at, :updated_at]
# Filter fields by type
string_fields = Enum.filter(schema.fields, &(&1.type == :string))
Summary
Functions
Finds a field by name in the schema.
Merges two schemas, with the right schema taking precedence for conflicts.
Creates a new Schema struct with the provided metadata.
Access
Access behavior implementation for fetching fields by name.
Access behavior implementation for updating fields.
Access behavior implementation for removing fields.
Types
@type t() :: %Drops.Relation.Schema{ fields: [Drops.Relation.Schema.Field.t()], foreign_keys: [Drops.Relation.Schema.ForeignKey.t()], indices: [Drops.Relation.Schema.Index.t()], primary_key: Drops.Relation.Schema.PrimaryKey.t() | nil, source: atom() }
Functions
@spec find_field(t(), atom()) :: Drops.Relation.Schema.Field.t() | nil
Finds a field by name in the schema.
Examples
iex> field = Drops.Relation.Schema.find_field(schema, :email)
iex> field.name
:email
Merges two schemas, with the right schema taking precedence for conflicts.
Parameters
left
- The base schemaright
- The schema to merge into the base, takes precedence
Returns
A merged Drops.Relation.Schema.t() struct.
Examples
iex> left = Drops.Relation.Schema.new(:users, pk, [], [field1], [])
iex> right = Drops.Relation.Schema.new(:users, pk, [], [field2], [])
iex> merged = Drops.Relation.Schema.merge(left, right)
iex> length(merged.fields)
2
Creates a new Schema struct with the provided metadata.
Parameters
source
- The table nameprimary_key
- Primary key informationforeign_keys
- List of foreign key relationshipsfields
- List of field metadataindices
- Index information
@spec new(atom(), [Drops.Relation.Schema.Field.t()], keyword()) :: t()
@spec new( atom(), Drops.Relation.Schema.PrimaryKey.t(), [Drops.Relation.Schema.ForeignKey.t()], [Drops.Relation.Schema.Field.t()], [Drops.Relation.Schema.Index.t()] ) :: t()
Access
Access behavior implementation for fetching fields by name.
Examples
iex> schema[:email] # Returns the email field
iex> schema[:id] # Returns the id field
Access behavior implementation for updating fields.
Examples
iex> get_and_update(schema, :email, fn field -> {field, %{field | type: :string}} end)
Access behavior implementation for removing fields.
Examples
iex> pop(schema, :email) # Removes and returns the email field