Hydrex allows you to automattically fill up all virtual fields or your Ecto model at once.
In order to use it you need to define a virtual field in your Ecto model and a resolver function for that virtual field with the same name and finally you derive your model with Hydratable protocol.
defmodule MyApp.Url do
#...
@derive {Hydrex.Hydratable, []}
schema "urls" do
field :link, :string
field :title, :string
# Virtual field
field :is_safe, :boolean, virtual: true
end
# resolver function
def is_safe(url) do
String.starts_with?(url.link, "https://")
end
endAnd when calling you can pass an opts list informing whether or not you want your ecto models hydrated.
import Hydrex.Hydratable.Utils, only: [maybe_hydrate: 2]
Repo.all(Url) |> maybe_hydrate(hydrate: true)If available in Hex, the package can be installed
by adding hydrex to your list of dependencies in mix.exs:
def deps do
[
{:hydrex, "~> 0.1.0"}
]
endDocumentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/hydrex.