Multiple Data Sources

Cube supports using multiple data sources, allowing the use of different databases for different cubes.

To configure multiple data sources, first explicitly declare a data source in some cubes by adding a dataSource property:

cube('Orders', {
  ...,

  dataSource: 'default',
});

cube('OrdersFromOtherDataSource', {
  ...,

  dataSource: 'datasource1',
});

Next, Cube must be configured so that it knows which database to use for each data source; first declare the data sources using the CUBEJS_DATASOURCES environment variable, then use decorated environment variables to configure each data source:

CUBEJS_DATASOURCES=default,datasource1
CUBEJS_DS_default_DB_TYPE=postgres
CUBEJS_DS_default_DB_NAME=ecom
CUBEJS_DS_default_DB_HOST=localhost
CUBEJS_DS_datasource1_DB_TYPE=postgres
CUBEJS_DS_datasource1_DB_NAME=ecom
CUBEJS_DS_datasource1_DB_HOST=remotehost

Decorated environment variables

Cube allows database-specific environment variables to be decorated with a data source name:

CUBEJS_[DS_<DATASOURCE>_]<ENV_VAR>

For example, using the default data source, CUBEJS_DB_TYPE could be decorated as:

CUBEJS_DS_default_DB_TYPE=postgres

For more information on environment variables that support decoration, check the Environment Variables reference, or a database-specific page if required.

For more complex scenarios involving multitenancy, Cube can also be configured to dynamically lookup data sources. For more information, see the Multitenancy page.

Did you find this page useful?