EctoMapSet (ecto_map_set v0.2.1) View Source

MapSet support for Ecto.

The MapSets are backed by arrays in postgres. Currently untested in other database engines.

Typed MapSets

Migration Example:

def change do
  create table(:my_sets) do
    add(:favorite_floats, {:array, :float})
  end
end

Schema Example:

def MySet do
  use Ecto.Schema
  schema "my_sets" do
    field :favorite_floats, EctoMapSet, of: :float
  end
end

Then when you retrieve your row, the data will be marshalled into a MapSet:

iex> Repo.get(MySet, id)
%MySet{favorite_floats: %MapSet<[47.0, 88.8]>}}

If you use an ecto changeset to marshal your data in, your mapset column may be any sort of enumerable. In most cases that will be a MapSet, a List, or a Stream.

NB: for PostgreSQL, if you are making a mapset of arrays, they must all have the same length. This is a limitation of PostgreSQL. This happens for a schema that looks like this:

schema "my_vectors" do
  field :vectors, EctoMapSet, of: {:array, :float}
end

Untyped MapSets

Migration Example:

def change do
  create table(:my_untyped_sets) do
    add(:favorite_terms, {:array, :binary})
  end
end

Schema Example:

def MyUntypedSet do
  use Ecto.Schema
  schema "my_untyped_sets" do
    field :favorite_terms, EctoMapSet, of: :term
  end
end

Then when you retrieve your row, the data will be marshalled into a MapSet:

iex> Repo.get(MySet, id)
%MySet{favorite_terms: %MapSet<[#PID<0.107.0>, :foo, "bar"}}

Safety

the EctoMapSet field declaration can take two safety options:

  • :safety sets the safety level
    • :unsafe returns an unsafe row with no safety checks.
    • :drop returns an unsafe row with all unsafe data redacted from the set (default).
    • :errors raises ArgumentError when you try to pull an unsafe row.
  • :non_executable
    • true any stored term with a lambda will trigger a safety check. Note in order to use this feature you must include the plug_crypto library.
    • false or nil (default) only unsafe atoms are checked.

Link to this section Summary

Link to this section Functions