So I've been reading about CouchDB lately, and I REALLY like it. It seems so simple, yet flexible and I LOVE the RESTful interface. But let's face it - unless you're building a SAP, you'll want your application to serve static HTML with SEO-friendly URLs rather than constant HTTP requests. I haven't actually used CouchDB yet in any project, but I am keen to explore it in near future.
The question is: how can I use it to build a static HTML website, for example a blog? I would like to store all my blog posts in CouchDB and then serve them as HTML, based on categories, tags and date. If I'm understanding this correctly, I would just define a set of shows in my design documents. So for example, in a design document for all posts in the 'Work' category, I would also add a separate function for the show template. However, I have a bit of a problem with storing my HTML inside a JavaScript function inside a JSON file! That's sounds super-painful to maintain. I've had a quick look at CouchApp and it seems to solve this issue, but it doesn't seem to be in active development, hence my question about other possible solutions.
I want to be able to structure my HTML / CSS / JS as I normally would have, but leverage the benefits of CouchDB, if possible, without any external backend. Or even better - I would LOVE to actually define my databases as JSON files, my map functions as regular .js
files and maintain the classic directory structure for everything else, so for example:
db/data
- this holds CouchDB
db/maps
- this holds my map functions
public/
- this holds everything else including other JS, CSS and HTML, like this:
public/css
public/js
public/categories.html
public/posts.html
public/index.html
etc.
Any thoughts on how can I achieve this? Or if it's even possible?
Bonus question: could someone be so kind and explain what reduce functions are? Are they the SQL equivalent of sum
and count
functions? When would I actually use a reduce function? And when would I need to write a custom one?
Thanks!
CouchDB is the DB thinked for the web, it's a RESTful API out of the box and that's why CouchApps are so simple, fast to develop, and doesn't need a backend because CouchDB it's actually your backend.
I know it's hard to maintain HTML pushed from CouchDB, it's expensive too, so the way I build CouchApps is creating a frontend App with Backbone, managing all my routes (friendly URLs), using templates (any templating framework, I use the simple requirejs !text), and develop the app as normal HTML/CSS/JS, making the Ajax calls to CouchDB Views (transforming the data with Lists, so just return clean JSON), and if I want a specific document do the same, just with Shows and I get JSON data again.
So at the end is working as a Web App talking with the RESTful API and interpreting that data to display it as you want.
Then you pushed that App to CouchDB (now it's a couchapp), so CouchDB it's actually serving your App to the final user and the API to your App.
Reduce:
It's a powerfull algorithm, as you say is equivalent with SQL sum
and count
, you have that ones in CouchDB reduce too (_sum
, _count
, _stats
).
I recommend you to read this link to understand reduce, and rereduce.
http://www.ramblingincode.com/building-a-couchdb-reduce-function/