Custom Function

what is customFunction

Using custom functions, we can personalize the data in our own way, like encrypting/decryption, hashing, etc.

usage

We need to specify the absolute path of the custom functions in the config.json file like below. Application requires the customFunction on the boot.

{
    "servicePersonalization" : {
        "customFunctionPath":"D:\\Repos\\oecloud.io\\oe-service-personalization_master\\test\\customFunction"
    }
}

Note:- In case of server you can simply set the environment variable custom_function_path

PersonalizationRule

  1. Rule should be defined in the PersonalizationRule model, for executing the custom functions we only pass the functionName as part of the rule. The postCustomFunction will be applied in the afterRemote hooks
{
    "modelName": "Product",
    "personalizationRule": {
      "postCustomFunction": {
        "functionName": "customFn"
      }
    },
    "scope": {
      "device": "android"
    }
}
  1. The preCustomFunction will be applied in the beforeRemoteHooks
{
    "modelName": "Product",
    "personalizationRule": {
      "preCustomFunction": {
        "functionName": "customFn"
      }
    },
    "scope": {
      "device": "android"
    }
}

customFunction Signature

Create a folder say customFunction, this folder path itself we have mentioned in the customFunctionPath in config.json and then create a file index.js within the created folder. In index.js write your own functions with single argument like below and atlast export these functions.

function updateRes(ctx) {
    ctx.result // fields in the response
}

function updateReq(ctx) {
    ctx.req.body // this is the request body
}

module.exports = {
    updateRes,
    updateReq
}

We can update the ctx.result object for updating in the response as shown in updateRes method, Also we can modify ctx.req.body object to actually update the request body before saving to database as shown in updateReq method.

While executing the custom function we pass the ctx object as argument to the custom function. Custom functions need not return anything and can update the ctx object if needed.

Note

  1. While creating the PersonalizationRule the specified functionName in the rule and the exported functionName should be same.
  2. To store updated/modified data in the database need to use preCustomFunction in the personalization rule
  3. To just modify the data through a Get request then need to use postCustomFunction in the personalization rule.