You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by kowsik <ko...@gmail.com> on 2008/09/25 17:32:31 UTC
Extending main.js
I frequently find myself writing little functions inside my reduce
function to sort the values or uniq the values. Yes, I realize that
are alternate ways to structure the key/value pairs to let couch do
the sorting for you. Currently main.js adds the "sum" method as a
utility method available during map/reduce. I'm thinking we should
have a REST method to add additional app-specific methods that are
available to the map/reduce function? These could be utility methods
or static lookup tables and so on.
The other possible hack is that main.js has a sandbox which is a
spidermonkey context. In either the map or the reduce functions, we
could do something like:
if (Object.prototype.my_cool_func == null) {
Object.prototype.my_cool_func = function() { ... }
}
I'm sure that this will work since the "Object" is part of the sandbox
context. Thoughts?
K.
Re: Extending main.js
Posted by Chris Anderson <jc...@apache.org>.
On Thu, Sep 25, 2008 at 8:32 AM, kowsik <ko...@gmail.com> wrote:
> I'm thinking we should
> have a REST method to add additional app-specific methods that are
> available to the map/reduce function? These could be utility methods
> or static lookup tables and so on.
The danger with this approach is that you absolutely need the source
code for each map and reduce function to correspond 1-1 with its
behavior. Otherwise, behavior could change without expiring the view
indexes.
I wrestled with the problem for while, before I realized that there's
no harm in including library code directly into maps and reduces. I've
got some scripts in the CouchRest project that upload filesystem code
to design documents, inlining the contents of a lib.js file into your
maps or reduces, if you include a comment like
//include-lib
within your function. This way I don't repeat myself, but each map and
reduce function contains its full specification.
CouchRest is located here: http://github.com/jchris/couchrest/tree/master
Once you've installed the gem, run
couchview --help generate
to get a feel for the directory structure it's looking for.
--
Chris Anderson
http://jchris.mfdz.com