一般官方解决的动态编译使用asyncModule 但是asyncModule 有一些问题(对于join 处理有问题,这个是一个bug)
const { transformDimensions, transformJoins, transformMeasures, convertStringPropToFunction } = require("./utils")
asyncModule(async () => {
const dynamicCubes = [
{
"name": "payments",
"sqlAlias": `payments`,
"sql": "SELECT * FROM public.\"payments\"",
"dataSource": "default",
"joins": {
"demoapp": {
"relationship": `belongsTo`,
"sql": "payments.__id = demoapp.__id"
}
},
"measures": {
"count": {
"sql": "COUNT(DISTINCT \"payments\".__id)",
"type": "number"
},
"total": {
"sql": "\"payments\".\"total\"",
"type": "sum"
}
},
"dimensions": {
"__id": {
"sql": "__id",
"type": "string",
"primaryKey": true
},
"name": {
"sql": "\"payments\".\"name\"",
"type": "string"
}
}
},
{
"name": "demoapp",
"sqlAlias": `demoapp`,
"sql": "SELECT * FROM public.\"demoapp\"",
"dataSource": "default",
"joins": {
"demoapp": {
"relationship": `belongsTo`,
"sql": "payments.__id = demoapp.__id"
}
},
"measures": {
"count": {
"sql": "COUNT(DISTINCT \"payments\".__id)",
"type": "number"
}
},
"dimensions": {
"__id": {
"sql": "__id",
"type": "string",
"primaryKey": true
},
"name": {
"sql": "\"payments\".\"name\"",
"type": "string"
}
}
}
]
dynamicCubes.forEach((dynamicCube) => {
const dimensions = transformDimensions(dynamicCube.dimensions);
const measures = transformMeasures(dynamicCube.measures);
const joins = transformJoins(dynamicCube.joins);
cube(dynamicCube.name, {
sql: dynamicCube.sql,
dimensions,
measures,
joins
});
});
}
)
module.exports = {
sql: ()=> `SELECT * FROM sf100000.call_center`,
joins: {
},
measures: {
count2: {
type: `count`
}
},
dimensions: {
ccCallCenterId: {
sql: ()=>`cc_call_center_id`,
type: `string`
},
ccName: {
sql: ()=>`cc_name`,
type: `string`
}
},
dataSource: `default`
};
https://cube.dev/docs/cube#parameters-sql-alias
https://cube.dev/docs/schema/dynamic-schema-creation
https://github.com/cube-js/cube.js/issues/1880
原文:https://www.cnblogs.com/rongfengliang/p/14664568.html