{"action":"created","issue":{"url":"https://api.github.com/repos/js-data/js-data/issues/525","repository_url":"https://api.github.com/repos/js-data/js-data","labels_url":"https://api.github.com/repos/js-data/js-data/issues/525/labels{/name}","comments_url":"https://api.github.com/repos/js-data/js-data/issues/525/comments","events_url":"https://api.github.com/repos/js-data/js-data/issues/525/events","html_url":"https://github.com/js-data/js-data/issues/525","id":366046159,"node_id":"MDU6SXNzdWUzNjYwNDYxNTk=","number":525,"title":"populate Record's relations returned from async methods","user":{"login":"crobinson42","id":5973579,"node_id":"MDQ6VXNlcjU5NzM1Nzk=","avatar_url":"https://avatars3.githubusercontent.com/u/5973579?v=4","gravatar_id":"","url":"https://api.github.com/users/crobinson42","html_url":"https://github.com/crobinson42","followers_url":"https://api.github.com/users/crobinson42/followers","following_url":"https://api.github.com/users/crobinson42/following{/other_user}","gists_url":"https://api.github.com/users/crobinson42/gists{/gist_id}","starred_url":"https://api.github.com/users/crobinson42/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crobinson42/subscriptions","organizations_url":"https://api.github.com/users/crobinson42/orgs","repos_url":"https://api.github.com/users/crobinson42/repos","events_url":"https://api.github.com/users/crobinson42/events{/privacy}","received_events_url":"https://api.github.com/users/crobinson42/received_events","type":"User","site_admin":false},"labels":[{"id":128526119,"node_id":"MDU6TGFiZWwxMjg1MjYxMTk=","url":"https://api.github.com/repos/js-data/js-data/labels/enhancement","name":"enhancement","color":"84b6eb","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-10-02T19:29:39Z","updated_at":"2018-11-09T14:02:51Z","closed_at":null,"author_association":"MEMBER","body":"## Description\r\n\r\nWhen using JSData server-side, it is common to immediately return the result from an async op, ie: \r\n\r\n```\r\n...\r\nasync getUsers(req, res) {\r\n  res.body = await containerStore.findAll('users', { active: true }, { with: ['posts'] })\r\n}\r\n...\r\n```\r\n\r\nThe problem with the above code is that the relation field, `posts` will not be populated due to the Mapper's `toJSON` method not being invoked on each record instance with the options `{ with: ['posts'] }`. \r\n\r\nThis is a hassle when you are required to write server-side controller methods like this:\r\n\r\n```\r\n...\r\nasync getUsers(req, res) {\r\n  res.body = await containerStore.findAll('users', { active: true }, { with: ['posts'] }).map(user => user.toJSON({ with: ['posts'] })\r\n}\r\n...\r\n```\r\n\r\n### Proposal\r\n\r\nAdding an option `populateRelations` to the `Container` class that defaults to true would make sense considering a `Container` class is typically used in a server-side environment. When the flag is `true` any async methods invoked with relations `with: [...]` option will be populated on the `Record` object automatically so that common methods will capture the relation fields, ie: `JSON.stringify(records)` or `console.log(records)`.\r\n\r\n## Environment\r\n\r\n- js-data version: 3.0.x\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/js-data/js-data/issues/comments/437368307","html_url":"https://github.com/js-data/js-data/issues/525#issuecomment-437368307","issue_url":"https://api.github.com/repos/js-data/js-data/issues/525","id":437368307,"node_id":"MDEyOklzc3VlQ29tbWVudDQzNzM2ODMwNw==","user":{"login":"crobinson42","id":5973579,"node_id":"MDQ6VXNlcjU5NzM1Nzk=","avatar_url":"https://avatars3.githubusercontent.com/u/5973579?v=4","gravatar_id":"","url":"https://api.github.com/users/crobinson42","html_url":"https://github.com/crobinson42","followers_url":"https://api.github.com/users/crobinson42/followers","following_url":"https://api.github.com/users/crobinson42/following{/other_user}","gists_url":"https://api.github.com/users/crobinson42/gists{/gist_id}","starred_url":"https://api.github.com/users/crobinson42/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crobinson42/subscriptions","organizations_url":"https://api.github.com/users/crobinson42/orgs","repos_url":"https://api.github.com/users/crobinson42/repos","events_url":"https://api.github.com/users/crobinson42/events{/privacy}","received_events_url":"https://api.github.com/users/crobinson42/received_events","type":"User","site_admin":false},"created_at":"2018-11-09T14:02:51Z","updated_at":"2018-11-09T14:02:51Z","author_association":"MEMBER","body":"Consideration for recursive depth of `.toJSON` needs to be taken because of these computed props can become circular depending on the defined relations."}}
{"actor":{"display_login":"crobinson42"}}