Ecto.Repo.load
You're seeing just the callback
load
, go back to Ecto.Repo module for more information.
Specs
load( module_or_map :: module() | map(), data :: map() | Keyword.t() | {list(), list()} ) :: Ecto.Schema.t() | map()
Loads data
into a struct or a map.
The first argument can be a a 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.
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.
To load data from non-database sources, use Ecto.embedded_load/3
.
Examples
iex> MyRepo.load(User, %{name: "Alice", age: 25})
%User{name: "Alice", age: 25}
iex> MyRepo.load(User, [name: "Alice", age: 25])
%User{name: "Alice", age: 25}
data
can also take form of {fields, values}
:
iex> MyRepo.load(User, {[:name, :age], ["Alice", 25]})
%User{name: "Alice", age: 25, ...}
The first argument can also be a types
map:
iex> types = %{name: :string, age: :integer}
iex> MyRepo.load(types, %{name: "Alice", age: 25})
%{name: "Alice", age: 25}
This function is especially useful when parsing raw query results:
iex> result = Ecto.Adapters.SQL.query!(MyRepo, "SELECT * FROM users", [])
iex> Enum.map(result.rows, &MyRepo.load(User, {result.columns, &1}))
[%User{...}, ...]