{"id":20298396,"url":"https://github.com/lukaszgrolik/merge-items","last_synced_at":"2025-04-11T12:55:42.122Z","repository":{"id":57294759,"uuid":"62908780","full_name":"lukaszgrolik/merge-items","owner":"lukaszgrolik","description":"Upserts documents (objects) into collections (arrays) by primary key","archived":false,"fork":false,"pushed_at":"2017-08-25T09:03:32.000Z","size":41,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T09:12:13.513Z","etag":null,"topics":["collection","inject","merge","upsert"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lukaszgrolik.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-08T18:23:13.000Z","updated_at":"2017-05-18T22:43:06.000Z","dependencies_parsed_at":"2022-08-30T18:21:50.311Z","dependency_job_id":null,"html_url":"https://github.com/lukaszgrolik/merge-items","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukaszgrolik%2Fmerge-items","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukaszgrolik%2Fmerge-items/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukaszgrolik%2Fmerge-items/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukaszgrolik%2Fmerge-items/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lukaszgrolik","download_url":"https://codeload.github.com/lukaszgrolik/merge-items/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248404086,"owners_count":21097650,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["collection","inject","merge","upsert"],"created_at":"2024-11-14T16:09:27.522Z","updated_at":"2025-04-11T12:55:42.099Z","avatar_url":"https://github.com/lukaszgrolik.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# merge-items\n\nUpserts documents (objects) into collections (arrays) by primary key\n\n## Getting started\n\n`npm i -S merge-items`\n\n```js\nimport mergeItems from 'merge-items';\n\nmergeItems(source, items, options);\n```\n\n## Example\n\n```js\nimport mergeItems from 'merge-items';\n\nconst source = [\n  {id: 1, name: 'Foo'},\n  {id: 2, name: 'Bar'},\n];\nconst items = [\n  {id: 1, name: 'FooFoo'},\n  {id: 3, name: 'Baz'},\n];\n\nmergeItems(source, items);\n// =\u003e [\n//   {id: 1, name: 'FooFoo'},\n//   {id: 3, name: 'Baz'},\n// ]\n\nsource;\n// =\u003e [\n//   {id: 1, name: 'FooFoo'},\n//   {id: 2, name: 'Bar'},\n//   {id: 3, name: 'Baz'},\n// ]\n```\n\n## Options\n\n### primaryKey\n\n- type: *string*\n- default: `id`\n\n```js\nconst source = [];\nconst items = [\n  {_id: 'abc', name: 'foo'},\n  {_id: 'zxc', name: 'bar'},\n];\n\nmergeItems(source, items, {\n  primaryKey: '_id',\n});\n```\n\n### mapInsert, mapUpdate, mapUpsert\n\n- `mapInsert(data)` - maps items to be inserted (new items)\n- `mapUpdate(data)` - maps items to be updated (existing items)\n- `mapUpsert(data, isNew)` - maps all items\n\n```js\nclass Person {\n  constructor(body) {\n    Object.assign(this, body);\n  }\n}\n\nconst source = [];\nconst newPerson = {\n  name: 'Bob',\n  age: 20,\n};\n\nmergeItems(source, newPerson, {\n  mapUpsert: (data, isNew) =\u003e {\n    if (isNew) {\n      return new Person(data);\n    } else {\n      return data;\n    }\n  },\n});\n// =\u003e Person {\n//   name: 'Bob',\n//   age: 20,\n// }\n```\n\n### afterInsert, afterUpdate, afterUpsert\n\n- `afterInsert(item, data)` - invokes after item was inserted (new item)\n- `afterUpdate(item, data)` - invokes after item was updated (existing item)\n- `afterUpsert(item, data, isNew)` - always invokes\n\n```js\nconst people = [];\n\nclass Person {\n  friendsIds = [];\n\n  constructor(body) {\n    Object.assign(this, body);\n  }\n\n  setFriends(friends) {\n    const newPeople = friends.map(f =\u003e {\n      return {\n        id: _.uniqueId(), // lodash function to generate unique ID\n        name: f,\n      };\n    })\n\n    mergeItems(people, newPeople);\n\n    this.friendsIds = newPeople.map(f =\u003e f.id);\n  }\n\n  getFriends() {\n    return people.filter(p =\u003e {\n      return this.friendsIds.includes(p.id);\n    });\n  }\n}\n\nconst newPerson = {\n  id: 1,\n  name: 'Bob',\n  age: 20,\n  friends: ['Alice', 'Charlie'],\n};\n\nmergeItems(people, newPerson, {\n  mapUpsert: (data, isNew) =\u003e {\n    const {friends, ...body} = data;\n\n    if (isNew) {\n      return new Person(body);\n    } else {\n      return body;\n    }\n  },\n  afterUpsert: (person, data, isNew) =\u003e {\n    const {friends} = data;\n\n    person.setFriends(friends);\n  },\n});\n// =\u003e Person {\n//   id: 1,\n//   name: 'Bob',\n//   age: 20,\n//   friendsIds: [\u003cuniqueId\u003e, \u003cuniqueId\u003e]\n// }\n\npeople;\n// =\u003e [\n//   Person {id: 1, name: 'Bob', age: 20, friendsIds: [\u003cuniqueId\u003e, \u003cuniqueId\u003e]},\n//   Person {id: \u003cuniqueId\u003e, name: 'Alice', friendsIds: []},\n//   Person {id: \u003cuniqueId\u003e, name: 'Charlie', friendsIds: []},\n// ]\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukaszgrolik%2Fmerge-items","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukaszgrolik%2Fmerge-items","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukaszgrolik%2Fmerge-items/lists"}