🔎JavaScript Query
In situations where you need to coordinate operations, such as triggering multiple queries, merging and storing their results to a dynamic variable, and then opening a modal, the process can become intricate when chaining several event handlers. This complexity cannot be addressed with a single line of code within {{ }}
. This is where JavaScript (JS) queries become essential. They enable you to interact with components and queries by crafting complex JS queries to execute these operations.
Combine query outcomes
The SQL query firstQuery
retrieves the id
, first_name
, last_name
, and id
columns from the users
table in a PostgreSQL database.
select id, first_name, last_name, id from users
The SQL query secondQuery
fetches the id
, city
, and name
columns from the teams
table in a PostgreSQL database.
select id, city, name from teams
Make a new JavaScript query -
thirdQuery
Insert the following code.
This code snippet utilizes the Promise.all()
method to gather the results of firstQuery
and secondQuery
. Subsequently, the join()
method merges their outcomes upon successful execution, utilizing the values of the id
field.
Return data
Use the return
syntax to return a result. For instance, the following code returns 2
.
return Math.floor(2.4)
The returned result can also be a Promise object. For instance, getData.run()
returns a Promise object.
return getData.run()
Get data
To retrieve data within your app, use JavaScript queries rather than{{}}
var myData = [myInput.value, myQuery.data, uploadedFile.files[0].name];
Control component
In JS queries, you can utilize methods provided by components to interact with UI components in your app. This functionality is not supported by the inline JS code enclosed within {{}}
.
The myApp.setValue()
method (or other component methods) is asynchronous and returns a Promise object. Accessing myApp.value immediately after setting the value of myApp does not return the updated value.
Run query
Call run()
method to run other queries, for example:
return getEmployees.run();
The return value of getEmployees.run()
is a Promise, so you can attach callbacks to handle the successful result or error.
Pass in parameters
You can pass parameters in the run()
method to separate the query implementation from its parameters.
For example, in SQL query getCompany
, you can define companyName and nrOfEmployees as parameters that need to be passed in for its execution.
Then you can pass in corresponding parameters to getCompany
.
Declare a function
You can define functions within a JS query to enhance readability.
Add preloaded scripts
QuickDEV allows you to import independent JS libraries and incorporate existing JS code, such as providing globally accessible methods or variables for reuse, at either the application or workspace level. Access app-level options under ⚙️ > Other > Scripts and Style.
For workspace-level, go to ⚙️ Settings > Advanced.
Within the JavaScript tab, you have the option to include preloaded JavaScript code to define global methods and variables, which can then be reused throughout your app.
Restrictions
For security reasons, several global variables and functions of window are disabled in QuickDEV.
Last updated