Reusability - Library Functions

Library Functions - Reuse your Functions

Inline Functions can only be used in the application where they were created. To use your Function or Composite Function in other applications, you can export it so it becomes a Library Function.

You can edit your Library Functions in the Function tab in the navbar and add them via the Function's menu located to the left of the graph in your application.

See the video below for a quick demonstration on exporting a Composite Function.

After exporting your Function or Composite Function (as in this case) go to the Functions section, the new Library Function will be available there. There are some things to consider when you export a Function.

  • Make sure you add the right modules.
  • You might need to set a Schema.
  • Use the params attribute in your code to pick up user values (if a Schema has been added).

Modules

This allows you to connect external libraries for the Function to work. You can specify a link to Github to connect the library's source code. Supported formats are.

[<@scope>/]<pkg>
[<@scope>/]<pkg>
[<@scope>/]<pkg>@<tag>
[<@scope>/]<pkg>@<version>
[<@scope>/]<pkg>@<version range>
<git:// url>
<github username>/<github project>

For instance, if you need to add aws-sdk as a dependency you simply add it to the Modules tab and then reference it as you normally would in your code.

const aws = require('aws-sdk');

Schema
A Function Schema is a JSON schema that provides a method for structuring and validating input into a Library Function as well as User Interface helpers that allow for applying automatic rendering.

As an example, consider the following Function which counts events with a defined value. Here we extract the params from the context (ctx) and use it in our code.

const { get } = require('lodash');
let counter = 0;

exports.handler = async(ev, ctx) => {
  const { route, params, log } = ctx;

  // See the use of params.fieldName and params.fieldValue
  let value = get(ev,params.fieldName);
  if(value == params.fieldValue){
    counter++;
  }

  if(!ev.eof){
    return;
  }  
  let result = {
    columnName: params.fieldName,
    count : counter,
  };
  route(result);
  counter = 0;
};

You need to set a Function Schema so the user of the Library Function can add the required values that you are picking up through the params attribute in the context (ctx).

See the exact JSON structure Schema below that is asking for a fieldName and a fieldValue used in the code above.

{
  "uiSchema": {
    "params": {
      "ui:options": {
        "orderable": false
      }
    },
    "fieldName": {
      "ui:help": "The field where the value will be searched",
      "ui:placeholder": "row.name"
    },
    "fieldValue": {
      "ui:help": "The value to search for",
      "ui:placeholder": "lolo"
    }
  },
  "type": "object",
  "properties": {
    "fieldName": {
      "type": "string",
      "title": "Field Name"
    },
    "fieldValue": {
      "type": "string",
      "title": "Field Value"
    }
  }
}

When the user uses the Library Function they will see these values in the Parameter's tab.

See us add a Schema and a module for the example at the start and what this will look like to the end user when they try to use the Library Function. To save some time, we aren't actually writing the Schema here just pasting it in.

The platform uses React-JsonSchema-Form for generating a React form based on a JSON Schema. All of parameters and examples are available at react-jsonschema-form


What’s Next

Continue to read about the Lolo Platform's event and setup context (ctx)