Cross-domain AJAX for CouchDB, distributed as a CouchApp.
First, create a new database in CouchDB to store the app. The instructions assume you’ll name it “couchdb-xd”.
$ git clone [email protected]:benvinegar/couchdb-xd.git $ cd couchdb-xd $ couchapp push . http://user:[email protected]/couchdb-xd
Afterwards, include the following script tag in your app/web page:
<script src="http://your-couch-host.com/couchdb-xd/_design/couchdb-xd/couchdb.js" type="text/javascript"></script>
You’ll now have access to the Couch global JavaScript object, which exposes a number of utility classes and methods for manipulating any CouchDB database hosted on that domain. See below for usage.
Initialize the library.
Couch.init(function() { // execute on ready });
Get, create, and delete databases.
var server = new Couch.Server('http://localhost:5984', 'user', 'pass'); server.get(server, 'dbname', function(resp) { }); server.destroy(server, 'dbname', function(resp) { }); server.create(server, 'dbname', function(resp) { });
Get, create, update, delete, and copy documents.
var db = new Couch.Database(server, 'dbname'); db.get('some-record', function(resp) { }); db.put('some-record', { hello: 'world' }, function(resp) { }); db.post({ hello: 'world' }, function(resp) { }); db.destroy('some-record', { rev: 'abcdef123456789' }, function(resp) { }); db.copy('some-record', 'new-record', { rev: 'abcdef123456789' }, function(resp) { });
After installing, you can run the provided unit tests at:
http://your-couch-host.com/couchdb-xd/_design/couchdb-xd/test/index.html
Only works with browsers that support window.postMessage (IE8+, Firefox 3.5+, Safari 4+, Chrome).
- Bulk Document API
- Database API
- _changes, _all_dbs
- http://wiki.apache.org/couchdb/HTTP_database_API
- Support non-postMessage browsers