(db, docId, diffFun)
| 730 | // the diffFun tells us what delta to apply to the doc. it either returns |
| 731 | // the doc, or false if it doesn't need to do an update after all |
| 732 | function upsert(db, docId, diffFun) { |
| 733 | return db.get(docId) |
| 734 | .catch(function (err) { |
| 735 | /* istanbul ignore next */ |
| 736 | if (err.status !== 404) { |
| 737 | throw err; |
| 738 | } |
| 739 | return {}; |
| 740 | }) |
| 741 | .then(function (doc) { |
| 742 | // the user might change the _rev, so save it for posterity |
| 743 | var docRev = doc._rev; |
| 744 | var newDoc = diffFun(doc); |
| 745 | |
| 746 | if (!newDoc) { |
| 747 | // if the diffFun returns falsy, we short-circuit as |
| 748 | // an optimization |
| 749 | return {updated: false, rev: docRev}; |
| 750 | } |
| 751 | |
| 752 | // users aren't allowed to modify these values, |
| 753 | // so reset them here |
| 754 | newDoc._id = docId; |
| 755 | newDoc._rev = docRev; |
| 756 | return tryAndPut(db, newDoc, diffFun); |
| 757 | }); |
| 758 | } |
| 759 | |
| 760 | function tryAndPut(db, doc, diffFun) { |
| 761 | return db.put(doc).then(function (res) { |
no test coverage detected
searching dependent graphs…