3

I am using Azure Cosmos DB. I have created a simple trigger in Azure Portal as follows:

enter image description here

  var context = getContext();
  var request = context.getRequest();

  // item to be created in the current operation
  var itemToCreate = request.getBody();
  itemToCreate["address"] = "test";

  // update the item that will be created
  request.setBody(itemToCreate);

Unfortunately this trigger is not being triggered when I insert new documents. I have also tried to set the "Trigger Type" to "Post". Am I missing anything?

fascynacja
  • 1,625
  • 4
  • 17
  • 35

3 Answers3

6

Great question! I always thought that triggers would run automatically :).

I believe the triggers are not run automatically whenever a document is inserted. What you would need to do is specify the trigger that you want to run when you're creating the document.

What you need to do is register the trigger by passing the trigger name as the request option when sending create document request.

For example, see the code here: https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-use-stored-procedures-triggers-udfs#pre-triggers (copied below as well). Notice the use of PreTriggerInclude in RequestOptions:

dynamic newItem = new
{
    category = "Personal",
    name = "Groceries",
    description = "Pick up strawberries",
    isComplete = false
};

Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
RequestOptions requestOptions = new RequestOptions { PreTriggerInclude = new List<string> { "trgPreValidateToDoItemTimestamp" } };
await client.CreateDocumentAsync(containerUri, newItem, requestOptions);
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • I have managed to create Java code which informs Azure to run trigger: RequestOptions options = new RequestOptions(); options.setPreTriggerInclude(Arrays.asList("pre")); . But the problem is that this table is already updated from place where I cannot change the code, so it would be perfect if the trigger would trigger himself ;-) – fascynacja Apr 01 '20 at 13:21
  • 2
    `so it would be perfect if the trigger would trigger himself` ... Me too :). Based on SQL experience, my expectation was that it would run itself....aah well...you learn something new everyday. – Gaurav Mantri Apr 01 '20 at 13:23
  • Guarav I will accept your answer, since is correct but let the question hang for a bit, maybe someone else would have some other suggestions:) – fascynacja Apr 01 '20 at 13:25
  • Absolutely! I am also eagerly waiting for our friends in Cosmos DB team to chime in and provide solution. – Gaurav Mantri Apr 01 '20 at 13:27
  • Just thinking out loud, may be handle this situation via Change Feed mechanism where you trigger a Function when a document is inserted? – Gaurav Mantri Apr 01 '20 at 15:29
2

Firing triggers automatically in relational databases makes sense since there is schema in the database, you kind of know what to handle in a trigger logic. In NoSQL database, since there is no schema, you may end up with a large script to handle all kind of exceptions. Large script in triggers means higher bills in Cloud. Making triggers automatic can make many customers bill really high specially in IOT solutions. You can read about Azure Cosmos DB pre/post triggers in my post. https://h-savran.blogspot.com/2020/03/create-triggers.html

Hasan Savran
  • 367
  • 2
  • 5
  • thank you for the answer. Your explanation makes sense although I would prefer to decide myself if I want to be billed more for my triggers or not:) also it should be up to me if i make my triggers heave or just doing one line changes, with all the consequences. But apparently not much choice in here. – fascynacja Apr 02 '20 at 09:23
  • and also what about creating the item directly from the portal?:) – fascynacja Apr 02 '20 at 09:26
0

The only way according to the answers from @Guarav Mantri and @Hasan Savaran is to specify the trigger while creating the item through the API. I have managed to do it in Java Azure SDK like that:

 RequestOptions options = new RequestOptions();
 options.setPreTriggerInclude(Arrays.asList("pre"));
 documentClient.createDocument(
                    collectionLink(),
                    documentToAdd,
                    options,
                    true);

Although I am not happy with this solution because for instance the trigger will not be triggered when creating the item via Portal.

fascynacja
  • 1,625
  • 4
  • 17
  • 35