Our New JS Snippets Feature Helps You Write JavaScript in the Appsmith Platform

Our New JS Snippets Feature Helps You Write JavaScript in the Appsmith Platform

Building an app on Appsmith is great fun. Why? Because it takes minutes to build a clean and consistent UI, and less than a few hours to make data connections. We might be tooting our own horn, but what we love about Appsmith (ask our in house devs!) is that we can use JavaScript anywhere on the platform to add additional transformations, functionalities, and actions using the moustache syntax. We’re aware that not everyone is a JavaScript enthusiast, everyone prefers a language of their choice! So we’ve come up with our new feature: “JS Snippets.” With this, you can get some help with writing JavaScript inside Appsmith. In this blog, I will tell you how we made this feature and how you can use it!

The Story!

We often see many questions in our community forums and discord channels seeking help with binding nested APIs, write data transformations, configure chained actions. Hence, we thought of building a library of snippets inside Appsmith applications that you can use directly and customize based on your data source.

Initially, we thought of the issue in a straightforward way, but at Appsmith, we always like to go the extra mile. So, we brainstormed this idea multiple times and decided to extend it to customize and add snippets particular to widgets and data sources. But, in order to do this kind of customization, we need a database application that validates and filters these snippets. So we built an internal Appsmith app that manages all the JS Snippets. This is how the application looks like:

616d7fa20c47d61ba4d30f73_Utt_sHUDc2EgaHOkHuFGSsKe3Pi4Dh-wcl8ypMRD3oua-X5HxcjaNdl7dL6W_jJs0JNdLrbFd23-hPZt4NpurHkYYcj5oM5U0rWDuCJ11J0W5ySQclygvx2-xsxQfxTskj4z5PQd=s0.png

Here, as we can see, on the left, we see a list of all the available snippets today. When these are clicked, we will see the details of the snippet and have options to verify it and publish it live directly to the JS Snippets library.

To customise these snippets, we define three formats for each of them.

  1. Snippet: The example code snippet written in JS
  2. Args: Customisable variables inside the snippets and their expected data type,
  3. Template: The dynamic template that helps devs customise their snippet with any variable on Appsmith

Here’s an example of how we ideated customizable JS snippet using these three forms for merging two queries in a table:

Snippet:

{{ 
fetchOrders.data.map((orderObj) => {
   const user = fetchUsers.data.find((userObj) => userObj.id == orderObj.refId) || {};
   return { ...orderObj, ...user };
})
}}

Args:

[
  {
    "identifier": "fetchOrders",
    "name": "Query1",
    "type": "OBJECT"
  },
  {
    "identifier": "fetchUsers",
    "name": "Query2",
    "type": "OBJECT"
  },
  {
    "identifier": "orderObj",
    "name": "row1",
    "placeholder": true,
    "type": "OBJECT"
  },
  {
    "identifier": "userObj",
    "name": "row2",
    "placeholder": true,
    "type": "OBJECT"
  },
  {
    "identifier": "id",
    "name": "key1",
    "type": "VAR"
  },
  {
    "identifier": "refId",
    "name": "key2",
    "type": "VAR"
  },
  {
    "identifier": "user",
    "name": "recordForMerge",
    "placeholder": true,
    "type": "VAR"
  }
]

Here’s a screenshot of how the snippet is rendered on Appsmith:

616d8016d003f33e0728bdf8_Zxy_qNm3evvTfxxgk-RCYL2sittNU95JHciTIYUEFtZAWoqIjIjpv_xg9w7xSyeMuPQ_OVmovVFWTek1b59MRQcHTQ2ikZZmvocRPMilVuLQu4Upe5uUO0xDn3IrqPXSkvAnEebi=s0.png

Users can now directly copy this snippet onto the widget, or customize the arguments, test it out, and use it in the widget.

CleanShot 2021-10-12 at 21.52.23@2x.png

How to use JS Snippets on Appsmith

We can find JS snippets inside the Appsmith applications from Omnibar. Click on the Search/Omnibar from the top navigation and choose the Use snippets option. Here, you can filter snippets based on data sources and UI widgets.

616d8035a1b64f0d1b410933_vIclDmCBkSyS6WfqJBC_jYxqLQm-NG_D7-mH9jIu4XLMPxfsy71koviLHC8ijDO9Kk0LR11z4jQXQTwqd7woJjZ3BhgXHF9HrdU9x0MiKRFG7tm6izEXvF5kx8qocLY8d_oVzXBR=s0.png

The other easy way is to access snippets from the slash commands. Just type in / inside the bindings, and choose the insert snippet option.

CleanShot 2021-10-12 at 22.20.09.gif


Special credits to Arun Vijayan, Sumit, Ajinkya, and Nikhil Nandagopal who had worked so hard on this feature!

Hope you’ll build some super cool applications using the JS Snippets on Appsmith! If you do, don’t forget to tell us about it, we would love to feature your work! Your contributions and feedback help us make Appsmith better and we really appreciate it.

Join the community! Come chat with us on Discord, or jump in on Github directly!

You can also follow us on Twitter and Linkedin.