Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Optional revision in db.getDoc #32

Merged
merged 3 commits into from

2 participants

@hsch

Hi again,

thanks for merging my previous request. Here is the feature that I actually needed.

Any chance of seeing this on npm sooner or later? (I did notice the deprecation and un-deprecation (hooray!) of this project, so I'm not sure what felixge's or norlin's plans are.)

Regards,
Hendrik

@felixge felixge merged commit 7be6bf3 into felixge:master
@felixge
Owner

Thanks, I will push out a new npm version once I get access to the npm package. Somebody else is maintaining that right now.

@felixge
Owner

I just pushed a new version to npm now that I got access again to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 1, 2011
  1. switching this off is probably more robust for tests

    Hendrik Schnepel authored
  2. optional revision argument for db.getDoc

    Hendrik Schnepel authored
  3. fixed docs: stream.close, not stream.end (fixes #27)

    Hendrik Schnepel authored
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.