Diff an Object or a Database Branch with the JS Client

Open inAnthropic

Prerequisites

  • TerminusDB running locally or a DFRNT Hub account
  • 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"
      }
   }
]

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?