docs/docs/app-builder/walkthrough/create-queries.md
The Query Panel at the bottom of the app-builder enables the creation and management of queries for interacting with connected data sources. This includes performing API requests, querying databases, and applying transformations or data manipulations using JavaScript and Python.
The Query Panel consists of two sections:
Depending on the data source you have selected, you can either choose GUI mode or SQL mode to configure your query.
For GUI mode: You will have to choose the Table name and Operations you want to perform.
For SQL mode Yow will have to enter the SQL query to perform your desired operation.
At the top of the Query Editor, you can click on the query name area to input a descriptive name, like fetchUsers.
If your query requires parameters, add them using the + Add button near the Parameters label.
Example: For a PostgreSQL query named fetchUsers that is fetching data from the allUsers table, you might set a parameter to a single user by passing in the id parameter.
SELECT * FROM allUsers WHERE id = {{parameters.id}}
Here, {{parameters.id}} is a parameter that you can define by clicking on the + Add button on the Query Panel next to the Parameters label.
Let's look at some examples with a PostgreSQL data source with a database table named feature_requests.
SELECT * FROM feature_requests;
{{queries.getAllRequests.data}}
INSERT INTO feature_requests (id, title, description, votes, priority)
VALUES (10, 'Toggle Component', 'We need a toggle component in future release.', 0, 2);
You can insert values from the components in queries. For instance, the above code can be updated to pick values from the components by using the double curly braces to pass the component values:
INSERT INTO feature_requests (id, title, description, votes, priority)
VALUES (10, '{{components.textinput1.value}}', '{{components.textinput2.value}}', 0, 2);
You can apply the same principles to upcoming examples.
To update existing data:
Example: Set up a query updateRequest to modify details of an existing product based on the id of the selected product in the Table component.
UPDATE feature_requests
SET
title = 'Updated Feature Title',
description = 'Updated Feature Description',
votes = 15,
priority = 2
WHERE id = '{{components.table1.selectedRow.id}}';
To delete data:
Example: Craft a query deleteRequest that removes a product from the database based on a parameter.
DELETE FROM feature_requests WHERE votes < {{parameters.minimumVotes}};
Transformations: After fetching data, you might want to format it (e.g., filtering out unnecessary fields or converting data types). ToolJet allows using JavaScript or Python for these transformations.
Event Handling: Link queries with application events for dynamic interactions. For example, in the updateRequest query, you can set up an event to automatically run the getAllRequests query right after updateRequest. This ensures that the application retrieves and displays the updated data in the relevant components.
</div> <div style={{paddingTop:'24px'}}>Preview and Run: Use the Preview button to test queries and view results in raw or JSON format before executing them within the app using the Run button.
Configuration Settings: