# Queries

If you have special handling requirements, you can add further specs for particular Event Handler Queries. Writing to and reading data from your data sources is made simpler using these queries. They can be used with a wide range of data sources, including databases, stream data sources, and conventional RESTful APIs. In the query repository or when making changes to an application, you can create queries.

All Data Queries within QuickDEV always return JSON data. Whether you're accessing a database or another data source, the result will be transformed into a JSON response. This uniformity is highly beneficial as it ensures ease of use for the data across various parts of QuickDEV without the necessity for data serialization between formats.

## Create a query

The query editor's appearance varies depending on the chosen data source type. For instance, consider a scenario where you're connecting to a PostgreSQL database.

<figure><img src="https://2228196961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOetSCIB8nFF7hAJCTarW%2Fuploads%2F6UgXh7rao8Wanjokld0N%2Fquery1.avif?alt=media&#x26;token=d572131b-4b2a-463c-a981-ec4b0615ad8a" alt=""><figcaption></figcaption></figure>

The **SQL Mode** caters to both novices querying the database and proficient developers well-versed in SQL language and their specific database specialties.

<figure><img src="https://2228196961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOetSCIB8nFF7hAJCTarW%2Fuploads%2FXBmajay8zzN2vH4lgn2Z%2Fquery2.avif?alt=media&#x26;token=5e9e5404-38cf-425b-8d57-dba945df8327" alt=""><figcaption></figcaption></figure>

QuickDEV additionally provides a **GUI Mode** for performing write operations on the database, including **INSERT**, **UPDATE**, and **DELETE**. This simplifies the process of binding user-generated data changes in your app back to the databases.

## Metadata

You have the capability to inspect the metadata of databases. &#x20;

<figure><img src="https://2228196961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOetSCIB8nFF7hAJCTarW%2Fuploads%2F5FHocxeh3lieDBFmb0aS%2Fimage%20(23).png?alt=media&#x26;token=27daca92-29a0-41a1-848e-c952a48b4410" alt=""><figcaption></figcaption></figure>

By clicking on "**Metadata**," you'll see the database tables and their respective fields presented in a tree structure.

<figure><img src="https://2228196961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOetSCIB8nFF7hAJCTarW%2Fuploads%2FjKcoXXaUtuVQ62mRt9dm%2Fimage%20(26)%20(1).png?alt=media&#x26;token=452bec15-0bd0-426d-ae2b-5b8f388153cc" alt=""><figcaption></figcaption></figure>

Metadata proves beneficial for query writing, providing auto-suggestions as you type. Additionally, it aids in understanding the structure of the database and its tables for any connected database.

## Dynamic queries

QuickDEV evaluates your query statement using JavaScript code enclosed within `{{ }}` in real-time. Subsequently, the result is presented below in a floating box, enabling you to verify the accuracy of the query statement.

## Triggered when

QuickDEV initiates your queries in two modes: it can either run automatically when "**inputs change or on page load,**" or **manually**, which allows you to trigger it in other event handlers, for example.

<figure><img src="https://2228196961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOetSCIB8nFF7hAJCTarW%2Fuploads%2FSIzuSQnU8X1e6QTDDVG1%2Fimage%20(34).png?alt=media&#x26;token=4f189725-755b-4a34-8142-e763b6d7b089" alt=""><figcaption></figcaption></figure>

### Inputs change or on page load

Queries configured in this mode automatically execute when dependent inputs change or upon page load. For instance, consider a query like:

```sql
select * 
  from customers 
 where customer_id = {{input.value}}
```

Its result will updates immediately whenever the value of input changes. This mode is recommended for queries reading data from data sources.

{% hint style="warning" %}
Be cautious when deciding to trigger a query. Data Queries that might have longer response times could potentially hinder the loading of application visuals and the display of your data.
{% endhint %}

### Manually invoked

In this mode, a query will only run when you explicitly initiate it by click a button or link. This mode is recommended for searches that send data to data sources since it might be required to confirm the accuracy and completeness of the input before running the query.

## Response-event handler

When the parsing and result processing of a Data Query is complete, you may utilize Event Handlers to start new processes. These event handlers are made up of onSuccess and onError triggers. Additionally, you can combine various Event Handlers as desired.

### onSuccess handler

The `onSuccess` handler is intended to execute specific actions following the successful completion of a data query. This functionality is crucial for initiating subsequent operations, such as running JavaScript queries or other queries that may require the response of an initial data query.

To use the `onSuccess` handler, go to the "Event Handlers" section in the lower part of the Data Query Editor within the "General" Tab. Then, opt for "on Success," and proceed to select your desired action.

### onError handler

The `onError` handler acts as the counterpart to the onSuccess handler, addressing instances where a data query encounters failure. This could happen due to various reasons, including network issues, API errors, or data inconsistencies.

<figure><img src="https://2228196961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOetSCIB8nFF7hAJCTarW%2Fuploads%2F4ao1tTvNhpklgImPlvrO%2Fquery4.avif?alt=media&#x26;token=de3fce38-7489-4831-8c3c-e91aee51a873" alt=""><figcaption></figcaption></figure>

To establish an event handling for a data query error case, just choose "Failure" from the Event Selection options for the **Event Handler**. Furthermore, you have the option to specify special case handling for individual Event Handlers.

<figure><img src="https://2228196961-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOetSCIB8nFF7hAJCTarW%2Fuploads%2FDFbiuzWYqF5xG9hH2EjU%2Fquery5.avif?alt=media&#x26;token=961afefa-0d2d-4acf-95a8-fde6999b3c50" alt=""><figcaption></figcaption></figure>

## Notification tab and Advanced tab

You may set up notifications, timeouts, periodic runs, and other features on the notification and advanced tabs. The Notification tab and Advanced settings differ according on your query's trigger mode.

<table data-full-width="false"><thead><tr><th width="135">Tab</th><th width="185">Settings</th><th width="253">Inputs change / On page load</th><th>Manually invoked</th></tr></thead><tbody><tr><td>Notification</td><td>Display a Success message after running</td><td>🚫</td><td>☑️</td></tr><tr><td></td><td>Display a Failure message after running</td><td>☑️</td><td>☑️</td></tr><tr><td>Advanced</td><td>Display a confirmation modal before running</td><td>🚫</td><td>☑️</td></tr><tr><td></td><td>Set timeout for query running</td><td>☑️</td><td>☑️</td></tr><tr><td></td><td>Perform query periodically</td><td>☑️</td><td>🚫</td></tr></tbody></table>
