Building and Executing Queries
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The query system in Budibase is a core feature that enables applications to interact with a wide variety of data sources. It provides a unified mechanism for defining, building, and executing queries against datasources like PostgreSQL, MySQL, REST APIs, and more. This allows developers to retrieve, create, update, and delete data from both internal Budibase DB tables and external systems.
The system is composed of three main parts: a frontend UI in the Budibase builder for creating and testing queries, a backend API for managing and executing them, and a worker thread system for processing queries asynchronously. This architecture ensures that long-running queries do not block the main server thread, providing a robust and scalable solution for data integration.
Query Data Structure
A query is defined by the Query document schema. This structure contains all the necessary information to execute a request against a datasource, including its configuration, parameters, and any post-processing steps.
Sources: packages/types/src/documents/workspace/query.ts, packages/server/src/definitions/openapi.ts:601-638
The following diagram illustrates the main components of a Query document.
Query Schema Fields
| Field | Type | Description |
|---|---|---|
_id | string | The unique identifier for the query. |
name | string | The user-defined name of the query. |
datasourceId | string | The ID of the datasource this query belongs to. |
parameters | string[] | An array of binding names required to execute the query. |
fields | object | A key-value map of configuration specific to the datasource type (e.g., SQL statement, API endpoint). |
queryVerb | string | The verb describing the query's action: create, read, update, or delete. |
schema | object | The expected schema of the data returned when the query is executed. |
transformer | string | An optional JavaScript function (as a string) to transform the data after execution. |
readable | boolean | A flag indicating if the query returns data. |
Sources: packages/types/src/documents/workspace/query.ts:18-42, packages/server/src/definitions/openapi.ts:601-638
Frontend: The Query Editor
The QueryEditor.svelte component provides the user interface within the Budibase builder for creating, editing, and testing queries. It allows users to configure all aspects of a query, from its name and datasource-specific fields to its bindings and JavaScript transformer.
Key functionalities include:
- Saving Queries: Persists the query configuration to the backend.
- Testing Queries: Allows users to provide values for bindings and execute the query in a test environment. The results, including the raw response, schema, and transformer output, are displayed in a drawer.
The editor is a central part of the data integration workflow, providing immediate feedback to developers as they build their queries.
This diagram shows the basic interaction flow within the Query Editor.
Backend: API Endpoints
The backend exposes RESTful API endpoints for interacting with queries. These are defined in the server's controller and documented in the OpenAPI specification.
Sources: packages/server/src/api/controllers/query/index.ts, packages/server/src/definitions/openapi.ts
Execute Query
This endpoint executes a saved query by its ID, passing in any required parameters.
- Endpoint:
POST /api/queries/{queryId} - Description: Executes a query that has been created within a Budibase app.
- Headers: Requires
x-budibase-app-id.
Sources: packages/server/src/definitions/openapi.ts:1003-1025, packages/server/src/api/controllers/query/index.ts:31-45
Request Body (executeQuery)
| Field | Type | Description |
|---|---|---|
parameters | object | A key-value map of bindings required for the query. |
pagination | object | For supported query types (like REST), this can contain page and limit for pagination. |
Sources: packages/server/src/definitions/openapi.ts:572-585
Response Body (executeQueryOutput)
| Field | Type | Description |
|---|---|---|
data | array | The data response from the query, after any transformations. |
extra | object | Additional information, such as headers from a REST response or the raw string response. |
pagination | object | Contains bookmark/anchor information for pagination if supported. |
Sources: packages/server/src/definitions/openapi.ts:586-600
Search Queries
This endpoint allows searching for queries by name.
- Endpoint:
POST /api/queries/search - Description: Searches for queries based on their name.
- Headers: Requires
x-budibase-app-id.
Sources: packages/server/src/definitions/openapi.ts:1027-1045, packages/server/src/api/controllers/query/index.ts:47-56
Query Execution Flow
Query execution is a multi-step process designed for robustness, involving the API server and a separate worker thread. This prevents long-running database queries from impacting the server's responsiveness.
Sources: packages/server/src/api/controllers/query/index.ts:31-45, packages/server/src/threads/query.ts:25-63
The following sequence diagram illustrates the end-to-end flow of executing a query.
Execution Steps
- API Request: A client sends a
POSTrequest to the/api/queries/{queryId}endpoint with necessary parameters. - Controller Handling: The query controller (
packages/server/src/api/controllers/query/index.ts) receives the request. - Thread Offloading: Instead of executing the query directly, the controller passes the query context and parameters to a dedicated worker thread using
threads.execute(). - Worker Execution: The worker thread (
packages/server/src/threads/query.ts) looks up the query and its associated datasource, retrieves the correct integration (e.g., Postgres, REST), and executes the query against the external datasource. - Transformation: If the query has a
transformerscript, the worker thread executes this JavaScript function on the returned data. - Response: The worker thread returns the final data payload to the main API server process.
- Final Response: The API controller sends the processed data back to the client as a JSON response.
Sources: packages/server/src/api/controllers/query/index.ts:31-45, packages/server/src/threads/query.ts:25-63
Conclusion
The Budibase query system provides a powerful and flexible way to integrate external data into applications. Its architecture, which separates the user interface, API layer, and execution logic, is a key strength. The use of worker threads for query execution ensures that the platform remains stable and responsive, even when dealing with slow or complex data requests. The combination of a user-friendly QueryEditor, a well-defined API, and a robust backend makes data integration a seamless part of the app-building process.