1

I have created a function triggered by Azure Cosmos DB by following https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-cosmos-db-triggered-function which is working.

Upon seeing logs, I am unable to identify if this trigger is for INSERT or UPDATE. I know that in AWS, when you add lambda as the dynamo trigger, you could identify that easily. You can even see what was original record vs updated one (for update)

Question:

  • How to identify INSERT vs UPDATE vs DELETE action? I am new to Azure, so it is possible that I am missing something in my function code.

Function Code:

module.exports = async function (context, documents) {
    context.log('1');
    if (!!documents && documents.length > 0) {
        context.log('Document Id: ', documents[0].id); <-- SHOWS ID OF THE RECORD
        context.log('Document[0]', documents[0]); <-- ENTIRE RECORD
        context.log('Documents', documents); <-- ALL RECORDS 
    }
}

context object from above:

{
  invocationId: '3ee0136e-d005-4517-a11e-c43ec9ca7c67',
  traceContext: {
    traceparent: '00-4938136b13476845524c7ae5059d84f3-badfbc56b5ef0067-00',
    tracestate: '',
    attributes: {
      OperationName: 'CosmosTrigger1'
    }
  },
  executionContext: {
    invocationId: '3ee0136e-d005-4517-a11e-c43ec9ca7c67',
    functionName: 'CosmosTrigger1',
    functionDirectory: 'C:\\home\\site\\wwwroot\\CosmosTrigger1',
    retryContext: null
  },
  bindings: {
    documents: [
      [
        Object
      ]
    ]
  },
  log: [
    Function(anonymous)
  ]{
    error: [
      Function: error
    ],
    warn: [
      Function: warn
    ],
    info: [
      Function: info
    ],
    verbose: [
      Function: verbose
    ]
  },
  bindingData: {
    invocationId: '3ee0136e-d005-4517-a11e-c43ec9ca7c67'
  },
  bindingDefinitions: [
    {
      name: 'documents',
      type: 'cosmosDBTrigger',
      direction: 'in'
    }
  ],
  done: [
    Function(anonymous)
  ]
}

Function Log: enter image description here

GThree
  • 2,708
  • 7
  • 34
  • 67

2 Answers2

1

Unfortunately, you can't distinguish between the two types (insert & update). The trigger consumes the Change Feed, and you can't filter it for a specific type of operation.

Today, you see all inserts and updates in the change feed. You can't filter the change feed for a specific type of operation.

Pexers
  • 953
  • 1
  • 7
  • 20
  • 1
    And per same doc, "Currently change feed doesn't log deletes." as well. So I think I need to find some other alternative for my use case. Thank you, @Pexers, for your response. – GThree Nov 02 '22 at 20:49
0

You can filter based on documents last modified timestamp.. this will be null by default. So when you consume an event from change feed and the last modified timestamp is null you can to a high degree of certainty treat that as an insert.. if an event has a last modified timestamp field and its not null then you can assume it was not an insert but an update and process accordingly.

As for determining deletes...Most update a soft delete property on the record so that it can be deleted later; which also allows us to consume that update on the change feed and check the soft delete property where you can infer the action.

coleh
  • 1
  • 1