How to Troubleshoot Apps for the Modern Connected Worker
Tearing the Sofa Apart: CouchDB and CouchApps from a Beginner's Perspective
1. Image: Cat vs The Internet, The Oatmeal http:// theoatmeal.com/
Tearing The Sofa Apart
CouchDB & CouchApp from a Beginner’s Perspective
2. It All Started When…
Pain point: Part III on CouchApps
The example Sofa in the book !=
the latest code over GitHub
(#*@#^&^+ FAIL!!!)
(* To be fair, it’s still a great introductory book
and the other sections are still relevant)
3. CouchDB in a Nutshell
Core philosophy:
Relax~
It’s a distributed
database
Data represented as http://guide.couchdb.org/
documents – as flexible
as the paper counterpart
5. All You Need is HTTP
It’s all HTTP REST-like API!
No binaries needed! :D
6. Basic Operations in a
Glance
Create a database
$ curl -X PUT http://localhost:5984/sofa
Double e: List all databases
$ curl -X GET http://localhost:5984/_all_dbs
Add a document
$ curl -X PUT http://localhost:5984/sofa/[id] -d
‘{“title”: “Hello world”, …}’
… etc.
8. “OK, that’s neat…”
“… how am I gonna query stuff?”
http://www.flickr.com/photos/yiie/4865201576/
9. Sofa: Blogging Engine Use
Cases
Author can write posts and edit their posts
Commenters can write comments
Core pages
Index page: Recent post listing
Post page: Individual post with comments
Two feeds: Recent post & recent comments
10. Design Document &
Queries
Design documents is special!
Special power: Can contain application
code that CouchDB can run!
We can write and save MapReduce view in it
We’ll use the views to do queries later
11. A Simple Design Document
{
“_id”: “_design/sofa”, // Determines the URL
“language”: “javascript”,
“views”: { … }, // Define your MapReduce views here
“validate_doc_update”: “function(…) {}”
// Validation function on document inserts/update
}
15. MapReduce Views in Sofa
Post page query with comments
“post-page”: {
“map”: “function(doc) {
if (doc.type == 'post') {
emit([doc._id], doc);
} else if (doc.type == 'comment') {
emit([doc.post_id, doc.created_at], doc);
}
}”
}
… will talk about how to filter an individual post later
16. MapReduce Views in Sofa
List and count tags
“tags”: {
“map”: “function(doc) {
if (doc.type == 'post' && doc.tags && doc.tags.length) {
for (var idx in doc.tags) {
if (doc.tags[idx]) {
emit([doc.tags[idx].toLowerCase(), doc.created_at], doc);
}
}
}
}”,
“reduce”: “_count” // CouchDB’s built-in reduce function
}
19. “Flexible data is cool, but
I need some structure”
Documents are free-form in CouchDB,
flexibility == WIN!
Define validate_doc_update function if you
need validation, e.g.
co de
Check for mandatory fields, w tten :)
ho go
s
to ’d for
ACL control, A sk e I
in cas
(
Field format checking, etc.
20. Why stop here?
“OMG! I can write web apps on
CouchDB without frameworks!”
http://www.flickr.com/photos/7386864@N08/3802078924
21. CouchApp: The Easier Way
Writing design documents is made easy by
CouchApp
Follow the file structure and CouchApp will
make the design document for you
22. Design Document
Structure
Co
pro rresp
{ jec ond
td
“_id”: “_design/sofa”, ire s to C
cto
“language”: “javascript”, ry ouch
str Ap
uc
“views”: { … }, tur p
e
“validate_doc_update”: “function(…) {}”,
“show”: { … }, // Formats a single document
“lists”: { … }, // Formats a list of documents
“_attachment”: { … }, // CSS, JS, images attached here
“templates”: { … }, // Templates stored here
“vendor”: { … } // Libraries and scripts
}
23. Accessing Show and List
Pages
Show (single document)
hp://localhost:5984/sofa/_design/sofa/_show/
edit/[id]
ID is optional; blank ID is good for pages to create new
documents
List (list of documents)
hp://localhost:5984/sofa/_design/sofa/_list/
index/[view-name]?[query-parameters]
(* I know the default URL’s ugly :( – fix this with URL rewrite module + virtual host seings)
24. Batteries Provided
Baeries (i.e. libraries) provided with
CouchApp projects:
jquery.cou.app.js: To interact with CouchDB, query,
insert, update…
mustae.js: Template engine
Evently: Writing interactive, event-based widgets
without spaghei jery code
Utilities: Atom feed generator, MD5 checksum, path
builder, validation functions…
25. Quick Demo of the Sofa
Code
Ask t
(in ca o show
se I’d code
forgo
Add/Editing post tten :
)
Listing recent posts
How to serve HTML and Atom feed?
Why showing an individual post is in “lists”
but not “show”?