pouchdb / upsert

PouchDB plugin for upsert() and putIfNotExists() functions
Apache License 2.0
149 stars 25 forks source link

diff function doesn't allow for asynchronous requests #16

Closed csaroff closed 8 years ago

csaroff commented 8 years ago

I would currently like to be able to make asynchronous requests from inside my diff function.

db.upsert(_id, function(doc, cb) {
  if(_.isEmpty(doc) {
    request('some.url', function(error, res) {
      doc.info = res.body;
      cb(null, doc);
    }
  }
  else {
  // Do something else
  }
}).then(function(res) {
  console.log(res);
});

AFAIK, this is currently impossible from the diff function.

I can see two options that don't involve updating this library.

  1. Don't use upsert. Custom handle the 404 and 409 errors.
  2. Make the request to some.url every time before calling upsert

I used to do (1) and it makes the code look really ugly. (2) Introduces a pretty senseless inefficiency.

I think that the syntax I proposed above looks hideous, but do you have any suggestions or recommendations here?

nolanlawson commented 8 years ago

Right, the solution is to not use upsert. By design, the goal of this repo is to just provide a handler for synchronized logic.

I'll also point out that it's a bit against the spirit of PouchDB (and "offline first") to try to do an HTTP request in order to update a local document.. but if you absolutely have to do it, then yes, I would say you should handle 409s and 404s yourself.

csaroff commented 8 years ago

Understood. Thanks for the comment.