View Source Dynamo.Type behaviour (Dynamo v0.2.0)
Behaviour for schema field types.
A type converts between an Elixir value and a DynamoDB attribute value
(%{"S" => "..."}, %{"N" => "1"}, …).
defmodule MyApp.Type.Money do
use Dynamo.Type
def dump(%Money{} = m), do: {:ok, %{"N" => Money.to_string(m)}}
def dump(_), do: :error
def load(%{"N" => n}), do: {:ok, Money.parse!(n)}
def load(_), do: :error
end
field :price, MyApp.Type.MoneyBuilt-in types
| Atom | Elixir value | DynamoDB |
|---|---|---|
:string | String.t() | S |
:integer | integer() | N |
:float | float() | N |
:decimal | Decimal.t() (optional dep) | N |
:boolean | boolean() | BOOL |
:binary | binary() | B |
:utc_datetime | DateTime.t() (ISO 8601) | S |
:naive_datetime | NaiveDateTime.t() | S |
:date | Date.t() | S |
:time | Time.t() | S |
:map | map() (string keys) | M |
:list | list() | L |
:string_set | MapSet.t(String.t()) | SS |
:number_set | MapSet.t(number()) | NS |
:binary_set | MapSet.t(binary()) | BS |
:any | inferred via Dynamo.Encodable | any |
{:list, type} | [type] | L |
:vector, {:vector, n} | [float()] (embedding) | L of N |
a Dynamo.Schema | that struct (embedded) | M |
nil is handled uniformly: it dumps to %{"NULL" => true} and loads back to
nil for every type. Fields that are nil are omitted from written items by
default (see Dynamo.Schema).
Summary
Callbacks
Casts loosely typed input (e.g. from params) into the Elixir value. Optional.
Converts an Elixir value into a DynamoDB attribute value.
Converts a DynamoDB attribute value back into an Elixir value.
Functions
Returns the list of built-in type atoms.
Dumps a value with the given type. nil always becomes NULL.
Dumps a value or raises ArgumentError with an actionable message.
Loads a DynamoDB attribute value with the given type. NULL always becomes nil.
Loads or raises.
Resolves a type spec (builtin atom or module) to a module implementing
Dynamo.Type. Raises ArgumentError for unknown types.
Types
@type dynamo_value() :: map()
Callbacks
Casts loosely typed input (e.g. from params) into the Elixir value. Optional.
@callback dump(term()) :: {:ok, dynamo_value()} | :error
Converts an Elixir value into a DynamoDB attribute value.
@callback load(dynamo_value()) :: {:ok, term()} | :error
Converts a DynamoDB attribute value back into an Elixir value.
Functions
Returns the list of built-in type atoms.
@spec dump(atom() | module(), term()) :: {:ok, dynamo_value()} | :error
Dumps a value with the given type. nil always becomes NULL.
Dumps a value or raises ArgumentError with an actionable message.
@spec load(atom() | module(), dynamo_value()) :: {:ok, term()} | :error
Loads a DynamoDB attribute value with the given type. NULL always becomes nil.
Loads or raises.
Resolves a type spec (builtin atom or module) to a module implementing
Dynamo.Type. Raises ArgumentError for unknown types.