enniel / adonis-acl

demo app: https://github.com/enniel/adonis-acl-blog-demo
MIT License
203 stars 50 forks source link

So many queries?! #26

Open twigs67 opened 5 years ago

twigs67 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

Nice job! Althought a great package, it definitely needed some optimization. If I get some free time, I'll allow the package to cache permissions/roles to limit hits on the db. Thanks for this!

On Wed, Dec 26, 2018 at 1:36 PM Abdulhadi Hallak notifications@github.com wrote:

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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/enniel/adonis-acl/issues/26#issuecomment-450006410, or mute the thread https://github.com/notifications/unsubscribe-auth/AO9K-KGJ3m8kRpACde49mYcgG6i8hIX9ks5u88GTgaJpZM4Y0dVe .

jacobwise commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

@hadihallak can you send a PR with your improvements?

ed-fruty commented 4 years ago

Does no any fix from 2018 in this repo?

jeffdrumgod commented 4 years ago

Project is apparently abandoned.