Skip to main content

alterCodePackage

The "alterCodePackage" action updates the code in the server. Optionally provide a comment explaining the change.

Each time you alter the "code" or "codeFormat" properties, the server increments the version number of the code package and stores the previous version in the code package history. Each time you alter the other properties, the server updates them in place. It does not change the version number or write the change to the code package history.

Altering the "codeName", "comment", "description", and "metadata" properties has no effect on how the server runs the code package. Altering anything else affects how the server runs the code package.

You cannot alter the  "codeType" and "codeLanguage" properties.

To activate a code package, set the "codeStatus" property to "deprecated", "testing", or "active". This allows the server to start running processes that use the code package.

To deactivate a code package, set the "codeStatus" property to "developing", "deleted", or "inactive". This causes the server to finish running existing processes that use the code package, and prevents the server from running the code package again.

Tip

Immediately deactivate code that harms data to stop the server from running the code again.

Warning

WARNING: deactivating a code package prevents the server from running processes that rely on that code package. For example, deactivating a code package may break transform processes, triggers, and jobs.

Tip

To prevent disruptions to running code, clone an existing code package, give it a new name, improve the clone, remove the original from all active server processes, replace it with the new code package, and deactivate the old code package.

The server loads active code packages into instances of the language runtime. For example, the server runs multiple instances of the Google V8 JavaScript engine. When you alter the "code" property, the server finishes running the previous version of the code package without affecting the code the server is currently running. The next time the server runs the code package, it will use the new code.

Request

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
   "databaseName": "faircom",
    "ownerName": "admin",
    "codeName": "convertTemperature",
    "newCodeName": "convertTemperature new",
    "codeLanguage": "javascript",
    "codeType": "transform",
    "codeStatus": "active",
    "comment": "optional change comment",
    "description": "optional new description",
    "metadata": {},
    "code": "optional new code to replace old code",
    "codeFormat": "utf8"
  }
}

"comment" is an optional string that explains the change to the code.

Minimal example - Change package comment

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "comment": "The new comment replaces the old comment."
  }
}

Example - Run a package in production

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "codeStatus": "active"
  }
}

Example - Run a package for testing

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "codeStatus": "testing"
  }
}

Example - Deprecate a package and keep running it

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName":"convertTemperature",
    "codeStatus": "deprecated"
  }
}

Example - Save a developed package but do not run it

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "codeStatus": "inactive"
  }
}

Example - Stop running and using package

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "codeStatus": "inactive"  
  }
}

Example - Delete a package

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "codeStatus": "deleted"
  }
}

Example - Change package metadata

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "metadata": {"keyword":"temperature"}
  }
}

Example - Remove package comment, description, and metadata

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "comment": "",
    "description": "",
    "metadata": {}
  }
}

Example - Change package comment, description, and metadata

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "comment": "New comment replaces old comment.",
    "description": "New description replaces old description.\nAnother description line.",
    "metadata": {"keyword":"temperature","favorites":true}
  }
}

Example - Rename a package

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "newCodeName": "convertTemperature2",
    "comment": "Changed the name from convertTemperature to convertTemperature2.",
    "description": null,
    "metadata": null
  },
  "requestId": "00000019"
}

Example - Change code

{
  "authToken": "MyTokenHere",
  "api": "admin",  "action": "alterCodePackage",
  "params": {
    "codeName": "convertTemperature",
    "newCodeName": "convertTemperature2",
    "comment": "Modified the code",
    "code": "// The server runs this 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}"
  }
}