Actions Definition
What are Actions?
Stryke allows you to define your business logic by writing snippets of code (in JavaScript) that focus directly on your functionality. All the infrastructure is setup and ready for you to leverage. All you need to do is write code to implement your logic.
An action represents an event or circumstance under which one of these snippets of code (also called scripts) is executed. This can be the click of a button, an API call, creating or modifying a record.
Actions provide you endless ways of enhancing and customising your app: from manipulating and validating data, to automating tasks, integrating with 3rd parties, generating reports, performing calculations, running complex queries on your data, and much much more.
In this section we will go over how to create actions, the different types of actions that are available and how to write code that uses Stryke's features.
Actions and Scripts
When an action is triggered, it executes a snippet of JavaScript code, which we call a Script. A script is simply the code that you write that implements the feature that the action will trigger.
As an example, a script for a before create trigger could implement validation on the record's data.
For more details on Scripts and specifically how to write code, please check the Scripts section.
Working with Actions
Your app's actions can be viewed under the app's dashboard, next to the list of entities. Through the actions' list a new action can be created or existing ones can be edited.
An action is defined by the fields listed below.
Fields
name
The unique name for this action. This name needs to be unique within the app instance it is created in and can only contain alpha numeric values without any spaces. The name field is used as the identifier for the action when calling it through the Stryke's API and will be used to name the script file containing the source code for its functionality.
label
A human friendly name for the action. The value of the label field is used to show the action in the app's UI, for example on "button" actions, the text on the button will be the value of the label field.
description
A description for this action.
action type
Stryke provides different types of actions to cover different functionality and scenarios.
The action type field allows setting which type of action we are creating.
button
Button actions of this type will be shown on the app's UI as a button, which when clicked will execute the action itself. The button will be shown on all records that belong to the entity type selected as the action's related entity. When writing code for an action of type button it will be possible to access the record from which the button was clicked via: stryke.data.record
api
API actions of this type will not be shown in the app's UI, instead once created, they can be executed via an HTTP request as follows:
POST https://api.stryke.io/v0/[app instance name]/action/[action name or ID]
Actions of type API can be useful when defining a middleware or any logic that needs be triggered externally, such as when using Stryke with your own front end or from your own service.
triggers
Trigger actions are inspired by database triggers and run before or after a certain record is create, updated or deleted. They are very useful for automating processes when data is changed, performing validation, data manipulation or augmenting data with other records or data coming from 3rd parties.
See the Triggers section to know more about triggers and how to use them.
Before Create Trigger
A before create trigger runs every time a new record of the chosen entity is created, just before the new record is saved.
In the action's source code, the record being created can be accessed through the Stryke Library as follows:
stryke.data.recordBefore
: not set (always null)
stryke.data.recordAfter
: the record that is being created
"Before create" trigger scripts must return the new record to be saved using the stryke.resolve()
function. The value passed to the resolve function will be used as the data of the record being created.
After Create Trigger
An after create trigger runs every time a new record of the chosen entity is created, just after the new record is saved. In the action's source code, the record being created can be accessed through the Stryke Library as follows:
stryke.data.recordBefore
: not set (always null)
stryke.data.recordAfter
: the record that is being created
"After create" trigger scripts must be ended with either stryke.resolve()
or stryke.error()
, but it is not necessary to pass any specific value to the stryke.resolve()
function.
Before Update Trigger
A before update trigger runs every time a record of the chosen entity is updated, just before the record is modified.
In the action's source code, the record being updated can be accessed through the Stryke Library as follows:
stryke.data.recordBefore
: the record that is being updated, before any update is applied
stryke.data.recordAfter
: the record that is being updated, with the updated values
"Before update" trigger scripts must return the record to be modified, in the state in which it is to be saved,
using the stryke.resolve()
function. The value passed to the resolve function will be used as the data of the record being updated.
After Update Trigger
An after update trigger runs every time a record of the chosen entity is updated, just after the record is modified.
In the action's source code, the record being updated can be accessed through the Stryke Library as follows:
stryke.data.recordBefore
: the record that is being updated, before any update is applied
stryke.data.recordAfter
: the record that is being updated, with the updated values
"After update" trigger scripts must be ended with either stryke.resolve()
or stryke.error()
, but it is not necessary to pass any specific value to the stryke.resolve()
function.
Before Delete Trigger
A before delete trigger runs every time a record of the chosen entity is deleted, just before that record is deleted.
In the action's source code, the record being deleted can be accessed through the Stryke Library as follows:
stryke.data.recordBefore
: the record that is about to be deleted
stryke.data.recordAfter
: not set (always null)
"Before delete" trigger scripts must be ended with either stryke.resolve()
or stryke.error()
, but it is not necessary to pass any specific value to the stryke.resolve()
function.
After Delete Trigger
An after delete trigger runs every time a record of the chosen entity is deleted, just after that record is deleted.
In the action's source code, the record being deleted can be accessed through the Stryke Library as follows:
stryke.data.recordBefore
: the record that is about to be deleted
stryke.data.recordAfter
: not set (always null)
"After delete" trigger scripts must be ended with either stryke.resolve()
or stryke.error()
, but it is not necessary to pass any specific value to the stryke.resolve()
function.
entity
Allows relating this action to a specific entity.
Depending on the action type and purpose, the entity field can be left blank or set.
An action that does not have the entity field set is executed independently from any record or entity. For example an API action that performs a task that is not specific to a certain entity does not need to have an entity set.
On the other hand, setting the entity field related the action to that particular entity and makes it's execution tied to records of that type. For example an action of type button needs to have the entity lookup set so that a button to execute it can be shown in the app's UI on records belonging to that entity. Similarly, actions of type trigger must have the entity field set so that when records of that entity are created/updated/deleted the trigger action can be executed.
Action Type | Entity Field |
---|---|
Button | Must be set to the entity whose records should show the button to execute the action |
API | Does not need to be related to an entity |
Triggers | Must be set to the entity for which we are implementing the trigger |
Return type
The action return type defines the expected type of the value returned by the action. Returning a value from an action is done via the function stryke.resolve()
. The return value of an action is included in the response of the action execution request. When returning a value from an action of type button, that value will be show in the app's UI.
Return types:
Void
void
An action with return typevoid
is an action that does not return any value.String
string
The action will return a string value. In the app's UI, the string will be shown on the same page where the action (eg: button) was triggered.Number
number
The action will return a numeric value. In the app's UI, the number will be shown on the same page where the action (eg: button) was triggered.Boolean
boolean
The action will return a true/false value. In the app's UI, the boolean value will be shown on the same page where the action (eg: button) was triggered.Object
object
The action will return a JSON object. When returning a JSON object from an action that is shown in the app's UI (ex: from a button), the JSON object will automatically be downloaded as a JSON file. Alternatively, the JSON object can also be opened in a new window from the action's result dialog. An HTML template can be used to make the JSON result more presentable to users. If you want to present an HTML output to users you can use thehtml
return type described below.HTML
html
The action will return an HTML document. When returning an HTML document from an action it is mandatory to set a template on the action. The template represents the HTML document that will be rendered and it can contain data returned by the action's code. The action's source needs to return a valid JSON object with the values that need to be injected in the HTML template via thestryke.resolve()
function. For more details on templates and how they are used, see the Templates docs.Text
text
The action will return a text value. When returning a JSON object from an action that is shown in the app's UI (ex: from a button), the text value will automatically be downloaded as a text file.CSV
csv
The action will return a text value formatted as a CSV document. The action needs to be developed so that the return value is in a comma separated format. When returning a CSV from an action that is shown in the app's UI (ex: from a button), the document will automatically be downloaded as a CSV file.
template
The template field is used to assign a template to the action. This field must be set when the action returns and HTML result. The HTML template contains the HTML of the document that will be returned by the action. This template can be injected with arbitrary data that the action generates.
An action returning an HTML document must return a JSON object via the stryke.resolve()
function.
For more details on templates and how they are used, see the Templates docs.
source code
This is the Javascript code that will be run when the action is executed. The editor under the action page allows viewing and modifying the source code. However, for a more complete experience you can install Stryke's Visual Studio Code extension: Stryke code. Stryke code allows creating and modifying scripts and templates, as well as executing scripts and visualising their logs and return values.
Please check the Scripts section for more details on how to wirte scripts.
Examples of useful actions
Below are some example use cases where actions can be useful. For some source code examples check out the script examples section, your 'Playground App', and our GitHub repo.
Before triggers to validate and manipulate data
Triggers are the best place to modify or validate data as it is saved by users. A record's alias can be programmatically computed based on some of its content. Advanced validations can be done on the content of the record's fields, such as ensuring that a certain numeric value is not incremented by more than a predetermined amount.
Change dependent records in after triggers
After triggers can be used to updated other resources, such as related records, based on the values of a record after it has been saved.
Button actions to generate reports
An action, in combination with a template, is a great way to generate a report to present to users. The report can contain data stored in the app, which can be queried via Stryke's query engine or potentially even augmented by data retrieved from 3rd party resources. The best way to trigger the generation of a report from the app's UI is via a button. This button can be placed on records of a specific entity. Alternatively, a report entity can be created, and the button can be placed on records of the report entity. Following this approach will also allow using the fields of the report record to specify any input parameters for the report generation, for example start and end dates for a period to which the report will apply.
Perform a custom task for your own client
An API action exposes an endpoint, through Stryke, for a specific app that can be called by any client that is authenticated. This is a great way to implement ad-hoc functionality that you need to serve to your own custom client that is using Stryke as a backend.
Retrieving data from 3rd parties
Actions can perform HTTP requests. This allows your buttons, triggers, api actions to retrieve data from 3rd parties to use to implement functionality of your app or augment your data.