node-mongo-tenant
node-mongo-tenant copied to clipboard
Use Plugin Multiple Times Per Schema
Hi, I've been using this plugin for a long time now but I have an interesting use-case that I'm trying to solve without actually forking this library. Basically, we already use the plugin to add a tenantId
to every mongoose model in our application, but we want to take it one step further. I wanted to add another field to the same model where I could chain the accessorMethod
's and continue to narrow down the search. For example:
User
{
name: "Aaron",
tenantId: "A",
tenantId2: "1"
}
{
name: "Alrik",
tenantId: "A",
tenantId2: "2"
}
{
name: "Someone",
tenantId: "B",
tenantId2: "2"
}
Basically as it stands right now I have to use User.byTenant("A")
to get the model that I need for querying. But what I'd like to be able to do is something like User.byTenant("A").byTenant2("1")
which would just return the Aaron user. I know it's not a typical use-case but just wondered if there would be an easy way to implement this. Otherwise if I end up forking it, do you have any recommendations?
@amerryma so you are looking for a compound tenant id, did i understand you right?
@alrik Yes, that appears to be what I'm looking for. Any thoughts?
@amerryma So your schema has two or more fields referencing different entities from the same collection? Or do the fields reference different collections? (multiple values from the same dimension or multiple values from a different dimension each?)
Maybe you can explain your use case a bit more in depth to make it a bit clearer what you want to achieve.