Ecto.embedded_load
embedded_load
, go back to Ecto module for more information.
Specs
embedded_load( module_or_map :: module() | map(), data :: map(), format :: atom() ) :: Ecto.Schema.t() | map()
Loads previously dumped data
in the given format
into a schema.
The first argument can be a an embedded schema module, or a map (of types) and determines the return value: a struct or a map, respectively.
The second argument data
specifies fields and values that are to be loaded.
It can be a map, a keyword list, or a {fields, values}
tuple. Fields can be
atoms or strings.
The third argument format
is the format the data has been dumped as. For
example, databases may dump embedded to :json
, this function allows such
dumped data to be put back into the schemas.
Fields that are not present in the schema (or types
map) are ignored.
If any of the values has invalid type, an error is raised.
Note that if you want to load data into a non-embedded schema that was
directly persisted into a given repository, then use Ecto.Repo.load/2
.
Examples
iex> result = Ecto.Adapters.SQL.query!(MyRepo, "SELECT users.settings FROM users", [])
iex> Enum.map(result.rows, fn [settings] -> Ecto.embedded_load(Setting, Jason.decode!(settings), :json) end)
[%Setting{...}, ...]