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!
tenant
in your code above? – Kestrel