Skip to main content

"createCodePackage"

JSON ADMIN "createCodePackage" action stores JavaScript code in the server

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 examples

{
  "api": "admin",
  "action": "createCodePackage",
  "params": {
    "databaseName": "faircom",
    "ownerName": "admin",
    "codeName": "convertTemperature",
    "codeLanguage": "javascript",
    "codeType": "transform",
    "codeStatus": "developing",
    "description": "1. Copies the value from the temperature property in source_payload to the temperature_celsius field.\n2. Converts temperature_celsius into Fahrenheit and stores it in the temperature_fahrenheit 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_celsius = record.source_payload.temperature\nrecord.temperature_fahrenheit = getFahrenheitFromCelsius(record.temperature_celsius)\nrecord.temperature_status = calculateTemperatureStatus(record.temperature_fahrenheit)\n\nfunction getFahrenheitFromCelsius(temperature_celsius){\n    return (temperature_celsius * 1.8) + 32;\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"
}
{
  "authToken": "replaceWithAuthTokenFromCreateSession",
  "debugInfo": {
    "request": {
      "authToken": "replaceWithAuthTokenFromCreateSession",
      "api": "admin",
      "action": "createCodePackage",
      "params": {
        "databaseName": "faircom",
        "ownerName": "admin",
        "codeName": "convertTemperature",
        "codeLanguage": "javascript",
        "codeType": "transform",
        "codeStatus": "developing",
        "description": "1. Copies the value from the temperature property in source_payload to the temperature_celsius field.\n2. Converts temperature_celsius into Fahrenheit and stores it in the temperature_fahrenheit 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_celsius = record.source_payload.temperature\nrecord.temperature_fahrenheit = getFahrenheitFromCelsius(record.temperature_celsius)\nrecord.temperature_status = calculateTemperatureStatus(record.temperature_fahrenheit)\n\nfunction getFahrenheitFromCelsius(temperature_celsius){\n    return (temperature_celsius * 1.8) + 32;\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}"      },
      "debug": "max"
    }
  },
  "errorCode": 0,
  "errorMessage": ""
}

"codeStatus" is an optional property that you can set to one of the following states: "developing", "deleted", "inactive", "deprecated", "testing", or "active" to indicate what status the code in your code package is in. You can use "alterCodePackage" to transition from any state to any other.

The following table explains each state and whether or not the server can run code in that state.

codeStatus

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.

Use the createCodePackage API action to create and store JavaScript code in the server

API actionJSON ADMIN APIjsonActioncreateCodePackagecreateCodePackagescreatesCodePackagecreatesCodePackagescreate code packagecode packagecodePackages
Table 1. Params property summaries

Property

Description

Default

Type

Limits (inclusive)

databaseName

specifies the name of the database

Defaults to the session's "defaultDatabaseName" property

string

ownerName

specifies the name of the owner

Defaults to the session's "defaultOwnerName" property

string

codeName

specifies the name of the code package

Required - No default value

string

codeLanguage

specifies the programming language of the code in the "code" property

Required - No default value

enum

"javascript"
"json"

codeType

specifies how and where a FairCom server can run the JavaScript code

Required - No default value

enum

"integrationTableTransform"
"getRecordsTransform"

codeStatus

specifies the status of the code. The server only runs code with a status of "deprecated", "testing", or "active".

"developing"

enum

"developing"
"deleted"
"inactive"
"deprecated"
"testing"
"active"

description

describes the code package

""

string

metadata

contains user-defined properties that add keywords and tags about the code package

""

string

comment

describes the latest change to the code

""

string

codeFormat

specifies the encoding of the code in the code property

"utf8"

string

"utf8"

code

contains the source code stored in the server

""

Json string



"databaseName" is an optional string specifying the database name of the code package. It defaults to the session's "defaultDatabaseName" property when omitted or set to null. The package's unique identifier is the combination of "databaseName", "ownerName", and "codeName".

"ownerName" is an optional string specifying the account name that owns the code package. It defaults to the session's "defaultOwnerName" property when omitted or set to null. The package's unique identifier is the combination of "databaseName", "ownerName", and "codeName".

"codeName" is a required string containing the user-defined name for the code package. The package's unique identifier is the combination of "databaseName", "ownerName", and "codeName".

"codeLanguage" is a required string. It 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.

The "codeType" property defines how and where a FairCom server can run the JavaScript code. See Code Package Types for how to use each type of Code Package. The following enumerated values are supported:

"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 from any state to any other. See "Use "codeStatus" to make a package runnable".

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

"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 phrase to find code packages.

"comment" is an optional string explaining the code change.

"codeFormat" is an optional string that specifies the encoding of code in the code property. You must encode you code to embed it in a JSON string. "codeFormat" currently only supports the "utf8" encoding, requiring you to use JSON rules to escape problem characters in your code with the \ backslash character, such as \n.

"code" contains the source code. Before embedding it in a JSON string, encode the source code using the format specified in the "codeFormat" property.