Skip to content
Browse files

Merged changes from flashingpumpkin

  • Loading branch information...
2 parents 2ee3837 + 0388fdd commit 081db3e1c4244397290148c0de1f429db85cb349 @devioustree devioustree committed Dec 22, 2010
Showing with 63 additions and 2 deletions.
  1. +24 −2 lib/couch-client.js
  2. +39 −0 viewTest.js
View
26 lib/couch-client.js
@@ -270,7 +270,8 @@ function CouchClient(url) {
});
}
- function view(viewName, obj, callback) {
+ var viewQueue = [];
+ function view(view, obj, callback) {
if (typeof obj === 'function') {
callback = obj;
obj = null;
@@ -284,7 +285,28 @@ function CouchClient(url) {
viewName = viewName + '?' + getParams;
}
}
- request("GET", viewName, callback);
+ function consumeQueue(){
+ var pending = viewQueue.shift();
+ if (viewQueue.length) process.nextTick(consumeQueue);
+ var view = pending[0], callback = pending[1];
+ request("GET", view, function(err, result){
+ if(result.error){
+ if(result.error == 'not_found'){
+ err = new Error("Design document not found: " + JSON.stringify(view));
+ err.errno = process.ENOENT;
+ }else{
+ err = new Error("CouchDB Error " + JSON.stringify(result));
+ }
+ }
+ if(err){
+ callback(err);
+ return;
+ }
+ callback(null, result);
+ });
+ }
+ viewQueue.push([view, callback]);
+ process.nextTick(consumeQueue);
}
// Expose the public API
View
39 viewTest.js
@@ -0,0 +1,39 @@
+var CouchClient = require('./lib/couch-client');
+
+var Meetings = CouchClient("http://hack:me@localhost:5984/meetings");
+
+var designDoc = {
+ '_id': '_design/all'
+ , 'language': 'javascript'
+ , 'views': {
+ 'all': {
+ 'map': (function(doc){
+ emit(doc._id, doc)
+ }).toString()
+ }
+ }
+};
+
+Meetings.view(Meetings.uri.pathname + '/_design/all/_view/all', function(err, result){
+ if(!err || result)
+ throw new Error('Fail. View is not in database, should have yielded an error.');
+
+ Meetings.save(designDoc, function(err, result){
+ if (err)
+ throw err;
+
+ Meetings.save({some: 'Document'}, function(err, result){
+ if (err)
+ throw err;
+
+ Meetings.view('/meetings/_design/all/_view/all', function(err, result){
+ if (err || !result){
+ throw new Error('Fail. View is in database but didn\'t yield any results');
+ }
+ if(result.rows.length != 1)
+ throw new Error('Fail. Wrong number of documents.');
+ process.exit(0);
+ });
+ });
+ });
+});

0 comments on commit 081db3e

Please sign in to comment.
Something went wrong with that request. Please try again.