Execution Environment (JavaScript)
Cube Data Model Compiler uses Node.js VM to execute data model compiler code. It gives required flexibility allowing transpiling data model files before they get executed, storing data models in external databases and executing untrusted code in a safe manner. Cube data model JavaScript is standard JavaScript supported by Node.js starting in version 8 with the following exceptions.Require
Being executed in VM, data model JavaScript code doesn’t have access to Node.js require directly. Insteadrequire() is implemented by Data
Model Compiler to provide access to other data model files and to regular
Node.js modules. Besides that, the data model require() can resolve Cube
packages such as Funnels unlike standard Node.js require().
Node.js globals (process.env, console.log and others)
Data model JavaScript code doesn’t have access to any standard Node.js globals likeprocess or console. In order to access process.env, utility functions
can be added outside the model/ directory:
tablePrefix.js:
console.log
Data models cannot accessconsole.log due to a separate VM
instance that runs it. Suppose you find yourself writing complex
logic for SQL generation that depends on a lot of external input. In that case,
you probably want to introduce a helper service outside of the data model
directory that you can debug as usual Node.js code.
Cube globals (cube and others)
Cube definescube(), context() and asyncModule() global variable functions
in order to provide API for data model configuration which aren’t normally
accessible outside of a Cube data model.
Import / Export
Data model JavaScript files are transpiled to convert ES6import and export
expressions to corresponding Node.js calls. In fact import is routed to
Require method.
export can be used to define named exports as well as default ones:
constants.js:
import into the cube, wherever needed:
Users.js:
asyncModule
Data models can be externally stored and retrieved through an asynchronous operation using theasyncModule(). For more information, consult the dynamic
data model creation.
Context symbols transpile
Cube uses a custom transpiler to optimize boilerplate code around referencing cubes and cube members. There are reserved property names insidecube
definition that undergo reference resolve transpiling process:
sqlmeasuresdimensionssegmentstime_dimensiondrill_memberscontext_members
cube and context definitions are transpiled
to functions with resolved arguments. For example:
ratio outside of the
cube, you would define it as: