Skip to content

Commit

Permalink
Support for GeoCouch and bug fixes
Browse files Browse the repository at this point in the history
Added full support for GeoCouch. Some bug fixes and changes on URL
composition.
  • Loading branch information
landeiro committed Dec 3, 2011
1 parent c9b9e28 commit 53ff261
Show file tree
Hide file tree
Showing 9 changed files with 1,293 additions and 757 deletions.
190 changes: 149 additions & 41 deletions README.md
Expand Up @@ -20,9 +20,6 @@ With this method we can send all data necessary to query views in the same struc
{db:"developers",design:"basic_info",view:"by_name",args:{reduce:false},request:{keys:["Jane"]}}


## Requirements
Tested on node.js v0.4.7 and CouchDB 1.1.1

PJsonCouch requires knowing the [CouchDB API](http://techzone.couchbase.com/sites/default/files/uploads/all/documentation/couchbase-api.html) and does not intend to overcome the logic behind the original API.

## How is PJsonCouch organized?
Expand All @@ -40,73 +37,184 @@ PJsonCouch is inspired on the CouchDB HTTP API structure, so is divide in 4 main

How to connect to CouchDB without db definition and request a complex query to `_all_docs`

``` js
// Constructor without db definition
var test = PJsonCouch({protocol:"http",host:"127.0.0.1",port:5984});

var test = PJsonCouch({
host : "127.0.0.1",
port : 5984
});

// set a DB, this does not have callbackfuntion
test.setDB({db:"thisisatempdb"});

test.setDB({
db : "thisisatempdb"
});

// Login if your DB is protected
test.login({user:"landeiro",password:"123"},function(r){
test.login({
user : "landeiro",
password : "123"
}, function(r) {
// Get all docs from DB, supports all args and request data from original couchdb API
test.queryDB({action:"_all_docs", args:{descending:true,startkey:"objmyid6",endkey:"objmyid2"}}, function(r){log("*queryDB start/endkey*",r);});
test.queryDB({action:"_all_docs", request:{keys:["objmyid1","objmyid7"]}}, function(r){log("*queryDB keys*",r);});
test.queryDB({
action : "_all_docs",
args : {
descending : true,
startkey : "objmyid6",
endkey : "objmyid2"
}
}, function(r) {
log("*queryDB start/endkey*", r);
});
test.queryDB({
action : "_all_docs",
request : {
keys : ["objmyid1", "objmyid7"]
}
}, function(r) {
log("*queryDB keys*", r);
});
});

```

How to connect to CouchDB with db definition and execute some docs operations

var test = PJsonCouch({protocol:"http",host:"127.0.0.1",port:5984,db:"mydatabase"});
``` js
var test = PJsonCouch({
host : "127.0.0.1",
port : 5984,
db : "mydatabase"
});

// Docs for test
var docs = [{_id:"objmyid0",a:0},{_id:"objmyid1",a:1},{_id:"objmyid2",a:3}];
var docs = [{
_id : "objmyid0",
a : 0
}, {
_id : "objmyid1",
a : 1
}, {
_id : "objmyid2",
a : 3
}];

// Save bulk, supports all request data from original couchdb API
test.saveBulkDocs({docs:docs},function(r){
log("insert bulk",r);
// Get Doc, supports all arguments from original couchdb API
test.getDoc({id:"objmyid0"},function (gdoc) {
log("*get doc*",gdoc)
if (!gdoc.error) {
// Delete a Doc, supports all arguments from original couchdb API
test.deleteDoc({doc:gdoc}, function (r) { log("*deleted "+gdoc._id+"*",r);});
}
});
// Save bulk, supports all request data from original couchdb API
test.saveBulkDocs({
docs : docs
}, function(r) {
log("insert bulk", r);
// Get Doc, supports all arguments from original couchdb API
test.getDoc({
id : "objmyid0"
}, function(gdoc) {
log("*get doc*", gdoc)
if(!gdoc.error) {
// Delete a Doc, supports all arguments from original couchdb API
test.deleteDoc({
doc : gdoc
}, function(r) {
log("*deleted " + gdoc._id + "*", r);
});
}
});
});
```



Some random server operations


test.server({},function (r) {log("*welcome msg*",r);});
``` js
test.server({}, function(r) {
log("*welcome msg*", r);
});

// Show active tasks
test.server({action:"_active_tasks"},function (r) {log("*active tasks*",r);});
test.server({
action : "_active_tasks"
}, function(r) {
log("*active tasks*", r);
});

// Start continuos replication database. Replication supports all configuration from original couchdb API
test.server({action:"_replicate",source:"thisisatempdb",target:"http://somedomain.com/land",continuous:true},function (r) {
log("*replicate*",r);
test.server({
action : "_replicate",
source : "thisisatempdb",
target : "http://somedomain.com/land",
continuous : true
}, function(r) {
log("*replicate*", r);
// Cancel continuos replication database, supports all configuration from original couchdb API
test.server({action:"_replicate",source:"thisisatempdb",target:"http://somedomain.com/land",continuous:true,cancel:true},function (r) {log("*cancel replication*",r);})

test.server({
action : "_replicate",
source : "thisisatempdb",
target : "http://somedomain.com/land",
continuous : true,
cancel : true
}, function(r) {
log("*cancel replication*", r);
})
});

```

Some random design doc operations

``` js
// List customers using view by_name from "Jane" to "Pedro"
test.queryDesign({design:"design_doc",list:"customers",view:"by_name",args:{startkey:"Jane",endkey:"Pedro"}},function (r) {log("*get list with view with args*",r);});

test.queryDesign({
design : "design_doc",
list : "customers",
view : "by_name",
args : {
startkey : "Jane",
endkey : "Pedro"
}
}, function(r) {
log("*get list with view with args*", r);
});
// Display a Doc using Show costumer, supports all arguments from original couchdb API
test.queryDesign({design:"design_doc",show:"customer",id:"3535e007bd9c765de0554c0a4900385b"},function (r) {log("*get show*",r);});

// Getting docs using view by_name with request keys ["Jane"] and argument limit=1
test.queryDesign({design:"design_doc",view:"by_name",args:{limit:1},request:{keys:["Jane","joey"]}},function (r) {log("*get view with request and args*",r);});

test.queryDesign({
design : "design_doc",
show : "customer",
id : "3535e007bd9c765de0554c0a4900385b"
}, function(r) {
log("*get show*", r);
});
// Getting docs using view by_name with request keys ["Jane"] and argument limit=1
test.queryDesign({
design : "design_doc",
view : "by_name",
args : {
limit : 1
},
request : {
keys : ["Jane", "joey"]
}
}, function(r) {
log("*get view with request and args*", r);
});
```

GeoCouch Integration

``` js
// Make a bounding box request
test.queryDesign({
db : "places",
design : "main",
spatial : "points",
args : {
bbox : "0,0,180,90"
}
}, function(data) {
console.log(data);
});
```

### Errors can be configured to return a lot of debug, like request content and headers.

Cannot connect do CouchDB


``` js
{ error: 'request_to_couchdb',
detail:
{ stack: 'Error: ECONNREFUSED, Connection refused\n at Socket._onConnect (net.js:576:18)\n at IOWatcher.onWritable [as callback] (net.js:165:12)',
Expand Down Expand Up @@ -148,7 +256,7 @@ Unauthorized
'content-type': 'text/plain;charset=utf-8',
'content-length': '64',
'cache-control': 'must-revalidate' } } }

```

See more examples [here](https://github.com/landeiro/PJsonCouch/wiki/PJsonCouch).

Expand Down

0 comments on commit 53ff261

Please sign in to comment.