Alembic v4.0.0 Alembic.ToEctoSchema behaviour View Source
The Alembic.ToEctoSchema
behaviour converts a struct in the Alembic
namespace to
an Ecto.Schema.t
struct.
Link to this section Summary
Types
nil
if an empty singletonstruct
- if a non-empty singleton[]
- if an empty collection[struct]
- if a non-empty collection
A module that defines __struct__/0
and __schema__(:fields)
as happens when use Ecto.Schema
is run in a module
Maps JSON API Alembic.Resource.type
to ecto_schema_module
for casting that
Alembic.Resource.type
Functions
Converts nested params
to nested ecto_schema_module
and its associations
Link to this section Types
nil
if an empty singletonstruct
- if a non-empty singleton[]
- if an empty collection[struct]
- if a non-empty collection
A module that defines __struct__/0
and __schema__(:fields)
as happens when use Ecto.Schema
is run in a module.
ecto_schema_module_by_type() :: %{ optional(Alembic.Resource.type()) => ecto_schema_module() }
Maps JSON API Alembic.Resource.type
to ecto_schema_module
for casting that
Alembic.Resource.type
.
Link to this section Functions
to_ecto_schema(Alembic.ToParams.params(), ecto_schema_module()) :: struct()
Converts nested params
to nested ecto_schema_module
and its associations.
belongs_to
belongs_to
associations look for the foreign key in the params and set it in the Ecto.Schema.t
iex> Alembic.ToEctoSchema.to_ecto_schema(
...> %{
...> "post_id" => 1,
...> "text" => "Comment Text"
...> },
...> Alembic.TestComment
...> )
%Alembic.TestComment{
post_id: 1,
text: "Comment Text"
}
The assocation can also have nested params.
iex> Alembic.ToEctoSchema.to_ecto_schema(
...> %{
...> "post" => %{
...> "id" => 1,
...> "text" => "Post Text"
...> },
...> "text" => "Comment Text"
...> },
...> Alembic.TestComment
...> )
%Alembic.TestComment{
post: %Alembic.TestPost{
id: 1,
text: "Post Text"
},
post_id: 1,
text: "Comment Text"
}
Neither the nested id nor the foreign key is required to be set
iex> Alembic.ToEctoSchema.to_ecto_schema(
...> %{
...> "post" => %{
...> "text" => "Post Text"
...> },
...> "text" => "Comment Text"
...> },
...> Alembic.TestComment
...> )
%Alembic.TestComment{
post: %Alembic.TestPost{
text: "Post Text"
},
text: "Comment Text"
}
When the association can be marked as empty with nil
in the params
iex> Alembic.ToEctoSchema.to_ecto_schema(
...> %{
...> "post" => nil,
...> "text" => "Comment Text"
...> },
...> Alembic.TestComment
...> )
%Alembic.TestComment{
post: nil,
text: "Comment Text"
}
The id of the nested association params will override the foreign key when both are given
iex> Alembic.ToEctoSchema.to_ecto_schema(
...> %{
...> "post" => %{
...> "id" => 1
...> },
...> "post_id" => 2
...> },
...> Alembic.TestComment
...> )
%Alembic.TestComment{
post: %Alembic.TestPost{
id: 1
},
post_id: 1
}
has_many
has_many
associations look for nested parameters in an array
iex> Alembic.ToEctoSchema.to_ecto_schema(
...> %{
...> "comments" => [
...> %{
...> "id" => 1,
...> "text" => "Comment 1"
...> },
...> %{
...> "id" => 2,
...> "text" => "Comment 2"
...> }
...> ]
...> },
...> Alembic.TestPost
...> )
%Alembic.TestPost{
comments: [
%Alembic.TestComment{
id: 1,
text: "Comment 1"
},
%Alembic.TestComment{
id: 2,
text: "Comment 2"
}
]
}
has_one
has_one
association look for nested parameters
iex> Alembic.ToEctoSchema.to_ecto_schema(
...> %{
...> "profile" => %{
...> "image_url" => "https://example.com/image.png"
...> }
...> },
...> Alembic.TestAuthor
...> )
%Alembic.TestAuthor{
profile: %Alembic.TestProfile{
image_url: "https://example.com/image.png"
}
}
many_to_many
many_to_many
work the same way as has_many
: associations parameters are nested in an array
iex> Alembic.ToEctoSchema.to_ecto_schema(
...> %{
...> "tags" => [
...> %{"name" => "first"},
...> %{"name" => "second"}
...> ],
...> "text" => "Tagged Post"
...> },
...> Alembic.TestPost
...> )
%Alembic.TestPost{
tags: [
%Alembic.TestTag{
name: "first"
},
%Alembic.TestTag{
name: "second"
}
],
text: "Tagged Post"
}
to_ecto_schema( %{type: Alembic.Resource.type()}, Alembic.ToParams.params(), ecto_schema_module_by_type() ) :: struct()
Link to this section Callbacks
to_ecto_schema( struct(), Alembic.ToParams.resource_by_id_by_type(), ecto_schema_module_by_type() ) :: ecto_schema()
Parameters
struct
- anAlembic.Document.t
hierarchy structattributes_by_id_by_type
- Maps a resource identifier’sAlembic.ResourceIdentifier.type
andAlembic.ResourceIdentifier.id
to itsAlembic.Resource.attributes
fromAlembic.Document.t
included
.ecto_schema_module_by_type
-
Returns
nil
if an empty singletonstruct
- if a non-empty singleton[]
- if an empty collection[struct]
- if a non-empty collection