Skip to content

Upsert functionality #26

@sarankup

Description

@sarankup

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions