Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #32 from hsch/master

Optional revision in db.getDoc
  • Loading branch information...
commit 7be6bf3810596203d66fedeb3d4ab7280c6ef783 2 parents 59680db + 87539cf
@felixge authored
Showing with 64 additions and 7 deletions.
  1. +3 −3 README.md
  2. +12 −3 lib/couchdb.js
  3. +1 −1  test/common.js
  4. +48 −0 test/test-db.js
View
6 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)
@@ -288,7 +288,7 @@ Returns an `events.EventEmitter` stream that emits the following events:
* `data(change)`: Emitted for each change line in the stream. The `change` parameter holds the change object.
* `heartbeat`: Emitted for each heartbeat send by CouchDB, no need to check this for most stuff.
-* `end(hadError)`: Emitted if the stream ends. This should not happen unless you manually invoke `stream.end()`.
+* `end(hadError)`: Emitted if the stream ends. This should not happen unless you manually invoke `stream.close()`.
See the [CouchDB docs](http://wiki.apache.org/couchdb/HTTP_database_API#Changes) for available `query` parameters.
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
2  test/common.js
@@ -11,4 +11,4 @@ global.checkCallbacks = function(callbacks) {
};
// Provide a port/host here if your local db has a non-default setup
-GLOBAL.client = couchdb.createClient(undefined, undefined, undefined, undefined, 20);
+GLOBAL.client = couchdb.createClient(undefined, undefined, undefined, undefined, 0);
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
Please sign in to comment.
Something went wrong with that request. Please try again.