5

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!

Community
  • 1
  • 1
goodpixels
  • 503
  • 1
  • 5
  • 20

3 Answers3

4

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/

7ictor
  • 91
  • 6
  • Thank you for this answer. I have been looking around and there doesn't seem to be a good explanation on the inner workings of a CouchApp. Do you have any links? From what I've gathered (in bits and pieces) is that you can serve an index.html page from Couch, which is stored as an attachment. In that page you can make AJAX requests to Couch views/lists/shows/etc?, and then use them on that single index.html page. This is quite powerful. Other questions. How do you make the attachment URL more attractive? How is Couch handling additional files, etc. etc. – Zach Smith Mar 23 '16 at 08:20
2

There's also a nodejs based tool called also couchapp - which is my tool of choice. here. It's Stable and battle-hardened.

Since in the end of the day With this tool you export a module which is the design-document, you can create whatever structure you like.

With a little smart setup with npm scripts involving webpack and couchapp, you can write your views in ES6 with arrow-functions, consts and stuff, transpile them to ES5 which greasemonky in CouchDB understands, and deploy them to your DB of choice in a sinlge-liner npm-script (you can obviously get even fancier...)

Radagast the Brown
  • 3,156
  • 3
  • 27
  • 40
-1

The alternative to the Python based CouchApp tool is the Erlang based Erica tool. In fact the former is now deprecated.

gdelfino
  • 11,053
  • 6
  • 44
  • 48
  • Not sure the "deprecated" statement is true. Seems like couchapp has been updated more recently than Erica, however both have issues with the latest versions of their environments (python3 and erlang). – Mike McKay May 03 '19 at 13:21