EctoUtils.Schema (ecto_utils v0.2.0)

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

Link to this macro

__using__(opts)

(macro)

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
Link to this function

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.
Link to this function

preload_put_assoc(repo, changeset, attrs, field, filters \\ [])

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.