Search code examples
couchdbextensibilityspidermonkey

CouchDB - share functions across views, across design documents, across databases


Ok, here's the thing.

I have a good JS background, had my share of JS in the past, and have lots of cool bare-bones tools I take with me from project to project that act like a library.

I'm trying to formulate work with CouchDB. Now, after getting used to luxury of cool tools that you wrote and simplify the language for you - I find it a little frustrating to write many things in bare-bones manner.

I'm looking for a way I can load to the database context a limited, highly efficient and generic set of tools that focus on the pure language and makes the work with the language much more groovy (and gosh, no, im not talking about jquery or any of the even more busty libraries out there).

If on top of that, there could be found a way where I can add to the execution context of the couchDB JS engine some of my own logic tools (BL model functions) - it would present a great and admirable power and make couchDB the new home for a JavaScript-er like me.

Maybe I'm aiming too low. I'd be satisfied with a way I can allocate a set of extensions even for a specific database, and I don't mind do it for every database in separate. Or worse - to add it to every design document, so I can teach for example several views in the same design-doc what a Person is, what a Worker is, and use their methods to retrieve data from them according to logic in a reusably coded manner.

Can anybody point me the the way?

Whatever way you can point me - I'll be very verrry grateful. If there are ways for all of these - then great. Trust me to know the difference of what logic belongs to what layer...

You open my possibilities - I promise to use them :D


Solution

  • CouchDB now supports code sharing as CommonJS modules.

    http://docs.couchbase.org/couchdb-release-1.1/index.html#couchdb-release-1.1-commonjs

    http://caolanmcmahon.com/posts/commonjs_modules_in_couchdb

    In this way, you can share your javascript modules between views, lists, and shows in the same design doc. (Server-side)

    Also, you can load these modules on the browser side with this library: https://github.com/couchapp/couchapp/blob/master/couchapp/templates/vendor/couchapp/_attachments/jquery.couch.app.js

    You also might want to look at Kanso:

    http://kansojs.org/

    It does a really good job of making your javascript work seemless between the server and client.