otto()
creates a JavaScript VM that receives and sends data through the defined javascript function for processing. The parameter passed to the function has been converted from a go map[string]interface{} to a JS object of the following form:
{
"ns":"message.namespace",
"ts":12345, // time represented in milliseconds since epoch
"op":"insert",
"data": {
"id": "abcdef",
"name": "hello world"
}
}
NOTE when working with data from MongoDB, the _id field will be represented in the following fashion:
{
"ns":"message.namespace",
"ts":12345, // time represented in milliseconds since epoch
"op":"insert",
"data": {
"_id": {
"$oid": "54a4420502a14b9641000001"
},
"name": "hello world"
}
}
otto({"filename": "/path/to/transform.js"})
// transform() is also available for backwards compatibility reasons but may be removed in future versions
// transform({"filename": "/path/to/transform.js"})
message in
{
"_id": 0,
"name": "transporter",
"type": "function"
}
config
otto({"filename":"transform.js"})
transform function (i.e. transform.js
)
module.exports=function(doc) {
doc["data"]["name_type"] = doc["data"]["name"] + " " + doc["data"]["type"];
return doc
}
message out
{
"_id": 0,
"name": "transporter",
"type": "function",
"name_type": "transporter function"
}