moleculer-db
moleculer-db copied to clipboard
add multi tenant support
Allow support of multi tenants
In settings
{
// this could be specific to the adapters
tenantStrategy: {
// if these keys are not preset we throw validation error
// and based on tenant strategy we might have to add these to every query we make to db
tenantKeys: ['accountId'], // or (config, params) => ['accountId']
// for mongo adapter return new db
getDB: (config, params) => config.client.db(`${config.collection}-${params.accountId}`),
// for mongo adapter return new collection
getCollection: (config, params) => config.db.collection(`${config.collection}-${params.accountId}`),
// for mongoose
getModel: (config, params) => config.model,
// if exists will override the adapter find or update the params and call adapter's find
find: () => null
// ... other methods
}
}
And tenant strategies can be implemented by the adapters itself, example incase of mongoose, whether to use that as shardKey or use a separate collection or use a separate db
Was there a solution or work around for this? If not, is there any guidance on how best to implement multi-database support within a service?
Any update on this issue? I have similar requirement- to dynamically connect to separate databases, based on some request parameters.