EctoUtils.Schema (ecto_utils v0.2.1)
Utility module containing functions which aim to augment modules that use Ecto.Schema
Link to this section Summary
Functions
Allows you to execute any EctoUtils.Schema
function in the module that use
-es this
macro.
Ecto sanitizes inputs passed in via attrs such that trying to cast a field to nil
will
be determined to be a no-op and will silently fail.
Helper function for handling the boilerplate of preloading associations before setting them as an association, particularly useful for many-to-many or has-many relations.
Link to this section Functions
Allows you to execute any EctoUtils.Schema
function in the module that use
-es this
macro.
This is useful for building a custom MyApp.Schema
module that you use rather than the default
Ecto.Schema
, allowing for convenient extension capabilities, building off of EctoUtils
's
extended base functionality.
Usage:
defmodule MyApp.Schema do
use Ecto.Schema
use EctoUtils.Schema, repo: MyApp.Repo
end
mark_nilable(changeset, params, field)
Specs
mark_nilable(Ecto.Changeset.t(), params :: map(), field :: atom()) :: Ecto.Changeset.t()
Ecto sanitizes inputs passed in via attrs such that trying to cast a field to nil
will
be determined to be a no-op and will silently fail.
This function lets you explicitly mark fields where nil
casts are valid, and will perform
them for you.
Use as follows:
def changeset(%MyApp.User{} = struct, attrs) do
struct
|> cast([:is_deleted])
|> mark_nilable(params, :is_deleted)
end
# When called as `changeset(%MyApp.User{is_deleted: true}, %{is_deleted: nil}})`, will return
# a changeset containing the `is_deleted: nil` change, where without using this function,
# no change would be detected.
preload_put_assoc(repo, changeset, attrs, field, attr_key, filters \\ [])
Specs
preload_put_assoc( repo :: module(), Ecto.Changeset.t(), attrs :: map(), field :: atom(), attr_key :: atom(), filters :: Keyword.t() ) :: Ecto.Changeset.t()
Helper function for handling the boilerplate of preloading associations before setting them as an association, particularly useful for many-to-many or has-many relations.