Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Cosmos DB Document Change Trigger With Old and new document?

I have the cosmos db trigger in azure functions and it fires when the document is changed which is fine.

But my document is big and i only need the updated property in the trigger.

I can solve this by comparing the old and new document but in trigger i only get the updated document.

So is there a way to get the old and updated document in trigger.

My Azure Function trigger is

module.exports = async function (context, documents) {
    if (!!documents && documents.length > 0) {
        context.log('Document Id: ', documents[0].id);
        context.log('Document : ', documents[0]);
    }
    context.done(); }

My Function Bindings are

{   "bindings": [
    {
      "type": "cosmosDBTrigger",
      "name": "documents",
      "direction": "in",
      "leaseCollectionName": "leases",
      "connectionStringSetting": "AzureWebJobsCosmosDBConnectionString",
      "databaseName": "ToDoList",
      "collectionName": "Items",
      "createLeaseCollectionIfNotExists": true
    } ],   "disabled": false }

Thanks in Advance

like image 394
Tushar patel Avatar asked Sep 20 '25 01:09

Tushar patel


2 Answers

There is no way, at this point, to obtain the previous version or to just get the delta.

The Change Feed contains the operation and payload, not a reference to the previous state.

like image 140
Matias Quaranta Avatar answered Sep 22 '25 16:09

Matias Quaranta


There is an open suggestion @ https://feedback.azure.com You might implement a pattern as stated in above link:

  1. Store every version/change as a separate item
  2. Read the change feed to merge/consolidate changes and trigger appropriate actions downstream.

So basically you need another function with a change feed listener that stores every document in a separate "version" collection. Afterwards you can add a CosmosDB binding, that gets the latest version from latter collection.

like image 22
DSpirit Avatar answered Sep 22 '25 16:09

DSpirit