{"action":"created","issue":{"url":"https://api.github.com/repos/strongloop/loopback/issues/948","labels_url":"https://api.github.com/repos/strongloop/loopback/issues/948/labels{/name}","comments_url":"https://api.github.com/repos/strongloop/loopback/issues/948/comments","events_url":"https://api.github.com/repos/strongloop/loopback/issues/948/events","html_url":"https://github.com/strongloop/loopback/issues/948","id":52760167,"number":948,"title":"Suggestion: Post including related data","user":{"login":"bkniffler","id":4349324,"avatar_url":"https://avatars.githubusercontent.com/u/4349324?v=3","gravatar_id":"","url":"https://api.github.com/users/bkniffler","html_url":"https://github.com/bkniffler","followers_url":"https://api.github.com/users/bkniffler/followers","following_url":"https://api.github.com/users/bkniffler/following{/other_user}","gists_url":"https://api.github.com/users/bkniffler/gists{/gist_id}","starred_url":"https://api.github.com/users/bkniffler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bkniffler/subscriptions","organizations_url":"https://api.github.com/users/bkniffler/orgs","repos_url":"https://api.github.com/users/bkniffler/repos","events_url":"https://api.github.com/users/bkniffler/events{/privacy}","received_events_url":"https://api.github.com/users/bkniffler/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/strongloop/loopback/labels/enhancement","name":"enhancement","color":"84b6eb"},{"url":"https://api.github.com/repos/strongloop/loopback/labels/question","name":"question","color":"cc317c"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2014-12-23T17:44:05Z","updated_at":"2015-04-10T11:00:25Z","closed_at":"2015-04-03T15:58:56Z","body":"I suggest implementing a possibility to handle new/updated/removed associated child data when posting an item.\r\n\r\nLets say we have a \"Shelf\" and a \"Book\" model with a 1:n relation, I'd like to be able to post from the client:\r\n```\r\n[Shelf]\r\n{\r\n name:\"Shelf 1\",\r\n books: [{ name:\"Book 1\" },{ name:\"Book 2\" }]\r\n}\r\n```\r\nCurrently, this scenario must be handled either client-side, posting shelf and on response, posting the associated books; or server-side with custom methods or by using hooks (which is what I'm trying atm):\r\n```\r\n.beforeSave = function (next, item) {\r\n        // Store books in ctx\r\n        next();\r\n    }\r\n.afterSave = function (next, item) {\r\n        // compare ctx.books and item.books\r\n        // delete items not present in ctx.books, but in item.books \r\n        // add items not present in item.books but in ctx.books\r\n        // possibly update those books present in both\r\n        next();\r\n    }\r\n```\r\nI wouldn't consider this a very clean solution and when done in a generic, plug-and-playable way, there are a few things to take care of (like handling all relations, 1:1, 1:n or n:n). But in my opinion, with a product that generates such complete rest apis as Loopback does, this should be a build-in feature. How do you guys see this? Do you have a better idea? Would you consider implementing this?"},"comment":{"url":"https://api.github.com/repos/strongloop/loopback/issues/comments/91517018","html_url":"https://github.com/strongloop/loopback/issues/948#issuecomment-91517018","issue_url":"https://api.github.com/repos/strongloop/loopback/issues/948","id":91517018,"user":{"login":"bkniffler","id":4349324,"avatar_url":"https://avatars.githubusercontent.com/u/4349324?v=3","gravatar_id":"","url":"https://api.github.com/users/bkniffler","html_url":"https://github.com/bkniffler","followers_url":"https://api.github.com/users/bkniffler/followers","following_url":"https://api.github.com/users/bkniffler/following{/other_user}","gists_url":"https://api.github.com/users/bkniffler/gists{/gist_id}","starred_url":"https://api.github.com/users/bkniffler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bkniffler/subscriptions","organizations_url":"https://api.github.com/users/bkniffler/orgs","repos_url":"https://api.github.com/users/bkniffler/repos","events_url":"https://api.github.com/users/bkniffler/events{/privacy}","received_events_url":"https://api.github.com/users/bkniffler/received_events","type":"User","site_admin":false},"created_at":"2015-04-10T11:00:25Z","updated_at":"2015-04-10T11:00:25Z","body":"Sure, I'd be happy to see a suggestion for this problem, and I imagine it'd be helpful for others too. Thanks!"}}