cube-js / cube-js/cube

How to use Pre-aggregation when creating dynamic schema

Open
#6,422 2 comments 0 reactions 1 assignee Claimed by @hassankhan View on GitHub
docs question
Dominant language
Rust
Stars
20.8k
Forks
2.1k
Avg merge
1d 2h
Merged PRs (30d)
181

Description

**Problem**

I create dynamic cubes according to this tutorial: [https://cube.dev/docs/schema/advanced/dynamic-schema-creation]
And I have question about how to set pre-aggregation field as the measures and the dimensions in cubes are not known in advance. If I just simply fill the pre-aggregation with a string field, the pre-aggregation will not take effect. Here is the example code below:

**Related Cube.js schema**
```javascript

asyncModule(async () => {
const {
securityContext: {cubeId},
} = COMPILE_CONTEXT;

const dynamicCubes = await (
await fetch(`http://` + serverAddress + `/api/v1/cubes/${cubeId}`)
).json();

if (dynamicCubes.forEach) {

dynamicCubes.forEach(dynamicCube => {
dynamicCube.id = 'ns_' + dynamicCube.id.replace(/-/g, '_')
})

dynamicCubes?.forEach(dynamicCube => {
const dimensions = transformDimensions(dynamicCube.dimensions);
const measures = transformMeasures(dynamicCube.measures);
cube(dynamicCube.id, {
name: dynamicCube.name,
sql: dynamicCube.sql,
title: dynamicCube.title,
dataSource: dynamicCube.dataSource,
dimensions,
measures,
preAggregations:{
main: {
type: 'rollup',
measures: Object.keys(measures).map(key => `CUBE.${key}`),
dimensions: Object.keys(dimensions).map(key => `CUBE.${key}`),
granularity: `day`,
}
}
});
});
} else {
logger.log("error", "dynamicCubes is null, detail:" + JSON.stringify(dynamicCubes))
}
})```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.