merge-items
Upserts documents (objects) into collections (arrays) by primary key
Getting started
npm i -S merge-items
; ;
Example
; const source = id: 1 name: 'Foo' id: 2 name: 'Bar';const items = id: 1 name: 'FooFoo' id: 3 name: 'Baz'; ;// => [// {id: 1, name: 'FooFoo'},// {id: 3, name: 'Baz'},// ] source;// => [// {id: 1, name: 'FooFoo'},// {id: 2, name: 'Bar'},// {id: 3, name: 'Baz'},// ]
Options
primaryKey
- type: string
- default:
id
const source = ;const items = _id: 'abc' name: 'foo' _id: 'zxc' name: 'bar'; ;
mapInsert, mapUpdate, mapUpsert
mapInsert(data)
- maps items to be inserted (new items)mapUpdate(data)
- maps items to be updated (existing items)mapUpsert(data, isNew)
- maps all items
{ Object; } const source = ;const newPerson = name: 'Bob' age: 20; ;// => Person {// name: 'Bob',// age: 20,// }
afterInsert, afterUpdate, afterUpsert
afterInsert(item, data)
- invokes after item was inserted (new item)afterUpdate(item, data)
- invokes after item was updated (existing item)afterUpsert(item, data, isNew)
- always invokes
const people = ; friendsIds = ; { Object; } { const newPeople = friends ; thisfriendsIds = newPeople; } { return people; } const newPerson = id: 1 name: 'Bob' age: 20 friends: 'Alice' 'Charlie'; ;// => Person {// id: 1,// name: 'Bob',// age: 20,// friendsIds: [<uniqueId>, <uniqueId>]// } people;// => [// Person {id: 1, name: 'Bob', age: 20, friendsIds: [<uniqueId>, <uniqueId>]},// Person {id: <uniqueId>, name: 'Alice', friendsIds: []},// Person {id: <uniqueId>, name: 'Charlie', friendsIds: []},// ]