Skip to main content

Differences

A Content Edit Save with Values set to Differences takes a map of field changes keyed by record id, and dari produces it two ways: diff compares an asset before and after an edit and reports only what moved, and toDifferences() hands over everything a new record holds. Both return a Differences, whose byRecord is the value the Save wants. The three shapes a script hands to a Save are drawn under Handing content to a Save; this page is what the map holds and how it is worked out.

Comparing an edit​

A Save step takes only what changed, so compare the asset before and after:

1
const { Record, diff } = dari;
2
3
const article = Record.fromState(payload.values);
4
article.put("headline", "Storm closes harbor early");
5
6
diff(payload.values, article.toState()).byRecord;

Bind that value to a Content Edit action Save with Values set to Differences. It covers the asset and every embedded object inside it in one piece.

You do not have to track what you touched; diff works it out by comparing, and it is precise about where a change belongs. Changing a field on an embedded object reports a change on that object and none on its parent. Adding to a list reports both the parent's list and the new object. Changing nothing reports nothing.

How values are compared​

diff compares as Dari does when it saves, not by strict equality, so it reports the changes a save would make and no others:

  • null, false, empty text, an empty list and an empty object all count as equal, so clearing a field that was already empty is not a change.
  • A field missing from one side counts as null on that side.
  • Lists compare item by item, in order. A list whose order carries no meaning, such as a set of tags the CMS may reorder, would otherwise report as a change every run. Name those fields and diff compares them as unordered:
1
diff(payload.values, article.toState(), { unorderedFields: ["tags", "sections"] });

What the map holds​

byRecord is keyed by the id of each record that changed, the asset and any embedded object alike, with the changed fields under each in their stored form. What diff gives back answers a few questions besides "what changed":

CallWhat it gives you
changes.byRecordEvery change, keyed by record id: the value the Differences field wants
changes.isEmptyWhether anything changed at all
changes.recordIdsThe ids of the records that changed
changes.forRecord(id)Just that record's changed fields
changes.embeddedFor(id)Every changed record except the one named: the embedded objects, ready for a Save
changes.toJson(indent)The whole thing as JSON text, for logging while you work out why something is or is not changing

diff is the writer's tool: it builds the map a Save takes, keyed by record id, with no before values and no labels. To read what changed between two revisions, to report an editor's changes or have an AI Agent review them, use a Content Compare step instead, which labels each change and marks text up the way Brightspot's Compare view does.

Creating​

For an asset you are creating, there is nothing to compare against. Ask the record for all of it:

1
const story = new Record("com.example.Story")
2
.put("headline", input.headline)
3
.put("lead", new Record("com.example.Caption").put("text", input.caption));
4
5
story.toDifferences().byRecord;

Pick the content type on the Save step itself; the script supplies everything else. The step fills in the new content's own _id and _type, so nothing in the script has to agree with them. toDifferences() is also the right call when a script may be creating or editing and doesn't know which, as in the fetch-or-create shape: it hands over the whole record either way, and the Save's own lookup decides.

What a Save does with the map​

The Save reads the map as one record per key. The key that names the asset itself is written as the asset's changes, and its identity and type come from the step, from the asset it looked up or the type you picked, overwriting the map if it disagrees. Every other key is an embedded object, which carries its own identity and type, which is what dari writes. Before saving, the step checks that the value is an object keyed by record ids with an object of fields under each, and fails with the reason if not; whether those fields suit the record is checked by the save itself. A map that names no single root is refused.

A run that changes nothing​

A Save that matches a record and changes no value fails rather than reporting a silent success. isEmpty tells you before the Save does, so a step can skip a pointless save:

1
const changes = diff(payload.values, article.toState());
2
3
({ changed: !changes.isEmpty, differences: changes.byRecord });

An Expression condition on changed then routes around the Save, which binds differences directly. How to make both pickable is under Making the result pickable. A Save with Create If Missing is exempt from the rule: it asks for content to exist in a given state, and finding it already so is the request being satisfied, so the fetch-or-create shape needs no such guard.

Was this page helpful?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.