A JSON-LD patch container for TerminusDB document diffs.
A Patch holds the raw JSON-LD patch content produced by TerminusDB.Diff
operations. It provides convenience projections for extracting the "before"
and "after" (update) states from SwapValue operations.
Quick start
{:ok, patch} = TerminusDB.Diff.diff_object(config,
before: %{"@id" => "Person/1", "name" => "old"},
after: %{"@id" => "Person/1", "name" => "new"}
)
patch.update # => %{"name" => "new"}
patch.before # => %{"name" => "old"}
Summary
Functions
Extracts the "before" values from SwapValue operations, recursively.
Creates a deep copy of the patch.
Parses a JSON string into a %Patch{}.
Parses a JSON string into a %Patch{}, or raises.
Serializes a %Patch{} to a JSON string.
Extracts the "after" (updated) values from SwapValue operations,
recursively.
Types
Functions
Extracts the "before" values from SwapValue operations, recursively.
Includes all fields from the patch content, preserving non-SwapValue
values as-is. This is intentionally asymmetric with update/1, which
only includes SwapValue fields — before/1 aims to reconstruct the
full "before" state while update/1 shows only what changed.
Examples
iex> patch = %TerminusDB.Patch{content: %{"name" => %{"@op" => "SwapValue", "@before" => "old", "@after" => "new"}}}
iex> TerminusDB.Patch.before(patch)
%{"name" => "old"}
Creates a deep copy of the patch.
Examples
iex> patch = %TerminusDB.Patch{content: %{"name" => "value"}}
iex> copy = TerminusDB.Patch.copy(patch)
iex> copy == patch
true
Parses a JSON string into a %Patch{}.
Examples
iex> {:ok, patch} = TerminusDB.Patch.from_json(~s({"name": {"@op": "SwapValue", "@before": "old", "@after": "new"}}))
iex> patch.content["name"]["@after"]
"new"
Parses a JSON string into a %Patch{}, or raises.
Examples
iex> patch = TerminusDB.Patch.from_json!(~s({"name": {"@op": "SwapValue", "@before": "old", "@after": "new"}}))
iex> patch.content["name"]["@before"]
"old"
Serializes a %Patch{} to a JSON string.
Examples
iex> patch = %TerminusDB.Patch{content: %{"name" => %{"@op" => "SwapValue", "@before" => "old", "@after" => "new"}}}
iex> json = TerminusDB.Patch.to_json(patch)
iex> is_binary(json)
true
Extracts the "after" (updated) values from SwapValue operations,
recursively.
Only fields containing SwapValue operations are included in the result.
Non-SwapValue fields are omitted. This is intentionally asymmetric with
before/1, which includes all fields (preserving non-SwapValue values)
to allow full reconstruction of the "before" state.
Examples
iex> patch = %TerminusDB.Patch{content: %{"name" => %{"@op" => "SwapValue", "@before" => "old", "@after" => "new"}}}
iex> TerminusDB.Patch.update(patch)
%{"name" => "new"}