I made a couple of small node.js modules to help me develop couchapps.
CouchApp is two things: a concept and a tool. The concept is an application that runs on CouchDB, using its API. The API has three basic components for presenting data (it has others for editing data but that isn’t covered in this post):
- views: A view in CouchDB is a Map and optionally a Reduce function, which enables indexing JSON documents using the powerful MapReduce programming model.
- lists: A list is a function that renders documents from a view, into an any format (commonly HTML).
- shows: A show function renders a single document, into any format.
With these simple functions, a blog or a photo gallery can be made viewable. The URLs can get a bit awkward, but CouchDB provides a rewrites feature to solve it.
This can be a lot of files, when there is one file for each function. Here is the CouchOne pages wiki CouchApp. It has a lot of views.
Mikeal Rogers had perhaps the first solution to this problem: node.couchapp.js. It runs on node.js and allows programmatic building of a design document, and puts functions in strings to prepare the JSON using Function.toString(). This loses the python CouchApp’s advantage of being able to go back and forth from file to CouchDB without losing anything.
fn is a function, I get the source without the comments. I want the comments. It also isn’t indented properly. With Esprima I wrote code to extract the raw function source and fix up the indentation. Examples of this are in the READMEs of the two projects, json2js and js2json.
The projects below are still in early development and barely tested, but I’m putting them to use right now, by finally putting one of my fancy domain names to good use. ;) In the meantime, if this interests you, please check them out and give me feedback.
[…] Ben Atkin Skip to content HomeAbout ← js2json and json2js […]
This is really awesome! I will definitively try it out. Thanks.
This is a great idea, however I have a couple of remarks:
1. This only works if commonjs modules (“module.exports”) are used, which seems to be an unnecessary restriction. I, for one, do not organize my code that way.
2. js2json and json2js fit well into the same project, since one assumes as input the output of the other. The separation may lead to compatibility problems.
How do you organize your code? I was already thinking about RequireJS support. I’ll make an issue on GitHub.
I agree that they could fit well into the same project, especially because now they both depend on falafel. I’ll work on a name and a CLI.
I do not use RequireJS either. I use regular js objects and functions. The whole commonjs module deal is overhyped and it’s popular because nodejs is popular. ECMA Harmony will not use the commonjs approach for modules, so code based on them will eventually have to die. RequireJS, although better, will not stick either. But that is another discussion.
The real question here is why do js2json and json2js, which imo can become popular libraries at least in the couchdb space, impose the use of commonjs modules. A restriction such as this (being it commonjs or requirejs or whatever) will hurt the project. I for one, cannot use it.
Hail to the agnostic libraries.
Think about it this way, your libraries turn js code into json and back. That’s your pitch. That’s the value you are providing. Where in here, do we need to include commonjs or requirejs into the picture? That’s narrowing the use of the libraries. That’s unnecessary limiting.
Please notice that all of my criticism comes from a deep and thankful appreciation of your work. Thanks for that.