Ofir Assif
Ofir Assif

Reputation: 1

Can I set a dynamic database as a source for a dataset?

I have multiple databases which have the same exact schema (different data on each one, database per customer of a SaaS platform).

I would like to create a Dashboard (with charts, datasets) which could be populated by the permissions of the logged in user.

This means the dashboard will query the data from a specified source database, instead of a pre-defined one.

The premise is basically to de-couple a chart / dataset from a database and allow it to be parametrised.

Upvotes: 0

Views: 2196

Answers (1)

anon
anon

Reputation:

This is a case that is not really supported by Superset, but there's one workaround that I can think of that might work: you can define a DB_CONNECTION_MUTATOR in your superset_config.py that routes to a different database depending on the user.

In your superset_config.py, add this function:

def DB_CONNECTION_MUTATOR(uri, params, username, security_manager, source):
    user = security_manager.find_user(username=username)
    if url.database = "db_name" and user and user.email.endswith("@examplea.com"):
        uri.host = "host-for-examplea.com"
    return uri, params

In the function above we're changing the host of the SQLAlchemy URL to host-for-examplea.com if the user has an email ending in @examplea.com.

To make it work, create a default database (which we called db_name in this example), and create all the charts/dashboards based on it. Then, users should be redirected to specific databases by the DB_CONNECTION_MUTATOR.

One serious problem that might happen is with caching, though. You should make sure that all caches are disabled to prevent users from seeing data from other databases.

Upvotes: 3

Related Questions