Executing Logic Based on Data Changes with Triggers

Todd WackerTodd Wacker

In this tutorial, we'll create a Trigger. Triggers allow you to execute a Script whenever your data is created, updated, or deleted!

What is a Trigger?

When you're building an application, you want events to happen based on what's going on in the application. In Syncano, you can use Triggers to achieve this.

Triggers watch your data for specific changes, and run a custom Script once those changes are observed. Triggers can be used to send a welcome email when a new user signs up, validate data on the backend before allowing it to be saved, and any other custom logic you want to run in your application!

Creating a Trigger from the Syncano Dashboard

We'll break this down into simple steps, but first make sure you have a Syncano account (sign up here), and then open up your Dashboard!

1. Adding the Trigger Socket

Once you’ve opened your Dashboard, make sure you’re on the Sockets page, which you can find at the top of the left sidebar. If this is your first Socket, you may see a list of all of the available Sockets. If not, click the “Add” button in the top right corner!

add

Then “Add” a Trigger. This will begin the customization and creation of your Trigger!

add-trigger

The first screen will ask you for the "Label", "Signals", "Data Classes", and "Script" that you want to use for your Trigger.

We're going to add a Trigger that will update every time a new user_profile is created. In Syncano, every time a new user is created, a corresponding user_profile is also created.

Complete the form with the data below and then select "Go to Script Creation". Note that you'll need to manually type in user_create_script. This will be the name for the Script we create in the next step.

trigger-form-1

2. Linking the Script

Now that we've set up the basics for our Trigger, we need to link the Script we want to run every time this Trigger occurs. We'll tie this Trigger to a Script called user_create_script.

We'll need to tell Syncano what runtime we want to use for our Script. The screenshot below shows this. We'll be running this particular Script in NodeJS.

trigger-form-2

With everything linked correctly, we have a Trigger set up. However, our user_create_script doesn't actually do anything yet. We'll write the logic for our Script in the next step.

3. Writing the Script

You'll want to select "Scripts" under "Sockets" in your Syncano Dashboard.

select-script

Then, click on the user_create_script to open up the Script Editor.

In the Script Editor, you'll be able to write the code that will run with this Script, create CONFIG variables, and view the result of running your Script. Create a simple Script as shown below. Be sure to click the "Save" button at the top once you've written your Script to save the changes.

script-editor

Executing the Trigger

Now that we have everything set up, let's try out our new Trigger! Select "Users & Groups" in your Syncano Dashboard and click "Add a User".

add-user

Create a user with whatever username and password you want and select "Confirm".

user-create

Now go back to "Sockets" in your Dashboard and select "Traces" next to your user_create Trigger.

traces

You should see that your Trigger was successfully run one time. If you click on the Trace, you can see the console output.

trace-success

Next Steps

Congratulations on creating your first Trigger! You can use Triggers in more sophisticated ways to add all kinds of additional functionality to your next application.

If you have any questions, feel free to join our Slack community or email us at [email protected]. Good luck!

Build powerful apps in half the time

Use our serverless platform to set up your backend in minutes.

Learn more
Todd Wacker
Author

Todd Wacker

Dev Evangelist, Developer