Skip to main content

JavaScript Transform tutorial

FairCom's JavaScript Transform can be used to process MQTT messages sent to a topic

These tutorials show how to interact with the FairCom Edge database server to pull, store, view, and transform data from MQTT messages.

Tutorial on creating a JavaScript transform

This tutorial creates a transform that processes MQTT messages sent to the topic "robot1_temperature". The transform extracts a Celsius temperature from the JSON payload, converts it to Fahrenheit, and writes each temperature to the appropriate fields in the table "temperature_celsius" and "temperature_fahrenheit". The transform also creates a status field named temperature_status and populates it with information about the temperature, such as "cold" and "normal".

Send an MQTT message

Send a message to the MQTT topic named "robot1_temperature".

{
  "temperature": 21.111
}

When a record is inserted into an integration table, the source_payload field contains JSON data, such as the following JSON object, which contains a Fahrenheit temperature.

Create JavaScript code

Use the following tutorial to convert a Fahrenheit temperature to Celsius

The following code extracts the "temperature_celsius" property from the source_payload field of the current record in an integration table. It converts the Celsius temperature to Fahrenheit and stores the result in the temperature_fahrenheit field in the current record of an integration table. The temperature in Celsius is then stored in the temperature_celsius field in the integration table.

record.temperature_celsius = record.source_payload.temperature
record.temperature_fahrenheit = getFahrenheitFromCelsius(record.temperature_celsius)
record.temperature_status = calculateTemperatureStatus(record.temperature_fahrenheit)

function getFahrenheitFromCelsius(temperature_celsius){
  return (temperature_celsius * 1.8) + 32;
}
function calculateTemperatureStatus(temperature_fahrenheit){
  switch (true) {
    case (temperature_fahrenheit < 0):
      return "alert: too cold";
    case (temperature_fahrenheit < 32):
      return "cold";
    case (temperature_fahrenheit > 80):
      return "hot";
    case (temperature_fahrenheit > 140):
      return "alert: too hot";
    default:
      return "normal";
  }
}

Create a code package

Run the following "createCodePackage" action to create a JavaScript code package named "convertTemperature".

{
  "api": "admin",
  "action": "createCodePackage",
  "params": {
    "databaseName": "faircom",
    "ownerName": "admin",
    "codeName": "convertTemperature",
    "codeLanguage": "javascript",
    "codeType": "integrationTableTransform",
    "codeStatus": "developing",
    "description": "1. Copies the value from the temperature property in source_payload to the temperature_fahrenheit field.\n2. Converts temperature_fahrenheit into Celsius and stores it in the temperature_celsius field.\n3. Stores alerts about temperature in temperature_status field.",
    "metadata": {},
    "comment": "optional comment about the current version",
    "codeFormat": "utf8",
    "code": "// The server runs the following JavaScript code, which is registered in the server as a transform method.\nrecord.temperature_fahrenheit = record.source_payload.temperature\nrecord.temperature_celsius = getCelsiusFromFahrenheit(record.temperature_fahrenheit)\nrecord.temperature_status = calculateTemperatureStatus(record.temperature_fahrenheit)\n\nfunction getCelsiusFromFahrenheit(temperature_fahrenheit){\n    return (temperature_fahrenheit - 32) / 1.8;\n}\n\nfunction calculateTemperatureStatus(temperature_fahrenheit){\n    switch (true) {\n        case (temperature_fahrenheit < 0):\n            return \"alert: too cold\";\n        case (temperature_fahrenheit < 32):\n            return \"cold\";\n        case (temperature_fahrenheit > 80):\n            return \"hot\";\n        case (temperature_fahrenheit > 140):\n            return \"alert: too hot\";\n        default:\n            return \"normal\";\n    }\n}"  
  },
  "authToken": "replaceWithAuthTokenFromCreateSession"
}

Note that for the "code" value to be valid JSON, it must be a single string with no line breaks.

Create a transform

{
  "api": "hub",
  "action": "createTransform",
  "params": {
    "transformName": "transformFahrenheitToCelsius",
    "transformActions": [
      {
        "transformStepMethod": "javascript",
        "transformService": "v8TransformService",
        "inputFields": ["source_payload"],
        "outputFields": [
          "temperature_celsius",
          "temperature_fahrenheit",
          "temperature_status"
        ],
        "transformParams": {
          "codeName": "convertTemperature",
          "databaseName": "ctreeSQL",
          "ownerName": "admin",
          "codePackagePoperties": {},
          "outputFieldDefinitions": [
            {
              "name": "temperature_celsius",
              "type": "double"
            },
            {
              "name": "temperature_fahrenheit",
              "type": "double"
            },
            {
              "name": "temperature_status",
              "type": "varchar",
              "length": 100
            }
          ]
        }
      }
    ]
  },
  "authToken": "replaceWithAuthTokenFromCreateSession"
}

"codePackageProperties" is an optional parameter that lets the user add run-time properties and values to JavaScript before the server runs the code When the value is an object, it contains properties and values that the server adds to the JavaScript engine.

"codePackageProperties": {
  "myCustomProperty1": "a string that the JavaScript code will use",
  "myCustomProperty2": 42
},

transform codepackage tutorial

code packagecodepackagetransformintegrationTableTransformintegration table transformtutorial