User Functions
Actions are written using JavaScript, but user functions are used for JavaScript content that is used repeatedly or that requires preparation for calls such as SQL or REST.
User functions are created using the GUI, and the created functions can be called from actions.
User Function Types
Two types of user functions exist.
Function | Scope |
---|---|
Local Function | |
Global Function |
Tips
JavaScript
JavaScript processing used repeatedly in actions can be registered as functions.
How to create
- Click the
"Add"
button in the left panel of the Action Editor. - Select
"Script"
from the Function Type. - Enter a string of alphanumeric characters starting with any letter + underscore for the function name.
- Click the
"Save"
button. - Select the function type.
- Displays the JavaScript function editor.
- Write the function process in the JavaScript editor.
Tips
Arguments
Arguments will be the only param object.
Name | Type | Add Arguments |
---|---|---|
param | object | not allowed |
To get a value from arguments param in a JavaScript function, write the following in the function editor
On the other hand, when calling a function, call it as follows
Tips
Sample
Here is a sample of using a JavaScript function that adds two numbers in an action.
Step 1
Prepare three numeric fields and buttons that will be Arguments in the UI.
Component | Component ID | Component Label |
---|---|---|
Number Field | number_field_1 | Number Field 1 |
Number Field | number_field_2 | Number Field 2 |
Number Field | number_ans | Result Field |
Button | button_calc | Calculate button |
Step 2
Open the button_calc action editor and Create a JavaScript function named my_func.
[Contents of my_func]
Step 3
Write a call to my_func on the action board and display the result in the calculated result field.
[Action Board Contents]
Step 4
Save the action and deploy the application.
Step 5
From the application, enter numeric values in Numeric Field 1, Numeric Field 2, and click the Calculate button. Verify that the addition result is displayed in the Calculation Result field.
SQL
Issue SQL to the database and retrieve the results. The SQL editor contains only SQL statements.
Tips
How to create
- Click the
"Add"
button in the left panel of the Action Editor. - Select
"SQL"
from the function types. - Enter a string of alphanumeric characters starting with any letter + underscore for the function name.
- Click the
"Save"
button. - Displays the SQL function editor.
- Write SQL in the SQL editor.
Arguments
Arguments will be the only param object.
Name | Type | Add Arguments |
---|---|---|
param | Object | not allowed |
Tips
Asynchronous functions and await operators
SQL functions are processed asynchronously. To wait until the value is returned, use the await operator during SQL function execution as follows.
await SQL function
The return value of the SQL function will be an object containing the following properties
Property | Type | Description |
---|---|---|
data | object array | Array of ROW data in SQL results, or results of executing a DML statement |
sqlState | string | SQLSTATE value |
errorCode | string | Error code |
errorMessage | string | Error message |
Tips
About Transaction Control
Transactions are action units.
If there are multiple SQL function calls in an action, all will roll back if any one exception occurs. Commit only when all SQL function calls succeed. Commit and rollback are implicit. All rollbacks occur even if throw new Error(); is called by the user description in the action. However, if an exception is caught with a try-catch statement, the calling action will not supplement the exception unless throw is performed in the catch clause, so no rollback will be performed.
Sample
The following is an example of issuing a SQL SELECT to a table in an action and displaying the results as a JSON string in a textarea component.
Step 1
Prepare a textarea component that displays the results in the UI and a button that executes the action.
Component | Component ID | Component Label |
---|---|---|
textarea | text_area_1 | Result JSON |
button | button_sql | SELECT |
Step 2
Create a table named t1 in a database named mydb with the following configuration.
Column name | Data type | M | D | fsp | PK | NN | UQ | AI |
---|---|---|---|---|---|---|---|---|
id | INT | True | True | |||||
name | VARCHAR | 20 |
Store the data for testing from the Data tab of the column.
id | name |
---|---|
1 | a |
2 | b |
Step 3
Open the button_sqlaction editor and create an SQL function named sql1.
[Contents of sql1]
Step 4
Write SQL function calls on the action board to display the results of data acquisition in a text area.
Tips
[Action Board Contents]
Step 5
Save the action and deploy the application.
Step 6
Click the SELECT button from the application. Verify that the SQL SELECT results are displayed in JSON format in the Results JSON text area.
REST
REST functions are functions for calling the REST API or Amazon API Gateway.
How to create
- Click the
"Add"
button in the left panel of the Action Editor. - Select
"REST"
from the function type. - Enter a string of alphanumeric characters starting with any letter + underscore for the function name.
- Click the
"Save"
button. - Displays the REST function editor.
- Configure the REST editor to call the REST API.
Basic Settings
Parameters | Description |
---|---|
Function name | The name set when the function was created is displayed. |
Arguments | Argument is the only param object, as are other user functions. |
Method | Depending on the API to be called, choose from the following
|
URL | Fill in the REST API URL. You can embed parameter values at any point using the following format. ${param.property_name} |
Request headers | Set the request header. Click the "Add" button to add a header name and value entry. Typical header names are available as options, but they can also be entered arbitrarily. |
Request body | For the POST/PUT/PATCH/DELETE method, request body can be specified. The input format changes depending on the Conent-Type specified in the header. |
Binary acquisition | The return value of the response is a string, but for services that return binaries, check this box to obtain the return value as a Buffer object. |
Request headers
Request headers are set from the [Headers]
tab. The request header is provided as an option, but can also be entered directly.
Header name | Value |
---|---|
Accept | |
Accept-Charset | |
Accept-Encoding | |
Accept-Language | |
Authorization | |
Cache-Control | |
Connection | |
Content-Length | |
Content-Type | |
Content-Length | |
Cookie | |
Date | |
Expect | |
From | |
Host | |
Proxy-Authorization | |
Referer | https://example.com/ |
User-Agent | |
X-Amz-Content-Sha256 | |
X-Amz-Date | |
X-Amz-Security-Token | |
X-API-Key |
Tips
Request body
The request body is set from the [Body]
tab. The input format changes depending on the Content-Type specification in the request header.
Content-Type of the request header | Input Format | Input details |
---|---|---|
application/json | textarea | Text |
application/octet-stream | text field | File path |
application/x-www-form-urlencoded | "Add" button Text field (name) Text field (value) | Parameter name Parameter value |
application/xml | textarea | text |
multipart/form-data | "Add Text Part" button "Add File Part" button Text field (name) Text field (value) | Parameter name Parameter value |
text/css | textarea | text |
text/html | textarea | text |
text/plain | textarea | text |
Asynchronous functions and await operators
REST functions are processed asynchronously. To wait for the value to return, use the await operator during REST function execution as follows
Return value
The return value of the REST function will be an object containing the following properties.
Property | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
status | object | HTTP Status Object
| ||||||
headers | object | Response header | ||||||
body | string | Buffer | Response | ||||||
errorMessage | string | Eror message |
Sample
The following is an example of calling a GET type REST API (one parameter) in an action and displaying the result as a JSON string in a textarea component.
Tips
Parameter | JSON string |
---|---|
URL | https://bl2dqouo8g.execute-api.ap-northeast-1.amazonaws.com/dev/res1?name=string |
API key | bcVrf52Wvs3jAKMFYOK596hlLsTtmczH5tTCjQxI |
Step 1
Prepare text fields, text areas, and buttons in the UI.
Component | Component ID | Component Label |
---|---|---|
Text field | text_field_name | Name |
textarea | text_area_response | Response |
Button | button_rest | REST API |
Step 2
Open the button_rest action editor and Create a REST function named my_rest.
[Basic settings for my_rest]
Function name | my_rest |
---|---|
Arguments | param |
Method | GET |
URL | https://bl2dqouo8g.execute-api.ap-northeast-1.amazonaws.com/dev/res1?name=${param.name} |
Request header | Name- X-API-Key Value-bcVrf52Wvs3jAKMFYOK596hlLsTtmczH5tTCjQxI |
Binary acquisition | Unchecked |
Step 3
Write a call to the REST function on the action board and display the response in a text area.
Tips
[Action Board Contents]
Step 4
Save the action and deploy the application.
Step 5
From the application, enter any string in the “Name” text field and click the "REST API"
button. Confirm that the "Hello input string"
appears in the [Response]
text area.
Sample of calling asynchronous functions with synchronous JavaScript functions
When calling asynchronous functions (SQL functions, REST functions, built-in functions such as $fn.getFile, etc.) in JavaScript functions, they must be returned as Promise objects as shown in the sample.
In the following executeScript function, examples of executing REST functions, SQL functions, JavaScript functions, and built-in functions and returning the results are described.
The action board contains an example of setting the result returned from the executeScript function to the value of each table component.