couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MK>
Subject couchDB and node.js double bind
Date Sun, 22 May 2011 17:43:28 GMT
I'm pseudo cross posting this to the nodejs list, but the replies will
not cross-post so hopefully no one is too troubled by that.

I'm new to both and I've hit a bit of a conundrum.

I have a node request handler that presents a view of a couchDB
document.  The document contains an array of other document id's.  In
the view, I want to list those documents, and include a piece of
information from them (call it the "short description").  This means,
in addition to querying couch for the primary doc, I have to query it
for the short description of all the docs listed in the primary doc's
array of related pages.

Sounds simple.  I have the array, now all I have to do is

var shortDescs = new Array();
for (i in array) {
      //query couch for data

Except the only way to query couch from node is via asynchronous http
requests.  Ie, when this loop completes, it is very unlikely that the
shortDescs array will be properly populated.  So this is a dead end.

That's the way nodejs works, and it has that limitation.  The way couch
works also results in some limitations.  You cannot make a single
request for an arbitrary set of documents.  You can set up a "view"
function that will return data from all the documents that match a
specific criteria *but that criteria must be hardcoded*.  In other
words, you cannot create a view function that takes arbitrary
parameters, such as an array of ids.

In short, AFAICT, it is simply impossible to do this using couchDB and
nodejs together, which seems a shame.

I can see three choices to work around this:

- Make the primary document's array an array of objects which contains
the short desc from the other docs.  Obviously, this will result in a
tremendous amount of reduplication and wasted space if every doc has an
array of related docs that must contain information from those other
docs because I cannot query couch for an array of arbitrary docs by id.

- Do a whole bunch of ajax calls when the page loads to complete the
information for each doc in the array, because I cannot use node to
assemble information from an arbitrary array of http requests to
couchDB. Obviously, this will result in increased server load.

- call a couch view to return all short descriptions +
ids indiscriminately, and then choose the ones I want.  Probably the
best choice, but IMO still a very poor compromise.

Is there any other solution anyone sees here?

"Enthusiasm is not the enemy of the intellect." (said of Irving Howe)
"The angel of history[...]is turned toward the past." (Walter Benjamin)

View raw message