Overview of Triggers

Learn how to use triggers to apply changes to document requests in Cosmos DB.

Introduction

Another way we can use the database engine is with triggers. Cosmos DB triggers differ from the ones we can create in traditional SQL databases in two main ways:

  • We can create triggers that run both before and after a document action:

    • Pre-trigger

    • Post-trigger

  • Triggers are not executed automatically.

Definition

Triggers are JavaScript functions that have no input arguments and don’t return a value. We use getRequest() and getResponse() to interact with the engine.

Pre-triggers

For example, we use a container where we store users. Each user has an email property. We want to be sure the email is lowercase and that the domain property is created automatically. We can use a pre-trigger; with getRequest(), we access the request before the document is created.

Get hands-on with 1200+ tech skills courses.