Navigation

Create and Configure Triggers

Overview

You can create and configure triggers for all application event types through the Stitch user interface or by importing a valid trigger configuration file.

Create a Trigger

To create a trigger in the Stitch UI:

  1. Click Triggers under MongoDB Cluster in the left-hand navigation.
  2. Select the tab for the event type that you wish to create a trigger for.
  3. Click Add Trigger in the top right to open the trigger configuration page.
  4. Enter configuration values for the trigger and click Save at the bottom of the page.

To create a trigger with stitch-cli:

  1. Add a trigger configuration file to the triggers subdirectory of a local application directory. The filename can be anything you prefer, but must end with a .json extension.
  2. Import the application directory into your application.

Note

Stitch does not enforce specific filenames for trigger configuration files. However, once imported, Stitch will rename each configuration file to match the name of the trigger it defines, e.g. mytrigger.json.

Trigger Configuration

Common Parameters

All event triggers share a set of common configuration parameters that are defined with the following configuration file schema:

/triggers/<trigger name>.json
{
   "type": <string>,
   "name": <string>,
   "function_name": <string>,
   "config": <document>,
   "disabled": <boolean>
}

All triggers share the following configuration parameters:

Field Description

Trigger Type

type
Required. The type of application event that the trigger listens for and responds to.

Trigger Name

name
Required. The name of the trigger.

Linked Function

function_name
Required. The name of the Stitch function that the trigger executes whenever it fires. The trigger passes the application event object that caused it to fire as the only argument to this function.

Database Trigger Parameters

Database event triggers have additional configuration parameters that are defined as a nested object in the config field of the trigger configuration file:

config: {
  "service_name": <string>,
  "database": <string>,
  "collection": <string>,
  "operation_types": [<string>, ...],
  "full_document": <boolean>,
  "match": <document>
}

Database event triggers have the following additional configuration parameters:

Field Description

Cluster

config.service_name
Required. The name of the MongoDB Service that the trigger is associated with.

Database Name

config.database
Required. The MongoDB database that contains the watched collection.

Collection Name

config.collection
Required. The name of the collection that the trigger watches for change events.

Operation Types

config.operation_types
Required. A list of one or more database operation types that cause the trigger to fire. Each operation type must be formatted as a fully-capitalized string, e.g. "INSERT".

Full Document

config.full_document

Required. If true, indicates that UPDATE change events should include the most current majority-committed version of the modified document in the fullDocument field.

Note

This option only affects UPDATE change events. INSERT and REPLACE events always include the fullDocument field. DELETE events never include the fullDocument field. For more information, see the change events reference page.

Warning

Update operations executed from MongoDB Compass or the MongoDB Atlas Data Explorer fully replace the previous document. As a result, update operations from these clients will generate REPLACE change events rather than UPDATE events.

Match Expression

config.match
Optional. A $match expression document that Stitch includes in the underlying change stream pipeline for the trigger. This is useful when you want to filter change events beyond their operation type. The trigger will only fire if the expression evaluates to true for a given change event.

Authentication Trigger Parameters

Authentication event triggers have additional configuration parameters that are defined as a nested object in the config field of the trigger configuration file:

config: {
  "operation_type": <string>,
  "providers": [<string>, ...]
}

Authentication event triggers have the following additional configuration parameters:

Field Description

Operation Type

config.operation_type
Required. The authentication operation type that causes the trigger to fire. The operation type must be formatted as a fully-capitalized string, e.g. "LOGIN".

Providers

config.providers

Required. A list of one or more authentication provider types. The trigger will only listen for authentication events produced by these providers.

The following values are valid:

  • "oauth2-google"
  • "oauth2-facebook"
  • "custom-token"
  • "local-userpass"
  • "api-key"
  • "anon-user"