lucid icon indicating copy to clipboard operation
lucid copied to clipboard

Lifecycle event to hook relation in the "with" method

Open alexhenriquepv opened this issue 6 years ago • 6 comments

When I use the with method, ex:

const users = await User.query().with('posts').fetch()

I don't have an event to listen the fetchAll and modify the relation data using eager loading.

alexhenriquepv avatar Dec 13 '19 19:12 alexhenriquepv

When you use query builder, Lucid doesn't have any data about that row. That's why it's not triggering any lifecycle hook.

What you need to do (not recommended) is to select a row first.

const users  = await User.all();
for (let user of users) {
    cont posts = await user.posts();
    user.$sideLoaded.posts = posts
 }

wobsoriano avatar Dec 13 '19 23:12 wobsoriano

Ok @sorxrob. But we have the n + 1 problem, not recomended in the documentation. Screenshot_20191213-204245

alexhenriquepv avatar Dec 13 '19 23:12 alexhenriquepv

Ok @sorxrob. But we have the n + 1 problem, not recomended in the documentation. Screenshot_20191213-204245

Yes that will be the problem. There's no way currently to listen to an event via lifecycles with query builder.

wobsoriano avatar Dec 13 '19 23:12 wobsoriano

May I be suggesting this functionality here?

alexhenriquepv avatar Dec 13 '19 23:12 alexhenriquepv

I guess you can

wobsoriano avatar Dec 13 '19 23:12 wobsoriano

Hey there! 👋

Please, create a proper RFC using our template so we can better understand your needs and what should be done.

RomainLanz avatar Jun 26 '20 19:06 RomainLanz