Closed boxxxie closed 12 years ago
through help from #couchdb i was able to find a partial solution.
ddoc.views.lib = couchapp.loadFiles('./lib');
ddoc.views.test = { map: function(doc) { var bar2 = require('views/lib/utils2').bar; emit(doc._id,bar2); } };
this works cept that when i change the utils file it doesn't get updated when i do a push and now i have a bunch of old util files in my views.lib in my couchdb. also, the lib files have not worked for anything more complicated than "exports.var = 1;"
maybe i'm doing something wrong, but i don't have anything to compare my code to
Maybe something along the lines of https://github.com/thejh/node-astjourney/blob/master/example/example.js#files could help. @mikeal Thoughts? This implementation is quick and dirty, and it won't work for functions calling each other unless you put them above the functions for the design document, but apart from that, I think it works.
sorry, for some reason i didn't see this for a while.
all the functions get .toString()'d that are in the design document. in order to import them they have to be commonjs modules suitable for the couchdb view server. node.couchapp has hooks to help you define directories of commonjs modules for inclusion but you'll need to familiarize yourself with how to import them (the require path is relative to the design document).
parsing the ast is problematic. for one thing, the code executed in the view server is different from how it's defined here which means exceptions won't make sense. it'll also only work for node modules that can run in couchdb, which isn't actually that many (nothing working with files or sockets will work).
for one thing, the code executed in the view server is different from how it's defined here which means exceptions won't make sense
it'd be easy to preserve the original source plus add helpers at the end
it'll also only work for node modules that can run in couchdb, which isn't actually that many (nothing working with files or sockets will work)
Uh, I wasn't suggesting browserify for couch, just copying function statements inside the same file into the view functions and stuff.
I want to do something like this (or import external files and use functions in them).
var couchapp = require('couchapp') , path = require('path') ;
ddoc = { _id:'design/app' , rewrites : [ {from:"/", to:'index.html'} , {from:"/api", to:'../../'} , {from:"/api/", to:'../../'} , {from:"/", to:'_'} ] };
ddoc.views = {}; transactionDate = function(doc){ date_to_array = function(date){ return [date.getFullYear(), (date.getMonth() + 1), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()]; }; return date_to_array(new Date(doc.time.end)); };
ddoc.views.transactions_by_type = { map : function (doc) {
};
currently, this doens't work. I've tried variations and can't get anything to work. I've been told by people on #couchdb that reusing code in node.couchapp is possible.