Skip to content

Commit 1fee645

Browse files
feat: Reactive Queries/JS (#2906)
## Description Provide a concise summary of the changes made in this pull request - ## Pull request type Check the appropriate box: - [ ] Review Fixes - [ ] Documentation Overhaul - [ ] Feature/Story - Link one or more Engineering Tickets * - [ ] A-Force - [ ] Error in documentation - [ ] Maintenance ## Documentation tickets Link to one or more documentation tickets: - ## Checklist From the below options, select the ones that are applicable: - [ ] Checked for Grammarly suggestions. - [ ] Adhered to the writing checklist. - [ ] Adhered to the media checklist. - [ ] Verified and updated cross-references or added redirect rules. - [ ] Tested the redirect rules on deploy preview. - [ ] Validated the modifications made to the content on the deploy preview. - [ ] Validated the CSS modifications on different screen sizes. --------- Co-authored-by: Luis Ibarra <[email protected]> Co-authored-by: Luis Ibarra <[email protected]>
1 parent eee81df commit 1fee645

File tree

6 files changed

+297
-17
lines changed

6 files changed

+297
-17
lines changed

.vale.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ mdx = md
2121

2222
[*.md]
2323
BasedOnStyles = Google
24+
# TokenIgnores specifies inline tokens/patterns to ignore during linting.
25+
# This is useful for ignoring JSX/React style attributes that contain JavaScript.
26+
TokenIgnores = (\w+\s*=\s*\{\{[^}]*\}\})|(\{\{[^}]*\}\})
2427

Lines changed: 186 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,217 @@
11
# Query Settings
22

3-
This page is a reference guide that provides a description of all the settings available for configuring your queries.
3+
This page is a reference guide that provides a description of all the settings available for configuring your queries. You can find the following settings by clicking the **⚙️ gear icon** in the top-right corner of the query editor for an API or database query.
4+
5+
### Run behavior
6+
7+
The Run behavior property determines when your query executes.
8+
9+
10+
<img
11+
src="/img/query-settings.png"
12+
alt="Query Settings"
13+
style={{ maxHeight: "600px", width: "auto", display: "block", margin: "0 auto" }}
14+
/>
15+
<p style={{ textAlign: "center", fontSize: "0.9rem", color: "#666" }}>
16+
Query Settings
17+
</p>
18+
19+
20+
21+
#### Manual
22+
23+
<dd>
24+
25+
Queries execute only when explicitly triggered. They do not run automatically on page load or when any variables change.
26+
27+
You can trigger the query using:
28+
29+
- Widget events (for example **onClick**, **onOptionChange**).
30+
- JavaScript function calls using `.run()`.
31+
- The **Run** button in the query editor.
32+
33+
This mode provides full control over when and how the query is executed.
34+
35+
*Example:* If you want to execute a query when a button is clicked:
36+
37+
```js
38+
{{getUsers.run()}}
39+
```
40+
41+
42+
</dd>
43+
44+
#### On page load
45+
46+
<dd>
47+
48+
Queries with On page load behavior execute automatically once whenever the application or page is loaded. This is useful for fetching data needed immediately when the app starts, without requiring any user interaction.
49+
50+
The query runs each time:
51+
52+
- The page is loaded or reloaded
53+
- The app is opened or refreshed
54+
55+
You do not need to configure any widget actions or write custom JavaScript to trigger the query.
56+
57+
58+
*Example:* If you have a query that fetches user data and you want to display it whenever the app is opened, you can set the run behavior to **On page load**.
59+
60+
</dd>
61+
62+
63+
#### Automatic
64+
65+
<dd>
66+
67+
Queries with Automatic behavior execute whenever a variable they depend on changes. This includes values from:
68+
69+
- Widget properties (for example `{{Input1.text}}`, `{{Select1.selectedOptionValue}}`).
70+
- JavaScript object variables or function outputs (for example `{{JSObject1.value}}`, `{{JSObject1.function.data}}`).
71+
72+
When any of these values change, the query re-executes automatically. You do not need to use event handlers like **onTextChanged** or **onOptionChanged**.
73+
74+
This mode is useful for building dynamic, responsive apps where data needs to stay updated based on user input or state changes.
75+
76+
*Example:* If you have a query that filters a customer list based on user input, and you want the results to update automatically as the user types, you can bind the input value directly and set the run behavior to Automatic:
77+
78+
```js
79+
SELECT * FROM customers WHERE name ILIKE '%{{SearchInput.text}}%';
80+
```
81+
82+
Each time the value of` SearchInput.text` changes, the query runs automatically and updates the data shown in the UI.
83+
84+
:::note
85+
If a query is configured with Automatic run behavior and is also manually triggered through a widget event (such as **onTextChanged**), the query will be executed twice—once due to the reactive binding, and once from the event handler.
86+
To avoid unintended duplicate executions, it is recommended to use either the automatic behavior or an explicit trigger, but not both.
87+
:::
88+
89+
:::info
90+
Changes to values in the [Appsmith global object](/write-code/reference) do not trigger automatic re-execution of queries or JavaScript actions. For example, updates to `appsmith.store` will not cause a query to re-run unless combined with a reactive property or explicitly triggered.
91+
:::
92+
93+
</dd>
494

5-
You can find the following settings in the **Settings** tab of the query editor for an API or database query:
695

796
#### Encode query params
897

9-
<dd>Toggles whether Appsmith converts parameters' special characters into their <a href="https://en.wikipedia.org/wiki/URL_encoding">URL-encoded</a> equivalents. It also encodes the form body when the <b>Content-Type</b> header is set to <code>FORM_URLENCODED</code>. This setting is available for API queries.</dd>
98+
<dd>
99+
100+
101+
When enabled, Appsmith automatically encodes special characters in query parameters to ensure they are transmitted correctly over the network.
102+
103+
- All API query parameters are URL-encoded based on [RFC 3986](https://en.wikipedia.org/wiki/Percent-encoding).
104+
- If the Content-Type header is set to `application/x-www-form-urlencoded`, the request body is also encoded.
105+
106+
This setting is useful for APIs that expect properly encoded input, especially when sending form data or special characters like spaces, ampersands, or slashes. This setting is only available for API queries.
107+
108+
</dd>
10109

11110
#### Use Prepared Statements
12111

13-
<dd>Toggles whether Appsmith uses pre-compiled and parameterized SQL statements to construct and execute database queries. This method improves the security of your SQL queries. This setting is turned on by default, and available for SQL database queries. For more details, see <a href="/connect-data/concepts/how-to-use-prepared-statements">Prepared Statements</a>.</dd>
112+
<dd>
113+
114+
When enabled, Appsmith constructs and executes SQL queries using pre-compiled and parameterized statements. This improves both performance and security by separating query logic from user input.
115+
116+
- Helps protect against SQL injection attacks by preventing direct injection of unescaped input.
117+
- Improves query execution efficiency in certain database engines by allowing query plan reuse.
118+
119+
This setting is enabled by default and is only available for SQL database queries. For more information, see [Prepared Statements](/connect-data/concepts/how-to-use-prepared-statements).
120+
121+
122+
123+
</dd>
14124

15125
#### Query timeout
16126

17127
<dd>
18128

19-
Sets the time duration in milliseconds that the Appsmith server waits for the query to finish before it closes the connection. If your query takes longer than this duration, Appsmith throws a [timeout-error](/help-and-support/troubleshooting-guide/action-errors#timeout-error). This setting defaults to 10000 ms with a maximum of 60000 ms.
20129

21-
The Appsmith server has a default internal timeout of 60 seconds. If your queries take longer than this, you can set a value greater than 60 seconds. For self-hosted instances, you can set the `APPSMITH_SERVER_TIMEOUT` environment variable to a value greater than 60 seconds. For example, if you want a timeout of 80 seconds, use- `APPSMITH_SERVER_TIMEOUT=80`.
130+
Defines the maximum time (in milliseconds) that Appsmith waits for a query to complete before terminating the request.
131+
132+
- If the query does not respond within the specified timeout, Appsmith throws a [timeout error](/help-and-support/troubleshooting-guide/action-errors#timeout-error).
133+
134+
- The default timeout is `10000` milliseconds (10 seconds), with a maximum configurable limit of `60000` milliseconds (60 seconds).
135+
136+
For self-hosted instances, you can increase the server-level timeout beyond `60` seconds by setting the `APPSMITH_SERVER_TIMEOUT` environment variable. For example:
137+
138+
```js
139+
APPSMITH_SERVER_TIMEOUT=80
140+
```
141+
142+
This setting is available for both API and database queries.
143+
144+
145+
</dd>
146+
147+
#### Request confirmation before running query/API
148+
149+
<dd>
150+
151+
When enabled, Appsmith prompts the user for confirmation each time the API / query is about to run. This is useful for critical operations such as deleting records, sending emails, or triggering external workflows, where unintentional execution may have significant consequences.
152+
22153

23154
</dd>
24155

25-
#### Request confirmation before running query
26156

27-
<dd>When turned on, Appsmith asks the user for permission to run the query before each execution.</dd>
157+
#### Protocol
28158

29-
#### Request confirmation before running API
159+
<dd>
160+
161+
162+
Specifies the HTTP protocol version to use when making an API request. Choosing the correct version ensures compatibility with the target server and may improve performance depending on the API's infrastructure.
163+
164+
*Available options:*
165+
166+
**HTTP/1.1 (Default)** The most widely supported version of HTTP.
167+
168+
- Uses a new connection for each request-response pair.
169+
- Supported by nearly all web servers and APIs.
170+
- Ideal for general-purpose APIs with no specific protocol requirements.
30171

31-
<dd>When turned on, Appsmith asks the user for permission to run the query before each execution.</dd>
32172

33-
#### Run API on page load
173+
**HTTP/2 (h2):** A modern version of HTTP that enables multiplexing multiple requests over a single connection.
34174

35-
<dd>When turned on, your query is executed every time the page loads or refreshes. This is automatically turned on when you bind the query's data to be displayed in a widget, though you can choose to turn it off</dd>
175+
- Reduces latency by allowing multiple streams on one connection.
176+
- Improves performance for APIs that support it.
177+
- Requires server support for HTTP/2 over TLS (HTTPS).
36178

37-
#### Run query on page load
179+
**HTTP/2 Cleartext (h2c)**: A variant of HTTP/2 that operates over non-encrypted (HTTP) connections.
38180

39-
<dd>When turned on, your query is executed every time the page loads or refreshes. This is automatically turned on when you bind the query's data to be displayed in a widget, though you can choose to turn it off.</dd>
181+
- Less common and generally used in controlled environments or internal networks.
182+
- Requires explicit support from the target server.
183+
- Not recommended for public or production APIs due to lack of encryption.
184+
185+
This setting is only available for API queries.
186+
187+
</dd>
40188

41189
#### Smart JSON substitution
42190

43-
<dd>JavaScript objects and JSON objects are formatted similarly, however they have different rules for where quotation marks are required. When this setting is turned on, Appsmith intelligently adds or removes quotation marks from your JavaScript data as necessary to correctly cast them into JSON. This setting is turned on by default, however it may need to be turned off for some tasks such as sending raw binary data to an API. This setting is available for API queries. For a video guide on using this feature, see <a href="https://www.youtube.com/watch?v=-Z3y-pdNhXc">How to Use Smart JSON Substitution</a>.</dd>
191+
<dd>
192+
193+
When enabled, Appsmith intelligently formats and escapes JavaScript expressions to produce valid JSON output. This helps prevent syntax errors when passing dynamic values in API requests.
194+
195+
- Automatically adds or removes quotation marks as needed to ensure proper JSON structure.
196+
- Allows you to write JavaScript directly in bindings without manually formatting the resulting JSON.
197+
198+
This setting is enabled by default and is useful when sending structured data in API request bodies, headers, or parameters. In advanced cases—such as sending raw binary data or pre-formatted payloads—you may need to disable this setting to avoid unwanted formatting.
199+
200+
This option is only available for API queries. For a walkthrough, see How to Use [Smart JSON Substitution](https://www.youtube.com/watch?v=-Z3y-pdNhXc).
201+
202+
203+
</dd>
44204

45205
#### Smart BSON substitution
46206

47-
<dd>JavaScript objects and Binary JSON (BSON) objects are formatted similarly, however they have different rules for where quotation marks are required. When turned on, the query intelligently adds or removes quotation marks from your JavaScript data as necessary to correctly cast them into BSON. This setting is turned on by default, and available for <a href="/connect-data/reference/querying-mongodb">MongoDB</a> queries.</dd>
207+
<dd>
208+
209+
When enabled, Appsmith automatically formats and escapes JavaScript expressions to produce valid BSON (Binary JSON) output, which is required when interacting with MongoDB.
210+
211+
- Dynamically adds or removes quotation marks as needed to ensure correct BSON structure.
212+
- Allows you to use JavaScript bindings directly in MongoDB queries and commands without manually formatting them.
213+
214+
This setting is enabled by default and is available only for MongoDB queries. It helps avoid common syntax issues when constructing dynamic queries, filters, or update commands in MongoDB.
215+
216+
217+
</dd>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# JavaScript Settings
2+
This page provides information on the settings available for JS Objects and their functions in Appsmith. You can access these settings by clicking the **⚙️ gear icon** in the top-right corner of the JS Object editor and selecting a function from the dropdown list.
3+
4+
### Choose function run behavior
5+
6+
Each JavaScript function supports one of the following Run behavior modes:
7+
8+
<img
9+
src="/img/js-settings-mode.png"
10+
alt="JavaScript Settings Mode"
11+
style={{ maxHeight: "600px", width: "auto", display: "block", margin: "0 auto" }}
12+
/>
13+
<p style={{ textAlign: "center", fontSize: "0.9rem", color: "#666" }}>
14+
JavaScript Settings
15+
</p>
16+
17+
#### Manual
18+
19+
<dd>
20+
21+
22+
The function executes only when explicitly invoked. It will not run on page load or when any variables it references change.
23+
24+
You can trigger the function using:
25+
26+
- Widget events (for example **onClick**).
27+
- JavaScript calls using `JSObjectName.functionName()`.
28+
- The **Run** button in the editor.
29+
30+
This mode provides full control over execution timing and is best suited for user-driven actions.
31+
32+
*Example:* If you want to execute a function only when a user clicks a button, you can set the button's **onClick** event to:
33+
34+
```javascript
35+
{{CustomerActions.createCustomer()}}
36+
```
37+
38+
</dd>
39+
40+
41+
42+
#### On page load
43+
44+
<dd>
45+
46+
Functions configured with On page load run behavior execute automatically once when the page is loaded. This is useful for initializing state, setting default values, or triggering background logic without requiring user interaction.
47+
48+
The function runs each time:
49+
50+
- The page is loaded or reloaded
51+
- The app is opened or refreshed
52+
53+
This mode is ideal for setup tasks that should occur during the initial load of the application.
54+
55+
*Example:* If you want to initialize session data when the app loads, you can define a function like `initializeSession` and set its run behavior to On page load:
56+
57+
```js
58+
initializeSession: () => {
59+
storeValue("sessionStartedAt", moment().format());
60+
}
61+
```
62+
63+
</dd>
64+
65+
66+
#### Automatic
67+
68+
<dd>
69+
70+
Functions configured with Automatic run behavior execute whenever a variable or input they depend on changes. Appsmith automatically tracks all references used inside the function and re-runs the function when any of those values are updated.
71+
72+
This eliminates the need to manually bind the function to widget events such as **onTextChanged** or **onOptionChanged**.
73+
74+
The function runs each time:
75+
76+
- A referenced widget value changes (for example `{{Input1.text}}`, `{{Select1.selectedOptionValue}}`).
77+
- A dependent JavaScript object or function result is updated.
78+
- A function has a default parameter set to a reactive value (like `Input1.text`), and that value changes.
79+
80+
This mode is ideal for reactive logic where the output should always reflect the latest application state or user input.
81+
82+
*Example:* If you want to filter a product list based on a search input and selected category, you can define a function like `filterProducts`:
83+
84+
```javascript
85+
filterProducts: () => {
86+
return ProductQuery.run({
87+
search: SearchInput.text,
88+
category: CategoryDropdown.selectedOptionValue
89+
});
90+
}
91+
```
92+
93+
When `SearchInput.text` or `CategoryDropdown.selectedOptionValue` changes, the `filterProducts` function will run automatically.
94+
95+
:::note
96+
- If a function is set to run automatically and is also invoked manually (for example, through a widget’s event handler), it can execute twice, once due to the dependency change and once from the manual trigger. To prevent this, use either automatic behavior or manual invocation, not both.
97+
98+
- If a JavaScript object function triggers a query, and the query’s result is bound to a widget (such as a Table), both the function and the query become reactive. Any change in their shared dependencies, such as `Input1`.text—can cause both the function and the query to execute automatically.
99+
:::
100+
101+
102+
:::info
103+
Changes to values in the Appsmith global object do not trigger automatic re-execution of queries or JavaScript actions. For example, updates to appsmith.store will not cause a function or query to re-run unless combined with a reactive property or triggered explicitly.
104+
:::
105+
106+
</dd>

website/sidebars.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,8 @@ const sidebars = {
646646
},
647647
'write-code/reference/Built-in-JS-Libraries',
648648
'write-code/reference/Fetch-API',
649-
'write-code/reference/transform-data'
649+
'write-code/reference/transform-data',
650+
'reference/js-settings'
650651
],
651652
}, //Reference End
652653
{
26.4 KB
Loading
144 KB
Loading

0 commit comments

Comments
 (0)