Skip to main content

createCodePackage

The "createCodePackage" action stores JavaScript code in the server. You can configure the server to run this code in various scenarios, such as a transform or a job. You can write code as a transform method in a transform step as part of the transform processes. You can write code as a job that bulk processes data on a schedule or when fired by an event.

Request
{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "createcodePackage",
  "params": {
    "databaseName": "faircom",
    "ownerName": "admin",
    "codeName": "convertTempterature",
    "codeLanguage": "javascript",
    "codeType": "transform",
    "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_fahrehneit){
      \nreturn (temperature_fahrenheit - 32) / 1.8;
    \n}
    \n
    \nfunction calculateTemperatureStatus(temperature_fahrenheit){
      \nswitch (true) {
        \ncase (temperature_fahrenheit < 0):
        \nreturn \"alert: too cold\";
        \ncase (temperature_fahrenheit < 32):
        \nreturn\"cold\";
        \ncase (temperature_fahrenheit > 80):
        \nreturn \"hot\";
        \ncase (temperature_fahrenheit > 140):
        \nreturn \"alert: too hot\";
        \ndefault:
        \nreturn \"normal\";
      \n}
    \n}"
  }
}

"codeName" is a required string containing the user-defined name for the code package. When combined with "databaseName" and "ownerName", it is the package's unique identifier.

"codeType" is the type of code package. Currently, the only allowed enumerated values include: "transform".

"description" is an optional string that describes the code package. This server indexes this field with a full-text index so that you can search for any word or phrase.

"codeLanguage" is a required string that is the programming language's name. Any string value is accepted. Currently, "javascript", and "json" are the only types of code the FairCom server can use.

"metadata" contains user-defined properties that add keywords and tags about the code package. The server indexes this field with a full-text index so you can search for any word or phase to find code packages. The source code in the code property does not have access to data in the "metadata" field.

"code" contains the source code. Use the format specified in the "codeFormat" property to encode the source code before you embed it in a JSON string.

"codeFormat" is an optional string that specifies the encoding of code in the code property. It currently only supports utf8, which requires you to use JSON rules for escaping characters in your code so they can be safely embedded in a string.

"codeStatus" is optional and specifies a new status for the code. When you create a code package, it defaults to "developing". When you alter a code package, it defaults to the current state. You may set it to one of the following states: "developing", "deleted", "inactive", "deprecated", "testing", or "active". You can use "alterCodePackage" to transition any state to any other state.

Status

Server can run code

Description

"developing"

No

Indicates the code package is being developed. Allows a developer to store the code in the server in preparation for testing.

Changing to this state immediately prevents other processes from running the code. Currently-running instances of the code finish under the previous state. This state prevents the server from running this code in new processes.

"deleted"

No

Marks the code package as deleted. For version control purposes, code is never actually removed from the table.

Changing to this state immediately prevents other processes from running the code. Currently-running instances of the code finish under the previous state. This state prevents the server from running this code in new processes.

"inactive"

No

Marks the code package as inactive to prevent the server from running it.

Changing to this state immediately prevents other processes from running the code. Currently-running instances of the code finish under the previous state. This state prevents the server from running this code in new processes.

"deprecated"

Yes

Marks the code package as deprecated to notify users that they should start using another code package instead. The server continues to run deprecated code.

Changing to this state has no impact on processes that currently run this code. This state allows the server to run this code in new processes.

"testing"

Yes

Marks the code package as ready for the server to run as a test.

Changing to this state has no impact on processes that currently run this code. This state allows the server to run this code in new processes.

"active"

Yes

Marks the code package as ready for the server to run in production.

Changing to this state has no impact on processes that currently run this code. This state allows the server to run this code in new processes.