Different schema cubes for different tenants

Hi everyone, i found a way to load different cubes based on the tenant making the request and i tought it might be useful for someone else.
Here is my cube.js config file

const FileRepository = require(`@cubejs-backend/server-core/core/FileRepository`);

module.exports = {
  contextToAppId: ({ securityContext }) =>
    `CUBEJS_APP_${securityContext.tenant}`,
  scheduledRefreshContexts: async () => [
    {
      securityContext:{
        tenant: `dev`
      },
      securityContext:{
        tenant: `prod`
      }
    }
  ],
  repositoryFactory: ({ securityContext }) =>
    new FileRepository(`schema/${securityContext.tenant}`)
}

As you can see, i use the repositoryFactory to change the directory of the schema cubes based on the value tenant inside the securityContext.
So, in this case with only two tenants, the directories would be <cubejs-conf>/schema/dev and <cubejs-conf>/schema/prod

P.S. Many thanks to @antonrychkov and @Ray for helping me and suggesting this forum recipe

2 Likes