LocalDB
LocalDB copied to clipboard
Upsert functionality
Hi,
I like to have upsert function something like the below. Can this be added, please?
This code will take json as param, and then will check if already exists, if not it will do the insert operation based on the given id. If already exist, then it will upate.
upsert(object) {
if(object.id === undefined)
{
object.id = guid()
}
const foundObjects = sift({id: object.id}, JSON.parse(localStorage[this.name]));
if(foundObjects.length==0)
{
let table = JSON.parse(localStorage[this.name])
table.push(object)
localStorage[this.name] = JSON.stringify(table)
this.emit('$insert',object)
this.emit('$create',object)
return object
}
return this.update({id: object.id}, object);
}