Alembic v3.4.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 singleton
  • struct - 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

Link to this section Types

Link to this type ecto_schema() View Source
ecto_schema() :: nil | struct | [] | [struct, ...]
  • nil if an empty singleton
  • struct - if a non-empty singleton
  • [] - if an empty collection
  • [struct] - if a non-empty collection
Link to this type ecto_schema_module() View Source
ecto_schema_module() :: atom

A module that defines __struct__/0 and __schema__(:fields) as happens when use Ecto.Schema is run in a module.

Link to this type ecto_schema_module_by_type() View Source
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

Link to this function put_named_association(acc, params, ecto_schema_module, association_name) View Source
Link to this function to_ecto_schema(params, ecto_schema_module) View Source
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"
}
Link to this function to_ecto_schema(map, params, ecto_schema_module_by_type) View Source

Link to this section Callbacks

Link to this callback to_ecto_schema(struct, arg1, ecto_schema_module_by_type) View Source

Parameters

  • struct - an Alembic.Document.t hierarchy struct
  • attributes_by_id_by_type - Maps a resource identifier’s Alembic.ResourceIdentifier.type and Alembic.ResourceIdentifier.id to its Alembic.Resource.attributes from Alembic.Document.t included.
  • ecto_schema_module_by_type -

Returns

  • nil if an empty singleton
  • struct - if a non-empty singleton
  • [] - if an empty collection
  • [struct] - if a non-empty collection