cube.js 通过prepareCompiler获取 meta

meta 在cube.js 还是比较重要的(尽管当前没有太多的使用介绍),但是我们基于meta 可以进行
代码生成,进行数据分析处理(基于元数据,方便开发数据分析应用),playground 就是集成了次
进行界面开发

参考代码

  • app.js
 
const {prepareCompiler} = require("@cubejs-backend/schema-compiler")
module.exports = {
    preCompiler:function(content,options){
       return prepareCompiler({
            localPath: () => __dirname,
            dataSchemaFiles: () => Promise.resolve([
              { fileName: 'main.js', content }
            ])
          }, { adapter: 'postgres', ...options });     
    }
}
  • index.js
const app = require("./app")
const fs = require("fs")
const {PostgresQuery,ParamAllocator,compile} = require("@cubejs-backend/schema-compiler")
let {compiler,joinGraph,cubeEvaluator,metaTransformer} = app.preCompiler(fs.readFileSync("./app-schema.js").toString("utf-8"));
(async function(){
    await compiler.compile();
    console.log(JSON.stringify(metaTransformer.cubes))  // 此处获取meta
})()
  • app-schema.js
cube(`CallCenter`, {
    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`
      },
 
      ccClass: {
        sql: `cc_class`,
        type: `string`
      },
 
      ccHours: {
        sql: `cc_hours`,
        type: `string`
      },
 
      ccManager: {
        sql: `cc_manager`,
        type: `string`
      },
 
      ccMktClass: {
        sql: `cc_mkt_class`,
        type: `string`
      },
 
      ccMktDesc: {
        sql: `cc_mkt_desc`,
        type: `string`
      },
 
      ccMarketManager: {
        sql: `cc_market_manager`,
        type: `string`
      },
 
      ccDivisionName: {
        sql: `cc_division_name`,
        type: `string`
      },
 
      ccCompanyName: {
        sql: `cc_company_name`,
        type: `string`
      },
 
      ccStreetNumber: {
        sql: `cc_street_number`,
        type: `string`
      },
 
      ccStreetName: {
        sql: `cc_street_name`,
        type: `string`
      },
 
      ccStreetType: {
        sql: `cc_street_type`,
        type: `string`
      },
 
      ccSuiteNumber: {
        sql: `cc_suite_number`,
        type: `string`
      },
 
      ccCity: {
        sql: `cc_city`,
        type: `string`
      },
 
      ccCounty: {
        sql: `cc_county`,
        type: `string`
      },
 
      ccState: {
        sql: `cc_state`,
        type: `string`
      },
 
      ccZip: {
        sql: `cc_zip`,
        type: `string`
      },
 
      ccCountry: {
        sql: `cc_country`,
        type: `string`
      },
 
      ccRecStartDate: {
        sql: `cc_rec_start_date`,
        type: `time`
      },
 
      ccRecEndDate: {
        sql: `cc_rec_end_date`,
        type: `time`
      }
    },
 
    dataSource: `default`
  });
  • 效果

 

 

说明

官方关于meta 的获取基本原理与此类似

参考资料

https://www.cnblogs.com/rongfengliang/p/14641897.html
https://github.com/rongfengliang/cubejs-prepareCompiler-learning

posted on 2021-04-14 22:55  荣锋亮  阅读(100)  评论(0编辑  收藏  举报

导航