-
Notifications
You must be signed in to change notification settings - Fork 77
Support for nested resource endpoints #40
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
Comments
How do you think something like this would work? DS.defineResource({
name: 'post'
});
DS.defineResource({
parent: 'post',
name: 'comment'
});
// GET http://localhost/post/10/comment
DS.findAll('comment', {postId: 10})
.then(function(comments){
// Do something here
});
// GET http://localhost/post/10/comment/1
DS.find('comment', {postId: 10, commentId: 1})
.then(function(comment){
// Do something here
}); |
@MattAitchison I see what you're getting at. However, your example seems to preclude the idea that DS.defineResource({
name: 'post'
});
DS.defineResource({
parent: 'post',
name: 'comment'
});
// GET http://localhost/post/10/comment
DS.findAll('comment', { postId: 10 });
// GET http://localhost/comment
DS.findAll('comment');
// GET http://localhost/post/10/comment/1
DS.find('comment', 1, { postId: 10 });
// GET http://localhost/comment/1
DS.find('comment', 1); In addition to nested resources, I'd like also to in general support belongsTo, hasOne, and hasMany relationships. The two seem related, so the implementation of one would affect the implementation of the other. Once I've wrapped up relations in Reheat, I will move on to this. |
I just thought of it real quick. I didn't think about that part. I do like your example too. That was one of the other ways I was thinking about it. I have no clue how you wanted to implement relations but I have thought about it a little. Maybe they could work like they do in reheat. So you could do something like Maybe I am just making the relations more complicated than they need to be though. I haven't used any libraries for them before. |
@MattAitchison, @jmdobry Can you check out the way relations are done in ngActiveResource https://github.com/FacultyCreative/ngActiveResource |
Any news on this now that relations has been worked on? |
Would probably be something good to include in the wiki. I haven't done anything like this so I wont be able to help here, but thought I'd give this suggestion :-) |
@kentcdodds I seem to be missing something. Put what on the wiki? What suggestion? |
@kentcdodds I looked at the wiki and realized that the workaround you suggested a while back is irrelevant now that support for relations has been added. |
Perhaps it's been documented elsewhere now, but I just that that since @Patrik-Lundqvist was asking it may not be easy to find. Feel free to remove my out of date wiki entry :-) |
Relations are implemented and documented, but this nested resources issue is about something slightly different. It's about being able to handle nested resource endpoints, like |
@Patrik-Lundqvist A solution is in the works |
My current implementation is this: Nested Resource EndpointsAdd Example: DS.defineResource({
name: 'comment',
relations: {
belongsTo: {
post: {
parent: true,
localKey: 'postId',
localField: 'post'
}
}
}
});
// The comment isn't in the data store yet, so angular-data wouldn't know
// what the id of the parent "post" would be, so we pass it in manually
DS.find('comment', 5, { parentKey: 4 }); // GET /post/4/comment/5
// vs
DS.find('comment', 5); // GET /comment/5
DS.inject('comment', { id: 1, postId: 2 });
// We don't have to provide the parentKey here
// because angular-data found it in the comment
DS.update('comment', 1, { content: 'stuff' }); // PUT /post/2/comment/1
// If you don't want the nested for just one of the calls then
// you can do the following:
DS.update('comment', 1, { content: 'stuff' }, { nested: false ); // PUT /comment/1 |
Looks great! I'll try it out |
I think it should work with findAll, updateAll, and destroyAll, but I haven't tested it yet. |
Just tested DS.update with nested set to false but it didn't work. Should be:
|
@Patrik-Lundqvist Good catch. This is all on master now. I'll have to flesh out the tests a little more. |
I ran into some problem with the current implementation where I have minimized my path nesting. Lets take this example: In order to not have complicated paths when retrieving projects and its child resources I have minimized my paths as described here. For instance, |
Okay, so in addition to
You also want to be able able to do just |
Exactly, and also get/post projects with |
Yeah, the current implementation isn't that flexible. I'll have to rework
|
Alright! By the way, maybe nested resources could be used when loading relations also. So that |
The new implementation should take care of that. |
Now: Nested Resource EndpointsAdd Example: DS.defineResource({
name: 'comment',
relations: {
belongsTo: {
post: {
parent: true,
localKey: 'postId',
localField: 'post'
}
}
}
});
// The comment isn't in the data store yet, so angular-data wouldn't know
// what the id of the parent "post" would be, so we pass it in manually
DS.find('comment', 5, { params: { postId: 4 } }); // GET /post/4/comment/5
// vs
DS.find('comment', 5); // GET /comment/5
DS.inject('comment', { id: 1, postId: 2 });
// We don't have to provide the parentKey here
// because angular-data found it in the comment
DS.update('comment', 1, { content: 'stuff' }); // PUT /post/2/comment/1
// If you don't want the nested for just one of the calls then
// you can do the following:
DS.update('comment', 1, { content: 'stuff' }, { params: { postId: false } }); // PUT /comment/1 |
@Patrik-Lundqvist So in your case you would now do something like: DS.update('project', 5, { some: 'stuff' }, { params: { org_id: false } }); // If project "5" isn't already in the data store
DS.update('project', 5, { some: 'stuff' }, { params: { org_id: false, customer_id: 6 } }); And you should get |
Works great, very nice! But how will the parent property handle multiple parents? Lets say that a user both belongs to an organization and a team which should make both of these paths valid:
The second path would be called when loading relations for a team. |
I feel like I'm going down the rabbit hole...Let me think about this one. |
@Patrik-Lundqvist I'm going to consider this issue complete enough for the beta. Any other potential enhancements should go into their own issue. |
This doesn't appear to work with DS.defineResource({
name: 'category',
endpoint: '/categories',
relations: {
belongsTo: {
section: {
parent: true,
localKey: 'secId',
localField: 'section'
}
}
} Then calling: Category.findAll({ params: { secId: 1 } }) Produces this URL: http://dev.api.dimaslist.org:3000/v1/categories?params=%7B%22secId%22:1%7D Expected URL: http://dev.api.dimaslist.org:3000/v1/sections/1/categories |
@demisx You need to do |
👍 That worked! |
No description provided.
The text was updated successfully, but these errors were encountered: