TerminusDB represents objects such as documents and schemas in JSON-LD format. Use JSON Diff and Patch to easily compare these objects to obtain differences between them.
The Diff function returns a Patch object containing differences between the objects compared. Use Patch to implement manual or programmatic actions to resolve differences. Actions include:
Retain the original object.
Change to the new (or latest) version of the object.
Create a new version of the object.
The functionality above is demonstrated in three simple steps:
Create a new MongoDB database and insert three items - item_1 to item_3 into a collection object.
const { MongoClient } =require("mongodb");// Connection URLconsturl="mongodb://localhost:27017";constclient=newMongoClient(url);// Database NameconstdbName="user_shopping_list";asyncfunctionmain() {// Use connect method to connect to the serverawaitclient.connect();console.log("Connected successfully to server");// Create the database for our example (we will use the same database throughout the tutorialconstdb=client.db(dbName);constcollection=db.collection("user_1_items");let item_1 = { _id:"U1IT00001", item_name:"Blender", max_discount:"10%", batch_number:"RR450020FRG", price:340, category:"kitchen appliance", };constitem_2= { _id:"U1IT00002", item_name:"Egg", category:"food", quantity:12, price:36, item_description:"brown country eggs", };constinsertResult=awaitcollection.insertMany([item_1, item_2]);console.log("Inserted documents =>", insertResult);constitem_3= { item_name:"Bread", quantity:2, ingredients:"all-purpose flour", expiry_date:"2021-07-13 00:00:00", };awaitcollection.insertOne(item_3);return"done";}main().then(console.log).catch(console.error).finally(() =>client.close());
client =MongoClient(os.environ["MONGO_CONNECTION_STRING"])# Create the database for our example (we will use the same database throughout the tutorialconnection = client['user_shopping_list']collection_name = connection["user_1_items"]item_1 ={"_id":"U1IT00001","item_name":"Blender","max_discount":"10%","batch_number":"RR450020FRG","price":340,"category":"kitchen appliance"}item_2 ={"_id":"U1IT00002","item_name":"Egg","category":"food","quantity":12,"price":36,"item_description":"brown country eggs"}collection_name.insert_many([item_1,item_2])expiry_date ='2021-07-13T00:00:00.000'expiry = dt.datetime.fromisoformat(expiry_date)item_3 ={"item_name":"Bread","quantity":2,"ingredients":"all-purpose flour","expiry_date": expiry}collection_name.insert_one(item_3)
Modify an item
The 'document' new_item_1 represents a modified instance of item_1 with changes to properties max_discount and price.
Retrieve the original item in the collection and compare it to the modified item new_item_1.
consttbd_endpoint=newTerminusDBClient.WOQLClient("http://127.0.0.1:6363/", { user:"admin", organization:"admin", key:"root",});// Find the item back from database in case someone already changed ititem_1 =collection.findOne({ item_name:"Blender" });constpatch=tbd_endpoint.getDiff(item_1, new_item_1);console.log(patch);
tbd_endpoint =WOQLClient("http://localhost:6363/")# Find the item back from database in case someone already changed ititem_1 = collection_name.find_one({"item_name" : "Blender"})patch = tbd_endpoint.diff(item_1, new_item_1)pprint(patch.content)
Update the original item
Update the original item in the MongoDB database. If required, review the differences prior to the update.
Define a function patchMongo to get a patch object: