Transforms
Script transforms operate on results and cursors to transform them in-flight. Transforms can be applied to Views, Policies and inline for Cursors and BulkOperations.
Though aggregation and projection is often much faster and less expensive that running a script transform, sometimes complex computation or data-interleaving is required that would be impractical to accomplish within a single script instance.
Transforms work by applying a script to an output cursor. Depending on the complexity and number of documents, a cursor transformation may be executed and re-queued multiple times before the cursor is finally exhausted.
When a running transform hits a timeout or ops threshold of 80%, it will stop iterating over the cursor, run the after finalizing method, store the memo object and re-queue for another run.
class Transform
To create a transform, simply define a class annotated with @transform. All methods are optional.
const { transform } = require('decorators-transform')
@transform
class Transformer {
/**
* Called if there is an error in the source operation. If the method
* does not throw an error, Cortex throws the original error.
*
* @param err {Fault} thrown fault.
*/
error(err) {
throw err
}
/**
* Called if the result is not a cursor (but a single result value).
* The method must return a result if it's to be transformed. If the
* method does not return a value, Cortex outputs the original result value.
*
* @param result {*}
* @returns {*}
*/
result(result) {
return result
}
/**
* Cursor handler called only once and before all other processing.
*
* @param memo { Object } An empty object that can be modified and is saved
* for the duration of the transformation.
* @param cursor {Cursor} The output cursor.
*/
beforeAll(memo, { cursor }) {
}
/**
* Cursor handler called before each script run, and shares
* the current script context with each and after.
*
* @param memo { Object }
* @param cursor {Cursor}
*/
before(memo, { cursor }) {
}
/**
* Cursor handler called with each cursor object. Returning undefined
* filters the object from the output stream.
*
* @param object { Object } the current output document
* @param memo { Object }
* @param cursor {Cursor}\
* @returns {*} a value to be pushed to the output cursor.
*/
each(object, memo, { cursor }) {
return object
}
/**
* Cursor handler called after before() and each() was called.
*
* @param memo { Object }
* @param cursor {Cursor}
* @param count { Number } The number of documents pushed by the script
* runtime during the current execution.
*/
after(memo, { cursor, count }) {
}
/**
* Cursor handler called only once and after the cursor is exhausted.
*
* @param memo { Object }
* @param cursor {Cursor}
*/
afterAll(memo, { cursor }) {
}
}Types
Policy Transform
A policy transform can handle a result value as well as a cursor. The define one, set the policy action to "Transform" and the script value to a transform export.
script.arguments:
policy { Object }
_id { ObjectID } The policy id
name { String } The policy name
policyOptions { Object }
triggered {String[]} The list of triggered conditions (methods, paths, etc.)
View Transform
A view transform acts on the result form a view cursor. The define one, set the script value to a transform export.
script.arguments:
view { Object }
_id { ObjectID } The view id
name { String } The view name
viewOptions { Object } The original view cursor options (object, skipAcl, paths, pipeline, etc.)
Inline Cursor Transform
A transform may be applied to any QueryCursor, AggregationCursor, or top-level BulkOperation.
script.arguments:
cursorOptions { Object } The original cursor options (object, skipAcl, paths, pipeline, etc.)
There are a few ways to define a transform for a cursor.
A series of implicitly usable transform methods:
An implicit transform class:
A reference to a Library Script that exports an annotated transform class:
An inline export definition:
Bulk Operations
Transformations can be applied to top-level bulk operations only.
Examples
CSV View Transform
Inline CSV Transform
Account Policy
This example triggers the policy on any individual account read
Last updated
Was this helpful?