This functionality only works with data models written in JavaScript, not YAML.For similar functionality in YAML, see Dynamic data models with Jinja and Python.
asyncModule() function only available in the
execution environment. asyncModule() allows registering an
async function to be executed at the end of the data model compile phase so
additional definitions can be added. This is often useful in situations where
data model properties can be dynamically updated through an API, for example.
When creating data models via asyncModule(), it is important to be aware of
the following differences compared to statically defined ones with cube():
- The
sqlanddrill_membersproperties for both dimensions and measures must be of type() => stringand() => string[]accordingly
Generation
In the following example, we retrieve a JSON object representing all our cubes usingfetch(), transform some of the properties to be functions that return a
string, and then finally use the cube() global function to
generate data models from that data:
Usage with schema_version
It is also useful to be able to recompile the data model when there are changes
in the underlying input data. For this purpose, the schema_version
value in the cube.js configuration options can
be specified as an asynchronous function:
Usage with COMPILE_CONTEXT
TheCOMPILE_CONTEXT global object can also be used in conjunction with async
data model creation to allow for multi-tenant deployments of Cube.
In an example scenario where all tenants share the same cube, but see different
dimensions and measures, you could do the following:
Usage with data_source
When using multiple databases, you’ll need to ensure you set thedata_source property for any asynchronously-created
data models, as well as ensuring the corresponding database drivers are set up with
driverFactory() in your cube.js configuration
file.
For an example scenario where data models may use either MySQL or Postgres
databases, you could do the following: