cube.js playground暴露的几个不错的功能

通过源码我们查看playground 提供的功能就会看到自己很不错的直接可以拿来使用的工具
主要代码在packages/cubejs-server-core/src/core/DevServer.ts

获取数据库schema 信息

代码如下,属于一个内部方法,可以基于driver获取数据库的schema

 
app.get('/playground/db-schema', catchErrors(async (req, res) => {
      this.cubejsServer.event('Dev Server DB Schema Load');
      const driver = await this.cubejsServer.getDriver({
        dataSource: req.body.dataSource || 'default',
        authInfo: null,
        securityContext: null,
        requestId: getRequestIdFromRequest(req),
      });
      const tablesSchema = await driver.tablesSchema();
      this.cubejsServer.event('Dev Server DB Schema Load Success');
      if (Object.keys(tablesSchema || {}).length === 0) {
        this.cubejsServer.event('Dev Server DB Schema Load Empty');
      }
      res.json({ tablesSchema });
    }));

当然我们也可以自己编写,代码不难

获取定义的schema

核心是利用了FileRepository的能力,我们可以自己扩展(基于s3的。。。。)

  app.get('/playground/files', catchErrors(async (req, res) => {
      this.cubejsServer.event('Dev Server Files Load');
      const files = await this.cubejsServer.repository.dataSchemaFiles();
      res.json({
        files: files.map(f => ({
          ...f,
          absPath: path.resolve(path.join(this.cubejsServer.repository.localPath(), f.fileName))
        }))
      });
    }));

schema 生成

此处利用了schema-compiler 包内部的工具,当然我们也可以利用此工具包,进行schema 的生成

 app.post('/playground/generate-schema', catchErrors(async (req, res) => {
      this.cubejsServer.event('Dev Server Generate Schema');
      if (!req.body) {
        throw new Error('Your express app config is missing body-parser middleware. Typical config can look like: `app.use(bodyParser.json({ limit: \'50mb\' }));`');
      }
      if (!req.body.tables) {
        throw new Error('You have to select at least one table');
      }
      const driver = await this.cubejsServer.getDriver({
        dataSource: req.body.dataSource || 'default',
        authInfo: null,
        securityContext: null,
        requestId: getRequestIdFromRequest(req),
      });
      const tablesSchema = req.body.tablesSchema || (await driver.tablesSchema());
      const ScaffoldingTemplate = require('@cubejs-backend/schema-compiler/scaffolding/ScaffoldingTemplate');
      const scaffoldingTemplate = new ScaffoldingTemplate(tablesSchema, driver);
      const files = scaffoldingTemplate.generateFilesByTableNames(req.body.tables);
      const schemaPath = options.schemaPath || 'schema';
      await Promise.all(files.map(file => fs.writeFile(path.join(schemaPath, file.fileName), file.content)));
      res.json({ files });
    }));

参考资料

https://github.com/cube-js/cube.js/blob/master/packages/cubejs-server-core/src/core/DevServer.ts
https://cube.dev/docs/@cubejs-backend-server
https://cube.dev/docs/@cubejs-backend-server-core
https://cube.dev/docs/config

posted on   荣锋亮  阅读(187)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-02-02 golang 条件编译
2019-02-02 cget cmake 包管理工具
2019-02-02 buckaroo 试用
2019-02-02 buckaroo 去中心化的c++包管理工具
2018-02-02 coredns 编译模式添加插件

导航

< 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
点击右上角即可分享
微信分享提示