Writing SQL Queries
Last updated
Last updated
© 2024 UI Bakery
The simplest way to access your MySQL / PostgreSQL / Google Sheets data in UI Bakery is by using the SQL Query action step.
Connect a MySQL, PostgreSQL, or Google Sheets data source.
You will see all the available actions in the actions dropdown.
Choose a SQL Query action step.
Add your query, click Execute Action and check the Result section.
Add a component. It will be automatically configured to show the data.
Use Cmd + Esc (Mac) or Ctrl + Space (Windows/Linux) to autocomplete tables / columns.
A SQL query in UI Bakery is a simple string, so you can use a variables selector to reference the component's properties and other variables.
Everything that comes in the {{ }}
brackets is a JavaScript expression. This means that you can use variables, functions, and operators in the query body.
For example, here's how you can add '%' to a variable value to make it a wildcard search:
You don't need to cover variables with quotes by default, variables will be sent separately, and quotes will be added automatically. All new data sources send the query and variables separately, so you need to additionally configure the data source to send the query as a pure string.
Default values can also be set in case a variable is not provided or empty:
Tip: check the Payload section to see the query and variables that are sent to the database:
Multiselect array value can be directly used with no need to convert it to a string:
Once the action is complete, you need to either use an Auto trigger setting, so that the action will be triggered any time the referenced UI component values are changed, or assign the action to a component trigger:
Sending dates to the query doesn't require any special formatting:
JS Date object will be automatically converted to SQL date format which is YYYY-MM-DD
.
If your columns store date and time, and you only need to compare dates, wrap the date column with DATE()
function:
Use moment library to additionally convert JS Date to a specific format:
By default, all new data sources send the query and variables separately, so you need additionally configure the data source to send the query as a pure string.
To enable this setting on a data source level:
Go to the Data Sources Edit modal
Uncheck the Convert SQL queries to prepared statements
checkbox
Please note that switching off the prepared statements setting might bring some security risks to your application, such as SQL injections and other security breaches.
After that, the queries will be compiled and sent as a single string, so you can use JavaScript to generate the query:
Now the quotes will NOT be added automatically, so you need to add them manually and make sure the query is valid:
Sometimes you'll need to insert multiple items in a row. You can achieve it with the following small JavaScript snippet.
First, add a Code step that will take your data and generate the values of the INSERT
:
Based on the data, this will result in a similar string:
Now, add a SQL query as a second step with the rest of the INSERT statement. It'll also reference the {{data}}
property (the result of the previous step execution) that has the generated values of the query:
It's how you can generate parts of the query dynamically and implement more features in your apps faster.