Loopback Multitenancy Database Swap
Asked Answered
O

1

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!

Obnoxious answered 23/4, 2015 at 1:25 Comment(2)
What is tenant in your code above?Kestrel
Did you ever find a solution?Physiognomy
R
1

I make this project. I'ts an alternative.

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

Remunerative answered 24/7, 2015 at 20:50 Comment(1)
Paulo i have tested your project with this: ' curl --data "username=paulomcnally1&message=Hello world" 127.0.0.1:4000/demo1/tweets & curl --data "username=paulomcnally2&message=Hello world" 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?Reviewer

© 2022 - 2024 — McMap. All rights reserved.