W3cubDocs

/Phoenix

Ecto.DataType protocol

Casts and dumps a given struct into an Ecto type.

While Ecto.Type allows developers to cast/load/dump any value from the storage into the struct based on the schema, Ecto.DataType allows developers to convert existing data types into primitive Ecto types without the schema information.

For example, Elixir’s native Date struct implements the Ecto.DataType protocol so it is properly converted to a tuple when directly passed to adapters:

defimpl Ecto.DataType, for: Date do
  def dump(%Date{day: day, month: month, year: year}) do
    {:ok, {year, month, day}}
  end
end

Summary

Types

t()

Functions

dump(value)

Invoked when the data structure has not been dumped along the way and must fallback to its database representation

Types

t()

t() :: term

Functions

dump(value)

dump(term) :: {:ok, term} | :error

Invoked when the data structure has not been dumped along the way and must fallback to its database representation.

© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/ecto/Ecto.DataType.html