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

有些话、适合烂在心里 提交于 2019-12-05 06:20:48

问题


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


回答1:


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.




回答2:


You can find some helpful tools here : https://github.com/vivekpathak/casters

The running examples and test cases may particularly help you.



来源:https://stackoverflow.com/questions/8300546/couchdb-share-functions-across-views-across-design-documents-across-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!