Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert functionality #26

Open
sarankup opened this issue Dec 23, 2021 · 0 comments
Open

Upsert functionality #26

sarankup opened this issue Dec 23, 2021 · 0 comments

Comments

@sarankup
Copy link

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);
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant