{"action":"opened","issue":{"url":"https://api.github.com/repos/vuex-orm/vuex-orm/issues/73","repository_url":"https://api.github.com/repos/vuex-orm/vuex-orm","labels_url":"https://api.github.com/repos/vuex-orm/vuex-orm/issues/73/labels{/name}","comments_url":"https://api.github.com/repos/vuex-orm/vuex-orm/issues/73/comments","events_url":"https://api.github.com/repos/vuex-orm/vuex-orm/issues/73/events","html_url":"https://github.com/vuex-orm/vuex-orm/issues/73","id":295808706,"number":73,"title":"New Action: insertOrUpdate","user":{"login":"iNaD","id":532247,"avatar_url":"https://avatars0.githubusercontent.com/u/532247?v=4","gravatar_id":"","url":"https://api.github.com/users/iNaD","html_url":"https://github.com/iNaD","followers_url":"https://api.github.com/users/iNaD/followers","following_url":"https://api.github.com/users/iNaD/following{/other_user}","gists_url":"https://api.github.com/users/iNaD/gists{/gist_id}","starred_url":"https://api.github.com/users/iNaD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iNaD/subscriptions","organizations_url":"https://api.github.com/users/iNaD/orgs","repos_url":"https://api.github.com/users/iNaD/repos","events_url":"https://api.github.com/users/iNaD/events{/privacy}","received_events_url":"https://api.github.com/users/iNaD/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-09T09:40:20Z","updated_at":"2018-02-09T09:40:20Z","closed_at":null,"author_association":"NONE","body":"Today I stumbled upon a common use case which currently isn't covered by vuex-orm.\r\n\r\n## Idea\r\n\r\nAdd an action `insertOrUpdate` (couldn't think of a better name) which will insert new records into the collection or update records already existing.\r\n\r\n```js\r\n// Initial State\r\n{\r\n    entities: {\r\n        users: {\r\n            data: {\r\n                '1': { id: 1, name: 'John', roles: [3] }\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\n// `insertOrUpdate` is going to add new records and update existing data (see `update`).\r\nstore.dispatch('entities/users/insertOrUpdate', {\r\n    data: [\r\n        { id: 1, name: 'Peter' },\r\n        { id: 2, name: 'Hank' },\r\n    ]\r\n})\r\n  \r\n// State after `insertOrUpdate`.\r\n{\r\n    entities: {\r\n        users: {\r\n            data: {\r\n                '1': { id: 1, name: 'Peter', roles: [3] },\r\n                '2': { id: 2, name: 'Hank' }\r\n            }\r\n        }\r\n    }\r\n}\r\n```\r\n\r\n## Use case\r\n\r\nIn my case relationships aren't always returned by the API. You can specify which relationships should be embedded in the response to reduce data transfer and queries on the database.\r\n\r\nE.g. when loading the current user, I will fetch his roles, but for listing all users this isn't needed, so I can fetch them when loading the detail view. But currently when loading all users without roles, all existing users will be overwritten with the provided data.\r\n\r\n## Current workaround\r\n\r\nMy current workaround is looking like the following and I think it would be helpful for many users to include a similar solution in vuex-orm.\r\n\r\n```js\r\nconst users; // Retrieved from some API\r\nconst promises = users.map(user => {\r\n    if (this.$store.getters['entities/users/find'](user.id) !== null) {\r\n        return this.$store.dispatch('entities/users/update', {\r\n            where: user.id,\r\n            data: user\r\n        });\r\n    }\r\n    return this.$store.dispatch('entities/users/insert', {\r\n        data: user\r\n    });\r\n});\r\n\r\nPromise.all(promises).then(() => {\r\n    // All users have been inserted or update, now we can do anything we want\r\n});\r\n```\r\n\r\n## Final words\r\n\r\nWhat are you thinking about this idea? Is it a good approach to extend vuex-orm or should I just use a self-written helper? If you like this idea, I could try to create a PR implementing this feature."}}
{"actor":{"display_login":"iNaD"}}