# `TerminusDB.Patch`
[🔗](https://github.com/thanos/terminusdb-client-elixir/blob/v0.3.3/lib/terminus_db/patch.ex#L1)

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

# `t`

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

# `before`

```elixir
@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`

```elixir
@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`

```elixir
@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!`

```elixir
@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`

```elixir
@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`

```elixir
@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"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
