View Source Dynamo.Update (Dynamo v0.2.0)

Builds DynamoDB UpdateExpressions from a keyword list or map of changes.

Used by Dynamo.Repo.update/3 and Dynamo.Repo.transaction/2. Values are dumped with the schema's field type when a schema is given, otherwise with :any.

Dynamo.Update.build(User, name: "Jane", login_count: {:increment, 1}, legacy: :remove)
#=> {:ok, %{
#     "UpdateExpression" => "SET #a0 = :v0, #a1 = #a1 + :v1 REMOVE #a2",
#     "ExpressionAttributeNames" => %{"#a0" => "name", "#a1" => "login_count", "#a2" => "legacy"},
#     "ExpressionAttributeValues" => %{":v0" => %{"S" => "Jane"}, ":v1" => %{"N" => "1"}}
#   }}

Operators

  • value – SET field = value
  • {:increment, n} / {:decrement, n} – SET field = field + n (field must exist)
  • {:add, n} – ADD field n (atomic counter; creates the attribute if missing)
  • {:add, set} – ADD field set (adds elements to a set)
  • {:delete, set} – DELETE field set (removes elements from a set)
  • {:append, list} / {:prepend, list} – SET field = list_append(...)
  • {:if_not_exists, value} – SET field = if_not_exists(field, value)
  • :remove – REMOVE field

Placeholders are generated (#a0, :v0…) so attribute names never need to be valid identifiers and reserved words are always safe.

Summary

Functions

Builds the expression parts. schema may be nil (values dumped as :any). Key fields of the schema are rejected — updating a key requires delete + put.

Types

change()

@type change() ::
  term()
  | {:increment, number()}
  | {:decrement, number()}
  | {:add, number() | MapSet.t()}
  | {:delete, MapSet.t()}
  | {:append, list()}
  | {:prepend, list()}
  | {:if_not_exists, term()}
  | :remove

result()

@type result() :: %{required(String.t()) => String.t() | map()}

Functions

build(schema, changes)

@spec build(module() | nil, keyword() | map()) ::
  {:ok, result()} | {:error, Dynamo.Error.t()}

Builds the expression parts. schema may be nil (values dumped as :any). Key fields of the schema are rejected — updating a key requires delete + put.