Skip to main content

Resource query tutorial

A hands-on introduction to querying resources.

This tutorial explains how to create resource queries that interact with connected resources. There are two types of resource query:

  • API: Queries that use API requests to interact with API-based resources.
  • SQL: Queries that use SQL statements to interact with SQL-based resources such as databases and similar data stores.

Introduction

Queries for databases and similar types of structured data stores use SQL queries, such as PostgreSQL or MySQL databases.

There are two modes from which to select when writing an SQL query. The mode you choose depends on whether you need to read or write data:

  • SQL: Read data using an SQL statement.
  • GUI: Write data using a graphical query editor.

Prerequisites

This tutorial uses sample customer data and Retool Database, a built-in PostgreSQL database. To prepare Retool Database:

  1. Download sample_customers.csv.
  2. Navigate to Resources and select Retool Database.
  3. Click Import CSV and select Into a new table.
  4. Upload sample_customers.csv and then click Create table.

Once complete, navigate to Apps and then click Create new > App.

1. Add a resource query to read data

Click to open the Code tab, then click + to add a new query. Select Retool Database.

2. Write the query code

Write an SQL statement to retrieve data from the database. The following example retrieves all data from the newly created sample_customers table.

SELECT 
*
FROM
sample_customers

Click Save & Run to save the resource query. The results appear in the Output section below.

3. Display results in a table

With the query results available, you can now display them using a component. Right-click on the canvas, click Add component, and select the Table component.

You can reference the query output elsewhere in the app using the query's data property. This contains an object with key names that correspond to the column names. Each key contains an array of values.

Tables automatically reference the most recently created query or you can change this in the Data setting of the Inspector.

4. Edit table data

Tables are read-only by default but can be configured to allow editing. To do this, you specify which columns should be editable.

Position the cursor over the First table column, click •••, then select Make editable. Repeat this process for the Last table column. Once done, you can click to edit cells in these columns.

Although the table data is editable, there is no query to save the edits back to the database. When you make a table editable, Retool adds a Save action to the table. You configure this with an event handler that triggers a query when changes are saved.

5. Add a resource query to write data

Navigate back to the Code tab and click + to add another Resource query for Retool Database. As this query will write back changes, select GUI mode.

GUI mode provides you with a set of inputs to configure a writable query. This is a safer alternative to using raw SQL statements. Update the query with the following options.

OptionValueDescription
Tablesample_customersThe database table to update.
Action typeBulk update via primary keyUpdate multiple records that correspond to the primary keys.
Primary key columnidThe primary key column to identify records.
Array of records to updatetable1.changesetArrayThe edited records in the table.

Each table row corresponds to a database table record. When run, this query updates each record and uses the id to match edited values with the correct table record.

6. Configure the save action

The final step is to configure the table's Save action. Select the table to display its settings in the Inspector, then select the Save action.

Event handlers trigger queries and perform actions in response to events or user interactions, such as clicking the Save button. Add an event handler and select the query you just created.

7. Refresh data on save

Now that the save action is set up, you can edit table data and then save changes. However, the table does not immediately reflect the changes. This is because the query that retrieves data from the database needs to be run again.

Queries also support event handlers and can perform actions on success or failure. Add an event handler to the write query that triggers the read query on success. Now, whenever changes are successfully saved, the read query runs again and the table reflects the latest changes.