Flower Docs
  • OpenFlower overview
    • The "Hello World" tutorial
  • Videos
    • OpenFlower in 100 seconds
  • 🆕Setup and run
    • Cloud & Private Cloud
    • Self-hosting
      • Access local database or API
      • Google Cloud Platform
      • Heroku
      • Migration from Openblocks
      • Update MongoDB Versions
      • OpenFlower Version Update
      • Traefik loadbalancer
      • SMTP Server
    • Security
  • 🏨Workspaces & Teamwork
    • Workspaces
    • Members and Groups
    • Permissions for Resources
    • OAuth
      • KeyCloak
      • Google
      • GitHub
      • Generic OAuth Provider
    • Query library
    • OpenFlower Marketplace
  • ✨Build Applications
    • Create a new App
      • Modules
      • Version and Release Management
    • App Editor
      • Visual Components
        • Common Component Settings
        • File upload
        • Charts and graphs
        • Image
        • Option lists
        • List View
        • Drawer
        • Google Maps
        • Table
        • Messages / Toast
        • Calendar
      • Date handling
      • Bulk Editing
      • Layers
      • Data selection & Javascript
      • Use Markdown
      • Keyboard shortcuts
    • App Navigation
    • App Interaction
      • Event handlers
    • Themes & Styling
      • Design an efficient and user-friendly form
      • Customize Styles
      • Component Styling Possibilities
  • 🚀Connect your Data
    • Data source basics
    • Data sources in OpenFlower
      • APIs as Datasource
        • REST API
        • GraphQL
        • Google Sheets
      • SQL Databases
        • MySQL
        • MariaDB
        • PostgreSQL
        • Microsoft SQL Server
        • Oracle
      • NoSQL Databases
        • MongoDB
        • CouchDB
        • DynamoDB
      • InMemory Databases
        • Redis
      • File Storages
        • S3 File Storage
      • BigData & OLAP
        • Big Query
        • Snowflake
        • ClickHouse
        • Elasticsearch
      • Websocket Datasource
    • Query basics
      • Bind Query Data to Components
      • Query library
  • 🪄Workflows
    • n8n Integration
  • 💫Business Logic in Apps
    • Write JavaScript
      • JavaScript query
      • Temporary state
      • Transformers
      • Data responder
      • Built-in JS functions
  • 🙌Publish Apps
    • Share an App
    • Publish an App
    • Embedd an App
      • Embed Apps in React
      • Native embed SDK
        • Build the SDK from Source
  • 🔥OpenFlower Extension
    • Opensource Contribution
      • Develop UI components for Apps
      • Develop Data Source Plugins
    • Use third-party libraries in Apps
      • Day.js Date handling
      • Import your own JavaScript Library
    • Custom component
    • OpenFlower Open REST API
Powered by GitBook
On this page
  • Quickstart
  • Use cases
  • Transform timestamp
  • Sort query data
  • Join two queries
  • Read-only operations
  1. Business Logic in Apps
  2. Write JavaScript

Transformers

Transformers are designed for data transformation and reuse of your multi-line JavaScript code. Data from queries or components might not meet your needs in business scenarios. Also, you may use the same code block several times within an app. In such cases, a transformer is what you need.

Compared with inline code in {{ }}, transformer supports multi-line code blocks. And unlike JavaScript query, transformer is designed to do read-only operations, which means that you cannot trigger a query or update a temporary state inside a transformer.

Quickstart

Click + New > Transfromer in a query editor to create a transformer.

Then write your JS code in the transformer. You can click Preview to get the return value and access it by transformerName.value in your app.

In the following example, transformer1 uses the data of star rating in rating1 to calculate a score.

{{ }} is disallowed inside a transformer or JS query. {{ }} is only used for the purpose of single-line JS expression, whereas a transformer or JS query is for multiple lines of JS code.

Use cases

Transform timestamp

Use the moment().format() method to transform timestamp formats. The following example converts the timestamp value of start_time returned by query1 to YYYY-MM-DD format.

return query1.data.map(it => {
     return {
         ...it,
         start_time: moment(it.start_time).format('YYYY-MM-DD')
     };
})

Sort query data

return _.orderBy(query1.data, 'amount', 'asc')

Join two queries

The example code below shows how to join query results of getUsers and getOrders on user id.

const users = getUsers.data;
const userOrders = getOrders.data;
return users.map(user => ({
  ...user,
  orders : userOrders.find(order => order.customer_id === user.id),
}));

Read-only operations

Only read-only operations are allowed inside a transformer. It means that you cannot set values of components or temporary states, or trigger queries. For those operations, use JavaScript queries instead.

For example, you cannot call the method setText() of a text component in a transformer.

Instead, calling the method setText() in a JavaScript query reports no error.

In another example, transformersort1 aims at sorting the data of getUsers by first_name, but the sort() method may change the original data, so an error occurs.

PreviousTemporary stateNextData responder

Last updated 7 months ago

Use the _.orderBy() method provided by to sort data. The following example returns query1.data sorted by amount column in ascending order.

In this case, use the method _.orderBy() provided by instead.

💫
lodash
lodash