{"action":"created","issue":{"url":"https://api.github.com/repos/paularmstrong/normalizr/issues/182","repository_url":"https://api.github.com/repos/paularmstrong/normalizr","labels_url":"https://api.github.com/repos/paularmstrong/normalizr/issues/182/labels{/name}","comments_url":"https://api.github.com/repos/paularmstrong/normalizr/issues/182/comments","events_url":"https://api.github.com/repos/paularmstrong/normalizr/issues/182/events","html_url":"https://github.com/paularmstrong/normalizr/issues/182","id":192875325,"number":182,"title":"Question about result key","user":{"login":"batusai513","id":996671,"avatar_url":"https://avatars.githubusercontent.com/u/996671?v=3","gravatar_id":"","url":"https://api.github.com/users/batusai513","html_url":"https://github.com/batusai513","followers_url":"https://api.github.com/users/batusai513/followers","following_url":"https://api.github.com/users/batusai513/following{/other_user}","gists_url":"https://api.github.com/users/batusai513/gists{/gist_id}","starred_url":"https://api.github.com/users/batusai513/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/batusai513/subscriptions","organizations_url":"https://api.github.com/users/batusai513/orgs","repos_url":"https://api.github.com/users/batusai513/repos","events_url":"https://api.github.com/users/batusai513/events{/privacy}","received_events_url":"https://api.github.com/users/batusai513/received_events","type":"User","site_admin":false},"labels":[{"id":373884449,"url":"https://api.github.com/repos/paularmstrong/normalizr/labels/Type:%20Question","name":"Type: Question","color":"cc317c","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-12-01T15:21:48Z","updated_at":"2016-12-02T02:38:28Z","closed_at":"2016-12-01T21:17:09Z","body":"# Problem\r\n\r\nI'm trying to normalize a response with data and included keys `{data: {}, included: []}`, the included is always an array with different entities and i want the key in the result after normalize to be an object with each key as the type of the entity.\r\n\r\n**Input**\r\n\r\n```json\r\nvar response = {\r\n  \"data\": {\r\n    \"type\": \"process-definition\",\r\n    \"id\": \"PTBSuHitXNn1w9b3QSCwjXaVm\",\r\n    \"attributes\": {\r\n      \"name\": \"Test 3\",\r\n      \"description\": \"Hola\",\r\n      \"created-at\": \"2016-11-18T15:49:19.600Z\",\r\n      \"updated-at\": \"2016-11-29T15:32:16.326Z\"\r\n    },\r\n    \"relationships\": {\r\n      \"creator\": {\r\n        \"data\": {\r\n          \"type\": \"user\",\r\n          \"id\": \"57fba0e8aa18377f659da302\"\r\n        }\r\n      },\r\n      \"current-version\": {\r\n        \"data\": {\r\n          \"type\": \"process-definition-version\",\r\n          \"id\": \"VAicZthn7Ahsb3r8PEjJs229\"\r\n        }\r\n      },\r\n      \"versions\": {\r\n        \"data\": [\r\n          {\r\n            \"type\": \"process-definition-version\",\r\n            \"id\": \"s1Rnn1Dw2qtjbw9KXgz36axi\"\r\n          },\r\n          {\r\n            \"type\": \"process-definition-version\",\r\n            \"id\": \"KkpWg6sPfs8nGW8X6BBsQMXk\"\r\n          }\r\n        ]\r\n      }\r\n    }\r\n  },\r\n  \"included\": [\r\n    {\r\n      \"type\": \"process-definition-version\",\r\n      \"id\": \"VAicZthn7Ahsb3r8PEjJs229\",\r\n      \"attributes\": {\r\n        \"version\": 8,\r\n        \"description\": null,\r\n        \"created-at\": \"2016-11-29T15:59:13.900Z\",\r\n        \"updated-at\": \"2016-11-29T15:59:13.900Z\"\r\n      },\r\n      \"relationships\": {\r\n        \"creator\": {\r\n          \"data\": {\r\n            \"type\": \"user\",\r\n            \"id\": \"57fba0e8aa18377f659da302\"\r\n          }\r\n        },\r\n        \"process-definition\": {\r\n          \"data\": {\r\n            \"type\": \"process-definition\",\r\n            \"id\": \"PTBSuHitXNn1w9b3QSCwjXaVm\"\r\n          }\r\n        }\r\n      }\r\n    },\r\n    {\r\n      \"type\": \"user\",\r\n      \"id\": \"57fba0e8aa18377f659da302\",\r\n      \"attributes\": {\r\n        \"first-name\": \"Charly\",\r\n        \"last-name\": \"Palencia\",\r\n        \"username\": \"chalien\",\r\n        \"email\": \"chalien@test.com\"\r\n      },\r\n      \"relationships\": {\r\n        \"user-groups\": {\r\n          \"data\": []\r\n        }\r\n      }\r\n    },\r\n    {\r\n      \"type\": \"process-definition-version\",\r\n      \"id\": \"s1Rnn1Dw2qtjbw9KXgz36axi\",\r\n      \"attributes\": {\r\n        \"version\": 1,\r\n        \"description\": null,\r\n        \"created-at\": \"2016-11-18T22:21:13.964Z\",\r\n        \"updated-at\": \"2016-11-18T22:21:13.964Z\"\r\n      },\r\n      \"relationships\": {\r\n        \"creator\": {\r\n          \"data\": {\r\n            \"type\": \"user\",\r\n            \"id\": \"57fba0e8aa18377f659da302\"\r\n          }\r\n        },\r\n        \"process-definition\": {\r\n          \"data\": {\r\n            \"type\": \"process-definition\",\r\n            \"id\": \"PTBSuHitXNn1w9b3QSCwjXaVm\"\r\n          }\r\n        }\r\n      }\r\n    }\r\n  ]\r\n}\r\n```\r\n\r\nHere's how I'm using normalizr:\r\n\r\n```js\r\nvar processDefinitionSchema = new Schema('processDefinitions');\r\nvar processDefinitionVersionsSchema = new Schema('processDefinitionVersions');\r\nvar usersSchema = new Schema('users');\r\n\r\nvar processDefinitionsIncludedSchema = {\r\n  user: usersSchema,\r\n  'process-definition-version': processDefinitionVersionsSchema,\r\n};\r\n\r\nvar PROCESS = {\r\n  data: processDefinitionSchema,\r\n  included: arrayOf(processDefinitionsIncludedSchema, { schemaAttribute: 'type' }),\r\n};\r\n\r\nconst normalizeOptions = {\r\n  assignEntity(obj, objKey, value) {\r\n    var newObj;\r\n    if (objKey == 'relationships') {\r\n      newObj = Object.keys(value).reduce((newVal, key) => {\r\n        if (value[key].data) {\r\n          if (Array.isArray(value[key].data)) {\r\n            return Object.assign({}, newVal, { [key]: value[key].data.map(item => item.id) });\r\n          } else {\r\n            return Object.assign({}, newVal, { [key]: value[key].data.id });\r\n          }\r\n        } else {\r\n          return Object.assign({}, newVal, { [key]: value[key].data });\r\n        }\r\n      }, {});\r\n      Object.assign(obj, newObj);\r\n    }\r\n\r\n    if (objKey == 'attributes') {\r\n      Object.assign(obj, value);\r\n    } else {\r\n      obj[objKey] = value;\r\n    }\r\n    delete obj.attributes;\r\n  },\r\n};\r\n\r\nnormalize(response, PROCESS, normalizeOptions);\r\n\r\n```\r\n\r\n**Output**\r\n\r\nHere's what I expect to see when I run the above:\r\nWhat i want :\r\n```js\r\n{\r\n    result: {\r\n      data: 'PTBSuHitXNn1w9b3QSCwjXaVm',\r\n      included: {\r\n        'process-definition-version': ['VAicZthn7Ahsb3r8PEjJs229', 's1Rnn1Dw2qtjbw9KXgz36axi'],\r\n        'user': '57fba0e8aa18377f659da302'\r\n      }\r\n    },\r\n    entities: { ... }\r\n}\r\n```\r\nWhat i actually get:\r\n```js\r\n{\r\n    result: {\r\n      data: 'PTBSuHitXNn1w9b3QSCwjXaVm',\r\n      included: [\r\n        {\r\n           id: \"VAicZthn7Ahsb3r8PEjJs229\",\r\n           schema: 'process-definition-version'\r\n        },\r\n        {\r\n           id: \"s1Rnn1Dw2qtjbw9KXgz36axi\",\r\n           schema: 'process-definition-version'\r\n        },\r\n        {\r\n           id: \"57fba0e8aa18377f659da302\",\r\n           schema: 'user'\r\n        },\r\n      ]\r\n    },\r\n    entities: { ... }\r\n}\r\n```\r\nAll the entities are fine.\r\n\r\nThanks in advanced for your help :) 🙇🏻"},"comment":{"url":"https://api.github.com/repos/paularmstrong/normalizr/issues/comments/264358396","html_url":"https://github.com/paularmstrong/normalizr/issues/182#issuecomment-264358396","issue_url":"https://api.github.com/repos/paularmstrong/normalizr/issues/182","id":264358396,"user":{"login":"batusai513","id":996671,"avatar_url":"https://avatars.githubusercontent.com/u/996671?v=3","gravatar_id":"","url":"https://api.github.com/users/batusai513","html_url":"https://github.com/batusai513","followers_url":"https://api.github.com/users/batusai513/followers","following_url":"https://api.github.com/users/batusai513/following{/other_user}","gists_url":"https://api.github.com/users/batusai513/gists{/gist_id}","starred_url":"https://api.github.com/users/batusai513/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/batusai513/subscriptions","organizations_url":"https://api.github.com/users/batusai513/orgs","repos_url":"https://api.github.com/users/batusai513/repos","events_url":"https://api.github.com/users/batusai513/events{/privacy}","received_events_url":"https://api.github.com/users/batusai513/received_events","type":"User","site_admin":false},"created_at":"2016-12-02T02:38:28Z","updated_at":"2016-12-02T02:38:28Z","body":"@paularmstrong Thanks for your help, it was very helpful"}}
{"actor":{"display_login":"batusai513"}}