Skip to content
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

So many queries?! #26

Open
twigs67 opened this issue Nov 27, 2018 · 8 comments
Open

So many queries?! #26

twigs67 opened this issue Nov 27, 2018 · 8 comments

Comments

@twigs67
Copy link

twigs67 commented Nov 27, 2018

Hello,

I'm not sure if I'm doing something wrong or this is by design, but when the application initially loads the system is making over 70+ queries on permissions/permission_role/role tables.

  1. Is this by design?
  2. If so, is there any way to limit the number of queries or make them more efficient?

Thanks

@twigs67
Copy link
Author

twigs67 commented Nov 27, 2018

I figured out why there are so many queries...

//Role.js
  async getPermissions () {
    const permissions = await this.permissions().fetch()
    return permissions.rows.map(({ slug }) => slug)
  }

If a user has 40+ permissions, this is hitting the database 40+ times. Perhaps a more efficient route would be to make use of Adonis.pivotTable()?

@hadihallak
Copy link

you can check my fork where I did some optimizations to how the plugin query permissions and roles
https://github.com/hadihallak/adonis-acl

i've changed it so that instead of it querying data everytime you use the middleware or use the can/is methods inside a controller, it will now use the prefetched permissions and roles already populated from the main "init" middleware.

@twigs67
Copy link
Author

twigs67 commented Dec 27, 2018 via email

@jacobwise
Copy link

@twigs67 I would just do something like this:

return await Role.query() .where("id", id) .with("permissions") .fetch();

Does that work for your use case?

@victorfariasoliveira
Copy link

@jacobwise I do not know if this works for @twigs67, but for me it took a user and needed to load their documents with their permissions this works, thanks

@jeffdrumgod
Copy link

@hadihallak can you send a PR with your improvements?

@ed-fruty
Copy link

Does no any fix from 2018 in this repo?

@jeffdrumgod
Copy link

Project is apparently abandoned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants