cube.js 动态编译问题

一般官方解决的动态编译使用asyncModule 但是asyncModule 有一些问题(对于join 处理有问题,这个是一个bug)

解决方法

  • 使用repositoryFactory
    不是很难,自己实现一个基于文件或者数据api的schemaFactory
  • 使用sqlAlias
    好处是cube 的名称就可以不用使用cube 动态编译的name了,join 的时候直接使用就可以了,是一个比较简单的方法
 
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
    });
  });
}
)
  • 使用require 引用schema js 文件模式
    可以导出cube.js的schema 为一个cmd 的模块就可以了
 
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

posted on 2021-04-15 21:32  荣锋亮  阅读(125)  评论(0编辑  收藏  举报

导航