question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow withRelated functions to be executed for nested relations

See original GitHub issue

Currently, if I specify that a relationship should be eager loaded on a fetch, it’s possible to specify an object within the “withRelated” list passed to the fetch options, and this can be used to modify the query used when loading a particular relation. However, this only works for single-level relationships.

This is a feature request, to make this work for multiple levels. One possible solution would be to allow specifying an array of functions, and pop the array and pass to the next level of the relation loading.

Eg.

model.fetch({
    withRelated: [
        {'relation1.subrelation': [null, (query) => { query.sortBy('id'); }]}
    ]
});

Which would not execute anything when loading “model.relation1”, but would execute the provided function when loading “relation1.subrelation”.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
vellotiscommented, Jul 14, 2016

Actually if you look the implementation then this should work as well.

model.fetch({
    withRelated: [
        {'relation1':  () => { /*do nothing or something*/ }},
        {'relation1.subrelation': (query) => { query.sortBy('id'); }}
    ]
});
0reactions
crunchtime-alicommented, Jul 19, 2017

The project leadership of Bookshelf recently changed. In an effort to advance the project we close all issues older than one year.

If you think this issue needs to be re-evaluated please post a comment on why this is still important and we will re-open it.

We also started an open discussion about the future of Bookshelf.js here https://github.com/bookshelf/bookshelf/issues/1600. Feel free to drop by and give us your opinion. Let’s make Bookshelf great again

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Retrieving deeply nested relations using bookshelf's ...
Before I post all of the code and associated DB models is it possible to go deeply nested like this or can bookshelf...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
Dynamic properties allow you to access relationship functions as if they were properties defined on the model: $phone = User::find(1)->phone;.
Read more >
Query examples | Objection.js
However, allowing the client to execute expressions like this without any limitations is not very secure. Therefore the QueryBuilder has the allowGraph method....
Read more >
Laravel (5.7) Eloquent Relationships - w3resource
In defining this relationship, we need three database tables: users, roles, and role_user. The role_user table will be derived from the ...
Read more >
API Reference - Bookshelf.js
Override this in your bookshelf instance to define a custom function that will ... You can also execute callbacks on relations queries (eg....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found