Javascript Plugins

This plugin allows for a plugin that is implemented in Javascript to be loaded into MLDB to extend its functionality.

Configuration

A new plugin of type javascript named <id> can be created as follows:

mldb.put("/v1/plugins/"+<id>, {
    "type": "javascript",
    "params": {
        "address": <string>,
        "source": <PackageElementSources>,
        "args": <Any>
    }
})

with the following key-value definitions for params:

Field, Type, DefaultDescription

address
string

URI or location of script to run (use this parameter OR 'source')

source
PackageElementSources

source of script to run (use this parameter OR 'address')

args
Any

Arguments to pass to script

With PackageElementSources defined as:

Field, Type, DefaultDescription

main
string

source for the main element of the plugin

routes
string

source for the routes element of the plugin

status
string

source for the status element of the plugin

Returning a result

In order to return a result, the value of the last statement evaluated must evaluate to the result. For example, if the last line of a script is "hello", then the script will return a string "hello" as its result.

Results of script run

Running a Javsascript script directly as a plugin, procedure or function will return a ScriptOutput result, that looks like this:

Field, Type, DefaultDescription

result
JSON

Result of running script

logs
ARRAY [ ScriptLogEntry ]

Log entries created by script

exception
LINK std::shared_ptr

Exception thrown by script

extra
Any

Extra information from language

with log entries looking like

Field, Type, DefaultDescription

ts
Date
"1970-01-01T00:00:00Z"

Timestamp at which message was received

s
string

Stream on which message was received

c
ScriptLogContent

Content of stream

closed
bool
false

Stream is closed

Exceptions

Exceptions are represented as

Field, Type, DefaultDescription

message
string

Exception description

where
string

Full description of where exception came from

scriptUri
string

URI of script that caused the exception

lineNumber
int
-1

Number of the line that caused the exception

columnStart
int
-1

Start column in the line

columnEnd
int
-1

End column in the line

lineContents
string

Contents of the line indicating where the exception was

context
ARRAY [ string ]

What we were doing when we threw the exception

stack
ARRAY [ ScriptStackFrame ]

Call stack for exception

extra
Any

Extra information from language about exception

with stack frames like

Field, Type, DefaultDescription

scriptUri
string

URI of script in this frame

functionName
string

Name of function in this frame

where
string

Where is the frame, in natural format for language

lineNumber
int
-1

Line number

columnStart
int
-1

Column number of error

columnEnd
int
-1

End column number of error

extra
Any

Extra stack from information from language

Server-side Javascript API

Objects available

Accessing plugin creation parameters

The variable plugin.args gives the arguments that the plugin was instantiated with. This can be used by the user of the plugin to provide configuration details of how the plugin should load.

Logging

These accept multiple arguments and will accept any combination of scalars (which are turned into strings then serialized) or objects (which are serialized as their JSON representation).

Calling out to MLDB

MLDB is a REST server, so all routes that are available on the REST interface are also available from a Javascript plugin or script.

To call out, you can use mldb.perform(method, route, queryParams, payload, httpHeaders). The parameters are:

The result comes back as an object with the following fields:

There are also convenience methods defined as:

The following asynchronous methods are also defined. They will not wait for the resource to be created, but will return a 201 immediately with the URI of the created resource, which can be polled until it is ready.

Serving Static Content

plugin.serveStaticFolder(route, dir) will serve up static content under dir on the given plugin route route.

plugin.serveDocumentationFolder(route, dir) will serve up documentation under dir on the given plugin route route. This will render markdown files under a .md extension when accessed under .html. See the Documentation Serving page for more details.

Random Number Generator

By calling var rng = mldb.createRandomNumberGenerator(), you can obtain a random number generator that has access to more functionality than the built-in Javascript version. The methods supported are:

JSON diff and patch

There are functions, useful for unit testing, to generate a JSON diff and to apply a JSON patch:

SQL

The following functions aid in working with SQL from within Javascript.

Interacting with MLDB data types

MLDB's atomic types are represented in Javascript as follows:

Filesystem access

There are two functions that allow access to the virtual filesystem of MLDB:

Stream object

The Stream object has the following methods:

Dataset objects

A dataset object can be created using mldb.createDataset(config). The config is the same as would be POSTed to /v1/datasets to create the dataset. If no id is passed in the config argument to createDataset(config), then the argument will be modified with the id of the created dataset. Similarly for the params object; if default parameters are not specified, they will be filled out with the actual parameters used.

This object has the following methods defined:

Procedure objects

A procedure object can be created using mldb.createProcedure(config). The config is the same JSON object that would be POSTed to /v1/procedures to create the procedure. If no id is passed, then the procedure is anonymous and will have an id auto-generated.

The procedure object has the following methods defined:

Function objects

A function object is created similarly to procedure and dataset objects.

It has the following methods defined:

Debugging

The following are useful for debugging MLDB, but should not be used in normal use of MLDB: