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!
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.
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.
I make this project. I'ts an alternative.
https://github.com/paulomcnally/loopback-example-multitenant
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With