views:

102

answers:

2

For context: this is an HTML app, with little or no browser side JavaScript. I can't easily change that so need to do this on the server.

CouchDB is built to not have side effects. This is fair enough. But there seems to be no method that i can conceive of with shows, views, lists to change what is shown to a user with subsequent requests, or based on user objects, without writing data.

And can a get request for document result in the creation of a new record? Im guessing not as that would be a side effect.

But if you can, you could just create a log and then have a view that picks an advert firm a set of documents describing adverts which is affected by the change in the log when a previous ad was shown.

I'm not actually going to show adverts on my site, I'm going to have tips, and article summaries and minor features that vary from page load to page load.

Any suggestions appreciated.

I've wrapped my head around how to work with the grain for the rest of the functionality I need, but this bit seems contrary to the way couchdb works.

A: 

Look into the various methods of selecting a random document from a view. That should enable you to choose a random document (presumably representing an ad, tip, etc.) to display.

jhs
Ok, at least knowing it is possible is good enough. I'm reading the definitive guide to couchdb and will keep a keen eye on the view chapters.
Jim
+1  A: 

I think you're going to need a list function that receives a set of documents from the view and then chooses only one to return, either at random or some other method. However, because you're inside a list function you gain access to the user's request details, including cookies (which you can also set, btw.) That sounds more like what you want.

In addition, you could specify different Views for the list function to use at query-time. This means you could, say, have only random articles show up on the homepage, but any type of content show up on all others.

Note: You can't get access to the request in a map/reduce function and you'll run into problems if you do something like Math.random() inside a map function.

So a list function is the way to go.

http://guide.couchdb.org/draft/transforming.html

duluthian
Having access to the users cookie would allow me to get the users document in the users table and then use that data to apply a transformation to the ordering of the rows in the list function. But, because this would break the ability to cache, as I understand it, couchdb will not let me query ten users profile from within a list or show function to alter the results. This means I need a web framework in front to do this processing, I believe.
Jim
My understanding is that CouchDB generates Etags for show and list functions by hashing the final output. So the ability to cache shouldn't break.
duluthian
Also, remember that you use a list function with a View (map/reduce function pair). So your View could emit fields from the User document AND also emit the documents you select from to show the user. Then your list function would have access to user-related fields as well as the list of documents to pick from.
duluthian
One more thing: It sounds like you may be trying to access the _users database and storing preferences in there. If you do that, then you can't access that data in a View defined in another database. (Although you maybe can access it as part of the req object in a list or show function. I'm not sure, but it'd be something like req.userCtx.yourFieldName.)
duluthian