-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial PR for allowing multi tenant strategies #5
Conversation
…ter, so we can support multiple adapters i.e per tenant all tests passing, need to add tests for tenantStrategy, this is initial design for tenantStrategy to see if it works and also to get feedback
Approach 2: This can be instead of |
…rvices. This allows for use cases where we need to create models dynamically @see moleculerjs#5
…rvices. This allows for use cases where we need to create models dynamically @see moleculerjs#5
@mailaneel I am not familiar with tenants. Could you show me a simple use case why it is good and need to implement? Thank you! |
for my use case I want to use separate database for each each business that subscribes to my product. I am calling each business here as tenant and they will have a separate database. In my code I want to be able to connect to these databases dynamically based on some parameters that are available in my context. Example JWT Token {
"name": "ABC company",
"tenantId": "123456"
} Now in my middleware I will set this data into context.meta key so I can access this information in all And based on the data available in |
Thanks. And how does |
tenantKeys work similar to shardKey in mongodb or mongoose lets say, we have configured tenantkeys as [“tenantId”] this is useful when all tenants data is stored in same collection where every doc will have tenantId We will need a getter, to get all tenantKeys values from ctx.. this should be provides in tenantStrategy as a function if tenantKeys are not present in query throw Error If we get a query for userId:1 -> this will be converted to userId:1, tenantId:12345 before passing to adapter |
the other way to think about it is just allow to get adapter based on context and let user deal with implementation details i.e whether to use tenantkeys or to use multiple databases.. |
Sorry, I need more time to consider it. Currently, I'm working on other things. |
I feel this is a valuable addition to the db adapters. As it stands out of the box, the db adapters are considered single app adapters (meaning that the adapters are primarily for apps that would have single or multiple dbs for the entire app to consume with user data and content mingling with other user data in the same collection relational to it). By adding multi tenant capabilities a developer is able to architect the app segregating user data by user and securing it. To give a better idea around multi tenant apps, user data is typically contained in one of 3 ways in MongoDB:
the preference would be segregation on the database level. Aside from security, this also enables the ability of no impact to other tenants of the system when a tenant requires recovery of lost data. Since the backup & disaster recovery strategy would consist of backups done per tenant database, if a tenant requires a restore of their data nothing would be altered or changed in any other tenant database. |
#3
@icebob This is my initial PR to get feedback on if I am right path or not, Right now all tests are passing.
Big change you will see is changing from
this.adapter
tothis.getAdapter(ctx)
Example of how I am thinking of using tenantStrategy
Pending Tasks: