TerminusDB.Patch (terminusdb_ex v0.3.3)

Copy Markdown View Source

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

t()

@type t() :: %TerminusDB.Patch{content: map() | [map()]}

Functions

before(patch)

@spec before(t()) :: map()

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"}

copy(patch)

@spec copy(t()) :: t()

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

from_json(json_string)

@spec from_json(String.t()) :: {:ok, t()} | {:error, term()}

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"

from_json!(json_string)

@spec from_json!(String.t()) :: t()

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"

to_json(patch)

@spec to_json(t()) :: String.t()

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

update(patch)

@spec update(t()) :: map()

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"}