RethinkDB query tutorial
Learn how to read, insert, update, and delete documents in RethinkDB.
You're viewing documentation for classic apps, which use Retool's original app editor. The new app builder is the recommended way to build apps and where Retool focuses new development.
After setting up a RethinkDB integration with Retool, your RethinkDB resource is available to use in the query editor.
Create a query
- Classic app
- Workflow or agent
Retool recommends using the new app builder so you can use natural language to build React-based apps using AI.
To create a RethinkDB query in a Retool app:
- In the Retool app IDE, open the Code tab, then click + in the page or global scope.
- Select Resource query.
- Choose your RethinkDB resource.
- Select a table, a method, and configure the method's parameters.
Workflows and agent custom tools use the same Resource query block to query RethinkDB.
- Add a Resource query block to your workflow or custom tool's function canvas.
- Choose your RethinkDB resource.
- Select a table, a method, and configure the method's parameters.
Custom tools use the same block-based canvas as workflows, so the steps are identical in either context.
Action types
Every RethinkDB query selects a table, then a method to run against it. Retool populates the table dropdown from your RethinkDB database's schema.
- get: Retrieves a single document by its primary key.
- find: Retrieves documents that match a set of conditions, optionally plucking specific attributes.
- insert: Inserts a new document.
- update: Updates an existing document, identified by its primary key.
- delete: Deletes a document by its primary key.
Query configuration fields
Each method exposes a different set of fields. All fields accept embedded expressions ({{ }}).
Document ID
Used by get, update, and delete. The primary key of the document to retrieve, update, or delete.
// Static ID
119de701-6b12-4ae2-9c1b-e10eb3d9e6ff
// Dynamic from table selection
{{ table1.selectedRow.data.id }}
Where clause
Used by find. A JSON object describing the conditions documents must match. Leave blank to return all documents in the table.
{ "firstName": {{ searchInput.value }} }
Attributes to pluck
Used by find. An array specifying which fields to return from each matching document. Leave blank to return full documents.
["id", "firstName", "lastName"]
Value to insert
Used by insert. A JSON object representing the document to create.
{
"firstName": {{ firstNameInput.value }},
"lastName": {{ lastNameInput.value }},
"email": {{ emailInput.value }}
}
Value to update
Used by update. A JSON object containing the fields to merge into the existing document identified by Document ID.
{
"status": {{ statusSelect.value }},
"updatedAt": {{ moment().toISOString() }}
}
Common use cases
The following examples demonstrate typical RethinkDB operations in Retool apps.
find and display documents
First, create a query named findUsersQuery using the find method against the users table, leaving the where clause blank to return every document.
Next, add a Table component to the app and set its Data property to {{ findUsersQuery.data }}.
filter documents with user input
First, create a query named filterUsersQuery using the find method against the users table.
Set the where clause to filter by a value from a Text Input component:
{ "status": {{ statusSelect.value }} }
Then, add an event handler to the input's Change event that runs filterUsersQuery.
insert a new document
First, add a Form component (form1) with input fields for the new document.
Next, create a query named insertUserQuery using the insert method against the users table, with the value to insert set to:
{
"firstName": {{ form1.data.firstName }},
"lastName": {{ form1.data.lastName }},
"email": {{ form1.data.email }}
}
Then, add an event handler to the form's Submit event that runs insertUserQuery and displays a success notification.
update a document
First, add a Form component with fields pre-populated from a selected table row.
Next, create a query named updateUserQuery using the update method against the users table:
| Field | Value |
|---|---|
| Document ID | {{ table1.selectedRow.data.id }} |
| Value to update | See below |
{
"status": {{ statusSelect.value }},
"email": {{ emailInput.value }}
}
Then, add an event handler that runs updateUserQuery when the form is submitted and refreshes findUsersQuery.
delete a document with confirmation
First, add a Button component in your table's action column.
Next, create a query named deleteUserQuery using the delete method against the users table, with Document ID set to {{ table1.selectedRow.data.id }}.
Then, add an event handler to the button's Click event:
- Action: Show confirmation modal.
- If confirmed, trigger
deleteUserQuery. - Then refresh
findUsersQuery.
Best practices
Follow these best practices to keep RethinkDB queries fast, efficient, and safe.
Performance
- Use find over get when scanning: Use the get method when you already know a document's ID; use find with a where clause for everything else, since it lets RethinkDB use available indexes.
- Pluck only the fields you need: Use the attributes-to-pluck field to reduce payload size instead of returning full documents when you only need a few fields.
- Cache responses: For data that doesn't change frequently, enable query caching to reduce round trips and improve response times.
- Set appropriate timeouts: Configure query timeouts based on expected response times to prevent hung requests.
Data integrity
- Validate user input: Sanitize and validate all user input before including it in a where clause, insert value, or update value.
- Handle errors gracefully: Configure error notifications and fallback behavior for failed queries to improve user experience.
- Verify responses: Check that a query's response contains the expected document or documents before using it in your app.
- Implement retry logic: For transient failures, use Retool's automatic retry settings or implement custom retry logic with exponential backoff.
- Log query activity: Enable query logging to track requests and responses for debugging and auditing.