Meteor-Community-Packages / meteor-roles

Authorization package for Meteor, compatible with built-in accounts packages
http://meteor-community-packages.github.io/meteor-roles/
MIT License
920 stars 164 forks source link

Best way to query users by roles/scopes #312

Closed Floriferous closed 4 years ago

Floriferous commented 4 years ago

Since v2, roles are no longer stored on the user documents, but in new collections.

There is a Roles.getUsersInRole query, but it says in the docs:

NOTE: This is an expensive query; it performs a full collection scan on the users collection since there is no index set on the 'roles' field. This is by design as most queries will specify an _id so the _id index is used automatically.

We're hesitating to port over to v2 and v3 as we query users by role a lot, and it's easy to set an index on the roles key.

What are you doing now to perform those queries? For us roles don't change much, so is there a good config of herteby:denormalize to get the roles cached on the user profiles? Am I missing some other simple query that I can run without having to query 2 or 3 collections every time (users, roles, roleAssignments)?

mfen commented 4 years ago

Are you sure that issue doesn't pertain only to v1? I see that disclaimer in the v1 documentation for that method, but not v2/v3.

While I haven't used or looked much into v3 yet, in v2 it queries by the roles._id or roles._id + roles.scope fields, which seem to be indexed.

Floriferous commented 4 years ago

I assumed a link to the docs would point me to the latest version! I can’t seem to find the docs for v3, it always says v1.2.14 on the website?

SimonSimCity commented 4 years ago

Sorry for the confusion here. The documentation mentioned in the readme is terribly outdated and should be removed or updated soon (see also https://github.com/Meteor-Community-Packages/meteor-roles/issues/284).

The best as of now, I suppose, is to read the place the documentation is generated from - which is the source code. There's nothing more to it - really 😊

The function you're looking it is described here (excerpt is fixed on a commit and might get outdated when future versions are published): https://github.com/Meteor-Community-Packages/meteor-roles/blob/32c230ded9cb92b27a3485d143cabf2732fef186/roles/roles_common.js#L798-L821

You can answer the question yourself by looking at the code. It currently has a different complexity, and it might hurt you at another end. The amount of roles you have is not important anymore, but rather how many assignments you have (user assigned to a role) which might slow down your query. Limiting this, so you have as many assignments as you have users will help you getting down the numbers here.

If you need this for a big database, please consider writing your own extension to this library where you define the query as an aggregation

I'm just managing this project in my spare time. If you find something very difficult, please take it as a possibility to dive in, write an outline about it and share it with the community here. It will help everyone coming after you, and spare me a lot of time.

I don't know the package herteby:denormalize, so I am afraid I can't offer any support here.