MCPcopy Index your code
hub / github.com/apache/pouchdb / upsert

Function upsert

lib/index-browser.js:727–753  ·  view source on GitHub ↗
(db, docId, diffFun)

Source from the content-addressed store, hash-verified

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

Callers 4

tryAndPutFunction · 0.70
_setupMethod · 0.70
onCompleteMethod · 0.70
createViewFunction · 0.70

Calls 2

tryAndPutFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…