3

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!

Mohamed Rashiq
  • 322
  • 1
  • 3
  • 12

1 Answers1

2

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

Pavel Tiunov
  • 1,163
  • 6
  • 8
  • Can this be dynamic, on a per quest basis? I want to be able to update the schema on the fly and my (limited) understanding of this code is that will not be supported. – oden Jan 22 '20 at 11:54
  • 1
    @oden It actually can. Please check https://cube.dev/docs/multitenancy-setup#multiple-schema-and-drivers for that. You can have one schema per tenant. And tenants can as granular as you want it to be. – Pavel Tiunov Jan 24 '20 at 05:31