Durable.Helpers (Durable v2.0.2)
View SourceHelper functions for working with workflow data in the pipeline model.
These functions provide a convenient, pipe-friendly API for updating the data that flows through workflow steps.
Usage
use Durable.HelpersThis imports all helper functions into your workflow module.
Keys
A workflow's data has string keys at every depth, as it is stored. Each
helper takes a key as an atom or a string and reads or writes the string:
assign(data, :status, "paid") sets data["status"].
Best practice
Read and write a workflow's data through these helpers. They let a step name its keys with atoms, as Elixir code usually does, over data that is stored with strings:
step :charge, fn order ->
total = get(order, :total) # order["total"]
city = get(order, [:customer, :city]) # order["customer"]["city"]
{:ok, assign(order, :charged, total)} # sets order["charged"]
endorder.total, order[:total] and %{total: total} = order don't read the
data: its keys are strings. Map.put(order, :total, 1) in the middle of a
step leaves :total beside the stored "total" until the step returns, so
a later get(order, :total) in that step still reads the old one;
assign/3 writes the string and has no such gap.
Examples
step :process, fn data ->
{:ok, data
|> assign(:status, :processing)
|> assign(%{started_at: DateTime.utc_now()})
|> increment(:step_count)}
end
Summary
Functions
Injects helper functions into the calling module.
Appends a value to a list in the data.
Merges a map into the data.
Assigns a value to a key in the data map.
Deletes a key from the data.
Gets a value from the data.
Gets a value from the data with a default.
Checks if a key exists in the data.
Increments a numeric value in the data.
Updates an existing value in the data using a function.
Updates a value in the data using a function.
Functions
Injects helper functions into the calling module.
Appends a value to a list in the data.
If the key doesn't exist, creates a new list with the value.
Examples
append(data, :items, new_item)
append(data, :events, %{type: :clicked, time: DateTime.utc_now()})
Merges a map into the data.
Examples
assign(data, %{order_id: 123, status: :pending})
Assigns a value to a key in the data map.
Examples
assign(data, :order_id, 123)
assign(data, "order_id", 123)
Deletes a key from the data.
Examples
delete(data, :temporary_value)
Gets a value from the data.
Returns nil if the key doesn't exist.
Examples
order_id = get(data, :order_id)A list is a path into the maps inside the data:
total = get(data, [:invoice, :total])
Gets a value from the data with a default.
Examples
count = get(data, :retry_count, 0)
Checks if a key exists in the data.
Examples
if has_key?(data, :order_id) do
# ...
end
Increments a numeric value in the data.
If the key doesn't exist, starts from 0.
Examples
increment(data, :retry_count)
increment(data, :total, 100)
Updates an existing value in the data using a function.
Raises if the key doesn't exist.
Examples
update(data, :count, &(&1 + 1))
Updates a value in the data using a function.
If the key doesn't exist, uses the default value.
Examples
update(data, :count, 0, &(&1 + 1))
update(data, :items, [], &[new_item | &1])