Skip to content
Advertisement

How to get data based on username in cube js

DEFINITION

I have connected my cube js backend to mongodb. I am storing documents inside my mongo db database in a following way.

THIS IS HOW MY DOCUMENT LOOKS LIKE IN MONGO DB

{
  anonymousId: "bdee014b-09e1-4d79-bcf1-c3cc91cd4101"
  user: "abc"
  url: "/index"
  eventType: "pageView"
  date: "22-08-2020"
  time: "20:33:9"
  data: "some data"
}

So now let us assume that I have 10 documents in my mongodb collection in which 5 documents belong to user: “abc” and 5 documents belong to user: “xyz”.

HOW I GET THE DATA RIGHT NOW IN THE BROWSER CONSOLE IS SHOWN BELOW

{
 annotation: {measures: {,…},…}
 data: [{Events.user: "xyz",…}, {Events.user: "xyz",…}, {Events.user: "xyz",…},…]
 0: {Events.user: "xyz",…}
 1: {Events.user: "xyz",…}
 2: {Events.user: "xyz",…}
 3: {Events.user: "abc",…}
 4: {Events.user: "abc",…}
 5: {Events.user: "xyz",…}
 lastRefreshTime: "2020-08-22T15:23:53.547Z"
 query: {measures: ["Events.pageView", "Events.buttonClick"], dimensions: ["Events.user", 
 "Events.allData"],…}
 refreshKeyValues: [[{unix_timestamp(): 1598109833}]]
 usedPreAggregations: {}
}

Here you can see in the data array that i get back as a response from the cube js backend has 6 objects in 4 of them belong to user XYZ and 2 of them belong to ABC.

MY CUBE.JS SCHEMA

cube(`Events`, {

sql: `SELECT * FROM test.events`,

refreshKey: {
    sql: `SELECT UNIX_TIMESTAMP()`
},

measures: {
    count: {
        type: `count`
    },

    pageView: {
        type: `count`,
        filters: [
            { sql: `${eventType} = 'pageView'` }
        ]
    },

    buttonClick: {
        type: `count`,
        filters: [
            { sql: `${eventType} = 'buttonClicked'` }
        ]
    }
},

dimensions: {

    anonymousId: {
        sql: `anonymousId`,
        type: `string`
    },

    url: {
        sql: `url`,
        type: `string`
    },

    user: {
        sql: `user`,
        type: `string`
    },

    allData: {
        sql: `data`,
        type: `string`
    },

    eventType: {
        sql: `eventType`,
        type: `string`
    },

    referrer: {
        sql: `referrer`,
        type: `string`
    },

 }

});

QUESTION

How can i segregate or we can say group data depending upon the user. What i mean is that i want the data to return two arrays for two different users. Lets say if xyz has 2 documents an array of 2 documents for that very user should be returned and abc has 4 documents an array of 4 documents should be returned.

They should not be returned combined. abc’s data should be returned seperately and xyz’s data should be returned seperately.

2 USERS SHOULD MAINTAIN 2 DIFFERENT ARRAYS

EXPECTATION

A trick or a way by which we write the schema in achieving this behavior,

Advertisement

Answer

You can use User Context or queryTransformer or Multitenant Compile Context, these three functions allow you to implement data access based on authorization data.

queryTransformer is a little more complicated, but it will allow you to customize access control more flexibly. It is better suited for complex cases.

Advertisement