O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required