6

I'd like to implement multitenancy in my loopback app. Right now, I'm trying to use middleware to redefine my datasources to point to different databases on my mongodb server for each request, based on the domain the request. The code runs, but it doesn't seem to be actually changing the datasource. Instead, it always uses the one defined in my datasources.json.

Right now, this is what I am doing. All of my models reference "my_db" and I'd like to have one database on my mongo server for each tenant.

var dataSourceObj = {
        my_db:{
          url: process.env.MONGOLAB_URI,
          connector: "mongodb",
          name: "my_db",
          database: tenant
        }
      }

      Object.keys(dataSourceObj).forEach(function(dataSource) {

        app.dataSources[dataSource].adapter.settings = dataSourceObj[dataSource];
        app.dataSources[dataSource].adapter.clientConfig = dataSourceObj[dataSource];
        app.dataSources[dataSource].settings = dataSourceObj[dataSource];
        app.dataSources[dataSource].connector.settings = dataSourceObj[dataSource];
        app.dataSources[dataSource].connector.clientConfig = dataSourceObj[dataSource];

      });

Does anyone have any ideas? Is this a silly way to do multi-tenancy?

Thanks!

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
ishmandoo
  • 413
  • 5
  • 10

1 Answers1

1

I make this project. I'ts an alternative.

https://github.com/paulomcnally/loopback-example-multitenant

McNally Paulo
  • 192
  • 1
  • 8
  • Paulo i have tested your project with this: ' curl --data "username=paulomcnally1&message=Hello world" http://127.0.0.1:4000/demo1/tweets & curl --data "username=paulomcnally2&message=Hello world" http://127.0.0.1:4000/demo2/tweets & '. Both tweets where writed on demo2 database. Is there any problem with concurrency or i'm doing something wrong? – Musma Jun 29 '16 at 13:12