Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback Multitenancy Database Swap

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!

like image 903
ishmandoo Avatar asked Apr 23 '15 01:04

ishmandoo


People also ask

What is multitenancy in database?

The term tenancy model refers to how tenants' stored data is organized: Single-tenancy: Each database stores data from only one tenant. Multi-tenancy: Each database stores data from multiple separate tenants (with mechanisms to protect data privacy). Hybrid tenancy models are also available.

What is multi tenant database architecture?

A multi-tenant OpenEdge database is a shared database with a shared schema and logically and physically isolated data storage on a per tenant or group basis. Each object (table, index, LOB) is stored in a partition. Partitions keep data physically separate for each tenant.


1 Answers

I make this project. I'ts an alternative.

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

like image 82
McNally Paulo Avatar answered Oct 24 '22 10:10

McNally Paulo