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.Money

Built-in types

AtomElixir valueDynamoDB
:stringString.t()S
:integerinteger()N
:floatfloat()N
:decimalDecimal.t() (optional dep)N
:booleanboolean()BOOL
:binarybinary()B
:utc_datetimeDateTime.t() (ISO 8601)S
:naive_datetimeNaiveDateTime.t()S
:dateDate.t()S
:timeTime.t()S
:mapmap() (string keys)M
:listlist()L
:string_setMapSet.t(String.t())SS
:number_setMapSet.t(number())NS
:binary_setMapSet.t(binary())BS
:anyinferred via Dynamo.Encodableany
{:list, type}[type]L
:vector, {:vector, n}[float()] (embedding)L of N
a Dynamo.Schemathat 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.

Resolves a type spec (builtin atom or module) to a module implementing Dynamo.Type. Raises ArgumentError for unknown types.

Types

dynamo_value()

@type dynamo_value() :: map()

Callbacks

cast(term)

(optional)
@callback cast(term()) :: {:ok, term()} | :error

Casts loosely typed input (e.g. from params) into the Elixir value. Optional.

dump(term)

@callback dump(term()) :: {:ok, dynamo_value()} | :error

Converts an Elixir value into a DynamoDB attribute value.

load(dynamo_value)

@callback load(dynamo_value()) :: {:ok, term()} | :error

Converts a DynamoDB attribute value back into an Elixir value.

Functions

builtin()

Returns the list of built-in type atoms.

dump(type, value)

@spec dump(atom() | module(), term()) :: {:ok, dynamo_value()} | :error

Dumps a value with the given type. nil always becomes NULL.

dump!(type, value, field \\ "value")

@spec dump!(atom() | module(), term(), String.t() | atom()) :: dynamo_value()

Dumps a value or raises ArgumentError with an actionable message.

load(type, value)

@spec load(atom() | module(), dynamo_value()) :: {:ok, term()} | :error

Loads a DynamoDB attribute value with the given type. NULL always becomes nil.

load!(type, value, field \\ "value")

Loads or raises.

resolve!(type)

@spec resolve!(atom() | module()) :: module()

Resolves a type spec (builtin atom or module) to a module implementing Dynamo.Type. Raises ArgumentError for unknown types.