Using REST to upsert a document in CouchDB

There's no REST equivalent of Cradle's merge when you want to perform an upsert; instead, insertion is handled by a HTTP POST request, while updating is handled by a PUT request.

How to do it...

Here's some HTML and a doUpsert method that looks at form elements on your HTML page and either creates a new document in the database or updates an existing document if one already exists and you pass both the ID and revision fields:

<!DOCTYPE html> <html> <head> <script src="//code.jquery.com/jquery-1.11.2.min.js"></script> <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> </head> <body> <p>Hello world</p> <p> <div id="debug"></div> </p> <p> <div id="json"></div> </p> <p> <div id="result"></div> ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.