elasticio / code-component

The holy grail of the elastic.io platform
Apache License 2.0
2 stars 12 forks source link

code-component

A code component for the elastic.io platform, runs a piece of a JavaScript code inside your integration flow.

Documentation

Pretty much the same way that you would use any other component in our system. It is deployed by default to production, so no need to deploy it yourself (although you could if you have extended it yourself). In our Dashboard start building your integration and include the Code component as well. You will see a picture similar to the one below:

image

However, don't let the simple look fool you - it has a full-fledged interface with many very useful features like the ones you would expect from your favourite desktop developing tool:

Available Variables and Libraries

Here are the available variables and libraries that can be used within the context of execution. The most up-to-date list can always be found in be used within the context of execution or in code.js of the component. Below is a sample for the reference. Built-in Node.js global objects are also supported.

Elastic.io Specific Functionality

Other Libraries/functions

Code component usage Examples

Use code is very simple, just do following:

async function run(msg, cfg, snapshot) {
  console.log('Incoming message is %s', JSON.stringify(msg));
  const body = { result : 'Hello world!' };
  // You can emit as many data messages as required
  await this.emit('data', { body });
  console.log('Execution finished');
}

Please note if you have a simple one-in-one-out function you can simply return a JSON object as a result of your function, it will be automatically emitted as data.

Common usage scenarios

Doing complex data transformation

JSONata is great however sometimes it's easier to do things in JavaScript, if you want to transorm an incoming message with code, just use following sample:

async function run(msg, cfg, snapshot) {
  return {
      addition: 'You can use code',
      keys: Object.keys(msg)    
  };
}

Calling an external REST API

It's very simple to code a small REST API call out of the Code component, see following example:

async function run(msg, cfg, snapshot) {
  const res = await request.get({
    uri: 'https://api.elastic.io/v1/users',
    auth: {
      user: process.env.ELASTICIO_API_USERNAME,
      pass: process.env.ELASTICIO_API_KEY
    },
    json: true  
  });
  return {
    fullName: res.body.first_name + " " + res.body.last_name,
    email: res.body.email,
    userID: res.body.id    
  }
}

Known issues and limitations