# goja function

`goja()` 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:

```js
{
    "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:

```js
{
    "ns":"message.namespace",
    "ts":12345, // time represented in milliseconds since epoch
    "op":"insert",
    "data": {
        "_id": {
            "$oid": "54a4420502a14b9641000001"
        },
        "name": "hello world"
    }
}
```

### configuration

```javascript
goja({"filename": "/path/to/transform.js"})
// js() is aliased to goja
// js({"filename": "/path/to/transform.js"})
```

### example (add columns, change namespace)

message in

```js
{
    "ns": "old_namespace",
    "data": {
        "_id": 0,
        "name": "abc",
        "type": "software"
    }
}
```

config

```js
goja({"filename":"transform.js"})
```

transform function in `transform.js`

```js
function transform(doc) {
    doc["data"]["name_type"] = doc["data"]["name"] + " " + doc["data"]["type"];
    data["ns"] = "items";  // new namespace
    return doc
}
```

message out

```js
{
    "ns": "items",
    "data": {
        "_id": 0,
        "name": "abc",
        "type": "software",
        "name_type": "abc software"
    }
}
```

**NOTE** - All JavaScript files using goja should have a function named `transform` that handles the transformation. 
No other function name will work.
