CollectionUpdate

#1

Use CollectionUpdate if you need to compare collections (find inserted, updated and removed elements):

var a = c3Make('[User]', [{id:'A'}, {id:'B'}, {id:'C',name:"Foo"}])
var b = c3Make('[User]', [{id:'B'}, {id:'C'}, {id:'D'}])

var u = CollectionUpdate.forArray(a,b)

u.inserted   // "A"
u.updated    // "C"
u.updatedOld // "C"
u.removed    // "D"

u.persist() // saves delta to DB

It uses id field as a key but it can be overridden using CollectionUpdateSpec.uniqueFieldPath

In v7.8 it adds forEntityArray & forEntityStream to compare in memory collection with db and eachUpserted & mapUpserted as typically same processing is needed for inserted or updated elements.

4 Likes