EctoReflection (EctoReflection v0.1.0) View Source
Provides some conveniences to work with Queries and Schemas
Link to this section Summary
Functions
Check if the association is defined on the Ecto.Schema
List all associations defined in a Ecto.Schema
List all associations defined in a Ecto.Schema
as binaries
Check if an attribute exists on an Ecto.Schema
.
Return a list of attributes for an Ecto.Schema
.
Return a list of attributes as binary()
for an Ecto.Schema
Check if a field is defined on a schema
Return a list of fields (virtual or non virtual) defined in an Ecto.Schema
Return a list of fields as binaries (virtual or non virtual) defined in an Ecto.Schema
Return a list of all modules that use Ecto.Schema
in the application.
Check if a non-virtual field is defined on a schema
Return a list of fields that to the database.
Return a list of fields that to the database as binaries.
Return the type for a Ecto.Schema
's attribute
Return a map of the schema and its types as a convenient shorthand
Check if the virtual field exists on the Ecto.Schema
Return a list of virtual fields defined on a schema
Return a list of virtual fields defined on a schema as binaries
Link to this section Functions
Specs
Check if the association is defined on the Ecto.Schema
iex> EctoReflection.association?(User, :projects)
true
iex> EctoReflection.association?(User, :unassociated)
false
Also safe with binaries
iex> EctoReflection.association?(User, "projects")
true
iex> EctoReflection.association?(User, "unassociated")
false
Specs
associations(Ecto.Schema) :: [atom()]
List all associations defined in a Ecto.Schema
iex> EctoReflection.associations(User)
~w[profile addresses projects todos]a
Specs
associations(Ecto.Schema, :binary) :: [binary()]
List all associations defined in a Ecto.Schema
as binaries
iex> EctoReflection.associations(User, :binary)
~w[profile addresses projects todos]
Specs
Check if an attribute exists on an Ecto.Schema
.
iex> EctoReflection.attribute?(Foo, :bars)
true
iex> EctoReflection.attribute?(Foo, :non_attribute)
false
You can also safely use a binary to check:
iex> EctoReflection.attribute?(Foo, "bars")
true
iex> EctoReflection.attribute?(Foo, "non_attribute")
false
Specs
attributes(Ecto.Schema) :: [atom()]
Return a list of attributes for an Ecto.Schema
.
If we have the following Ecto.Schema
defmodule Foo do
use Ecto.Schema
schema "foos" do
field :bars
field :bazes
timestamps()
end
end
We can do:
iex> EctoReflection.attributes(Foo)
~w[bars bazes id inserted_at password updated_at]a
Specs
attributes(Ecto.Schema, :binary) :: [binary()]
Return a list of attributes as binary()
for an Ecto.Schema
iex> EctoReflection.attributes(Foo, :binary)
~w[bars bazes id inserted_at password updated_at]
Specs
Specs
embeds(Ecto.Schema) :: [atom()]
Specs
embeds(Ecto.Schema, :binary) :: [binary()]
Specs
Check if a field is defined on a schema
iex> EctoReflection.field?(Foo, :bars)
true
iex> EctoReflection.field?(Foo, :non_field)
false
You can also safely pass in a binary
iex> EctoReflection.field?(Foo, "bars")
true
iex> EctoReflection.field?(Foo, "non_field")
false
Specs
fields(Ecto.Schema) :: [atom()]
Return a list of fields (virtual or non virtual) defined in an Ecto.Schema
iex> EctoReflection.fields(Foo)
~w[bars bazes id inserted_at password updated_at]a
Specs
fields(Ecto.Schema, :binary) :: [binary()]
Return a list of fields as binaries (virtual or non virtual) defined in an Ecto.Schema
iex> EctoReflection.fields(Foo)
~w[bars bazes id inserted_at password updated_at]a
Specs
Specs
relationships(Ecto.Schema) :: [atom()]
Specs
relationships(Ecto.Schema, :binary) :: [binary()]
Specs
Return a list of all modules that use Ecto.Schema
in the application.
This is determined by checking if the __schema__
function is defined on that module
Specs
Check if a non-virtual field is defined on a schema
iex> EctoReflection.source_field?(Foo, :bars)
true
iex> EctoReflection.source_field?(Foo, :password)
false
Also safe with binaries
iex> EctoReflection.source_field?(Foo, "bars")
true
iex> EctoReflection.source_field?(Foo, "password")
false
Specs
Return a list of fields that to the database.
In other words, all non-virtual fields
iex> EctoReflection.source_fields(Foo)
~w[id bars bazes inserted_at updated_at]a
Specs
source_fields(Ecto.Schema, :binary) :: [binary()]
Return a list of fields that to the database as binaries.
In other words, all non-virtual fields
iex> EctoReflection.source_fields(Foo, :binary)
~w[id bars bazes inserted_at updated_at]
Specs
source_schema(Ecto.Query) :: Ecto.Schema
Specs
type(Ecto.Schema, atom() | binary()) :: {atom(), atom() | module()} | {atom(), module(), module() | [atom()]}
Return the type for a Ecto.Schema
's attribute
Accepts an atom or a binary for the attribute
iex> EctoReflection.type(User, :projects)
{:has_many, Project}
Specs
types(Ecto.Schema) :: map()
Return a map of the schema and its types as a convenient shorthand
Say we have the following User Ecto.Schema
defmodule User do
use Ecto.Schema
schema "users" do
field(:username, :string)
field(:email, :string)
field(:age, :integer)
field(:password, :string, virtual: true)
field(:password_digest, :string)
has_one(:profile, Profile)
many_to_many(:addresses, Address, join_through: AddressUser)
has_many(:projects, Project)
has_many(:todos, through: [:projects, :todos])
timestamps()
end
end
Its types would look like this:
iex> EctoReflection.types(User)
%{
addresses: {:many_to_many, Address, AddressUser},
age: {:field, :integer},
email: {:field, :string},
id: {:field, :id},
inserted_at: {:field, :naive_datetime},
password: {:virtual_field, :string},
password_digest: {:field, :string},
profile: {:has_one, Profile},
projects: {:has_many, Project},
todos: {:has_many_through, Todo, [:projects, :todos]},
updated_at: {:field, :naive_datetime},
username: {:field, :string}
}
Specs
Check if the virtual field exists on the Ecto.Schema
iex> EctoReflection.virtual_field?(Foo, :password)
true
iex> EctoReflection.virtual_field?(Foo, :bars)
false
Also works with binaries
iex> EctoReflection.virtual_field?(Foo, "password")
true
iex> EctoReflection.virtual_field?(Foo, "bars")
false
Specs
virtual_fields(Ecto.Schema) :: [atom()]
Return a list of virtual fields defined on a schema
iex> EctoReflection.virtual_fields(Foo)
~w[password]a
Specs
virtual_fields(Ecto.Schema, :binary) :: [binary()]
Return a list of virtual fields defined on a schema as binaries
iex> EctoReflection.virtual_fields(Foo, :binary)
~w[password]