moleculer-db icon indicating copy to clipboard operation
moleculer-db copied to clipboard

add multi tenant support

Open mailaneel opened this issue 7 years ago • 2 comments

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

mailaneel avatar Jan 20 '18 10:01 mailaneel

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?

chrispynegar avatar Sep 06 '19 11:09 chrispynegar

Any update on this issue? I have similar requirement- to dynamically connect to separate databases, based on some request parameters.

yogesh-rs avatar Aug 05 '20 06:08 yogesh-rs