Skip to content

Added getDocs #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,22 @@ Wrapper for [DELETE /db-name](http://wiki.apache.org/couchdb/HTTP_database_API#D

Wrapper for [GET /db-name/doc-id\[?rev=\]\[&attachments=\]](http://wiki.apache.org/couchdb/HTTP_Document_API#GET). Fetches a document with a given `id` and optional `rev` and/or `attachments` from the database.

### db.getDocs(keys/startkey, [endkey])

Fetches multiple documents using [/\_all\_docs](http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API#Fetch_Multiple_Documents_With_a_Single_Request).

Example:

db.getDocs(['aa', 'bb', 'cc'], function (er, r) {
if (er) throw new Error(JSON.stringify(er));
// r => {"total_rows":10,"offset":2,"rows":[{...}]}
});

db.getDocs('aa', 'cc', function (er, r) {
if (er) throw new Error(JSON.stringify(er));
// r => {"total_rows":10,"offset":2,"rows":[{...}]}
});

### db.saveDoc(id, doc)

Wrapper for [PUT /db-name/doc-id](http://wiki.apache.org/couchdb/HTTP_Document_API#PUT). Saves a json `doc` with a given `id`.
Expand Down
24 changes: 24 additions & 0 deletions lib/couchdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,30 @@ Db.prototype.getDoc = function(id, rev, attachments, cb) {
return this.request(request, cb);
};

Db.prototype.getDocs = function (startkey, endkey, cb) {
// if startkey is an array of keys use POST to get docs
if (Array.isArray(startkey) && typeof(endkey) === 'function') {
cb = endkey;
return this.request({
path: '/_all_docs',
query: {include_docs: true},
data: {keys:startkey},
method: 'POST'
}, cb);
} else {
// if startkey and endkey are provided use default GET
var query = {
include_docs: true,
startkey: startkey,
endkey: endkey
}
return this.request({
path: '/_all_docs',
query: query
}, cb);
}
};

Db.prototype.saveDoc = function(id, doc, cb) {
if (typeof id == 'object') {
cb = doc;
Expand Down