2 * jqCouchDB - jQuery plugin for couchdb connections
3 * @requires jQuery > v1.0.3
4 * @requires couchDB >= v0.7.0a5575
6 * http://protoblogr.net
8 * Copyright (c) 2007 Jerry Jalava (protoblogr.net)
9 * Dual licensed under the MIT and GPL licenses:
10 * http://www.opensource.org/licenses/mit-license.php
11 * http://www.gnu.org/licenses/gpl.html
18 var db_exists = false;
19 var jqcouch_db = $.jqcouch.db;
21 jqcouch_db.on_error = function(request, caller) { db_exists = false; };
22 jqcouch_db.on_success = function(data, caller) { db_exists = data.exists; };
23 jqcouch_db.exists('/some_database');
27 $.jqcouch.db.create('/some_database');
30 jqcouch_db.on_success = function(data, caller)
34 jqcouch_db.info('/some_database');
38 * You don't have to override the on_error and on_success methods if you don't wan't to...
41 $.jqcouch.doc.save('/some_database/note1', {
45 $.jqcouch.doc.on_success = function(data, caller) {
47 $.jqcouch.doc.on_success = $.jqcouch.doc._original_on_success;
49 $.jqcouch.doc.get('/some_database/note1');
52 * You could also override the global function:
54 $.jqcouch.on_error = function(request,caller)
56 some_other_error_func(request, caller);
58 $.jqcouch.on_success = function(data,caller)
60 some_other_success_func(data, caller);
66 $.jqcouch = $.jqcouch || {};
68 $.jqcouch.parseJSON = function (json_str)
72 return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
73 json_str.replace(/"(\\.|[^"\\])*"/g, ''))) &&
74 eval('(' + json_str + ')');
81 $.jqcouch.toJSON = function (item,item_type)
94 var a = ['['], b, f, i, l = x.length, v;
95 for (i = 0; i < l; i += 1) {
98 if (typeof v == 'string') {
116 return isFinite(x) ? String(x) : 'null';
120 if (x instanceof Array) {
123 var a = ['{'], b, f, i, v;
127 if (typeof v == 'string') {
131 a.push(s.str(i), ':', v);
141 if (/["\\\x00-\x1f]/.test(x)) {
142 x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
149 Math.floor(c / 16).toString(16) +
150 (c % 16).toString(16);
153 return '"' + x + '"';
156 conv = function (x) {
157 var itemtype = typeof x;
180 var itemtype = item_type || typeof item;
201 throw("Unknown type for $.jqcouch.toJSON");
205 $.jqcouch.on_error = function(request, caller)
207 var error = "Error occured while talking with server!\n";
208 error += "request.status: "+request.status+"\n";
210 if (typeof caller != 'undefined')
212 error += "caller.method: "+caller.method+"\n";
213 error += "caller.action: "+caller.action+"\n";
214 if (caller.args != undefined)
216 error += "caller.args: "+caller.args.toString()+"\n";
220 var response = eval("(" + request.responseText + ")");
221 error += "response.error.reason: "+response.error.reason+"\n";
223 $.jqcouch.on_success = function(data, caller)
227 $.jqcouch.generate_query_str = function(query_args)
229 var query_string = '';
230 if (typeof query_args != 'undefined')
233 $.each(query_args, function(key,value){
234 query_string += key + '=' + value + '&';
236 query_string = query_string.substr(0,query_string.length-1);
242 $.jqcouch.server_url = '/';
244 $.jqcouch.ajax_setup = {
250 exists: function(path)
253 var server_url = $.jqcouch.server_url;
255 url: server_url + '_all_dbs',
257 global: $.jqcouch.ajax_setup.global,
258 cache: $.jqcouch.ajax_setup.cache,
261 contentType: 'application/json',
262 error: function(request){
263 $.jqcouch.db.on_error(request, {
264 method: 'jqcouch.db.exists',
270 success: function(data){
271 $.each(data, function(i,n){
277 $.jqcouch.db.on_success({exists: exists}, {
278 method: 'jqcouch.db.exists',
288 var server_url = $.jqcouch.server_url;
290 url: server_url + '_all_dbs',
292 global: $.jqcouch.ajax_setup.global,
293 cache: $.jqcouch.ajax_setup.cache,
296 contentType: 'application/json',
297 error: function(request){
298 $.jqcouch.db.on_error(request, {
299 method: 'jqcouch.db.get_all',
305 success: function(data){
306 $.jqcouch.db.on_success(data, {
307 method: 'jqcouch.db.get_all',
315 create: function(path)
317 if (path.substr(path.length-1,1) != '/')
322 var server_url = $.jqcouch.server_url;
324 url: server_url + path,
326 global: $.jqcouch.ajax_setup.global,
327 cache: $.jqcouch.ajax_setup.cache,
330 contentType: 'application/json',
331 error: function(request){
332 $.jqcouch.db.on_error(request, {
333 method: 'jqcouch.db.create',
339 success: function(data){
340 $.jqcouch.db.on_success(data, {
341 method: 'jqcouch.db.create',
349 remove: function(path)
351 if (path.substr(path.length-1,1) != '/')
356 var server_url = $.jqcouch.server_url;
358 url: server_url + path,
360 global: $.jqcouch.ajax_setup.global,
361 cache: $.jqcouch.ajax_setup.cache,
364 contentType: 'application/json',
365 error: function(request){
366 $.jqcouch.db.on_error(request, {
367 method: 'jqcouch.db.remove',
373 success: function(data){
374 $.jqcouch.db.on_success(data, {
375 method: 'jqcouch.db.remove',
385 if (path.substr(path.length-1,1) != '/')
390 var server_url = $.jqcouch.server_url;
392 url: server_url + path,
394 global: $.jqcouch.ajax_setup.global,
395 cache: $.jqcouch.ajax_setup.cache,
398 contentType: 'application/json',
399 error: function(request){
400 $.jqcouch.db.on_error(request, {
401 method: 'jqcouch.db.info',
407 success: function(data){
408 $.jqcouch.db.on_success(data, {
409 method: 'jqcouch.db.info',
419 var server_url = $.jqcouch.server_url;
422 url: server_url + '_restart',
424 global: $.jqcouch.ajax_setup.global,
426 error: function(request){
427 $.jqcouch.db.on_error(request, {
428 method: 'jqcouch.db.restart',
434 success: function(data){
435 $.jqcouch.db.on_success(data, {
436 method: 'jqcouch.db.restart',
445 on_error: function(request, caller)
447 $.jqcouch.on_error(request, caller);
449 on_success: function(data, caller)
451 $.jqcouch.on_success(data, caller);
453 _original_on_error: this.on_error,
454 _original_on_success: this.on_success
458 get: function(path, query_args)
460 path += $.jqcouch.generate_query_str(query_args);
462 var server_url = $.jqcouch.server_url;
464 url: server_url + path,
466 global: $.jqcouch.ajax_setup.global,
467 cache: $.jqcouch.ajax_setup.cache,
470 contentType: 'application/json',
471 error: function(request){
472 $.jqcouch.doc.on_error(request, {
473 method: 'jqcouch.doc.get',
479 success: function(data){
480 $.jqcouch.doc.on_success(data, {
481 method: 'jqcouch.doc.get',
489 get_all: function(path, query_args)
491 if (path.substr(path.length-1,1) != '/')
496 path += $.jqcouch.generate_query_str(query_args);
498 var server_url = $.jqcouch.server_url;
500 url: server_url + path,
502 global: $.jqcouch.ajax_setup.global,
503 cache: $.jqcouch.ajax_setup.cache,
506 contentType: 'application/json',
507 error: function(request){
508 $.jqcouch.doc.on_error(request, {
509 method: 'jqcouch.doc.get_all',
511 args: {query_args: query_args}
515 success: function(data){
516 $.jqcouch.doc.on_success(data, {
517 method: 'jqcouch.doc.get_all',
519 args: {query_args: query_args}
525 save: function(path, data)
527 if (path.substr(path.length-1,1) != '/')
532 if ( data._id == undefined
535 $.jqcouch.doc.post(path, data);
539 $.jqcouch.doc.put(path + data._id, data);
542 put: function(path, data)
544 var server_url = $.jqcouch.server_url;
546 url: server_url + path,
548 data: $.jqcouch.toJSON(data),
550 global: $.jqcouch.ajax_setup.global,
551 cache: $.jqcouch.ajax_setup.cache,
554 contentType: 'application/json',
555 error: function(request){
556 $.jqcouch.doc.on_error(request, {
557 method: 'jqcouch.doc.put',
563 success: function(data){
564 $.jqcouch.doc.on_success(data, {
565 method: 'jqcouch.doc.put',
573 post: function(path, data)
575 var server_url = $.jqcouch.server_url;
577 url: server_url + path,
579 global: $.jqcouch.ajax_setup.global,
580 cache: $.jqcouch.ajax_setup.cache,
583 contentType: 'application/json',
584 data: $.jqcouch.toJSON(data),
586 error: function(request){
587 $.jqcouch.doc.on_error(request, {
588 method: 'jqcouch.doc.post',
590 args: {path: path, data: data}
594 success: function(data){
595 $.jqcouch.doc.on_success(data, {
596 method: 'jqcouch.doc.post',
598 args: {path: path, data: data}
604 bulk_save: function(path, data, query_args)
606 if (path.substr(path.length-1,1) != '/')
610 path += '_bulk_docs';
611 path += $.jqcouch.generate_query_str(query_args);
613 var server_url = $.jqcouch.server_url;
615 url: server_url + path,
617 global: $.jqcouch.ajax_setup.global,
618 cache: $.jqcouch.ajax_setup.cache,
621 contentType: 'application/json',
622 data: $.jqcouch.toJSON(data),
624 error: function(request){
625 $.jqcouch.doc.on_error(request, {
626 method: 'jqcouch.doc.bulk_save',
628 args: {path: path, data: data}
632 success: function(data){
633 $.jqcouch.doc.on_success(data, {
634 method: 'jqcouch.doc.bulk_save',
636 args: {path: path, data: data}
642 del: function(path, revision)
644 if (typeof revision != 'undefined')
646 path += '?rev=' + revision;
649 var server_url = $.jqcouch.server_url;
651 url: server_url + path,
653 global: $.jqcouch.ajax_setup.global,
654 cache: $.jqcouch.ajax_setup.cache,
657 contentType: 'application/json',
658 error: function(request){
659 $.jqcouch.doc.on_error(request, {
660 method: 'jqcouch.doc.del',
666 success: function(data){
667 $.jqcouch.doc.on_success(data, {
668 method: 'jqcouch.doc.del',
677 on_error: function(request, caller)
679 $.jqcouch.on_error(request, caller);
681 on_success: function(data, caller)
683 $.jqcouch.on_success(data, caller);
685 _original_on_error: this.on_error,
686 _original_on_success: this.on_success
690 exists: function(path, name)
695 if (path.substr(path.length-1,1) != '/')
701 var server_url = $.jqcouch.server_url;
703 url: server_url + path,
705 global: $.jqcouch.ajax_setup.global,
706 cache: $.jqcouch.ajax_setup.cache,
709 contentType: 'application/json',
710 error: function(request){
711 $.jqcouch.view.on_error(request, {
712 method: 'jqcouch.view.exists',
718 success: function(data){
719 $.each(data.rows, function(i,n){
726 $.jqcouch.view.on_success({exists: exists, rev: rev}, {
727 method: 'jqcouch.view.exists',
735 get: function(path, query_args)
737 $.jqcouch.doc.on_error = $.jqcouch.view.on_error;
738 $.jqcouch.doc.on_success = $.jqcouch.view.on_success;
739 $.jqcouch.doc.get(path, query_args);
741 save: function(path, data)
743 if ( data._id == undefined
749 if (path.substr(path.length-1,1) != '/') {
753 $.jqcouch.view.put(path + data._id, data);
755 put: function(path, data)
757 $.jqcouch.doc.on_error = $.jqcouch.view.on_error;
758 $.jqcouch.doc.on_success = $.jqcouch.view.on_success;
759 $.jqcouch.doc.put(path, data);
763 $.jqcouch.doc.on_error = $.jqcouch.view.on_error;
764 $.jqcouch.doc.on_success = $.jqcouch.view.on_success;
765 $.jqcouch.doc.del(path);
767 temp: function(path, query_args, map)
769 if (path.substr(path.length-1,1) != '/')
773 path += '_temp_view';
774 path += $.jqcouch.generate_query_str(query_args);
776 var server_url = $.jqcouch.server_url;
778 if (typeof(map) == 'string') {
782 if (typeof(map['toSource']) == 'function') {
783 var map = map.toSource();
789 url: server_url + path,
791 global: $.jqcouch.ajax_setup.global,
792 cache: $.jqcouch.ajax_setup.cache,
796 contentType: 'application/javascript',
798 error: function(request){
799 $.jqcouch.view.on_error(request, {
800 method: 'jqcouch.view.temp',
801 action: '_temp_view',
802 args: {query_args: query_args}
806 success: function(data){
807 $.jqcouch.view.on_success(data, {
808 method: 'jqcouch.view.temp',
809 action: '_temp_view',
810 args: {query_args: query_args}
817 on_error: function(request, caller)
819 $.jqcouch.on_error(request, caller);
821 on_success: function(data, caller)
823 $.jqcouch.on_success(data, caller);
825 _original_on_error: this.on_error,
826 _original_on_success: this.on_success