Skip to content

Instantly share code, notes, and snippets.

@ericlowry
Last active August 11, 2023 05:46
Show Gist options
  • Save ericlowry/0f5de071403b162b42dae85feb4fd318 to your computer and use it in GitHub Desktop.
Save ericlowry/0f5de071403b162b42dae85feb4fd318 to your computer and use it in GitHub Desktop.
couchdb nano upsert - fast and simple
//
// async upsert( db, doc[, id] )
//
// attempts to insert a document, on document conflict, attempt to create a new version
//
const upsert = (db, doc, id) =>
// attempt to insert the doucment "as is"
db.insert(doc, id).catch(err => {
// for anything other than a document conflict...
if (err.statusCode !== 409)
// ...re-throw the error.
throw err;
// get the document id from the parameter or the document itself
const _id = id || doc._id;
// use the head() function to grab the existing document's etag header
return db.head(_id)
.then(({ etag }) => {
doc._id = _id;
doc._rev = etag.replace(/"/g, ''); // strip extraneous '"'s
return db.insert(doc);
})
.catch(err => {
throw err; // unexpected error from db.head()
});
});
module.exports = upsert;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment