-
Notifications
You must be signed in to change notification settings - Fork 1
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
Support nested relationship fetching #11
Comments
@agarzola Can we also add to this feature the ability to do this recursively? Let me know if you still need help on this. I saw you at Decoupled Days and learned about this project and am really wanting to use it as my management tool! |
Hi, @navenedrob! 👋 So using the example above, let’s say the const Location = new Model({
category: 'relationships.location_type',
}); Are you wanting to declare that nested relationship in the const Author = new Model({
birthplace: 'relationship.location_born',
}, {
related: {
birthplace: {
Location: 'category',
},
},
}); And so, when requesting a
Is that what you mean? |
This will also require a mechanism to avoid infinite loops. Say const Location = new Model({
category: 'relationships.location_type',
famous_authors: 'relationships.famous_authors',
}, {
related: {
famous_authors: {
Author: 'birthplace',
},
},
}); Fetching a My current thinking on this is that once a It feels a little arbitrary to stop there and not sooner (or later), but since relationships are hydrated function get_related_authors(book) {
const related_authors = book.author.birthplace.famous_authors;
return Promise.all( related_authors.map(author => author.fetch()) );
} This could also be implemented as a model method or, if/when we get event hooks implemented, it can be implemented that way. |
A
Book
, for example, would have a relatedAuthor
, which in turn might have a relatedLocation
record for the place of birth. Whenfetch
ing a book, it may be useful to have jsonmonger include not only the author, but the author’s location. The tricky part of this is that jsonmonger can’t know whichtype
might be related to a particular property, so the model itself needs to declare this in therelated
config somehow.An object might be the way to go here. Using the example above:
The result should be that the
include
query string param would look like this:which the API should use to include the related author and their related birthplace in the payload.
Suggestions welcome on this interface. I think it’s a bit awkward, but can’t think of another way to accomplish this.
The text was updated successfully, but these errors were encountered: