Skip to content

Commit 6a1701f

Browse files
Openblocks-docsgitbook-bot
Openblocks-docs
authored andcommitted
GitBook: [lowcoder-org#75] add docs: 'Transformers'
1 parent 12600eb commit 6a1701f

20 files changed

+91
-13
lines changed
116 KB
Loading
81.3 KB
Loading
Loading
81.6 KB
Loading
116 KB
Loading
55.1 KB
Loading
87.2 KB
Loading
68.7 KB
Loading
91.7 KB
Loading
Loading

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* [Event handlers](build-apps/event-handlers.md)
2525
* [Write JavaScript](build-apps/write-javascript/README.md)
2626
* [Use third-party libraries](build-apps/use-third-party-libraries.md)
27+
* [Transformers](build-apps/write-javascript/transformers.md)
2728
* [Temporary state](build-apps/write-javascript/temporary-state.md)
2829
* [Component guides](build-apps/component-guides/README.md)
2930
* [Option lists](build-apps/component-guides/option-lists.md)

docs/build-apps/event-handlers.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
In Openblocks, event handlers are responsible for collecting and processing events from components and queries, and executing subsequent actions. For example, for a **Button** component, you can add an event handler to trigger the **Run query** action **** in response to the button **Click** event.
44

5-
<figure><img src="../.gitbook/assets/image (19).png" alt=""><figcaption></figcaption></figure>
5+
<figure><img src="../.gitbook/assets/image (8) (1).png" alt=""><figcaption></figcaption></figure>
66

77
Set event handlers wisely to provide a reactive and responsive user experience (UX). For example, triggering a **get-all** query after **insert-new-data** query finishes enables table automatically refresh.
88

@@ -32,7 +32,7 @@ Running a query can result in success or failure, so queries have two events: **
3232

3333
There are a number of event handler actions available in Openblocks for handling different scenarios. Set them in the **Action** dropdown list in an event handler.
3434

35-
![](<../.gitbook/assets/image (1) (1).png>)
35+
![](<../.gitbook/assets/image (9) (1).png>)
3636

3737
{% hint style="info" %}
3838
See [advanced](event-handlers.md#advanced) on this page to know advanced settings.
@@ -48,7 +48,7 @@ Trigger the selected query.
4848

4949
To control a component, select a component in the **Component** dropdown list and call one of its methods in the **Method** dropdown list.
5050

51-
![](<../.gitbook/assets/image (17).png>)
51+
![](<../.gitbook/assets/image (29).png>)
5252

5353
### Set temporary state
5454

docs/build-apps/use-third-party-libraries.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,23 @@ https://unpkg.com/cowsay@1.5.0/build/cowsay.umd.js
7070

7171
Navigate to the left sidebar, click <img src="../.gitbook/assets/image (1).png" alt="" data-size="line"> > **Other** > **Scripts and style** > **Add a library**. Then paste the **cowsay** link here.
7272

73-
<figure><img src="../.gitbook/assets/Add a library (2).png" alt=""><figcaption></figcaption></figure>
73+
<figure><img src="../.gitbook/assets/Add a library (1).png" alt=""><figcaption></figcaption></figure>
7474

7575
Now you can write code in **JS query** to test its usage with `cowsay.say`:
7676

77-
<figure><img src="../.gitbook/assets/write code in JS query (2).png" alt=""><figcaption></figcaption></figure>
77+
<figure><img src="../.gitbook/assets/write code in JS query (1).png" alt=""><figcaption></figcaption></figure>
7878

7979
or in component **Properties**:
8080

81-
<figure><img src="../.gitbook/assets/or in Properties (2).png" alt=""><figcaption></figcaption></figure>
81+
<figure><img src="../.gitbook/assets/or in Properties (1).png" alt=""><figcaption></figcaption></figure>
8282

8383
And since you have set up cowsay just for **Openblocks's new application 1,** you can not use cowsay in another app.
8484

85-
<figure><img src="../.gitbook/assets/in another app (1).png" alt=""><figcaption></figcaption></figure>
85+
<figure><img src="../.gitbook/assets/in another app (2).png" alt=""><figcaption></figcaption></figure>
8686

8787
### Import on Workspace-level
8888

8989
Navigate to **Workspace settings** > **Advanced** > **JavaScript library** > **Add a library**. Then paste the **cowsay** link here and click **Save**. Now you can use **cowsay** library **** in any app of your workspace.\
90-
![](<../.gitbook/assets/Import on Workspace-level libraries (1).png>)
90+
![](<../.gitbook/assets/Import on Workspace-level libraries (2).png>)
9191

92-
<figure><img src="../.gitbook/assets/Import on Workspace-level libraries-advanced.png" alt=""><figcaption></figcaption></figure>
92+
<figure><img src="../.gitbook/assets/Import on Workspace-level libraries-advanced (1) (1).png" alt=""><figcaption></figcaption></figure>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Transformers
2+
3+
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.
4+
5+
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.
6+
7+
## Quickstart
8+
9+
Click **+ New > Transfromer** in a query editor to create a transformer.
10+
11+
<figure><img src="../../.gitbook/assets/transformers-1.png" alt=""><figcaption></figcaption></figure>
12+
13+
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.
14+
15+
In the following example, `transformer1` uses the data of star rating in `rating1` to calculate a score.
16+
17+
<figure><img src="../../.gitbook/assets/transformers-2.png" alt=""><figcaption></figcaption></figure>
18+
19+
{% hint style="warning" %}
20+
`{{ }}` 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.
21+
{% endhint %}
22+
23+
## Use cases
24+
25+
### Transform timestamp
26+
27+
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.
28+
29+
```javascript
30+
return query1.data.map(it => {
31+
return {
32+
...it,
33+
start_time: moment(it.start_time).format('YYYY-MM-DD')
34+
};
35+
})
36+
```
37+
38+
### Sort query data
39+
40+
Use the `_.orderBy()` method provided by [lodash](https://lodash.com/) to sort data. The following example returns `query1.data` sorted by `amount` column in ascending order.
41+
42+
```javascript
43+
return _.orderBy(query1.data, 'amount', 'asc')
44+
```
45+
46+
### Join two queries
47+
48+
The example code below shows how to join query results of `getUsers` and `getOrders` on user id.
49+
50+
```javascript
51+
const users = getUsers.data;
52+
const userOrders = getOrders.data;
53+
return users.map(user => ({
54+
...user,
55+
orders : userOrders.find(order => order.customer_id === user.id),
56+
}));
57+
```
58+
59+
## Read-only operations
60+
61+
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.
62+
63+
For example, you cannot call the method `setText()` of a text component in a transformer.
64+
65+
<figure><img src="../../.gitbook/assets/transformers-3.png" alt=""><figcaption></figcaption></figure>
66+
67+
Instead, calling the method `setText()` in a JavaScript query reports no error.
68+
69+
<figure><img src="../../.gitbook/assets/transformers-4.png" alt=""><figcaption></figcaption></figure>
70+
71+
In another example, transformer`sort1` aims at sorting the data of `getUsers` by `first_name`, but the `sort()` method may change the original data, so an error occurs.
72+
73+
<figure><img src="../../.gitbook/assets/transformers-5.png" alt=""><figcaption></figcaption></figure>
74+
75+
In this case, use the method `_.orderBy()` provided by [lodash](https://lodash.com/) instead.
76+
77+
<figure><img src="../../.gitbook/assets/transformers-6.png" alt=""><figcaption></figcaption></figure>

docs/data-sources/connect-to-databases/google-sheets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Navigate back to to [Google Sheets](https://docs.google.com/spreadsheets), and f
3737
1. Open the JSON file of the key.
3838
2. Copy the value of `client_email`, which is an identity used for access management of your sheet.
3939

40-
<figure><img src="../../.gitbook/assets/Google-sheets-key-client-email (1).jpeg" alt=""><figcaption></figcaption></figure>
40+
<figure><img src="../../.gitbook/assets/Google-sheets-key-client-email.jpeg" alt=""><figcaption></figcaption></figure>
4141
3. Click **Share** at the top right and paste the copied `client_email` value to add a member with access.
4242

4343
<figure><img src="../../.gitbook/assets/Google-sheets-share.jpg" alt=""><figcaption></figcaption></figure>

docs/data-sources/data-source-basics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Follow the steps below:
1515
1. Click **Data Sources** on Openblocks homepage.
1616
2. Click **New data source** on the upper right. This permission is restricted to workspace admins and developers.
1717

18-
<figure><img src="../.gitbook/assets/image (12) (1).png" alt=""><figcaption></figcaption></figure>
18+
<figure><img src="../.gitbook/assets/image (31) (1).png" alt=""><figcaption></figcaption></figure>
1919
3. Select the database or API type you need to connect.
2020

2121
<figure><img src="../.gitbook/assets/image (32) (1).png" alt=""><figcaption></figcaption></figure>

docs/queries/query-basics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Queries support you to read data from or write data to your data sources. You ca
66

77
You can connect to a data source that was already in your data source library or create a new one. For detailed information, see [data-source-basics.md](../data-sources/data-source-basics.md "mention").
88

9-
<figure><img src="../.gitbook/assets/image (31) (1).png" alt=""><figcaption></figcaption></figure>
9+
<figure><img src="../.gitbook/assets/image (12) (1).png" alt=""><figcaption></figcaption></figure>
1010

1111
## Create a query
1212

docs/self-hosting/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ By default the supervisor will run under the user `uid=9001`. You can specify th
6464
{% tabs %}
6565
{% tab title="Docker-Compose" %}
6666
Add an environment variable `LOCAL_USER_ID` in `docker-compose.yml` downloaded in your working directory.\
67-
<img src="../.gitbook/assets/local-user-id.png" alt="" data-size="original">
67+
<img src="../.gitbook/assets/local-user-id (1).png" alt="" data-size="original">
6868
{% endtab %}
6969

7070
{% tab title="Docker" %}

0 commit comments

Comments
 (0)