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   荣锋亮  阅读(101)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-04-14 easy-rules spring boot集成使用二
2020-04-14 easy-rules spring boot集成使用
2019-04-14 edgedb 基本试用
2018-04-14 osquery简单试用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示