LocalDB icon indicating copy to clipboard operation
LocalDB copied to clipboard

Upsert functionality

Open sarankup opened this issue 2 years ago • 0 comments

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);
  }

sarankup avatar Dec 23 '21 17:12 sarankup