Multi-user network database sharing

I would like to implement multithreading in my loopback application. Right now I'm trying to use middleware to override my data sources to point to different databases on my mongodb server for each request based on the request domain. The code runs, but it does not seem to actually change the data source. Instead, it always uses the one that is defined in my dysasources.json resources.

Right now, this is what I'm doing. All of my models reference "my_db" and I would 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 dumb way to do a multi-user contract?

Thanks!

+6
source share
1 answer

I am doing this project. I have an alternative.

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

+1
source

Source: https://habr.com/ru/post/985736/


All Articles