I have a couple of documents in my couchdb formatted like this:
{ type: "event", start: "2012-02-08T19:30+01:00", title: "foo" }
There is a view that lists these documents using start as key. This is because these dates sort naturally:
function (doc) {
if (doc.type == "event") emit(doc.start, doc)
}
I can then access the next three events by ways of the startkey query parameter: viewname?startkey=\"2012-02-08\"&limit=3
Now I use a list to format this view with some HTML:
function (doc, req) {
return provides("html", function() {
// something with getRow() to query the view
});
}
The current date is dynamic and cannot be specified directly in the map function of the view, but is there a way to set the startkey property in the list function? Before calling getRow() for the first time? Of course I could parse the key and compare it to the current date in the list, but that would invalidate the intentions of the query parameters altogether. Is there a better way?
The backstory
The list URL is actually rewritten from a much nicer URL. I have the following properties in rewrites.json:
{
"from": "/ausstellungen/rueckblick",
"to": "_list/rueckblick/exhibitions",
"query": {
"startkey": "\"2012-02-08\"",
"descending": true
}
}
The date is static, but the parameter should always reflect the current date.
No, the only way to do this without simply discarding rows in your _list function based on reading
new Date(which would cause caching trouble anyway) would be external to the database: either a smart reverse proxy or by making sure links to the view have the correct startkey. CouchDB’s rewriting does not support JavaScript intervention, and anything past that level you will not be able to modify what data is fetched off of disk.