0

Given the following Cube, I can use a function to create dynamic measures.

cube(`Creatives`, {
  sql: `SELECT * FROM public.creatives`,

  joins: {
    Events: {
      relationship: `hasMany`,
      sql: `${Events}.creative_id = ${CUBE}.id`,
    },
  },

  measures: {
    ...['impression'].reduce((all, event) => {
      return {
        ...all,
        [`Total_${event}_events`]: {
          type: `count`,
          title: `Total ${event} events`,
          filters: [
            {
              sql: `${Events.type} = '${event}'`,
            },
          ],
        },
      };
    }, {}),

  },

  dimensions: {
    ...
  },
});

But when I try to move the reducer to a function, similar to the examples I get

ReferenceEvents is not defined

Which obviously is because there's no variable within the scope of my function, where previously Cubes interpolation of the string was replacing it for me.

How can I get access to other Cubes in functions similar to the below? I.e. get (or pass in Events to createTotalEventsMeasure()

const createTotalEventsMeasure = (event) => ({
  [`Total_${event}_events`]: {
    type: `count`,
    title: `Total ${event} events`,
    filters: [
      {
        sql: (CUBE) => `${Events.type} = '${event}'`,
      },
    ],
  },
});

cube(`Creatives`, {
  sql: `SELECT * FROM public.creatives`,

  joins: {
    Events: {
      relationship: `hasMany`,
      sql: `${Events}.creative_id = ${CUBE}.id`,
    },
  },

  measures: {
    ...['impression'].reduce(
      (all, event) => ({
        ...all,
        ...createTotalEventsMeasure(event),
      }),
      {}
    ),
  },

  dimensions: {
    ...
  },
});
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108

0 Answers0