Functions
December 13, 2022 ยท View on GitHub
Custom logic can be added by using functions in the configuration.
The following configuration properties can use functions:
attribute.authorizeattribute.readonlyattribute.defaultattribute.value- custom validation keywords
- custom patch operators
- custom log providers
Functions should be pure: no global variable should be used nor side-effects created. Their parameters are read-only.
Defining functions
Functions are regular JavaScript files exporting a function and required using a reference.
export default () => Math.random()
and in the configuration:
collections:
example_collection:
attributes:
example_attribute:
default:
$ref: get_default_value.js
Inline functions
You can also directly write JavaScript functions inside the configuration.
collections:
example_collection:
attributes:
example_attribute:
default: (Math.random())
Only the function body should be specified (without the leading return
keyword) and it should be wrapped in parenthesis.
Constants
Everywhere a function can be used, a constant value can also be used instead.
collections:
example_collection:
attributes:
example_attribute:
default: 10
Parameters
Every function receives as its first argument an object containing parameters with information about the current context.
In the example below, the timestamp parameter is used.
export default ({ timestamp }) => timestamp
Parameters can be also be used when the function is inline.
collections:
example_collection:
attributes:
example_attribute:
default: (timestamp)
The following parameters are available to any function:
requestid{string}- UUID identifying the current request. Also available in response'smetadata.requestidpropertytimestamp{string}- ISO 8601, i.e.YYYY-MM-DDTHH:MM:SS.SSSprotocol{string}: possible value is onlyhttpip{string}: request IPorigin{string}- protocol + hostname + portpath{string}- only the URL path, with no query string nor hashmethod{string}- protocol-agnostic method, e.g.GETqueryvars{object}- query variables, as an objectheaders{object}- protocol headers specific to the engine, for example HTTP headers starting withX-Autoserver-format{string}- request payload and server response's formatcharset{string}- request payload's charsetcompress{string}- response's and request's compressionpayload{any}- request payloadpayloadsize{number}- in bytespayloadcount{number}- array length, if it is an arrayrpc{string}: possible values aregraphql,graphiql,graphqlprint,restorjsonrpcargs{object}: client arguments passed to the request, e.g.filterparams{object}: all client-specific parametersdatasize{number}- size of thedataargument, in bytesdatacount{number}- array length of thedataargument, if it is an arraysummary{string}- summary of the request, e.g.find_collection{child}commandpaths{string[]}- array with allcommandpathcollections{string[]}- array with allcollectioncommand{string}- amongcreate,find,upsert,patchanddeleteserverinfo{object}:host{object}:id{UUID}: unique to each host machine (using the MAC address)name{string}- hostnameos{string}- e.g.Linuxplatform{string}- e.g.linuxrelease{string}- e.g.4.8.0-52-genericarch{string}- e.g.x64memory{number}- total memory in bytescpus{number}- number of CPUs
versions{object}node{string}- Node.js version, e.g.v8.0.0autoserver{string}-autoserverversion, e.g.v0.0.1
process{object}:id{integer}: PIDname{string}: defaults to system hostname, but can be overriden using the configuration propertyname
The following parameters are available to any function except custom log providers and server-specific parameters:
commandpath{string}- command full path, e.g.""(top-level) orchild.grand_childcollection{string}: name of the collection, e.g.users
The following parameters are available to any function except custom log providers, server-specific parameters and custom patch operators:
value{any}: value of the current attribute. E.g.value === 'John'checks whether the current value equalsJohnmodel{object}: current model. E.g.model.first_name === 'John'checks whether the model'sfirst_nameequalsJohnpreviousvalue{any}: value of the attribute. If the current request is modified the current attribute,previousvalueis the value before that modification, andvalueafter that modification.previousmodel{object|undefined}: model. If the current request is modified the current model,previousmodelis the value before that modification, andmodelafter that modification. If the current request is creating the model (with acreateorupsertaction), this will beundefined.
The following parameters are available only to custom log providers:
log,error,protocols,exit,measures,measuresmessage,duration,event,phase,levelandmessage- see loggingstatus{string}- response's status, amongINTERNALS,SUCCESS,CLIENT_ERRORandSERVER_ERRORresponsedata{any}- response dataresponsedatasize{number}- in bytesresponsedatacount{number}- array length, if it is an arrayresponsetype{string}- amongmodel,models,error,object,html,textmetadata{object}- response's metadatamodelscount{number}- number of models returned, including nested onesuniquecount{number}- same asmodelscount, excluding duplicates
The following parameters are available for more specific cases:
arg1,arg2, etc.: see server-specific parametersarg: see custom validation and custom patch operatorstype: see custom patch operators
Server-specific parameters
Server-specific parameters can be added using the params
configuration property, which is an object
containing all server-specific parameters.
In the example below, the $secret_password server-specific parameters is made
available to any function.
params:
$secret_password: admin
export default ({ $secret_password }) => ($secret_password === 'admin' ? 1 : 0)
Server-specific parameters can be functions themselves:
- parameters (including other server-specific parameters) will be passed as the first argument like any other function. This will only be done if the parameter is a function, as as opposed to an object with function members.
- if the function is inline, positional arguments are
passed using the parameters
arg1,arg2, etc.
For example:
params:
$example_function: '($my_math_func(1, 10, 100, 2))'
$my_math_func: ((arg1 * arg2) + (arg3 * arg4))
$birth_date: 2005-01-01
$my_custom_func:
$ref: custom_func.js
$lodash:
$ref: lodash.node
$constants:
$ref: constants.json