Skip to content

Check if id exists then either update or create #10

Description

@polluterofminds

I am sure this has to be possible but I have not been able to make heads or tails of how to do it. I would like to check if an id already exists in the db, and if it does, I'd like to update it. Otherwise, I'd like to create a new item in the db with the given id.

I tried this:

if(mydb.read(idToCheck)) {
  mydb.update(idToCheck, { data: dataToPost })
} else {
  mydb.create({ _id: idToCheck, data: dataToPost })
}

But that returns true every time even if the id does not exist in the db. Any tips?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions