From 926af4ba11093dcaff13bea0e4ed9addfc67ab10 Mon Sep 17 00:00:00 2001 From: John Christopher Anderson Date: Wed, 16 Sep 2009 22:04:18 +0000 Subject: include_docs now take an _id (as well as a _rev) in the emitted value, to load docs other than the one doing the emitting. This means you can have one doc list a set of other docs to load in a single query. Enjoy! git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@815984 13f79535-47bb-0310-9956-ffa450edef68 --- share/www/script/test/view_include_docs.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'share/www/script/test/view_include_docs.js') diff --git a/share/www/script/test/view_include_docs.js b/share/www/script/test/view_include_docs.js index 8d50784d..06aafc56 100644 --- a/share/www/script/test/view_include_docs.js +++ b/share/www/script/test/view_include_docs.js @@ -29,6 +29,9 @@ couchTests.view_include_docs = function(debug) { with_prev: { map: "function(doc){if(doc.prev) emit(doc._id,{'_rev':doc.prev}); else emit(doc._id,{'_rev':doc._rev});}" }, + with_id: { + map: "function(doc) {if(doc.link_id) { var value = {'_id':doc.link_id}; if (doc.link_rev) {value._rev = doc.link_rev}; emit(doc._id, value);}};" + }, summate: { map:"function (doc) {emit(doc.integer, doc.integer)};", reduce:"function (keys, values) { return sum(values); };" @@ -84,6 +87,21 @@ couchTests.view_include_docs = function(debug) { T(resp.rows.length == 1); T(resp.rows[0].value == 4950); + T(db.save({ + "_id": "link-to-10", + "link_id" : "10" + }).ok); + + // you can link to another doc from a value. + resp = db.view("test/with_id", {key:"link-to-10"}); + T(resp.rows[0].key == "link-to-10"); + T(resp.rows[0].value["_id"] == "10"); + + resp = db.view("test/with_id", {key:"link-to-10",include_docs: true}); + T(resp.rows[0].key == "link-to-10"); + T(resp.rows[0].value["_id"] == "10"); + T(resp.rows[0].doc._id == "10"); + // Check emitted _rev controls things resp = db.allDocs({include_docs: true}, ["0"]); var before = resp.rows[0].doc; @@ -91,11 +109,13 @@ couchTests.view_include_docs = function(debug) { var after = db.open("0"); after.integer = 100; after.prev = after._rev; - T(db.save(after).ok); + resp = db.save(after) + T(resp.ok); var after = db.open("0"); - T(after._rev != after.prev); - T(after.integer == 100); + TEquals(resp.rev, after._rev, "fails with firebug running"); + T(after._rev != after.prev, "passes"); + TEquals(100, after.integer, "fails with firebug running"); // should emit the previous revision resp = db.view("test/with_prev", {include_docs: true}, ["0"]); -- cgit v1.2.3