Skip to content

Commit

Permalink
adding Database#allDocs()
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicbarnes committed Aug 11, 2014
1 parent aaef0eb commit c000af9
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/database.js
Expand Up @@ -70,3 +70,23 @@ Database.prototype.info = function (callback) {
return this.client.request("get", this.url())
.end(utils.callback(callback.bind(this)));
};

Database.prototype.allDocs = function (query, callback) {
if (typeof query === "function") {
callback = query;
query = null;
}

var url = this.url("_all_docs");
var req;

if (!query) {
req = this.client.request("get", url);
} else if (Array.isArray(query)) {
req = this.client.request("post", url).send({ keys: query });
} else {
req = this.client.request("get", url).query(query);
}

return req.end(utils.callback(callback.bind(this)));
};
52 changes: 52 additions & 0 deletions test/database.js
Expand Up @@ -285,4 +285,56 @@ describe("Database API", function () {
expect(doc.rev()).to.equal("rev");
});
});

describe("Database#allDocs(query, callback)", function () {
var db = srv.db("test");

it("should retrieve a list of documents", function (done) {
couchdb
.get("/test/_all_docs")
.replyWithFile(200, mockFile("all_docs.json"));

db.allDocs(function (err, results) {
if (err) return done(err);
expect(results.offset).to.equal(0);
done();
});
});

it("should include querystring params", function (done) {
couchdb
.get("/test/_all_docs?conflicts=true&skip=10")
.replyWithFile(200, mockFile("all_docs.json"));

var query = {
conflicts: true,
skip: 10
};

db.allDocs(query, function (err, results) {
if (err) return done(err);
expect(results.total_rows).to.equal(5);
done();
});
});

it("should send a post for multiple keys", function (done) {
var keys = [
"Zingylemontart",
"Yogurtraita"
];

couchdb
.post("/test/_all_docs", {
keys: keys
})
.replyWithFile(200, mockFile("all_docs-post.json"));

db.allDocs(keys, function (err, results) {
if (err) return done(err);
expect(results.total_rows).to.equal(2666);
done();
});
});
});
});
20 changes: 20 additions & 0 deletions test/mocks/database/all_docs-post.json
@@ -0,0 +1,20 @@
{
"total_rows" : 2666,
"rows" : [
{
"value" : {
"rev" : "1-a3544d296de19e6f5b932ea77d886942"
},
"id" : "Zingylemontart",
"key" : "Zingylemontart"
},
{
"value" : {
"rev" : "1-91635098bfe7d40197a1b98d7ee085fc"
},
"id" : "Yogurtraita",
"key" : "Yogurtraita"
}
],
"offset" : 0
}
41 changes: 41 additions & 0 deletions test/mocks/database/all_docs.json
@@ -0,0 +1,41 @@
{
"offset": 0,
"rows": [
{
"id": "16e458537602f5ef2a710089dffd9453",
"key": "16e458537602f5ef2a710089dffd9453",
"value": {
"rev": "1-967a00dff5e02add41819138abb3284d"
}
},
{
"id": "a4c51cdfa2069f3e905c431114001aff",
"key": "a4c51cdfa2069f3e905c431114001aff",
"value": {
"rev": "1-967a00dff5e02add41819138abb3284d"
}
},
{
"id": "a4c51cdfa2069f3e905c4311140034aa",
"key": "a4c51cdfa2069f3e905c4311140034aa",
"value": {
"rev": "5-6182c9c954200ab5e3c6bd5e76a1549f"
}
},
{
"id": "a4c51cdfa2069f3e905c431114003597",
"key": "a4c51cdfa2069f3e905c431114003597",
"value": {
"rev": "2-7051cbe5c8faecd085a3fa619e6e6337"
}
},
{
"id": "f4ca7773ddea715afebc4b4b15d4f0b3",
"key": "f4ca7773ddea715afebc4b4b15d4f0b3",
"value": {
"rev": "2-7051cbe5c8faecd085a3fa619e6e6337"
}
}
],
"total_rows": 5
}

0 comments on commit c000af9

Please sign in to comment.