February 14, 2018 By Carlos Santana 2 min read

New Node.js 8 runtime for IBM Cloud Functions

Introducing a new runtime for JavaScript IBM Cloud Functions based on Node.js version 8. Now you can take advantage of the new features that are offered by the recently released Node.js version 8 on Long Term Support (LTS).

This new runtime comes with pre-installed npm packages (bm_db, ibm-cos-sdk, cloudant, watson-developer-cloud) that are useful when integrating serverless functions with IBM Cloud services. For more details on the Node.js 8 environment, check the documentation.

What’s new in Node.js version 8

The next major release of Node.js brings a range or significant changes and additions for JavaScript users. This article highlights the most significant changes and features.

With version 6 of Node.js, you are taking advantage of ES2015 language specification, such as Object destructuring. Now with version 8 of Node.js, you can leverage more language features in your IBM Functions, like JavaScript ES2017 async functions, which makes it easier to write code that can use and create Promises.

Using async functions, waiting for a Promise to resolve is as simple as typing await before it, and proceeding as if the value were synchronously available (without using callbacks).

Example

In this easy to follow example, you can see how to use async and await with IBM Db2 Warehouse on Cloud service.

1. Create a JavaScript file “Employees.js” with the following code:

<br>
   const ibmdb = require('ibm_db')<br>
   const queryString = 'SELECT FIRST_NAME, LAST_NAME, EMAIL from GOSALESHR.employee FETCH FIRST 3 ROWS ONLY'<br>
   async function main ({<br>
   __bx_creds: { dashDB: { ssldsn } }<br>
   }) {<br>
     let connection, result, rows<br>
     try {<br>
      connection = await ibmdb.open(ssldsn)<br>
      rows = await connection.query(queryString)<br>
      connection.close()<br>
      result = { employees: rows }<br>
    } catch (err) {<br>
      console.error(err.message)<br>
      result = { message: err.message }<br>
    }<br>
    return result<br>
   };<br>

2. Deploy your function by using the CLI, and give it the name “Employees”.

bx wsk action update Employees Employees.js --kind nodejs:8

Important: When you create or update an Action, always specify the kind nodejs:8. If the kind is not explicitly provided, the action uses the default nodejs:6 version.

3. Bind the IBM Cloud service to your function to set the ssldsn as a default parameter.

<br>
bx wsk service bind dashDB Employees<br>

For more information about the new service bind for IBM Cloud Functions, see the following blog post.

If you don’t yet have the service created in your Namespace, use the following command to create the service instance.

<br>
bx service create dashDB Entry db2<br>

4. Invoke the Action from the CLI for testing, and retrieve the results:

<br>
bx wsk action invoke Employees -r<br>
<br>
  {<br>
    "employees": [<br>
      {<br>
        "EMAIL": "DPage@grtd123.com",<br>
        "FIRST_NAME": "Denis",<br>
        "LAST_NAME": "Pagé"<br>
      },<br>
      {<br>
        "EMAIL": "EMichel@grtd123.com",<br>
        "FIRST_NAME": "Élizabeth",<br>
        "LAST_NAME": "Michel"<br>
      },<br>
      {<br>
        "EMAIL": "EClermont@grtd123.com",<br>
        "FIRST_NAME": "Émile",<br>
        "LAST_NAME": "Clermont"<br>
      }<br>
    ]<br>
  }<br>

Get started with IBM Cloud Functions with your Free account.

More from

How to build a successful talent acquisition strategy

11 min read - The success of any organization relies on its ability to attract, retain and develop top talent. Talent acquisition refers to the ongoing strategy and process an organization and its HR department uses to source, attract, evaluate, hire and retain the highly-qualified new employees it needs to grow. A well-crafted talent acquisition strategy has become a critical component for organizations seeking to secure a competitive edge. Beyond simply filling open roles, a comprehensive talent acquisition strategy encompasses a holistic approach to…

Innovation and authentic demand

2 min read - Arvind Krishna recently spoke with former IBMer and author Danny Sabbah about a new book he co-authored, The Heart of Innovation: A Field Guide for Navigating to Authentic Demand. In their conversation, Arvind and Danny discussed the secret to determining authentic demand and building innovative products that achieve runaway success. Here are five facts about innovation, taken from the book and their conversation, that may surprise you: While major technology breakthroughs dominate our perception of innovation—think the Xerox machine, the…

How blockchain enables trust in water trading

3 min read - Australia pioneered water rights trading in the early 1900s, becoming a world leader in water sharing between valleys. The initiative extended throughout the states of Australia across the Murray-Darling Basin (MDB). However, findings from the water market’s inquiry of the MDB, completed by the Australian Consumer and Competition Commission (ACCC) and the Department of Climate Change, Energy, the Environment and Water (DCCEEW), highlighted a great many challenges of the system. These challenges include a combination of paper-based and digital processing, slow…

Sensors, signals and synergy: Enhancing Downer’s data exploration with IBM

3 min read - In the realm of urban transportation, precision is pivotal. Downer, a leading provider of integrated services in Australia and New Zealand, considers itself a guardian of the elaborate transportation matrix, and it continually seeks to enhance its operational efficiency. With over 200 trains and a multitude of sensors, Downer has accumulated a vast amount of data. While Downer regularly uncovers actionable insights from their data, their partnership with IBM® Client Engineering aimed to explore the additional potential of this vast dataset,…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters