JavaScript Query
With JS Query, users can leverage the power of JavaScript to navigate through different elements on the app, interact with them in real-time, retrieve and manipulate data.
Last updated
With JS Query, users can leverage the power of JavaScript to navigate through different elements on the app, interact with them in real-time, retrieve and manipulate data.
Last updated
There are cases where you want to orchestrate operations, for instance, after triggering two queries, you want to combine and store their results to a temporary state, and then open a modal. This process can be complicated when chaining several event handlers, and certainly cannot be done in one line of code in {{ }}
. That's where JavaScript (JS) query comes into play. It unleashes the ability to interact with components and queries by writing complex JS queries to achieve the following operations:
Interact with UI components
Trigger queries
Access third-party JS libraries
Customize functions
The following example is for you to quickly understand what JS query is and how it works.
SQL query query1
reads id
, first_name
, last_name
and tid
fields from table players
in a PostgreSQL database.
SQL query query2
reads tid
, city
and name
fields from table teams
in a PostgreSQL database.
Use a JS query to left join query1
and query2
on the same tid
in the following steps.
Create query3
, and choose Run JavaScript Code.
Insert the following code.
In this code snippet, the Promise.all()
method receives the results of query1
and query2
, and the join()
method joins their results after a successful run based on the values of tid
field.
Use return
syntax to return result. For example, the following code returns 3
.
The result returned can also be a Promise object. For example, query2.run()
returns a Promise object.
The return
statement is not necessary for scenarios where you want to omit results.
Use JS queries to access data in your app. Notice that there's no need to use {{ }}
notation.
In JS queries, you can use methods exposed by components to interact with UI components in your app. Such operation is not supported by the inline JS code in {{}}
.
The input1.setValue()
method (or other component methods) is asynchronous and returns a Promise object. Accessing input1.value
immediately after setting the value of input1
does not return the updated value.
run()
method and callbacksCall run()
method to run other queries, for example:
The return value of query.run()
is a Promise, so you can attach callbacks to handle the successful result or error.
You can pass parameters in the run()
method to decouple query implementation from its parameters.
For example, in SQL query query1
, you can define name
and status
as parameters that need to be passed in for its execution.
Then you can pass in corresponding parameters to query1
.
Demo
When you have several inputs in an app triggering the same query, passing parameters to this query allows you to reuse it anywhere.
Things might get fuzzy when you want to update SQL implementations, because you have to carefully check and update all duplicated queries. Now you can be relieved of this repeated SQL by introducing query parameters.
Then trigger this query in Run JavaScript of event handlers in each of the inputs.
You can declare functions inside a JS query for better readability.
Appizap supports importing third-party JS libraries and adding predefined JS code, such as adding global methods or variables for reuse either at app-level or workspace-level. You can find the app-level settings in General App Settings > Scripts and style.
For workspace-level, go to ⚙️ Settings > Advanced.
In JavaScript tab, you can add preloaded JavaScript code to define global methods and variables and then reuse them in your app. For importing libraries, see Use Third-party Libraries.
For security reasons, several global variables and functions of window are disabled in Appizap. Please report to our GitHub or Discord if you encounter any issues.