Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically generate schema for cube.js?

I have been working on a project to generate configurable dashboards. so i have to generate schema dynamically based on an api request. is there any way to do that?

it will be very helpful if there is any working example!

like image 543
Mohamed Rashiq Avatar asked Nov 27 '25 23:11

Mohamed Rashiq


1 Answers

There's an asyncModule function for this scenario. You can check example below:

const fetch = require('node-fetch');
const Funnels = require('Funnels');

asyncModule(async () => {
  const funnels = await (await fetch('http://your-api-endpoint/funnels')).json();

  class Funnel {
    constructor({ title, steps }) {
      this.title = title;
      this.steps = steps;
    }

    get transformedSteps() {
      return Object.keys(this.steps).map((key, index) => {
        const value = this.steps[key];
        let where = null
        if (value[0] === PAGE_VIEW_EVENT) {
          if (value.length === 1) {
            where = `event = '${value[0]}'`
          } else {
            where = `event = '${value[0]}' AND page_title = '${value[1]}'`
          }
        } else {
          where = `event = 'se' AND se_category = '${value[0]}' AND se_action = '${value[1]}'`
        }

        return {
          name: key,
          eventsView: {
            sql: () => `select * from (${eventsSQl}) WHERE ${where}`
          },
          timeToConvert: index > 0 ? '30 day' : null
        }
      });
    }

    get config() {
      return {
        userId: {
          sql: () => `user_id`
        },
        time: {
          sql: () => `time`
        },
        steps: this.transformedSteps
      }
    }
  }

  funnels.forEach((funnel) => {
    const funnelObject = new Funnel(funnel);
    cube(funnelObject.title, {
      extends: Funnels.eventFunnel(funnelObject.config),
      preAggregations: {
        main: {
          type: `originalSql`,
        }
      }
    });
  });
})

More info: https://cube.dev/docs/schema-execution-environment#async-module

like image 200
Pavel Tiunov Avatar answered Dec 02 '25 03:12

Pavel Tiunov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!