Definitions
CouchJs : the JavaScript interpreter for CouchDB
CouchDB : a NonSQL db where you execute most things in user-space with things such as JS because things tend to be expensive to execute on the database-level
Integrity : In information security, integrity means that data cannot be modified undetectably. More here.
The CouchJS website has no internal search, and after googling more-or-less confusing blog articles, I'm getting lost. Where can I find some simple examples (not just curls/telnets) like below around?
<script type="text/javascript" src="lib/jquery-1.7.2.js"></script>
<script type="text/javascript" src="lib/jquery-ui-1.8.21.custom.min.js"></script>
<script src="http://127.0.0.1:5984/_utils/script/jquery.couch.js"></script>
//# 1. initilalize the db -object
//# 2. save something to the db with the object
<script> db.test.save('hello world') </script>
Perhaps useful to other newbies
CouchDB a real world example, thread with references to free books etc
NonSQL: initialization of DB -object in browser console? DB -logic in the user-space?
Source code of CouchJS here.
https://stackoverflow.com/questions/11077093/what-are-ajax-options-in-couchjs-explain-this-odd-null-error-with-couchjs
Building on top of the Florian's answer -- because he removed his answer when I pointed out some errors here, please, skimread his answer in history. He suggested the article about document update handlers here and the example here. There are two problems at least, the XMLHttpRequest here takes a string, not object -- I edited his answer to add the XMLHttpRequest -manual for further investigation but for some reason someone cancelled the edit. So I am making my own answer having the most fixes but still irritating DOM 11 that I am still trying to solve.
// By this function we solve the problem with Object,
// changing JSON -object to string. The source is:
// https://stackoverflow.com/questions/111529/create-query-parameters-in-javascript
function EncodeQueryData(data)
{
var ret = [];
for (var d in data)
ret.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
return ret.join("&");
}
// We use ready CouchDB -example, source:
// http://wiki.apache.org/couchdb/HTTP_Document_API#PUT
var datas = {
"Subject":"I like Plankton",
"Author":"Rusty",
"PostedDate":"2006-08-15T17:30:12-04:00",
"Tags":["plankton", "baseball", "decisions"],
"Body":"I decided today that I don't like baseball. I like plankton."
};
// We use the default -function, please, see the O'Reilly.
// https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest
var xhr = new XMLHttpRequest();
// This may be wrong
xhr.open( 'POST', 'test.js', true );
xhr.setRequestHeader( 'Content-Type', 'application/json' );
xhr.open( 'POST', '127.0.0.1:5984/test/559c327683fe0acb96aff72bd174c258', true);
var msg = EncodeQueryData(datas);
xhr.send(msg);
TODO
- Now there is one error XMLHttpRequest "does not handle nested objects" but string so we need either more complex command or arrays
with XMLHttpRequest. I thank C.*rk for helping me with this.
- Probably RELATING TO test.js, under investigation the err:
"XMLHttpRequest cannot load
%3127.0.0.1:5984/test/559c327683fe0acb96aff72bd174c258. Cross origin
requests are only supported for HTTP. Error: NETWORK_ERR:
XMLHttpRequest Exception 101"
Related puzzles
- [solved] Explain DOM 11 error in the initialization of XMLHttpRequest with JSON -headers
I just cruised through the intro pages of the CouchJS site, and one of the first things I found was:
CouchDB also speaks Plain Old HTTP, so getting going with JavaScript
is a walk in the park with the XMLHttpRequest object.
Going from here is just a matter using the normal CouchDB HttpRestApi to get what you want.
So based on this, it sounds like you need a jquery plugin that abstracts/wraps the CouchDB Rest API to write your ajax calls.
So I googled jquery couch
and, as you said, there were quite a few results that were not clear cut or pointing to any helpful documentation or a plugin, with the exception of $.couch
, which looks like it's a beast, but the documentation is clean and it looks like it can do just about anything that the Rest API allows.
"You communicate with CouchDB via HTTP requests. So all you have to do is to use $.ajax and make these requests (instead of curl) (you might have to enable JSONP in the CouchDB settings)." ~Felix Kling's comment
JSONP can be enabled in http://127.0.0.1:5984/_utils/config.html
as an admin.
I collected some references but cannot yet say the best one and cannot yet say whether the AJAX -thing solves the q. Anyway this one looks useful, How do I POST new data to CouchDB using JavaScript/jQuery. I hope Felix Kling could answer more fully about this but here some issues for further research.
Perhaps related: something about AJAX and JSONP
CouchDB AJAX interaction
How do I perform an Ajax request to CouchDB (http://<usename>.couchone.com/)
Connection AJAX, CouchDB and JavaScript, something about JSONP -support
JQuery .ajax dosen't work with http://127.0.0.1:5984
How do I POST new data to CouchDB using JavaScript/jQuery