MCPcopy
hub / github.com/apache/pouchdb / upsert

Function upsert

lib/index.js:732–758  ·  view source on GitHub ↗
(db, docId, diffFun)

Source from the content-addressed store, hash-verified

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
732function 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
760function tryAndPut(db, doc, diffFun) {
761 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…