Diff an Object or a Database Branch with the JS Client

Open inAnthropic

Prerequisites

  • TerminusDB running locally — see Docker setup for instructions
  • A database with data (two states to compare)

What you'll achieve By the end of this guide, you will know how to compute diffs between database states and apply patches.

TerminusDB provides structural diff and patch operations — compare any two documents, branches, or commits to see exactly what changed at the field level, then apply those changes programmatically. This is git-like version control for data, not line-based text diff.

Diff an object

Return the diff from two objects

Example: JavaScript
const diffObjects = async () => {
   const before = { "@id" : "Person/Jane", "@type" : "Person", "name" : "Jane"}
   const after = { "@id" : "Person/Jane", "@type" : "Person", "name" : "Janine"}
   const options = {keep:{ "@id" : true }}

   //in the options you can list the properties that you would like to always include in the diff result.
   const diffResult = await client.getJSONDiff(before, after, options)

   console.log("the diff result ", JSON.stringify(diffResult,null,4))
}

Here is an example of a diff result between two objects

Example: JSON
{
   "name":{
      "@op":"ValueSwap",
      "@before":"Jane",
      "@after":"Janine"
   },
   "@id":"Person/Jane"
}

Get the patch of differences between branches or commits.

Example: JavaScript
const diffDocsVersion = async () => {
   const beforeVersion = "a73ssscfx0kke7z76083cgswszdxy6l"
   const afterVersion =  "73rqpooz65kbsheuno5dsayh71x7wf4"
   const options = {keep:{ "@id" : true, "name" : true }}

   const diffResult = await client.getVersionDiff(beforeVersion, afterVersion, null, options)

   console.log("the diff result ", JSON.stringify(diffResult,null,4))
}

Here is the example result

Example: JSON
[
   {
      "@id":"Person/Jane",
      "@type":"Person",
      "name" : "Jane"
      "age":{
         "@after":23,
         "@before":22,
         "@op":"SwapValue"
      }
   },
   {
      "@id":"Person/Tom",
      "@type":"Person",
      "name" : "Tom"
      "age":{
         "@after":10,
         "@before":null,
         "@op":"SwapValue"
      }
   }
]

The unfold option

Diffs between commits expand subdocuments inline by default — a change inside a subdocument is reported as a field-level patch on its parent. Pass unfold: false in the options to compare documents as stored: a changed subdocument is then reported as a swap of the subdocument ID rather than of its contents.

Example: JavaScript
const options = { keep: { "@id": true }, unfold: false }
const diffResult = await client.getVersionDiff(beforeVersion, afterVersion, null, options)

See Folded diffs for a worked example with real output.

Get the patch of difference between a document and an object.

Example: JavaScript
const diffDocToObject = async () => {
   const jsonObject = { "@id" : "Person/Jane", "@type" : "Person", "name" : "Jannet"}
   const options = {keep:{ "@id" : true, "name" : true }}

   //in the options you can list the properties that you would like to see in the diff result.
   const diffResult = await client.getVersionObjectDiff("main", jsonObject, "Person/Jane", options)

   console.log("the diff result ", JSON.stringify(diffResult,null,4))
}

Next steps

Was this helpful?