I am not able to understand how Trigger works in Cosmos DB Emulator. Please explain me how it is used.
I am not able to understand how Trigger works in Cosmos DB Emulator. Please explain me how it is used.
Firstly,you need to know that Trigger in cosmos db is different from the azure function cosmos db trigger. It can't be executed automatically. It should be defined specifically when you call the sdk method,please refer to the sample on the official document.
Create trigger:
string triggerId = "trgPreValidateToDoItemTimestamp";
Trigger trigger = new Trigger
{
Id = triggerId,
Body = File.ReadAllText($@"..\js\{triggerId}.js"),
TriggerOperation = TriggerOperation.Create,
TriggerType = TriggerType.Pre
};
Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
await client.CreateTriggerAsync(containerUri, trigger);
Of course,you could create the trigger via cosmos db portal or emulator UI:
Then you need to define the name of the trigger you created when you use corresponding sdk method. For example,you created Pre trigger and Trigger Operation as Create,then you could use below code to active it:
Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
RequestOptions requestOptions = new RequestOptions { PreTriggerInclude = new List<string> { "<your trigger name>" } };
await client.CreateDocumentAsync(containerUri, newItem, requestOptions);