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

Already on GitHub? Sign in to your account

Optional revision in db.getDoc #32

Merged
merged 3 commits into from Nov 4, 2011
Jump to file or symbol
Failed to load files and symbols.
+64 −7
Split
View
@@ -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
@@ -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
@@ -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
@@ -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