Search…
Scripting

Introduction

The Medable platform offers a JavaScript API for developers to create, read, update, and delete cortex objects; query and aggregate data; send custom notifications; perform callouts; console and debug logging; create object triggers; run scheduled jobs; create custom API routes and more.
All scripts run synchronously. There are no callbacks or timers and as such no implementation of functions such as setInterval, setTimeout or setImmediate. For example, a route script employing an HTTP callout might look like the following.
1
import http from 'http';
2
3
try {
4
const response = http.get('https://www.example.com/service?json');
5
return JSON.parse(response.body);
6
} catch(err) {
7
// handle me
8
}
Copied!

Environment Notes

Compatibility

The Javascript engine is ECMAScript 5/5.1 compliant. You can also use many ES2015 features and some decorators. The following babel plugins are enabled for that reason.
1
babel-plugin-check-es2015-constants
2
babel-plugin-transform-decorators-legacy
3
babel-plugin-transform-class-properties
4
babel-plugin-transform-regenerator
5
babel-plugin-transform-es2015-arrow-functions
6
babel-plugin-transform-es2015-block-scoped-functions
7
babel-plugin-transform-es2015-block-scoping
8
babel-plugin-transform-es2015-classes
9
babel-plugin-transform-es2015-computed-properties
10
babel-plugin-transform-es2015-duplicate-keys
11
babel-plugin-transform-es2015-function-name
12
babel-plugin-transform-es2015-literals
13
babel-plugin-transform-es2015-modules-commonjs
14
babel-plugin-transform-es2015-object-super
15
babel-plugin-transform-es2015-parameters
16
babel-plugin-transform-es2015-shorthand-properties
17
babel-plugin-transform-es2015-spread
18
babel-plugin-transform-es2015-template-literals
19
babel-plugin-transform-es2015-destructuring
20
babel-plugin-transform-es2015-for-of
21
babel-plugin-transform-es2015-typeof-symbol
22
babel-plugin-transform-object-rest-spread
Copied!

Decorators

The following decorators are built-in and can be imported using the decorators-{decoratorName} convention like in this example: autobind, decorate, deprecate, enumerable, extendDescriptor, lazyInitialize, nonconfigurable, nonenumerable, override, readonly
1
import autobind from 'decorators-autobind';
2
import readonly from 'decorators-readonly';
3
4
class Eye {
5
6
@readonly
7
pie = 'pie'; // initializable properties support.
8
9
@autobind
10
apple() {
11
return this.pie;
12
}
13
14
orange = () => this.pie; // bound functions support.
15
}
16
17
const eye = new Eye();
18
19
eye.apple.call(null); // 'pie' returned even though bound to null.
20
eye.orange.call(null); // 'pie' returned even though bound to null.
21
eye.pie = 'not allowed'; // throws
Copied!

Modules

Module loading is available via CommonJS; Standard and custom modules are loaded using require and defined using module.exports. Imports and exports are also supported.
For example, a module exported as c_utils, defined as:
1
import _ from 'underscore';
2
3
export default {
4
5
dateToAge: function(birthDate) {
6
if (!_.isDate(birthDate)) return 0;
7
8
const today = new Date(),
9
m = today.getMonth() - birthDate.getMonth();
10
11
let age = today.getFullYear() - birthDate.getFullYear();
12
13
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
14
age--;
15
}
16
return age;
17
}
18
};
Copied!
might be loaded and used in a route as follows:
1
import utils from 'c_utils';
2
3
const me = org.objects.Accounts
4
.find({
5
_id: script.principal._id
6
})
7
.paths('dob')
8
.next();
9
10
return utils.dateToAge(new Date(me.dob));
Copied!
All scripts run in strict mode. For triggers, jobs and routes the entry point to the script is called main, which can be seen in script stack traces. In order to get meaningful stack traces, it's good practice to name all functions.
Stack traces are available for debugging via the Error object's trace property or the getCurrentStackTrace global function. A stack trace is also provided with calls to the logger or console modules.
The console, consts, org and script auto-globals are always guaranteed to be available, though console printing only works on the development servers.

--

InsertOperation

Operation defaults are bypassCreateAcl(false), dryRun(false), grant(null), lean(true), skipAcl(false).
bypassCreateAcl(bool = true) dryRun(bool = true) execute() grant(level) lean(bool = true) locale(identifier) skipAcl(bool = true)JavaScript
1
const Items = org.objects.c_items;
2
3
const _id = Items.insertOne({c_key: 'unique key'}).execute();
4
5
return Items.find({_id: _id}).paths('c_key').next();
Copied!
InsertOperation.bypassCreateAcl(bool = true)
Skips createAcl checks.
Arguments
  • bool (Boolean)
Returns
this
InsertOperation.dryRun(bool = true)
The operation performs a dry run. Though no documents are inserted, before triggers are called.
Arguments
  • bool (Boolean)
Returns
this
InsertOperation.execute()
Executes the operation.
Returns
ObjectID|Object If the lean option is turned off, the inserted document is returned. When true, only the inserted identifier is returned.
InsertOperation.grant(level)
Sets the grant level for the operation.
Arguments
  • level (Number)
Returns
this
InsertOperation.lean(bool = true)
On by default, turning off this option reads the entire inserted document after execution.
Arguments
  • bool (Boolean)
Returns
this
InsertOperation.locale(identifier)
Specifies the locale of the operation for any localized strings.
Arguments
  • identifier (String) The locale identifier, which consists of a 2 or 3 letter language tag optionally followed by a region separated by _. For example, en, en_US, en_UK, etc.
Returns
this
InsertOperation.skipAcl(bool = true)
Skips acl checks.
Arguments
  • bool (Boolean)
Returns
this

UpdateOperation

Operation defaults are dryRun(false), grant(null), lean(true), skipAcl(false).
1
const Items = org.objects.c_items;
2
3
const _id = Items.updateOne({c_key: 'unique key'}, {c_key: 'very unique'})
4
.skipAcl()
5
.grant(consts.accessLevels.update)
6
.execute();
7
8
return Items.find({_id: _id}).paths('c_key').next();
Copied!
UpdateOperation.dryRun(bool = true)
The operation performs a dry run. Though no documents are updated, before triggers are called.
Arguments
  • bool (Boolean)
Returns
this
UpdateOperation.execute()
Executes the operation.
Returns
ObjectID|Object If the lean option is turned off, the entire updated document is returned. When true, only the identifier is returned.
UpdateOperation.grant(level)
Sets the grant level for the operation.
Arguments
  • level (Number)
Returns
this
UpdateOperation.lean(bool = true)
True by default, disabling this option reads and returns the entire updated document after execution. "modified" may also be used to return an object containing just the modified properties. Otherwise, simply the _id of the updated document is returned.
Arguments
  • bool (Boolean)
Returns
this
UpdateOperation.locale(identifier)
Specifies the locale of the operation for any localized strings.
Arguments
  • identifier (String) The locale identifier, which consists of a 2 or 3 letter language tag optionally followed by a region separated by _. For example, en, en_US, en_UK, etc.
Returns
this
UpdateOperation.merge(bool)
Enabling this option allows for an update operation on an array to include elements with and without identifiers. Elements with identifiers will update the existing array elements while those without identifiers will be pushed into the array as new elements. If disabled, and the update operation includes a mix of elements with and without identifiers, a kInvalidArgument will be returned with the reason "Pushed document has an _id and/or updated document is missing an _id."
Arguments
  • bool (Boolean)
Returns
this
UpdateOperation.pathDelete(path = null)
Deletes a single property or array value using the current cursor options. Passing a path argument overwrites the pathPrefix() option. For example, org.objects.c_object.updateOne({_id: _id}).pathDelete("c_docs.5a2c44f836412438e4c372c2.c_array.bar"); removes "bar" from the c_array string array in the c_docs document array.
Arguments
  • path (String) Optional. Overrides pathPrefix().
Returns
Object
UpdateOperation.pathPrefix(path = null)
Sets or removes the operation path prefix. When a prefix is present the operation can target documents in a write-through list or nested document array. For example, c_parent.updateOne({_id: parentId}, {$push: [{c_name: 'new child'}]}).pathPrefix('c_children').execute() creates a new c_child instance through the c_parent object's c_children list property.
Arguments
  • path (String)
Returns
this
UpdateOperation.pathPush(path = null, body)
Shortcut for updateOne(..., {$push: [body]}).pathPrefix(path).execute()
Arguments
  • path (String) Optional. Overrides pathPrefix().
  • body (Object) The value(s) to push.
Returns
Object
UpdateOperation.pathUpdate(path = null, body)
Shortcut for updateOne(..., {$set: [body]}).pathPrefix(path).execute()
Arguments
  • path (String) Optional. Overrides pathPrefix().
  • body (Object) The value(s) to update.
Returns
Object
UpdateOperation.skipAcl(bool = true)
Skips property acl checks.
Arguments
  • bool (Boolean)
Returns
this

DeleteOperation

Operation defaults are dryRun(false), grant(null), skipAcl(false).
dryRun execute grant skipAcl
1
const Items = org.objects.c_items;
2
3
return Items.deleteOne({c_key: 'unique key'})
4
.skipAcl()
5
.grant(consts.accessLevels.delete)
6
.execute();
Copied!
DeleteOperation.dryRun(bool = true)
The operation performs a dry run. Though no documents are deleted, before triggers are called.
Arguments
  • bool (Boolean)
Returns
this
DeleteOperation.execute()
Executes the operation.
Returns
Boolean true if the instance was deleted.
DeleteOperation.grant(level)
Sets the grant level for the operation.
Arguments
  • level (Number)
Returns
this
DeleteOperation.skipAcl(bool = true)
Skips acl checks.
Arguments
  • bool (Boolean)
Returns
this
Last modified 2mo ago