Transformers
Shape query results before they reach your app
Use a transformer when a query returns data in a shape that needs to be simplified or reshaped before it reaches your app.
Transformers are written in JavaScript.
What transformers receive
Transformers can access:
data- the query resultsparams- the bindings passed to the query
Create a transformer
- Create a datasource and query
- Open the transformer editor for the query
- Write JavaScript to reshape the response
- Send the query to test the output
- Save the query
Return shape
A transformer must return an array.
If you return a single object, Budibase wraps it as one row.
If you return an array of values, Budibase treats each value as a row.
Example
Use a transformer to count breweries by state.
const counts = {}
for (const brewery of data) {
const state = brewery.state
counts[state] = (counts[state] || 0) + 1
}
return Object.entries(counts).map(([state, count]) => ({
state,
count,
}))You can also add derived fields, such as a flag URL, before returning the final rows.
Common uses
- Flatten nested API responses
- Count or group records
- Add derived fields
- Convert API data into a table-friendly shape
Notes
- Keep the query working first, then refine the transformer
- Use
paramsfor binding values passed into the query - Re-test the query after changing the transformer
Updated 4 months ago
Did this page help you?