Permalink
Browse files

optional revision argument for db.getDoc

  • Loading branch information...
1 parent 8299e8f commit d78ad5ed08334e4872605b77ba46fd27d36e4c65 Hendrik Schnepel committed Nov 1, 2011
Showing with 62 additions and 5 deletions.
  1. +2 −2 README.md
  2. +12 −3 lib/couchdb.js
  3. +48 −0 test/test-db.js
View
4 README.md
@@ -192,9 +192,9 @@ Wrapper for [PUT /db-name](http://wiki.apache.org/couchdb/HTTP_database_API#PUT_
Wrapper for [DELETE /db-name](http://wiki.apache.org/couchdb/HTTP_database_API#DELETE).
-### db.getDoc(id)
+### db.getDoc(id, [rev])
-Wrapper for [GET /db-name/doc-id](http://wiki.apache.org/couchdb/HTTP_Document_API#GET). Fetches a document with a given `id` from the database.
+Wrapper for [GET /db-name/doc-id\[?rev=\]](http://wiki.apache.org/couchdb/HTTP_Document_API#GET). Fetches a document with a given `id` and optional `rev` from the database.
### db.saveDoc(id, doc)
View
15 lib/couchdb.js
@@ -374,10 +374,19 @@ Db.prototype.remove = function(cb) {
return this.request('DELETE', '', cb);
};
-Db.prototype.getDoc = function(id, cb) {
- return this.request({
+Db.prototype.getDoc = function(id, rev, cb) {
+ var request = {
path: '/'+id
- }, cb);
+ };
+ if (!cb && typeof rev === 'function') {
+ cb = rev;
+ }
+ else {
+ request.query = {
+ 'rev': rev
+ };
+ }
+ return this.request(request, cb);
};
Db.prototype.saveDoc = function(id, doc, cb) {
View
48 test/test-db.js
@@ -5,6 +5,7 @@ var
TEST_ID = 'my-doc',
TEST_ID2 = 'my-doc2',
TEST_DOC = {hello: 'world'},
+ createUpdate = function(rev) { return {_id: TEST_ID, _rev: rev, hello: 'universe'}; },
callbacks = {
A: false,
@@ -28,6 +29,9 @@ var
S: false,
T: false,
U: false,
+ V: false,
+ W: false,
+ X: false
},
db = client.db(DB_NAME);
@@ -61,17 +65,61 @@ db
// Create a document with a given id
db
.saveDoc(TEST_ID, TEST_DOC, function(er, r) {
+
+ // Remember revisions for save and update tests.
+ var revisions = {
+ };
+
if (er) throw new Error(JSON.stringify(er));
callbacks.D = true;
assert.equal(TEST_ID, r.id);
assert.ok('rev' in r);
+ revisions.first = r.rev; // keep for next test
+ // Get the document, check contents
db
.getDoc(TEST_ID, function(er, doc) {
if (er) throw new Error(JSON.stringify(er));
callbacks.U = true;
assert.equal(doc.hello, TEST_DOC.hello);
});
+
+ // Update the document, check if the revision parameter works
+ db
+ .saveDoc(createUpdate(revisions.first), function(er, r) {
+ if (er) throw new Error(JSON.stringify(er));
+ callbacks.D = true;
+ assert.equal(TEST_ID, r.id);
+ assert.ok('rev' in r);
+ revisions.second = r.rev;
+
+ // Get document without revision, must contain the latest text.
+ db
+ .getDoc(TEST_ID, function(er, doc) {
+ if (er) throw new Error(JSON.stringify(er));
+ callbacks.V = true;
+ assert.notEqual(doc.hello, TEST_DOC.hello);
+ assert.equal(doc._rev, revisions.second);
+ });
+
+ // Get document with first revision, must contain the earlier text.
+ db
+ .getDoc(TEST_ID, revisions.first, function(er, doc) {
+ if (er) throw new Error(JSON.stringify(er));
+ callbacks.W = true;
+ assert.equal(doc.hello, TEST_DOC.hello);
+ assert.equal(doc._rev, revisions.first);
+ });
+
+ // Get document with latest revision explicitly, must contain the latest text.
+ db
+ .getDoc(TEST_ID, revisions.second, function(er, doc) {
+ if (er) throw new Error(JSON.stringify(er));
+ callbacks.X = true;
+ assert.notEqual(doc.hello, TEST_DOC.hello);
+ assert.equal(doc._rev, revisions.second);
+ });
+ });
});
// Let couch create a document id for us

0 comments on commit d78ad5e

Please sign in to comment.