Image: Cat vs The Internet, The Oatmeal http:// theoatmeal.com/Tearing The Sofa ApartCouchDB & CouchApp from a Beginner’s ...
It All Started When…Pain point: Part III on CouchApps The example Sofa in the book !=   the latest code over GitHub      (...
CouchDB in a NutshellCore philosophy:Relax~It’s a distributeddatabaseData represented as        http://guide.couchdb.org/d...
Awesome Distributed DB     Features!ReplicationConflict managementLoad balancingClustering           http://guide.couchdb.o...
All You Need is HTTP  It’s all HTTP REST-like API!    No binaries needed! :D
Basic Operations in a             GlanceCreate a database$ curl -X PUT http://localhost:5984/sofaDouble e: List all data...
Easier Admin: Futon
“OK, that’s neat…”“… how am I gonna query stuff?”                         http://www.flickr.com/photos/yiie/4865201576/
Sofa: Blogging Engine Use          Cases Author can write posts and edit their posts Commenters can write comments Core pa...
Design Document &        QueriesDesign documents is special!  Special power: Can contain application  code that CouchDB ca...
A Simple Design Document{    “_id”: “_design/sofa”, // Determines the URL    “language”: “javascript”,    “views”: { … }, ...
MapReduce for DummiesMap: applies a functionto every single row andemits key-value dataReduce: (optional) Takedata spewed ...
Example: Sofa Blog Post           Structure{    “type”: “post”,    “author”: “John Doe”,    “created_at”: “2011-03-11T11:2...
MapReduce Views in SofaRecent posts query“recent-posts”: {  “map”: “function(doc) {     if (doc.type == post) {       emit...
MapReduce Views in SofaPost page query with comments“post-page”: {  “map”: “function(doc) {    if (doc.type == post) {    ...
MapReduce Views in SofaList and count tags“tags”: {  “map”: “function(doc) {    if (doc.type == post && doc.tags && doc.ta...
Querying Views$ curl -X GET http://localhost:5984/sofa/_design/sofa/_view/recent-postsCouDB response{    “total_rows”: 4,...
Filtering Data$ curl -X GET http://localhost:5984/sofa/_design/sofa/_view/post-page?startkey=[“Huh”]&endkey=[“Huh”, {}]Cou...
“Flexible data is cool, but I need some structure”Documents are free-form in CouchDB,flexibility == WIN!Define validate_doc_...
Why stop here?“OMG! I can write web apps onCouchDB without frameworks!”                       http://www.flickr.com/photos/...
CouchApp: The Easier Way Writing design documents is made easy by CouchApp Follow the file structure and CouchApp will make...
Design Document               Structure                                           Co                                      ...
Accessing Show and List               Pages        Show (single document)        hp://localhost:5984/sofa/_design/sofa/_s...
Batteries ProvidedBaeries (i.e. libraries) provided withCouchApp projects:  jquery.cou.app.js: To interact with CouchDB,...
Quick Demo of the Sofa        Code                               Ask t                             (in ca o show          ...
Learning MoreCouDB Wikihp://wiki.apache.org/couchdb/FrontPage/usr/local/share/couchdb/server/main.js(Seriously!)CouDB: ...
Contact Me! :)Clone my annotated Sofa CouApp!  hps://github.com/felixleong/sofa          @felixleong          hp://feli...
Upcoming SlideShare
Loading in …5
×

Tearing the Sofa Apart: CouchDB and CouchApps from a Beginner's Perspective

24,407 views

Published on

Presentation slides from my presentation, which talks about CouchDB and CouchApps from a beginner's perspective

Published in: Technology, Design
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
24,407
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
50
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Tearing the Sofa Apart: CouchDB and CouchApps from a Beginner's Perspective

    1. 1. Image: Cat vs The Internet, The Oatmeal http:// theoatmeal.com/Tearing The Sofa ApartCouchDB & CouchApp from a Beginner’s Perspective
    2. 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. 3. CouchDB in a NutshellCore philosophy:Relax~It’s a distributeddatabaseData represented as http://guide.couchdb.org/documents – as flexibleas the paper counterpart
    4. 4. Awesome Distributed DB Features!ReplicationConflict managementLoad balancingClustering http://guide.couchdb.org/
    5. 5. All You Need is HTTP It’s all HTTP REST-like API! No binaries needed! :D
    6. 6. Basic Operations in a GlanceCreate a database$ curl -X PUT http://localhost:5984/sofaDouble e: List all databases$ curl -X GET http://localhost:5984/_all_dbsAdd a document$ curl -X PUT http://localhost:5984/sofa/[id] -d‘{“title”: “Hello world”, …}’ … etc.
    7. 7. Easier Admin: Futon
    8. 8. “OK, that’s neat…”“… how am I gonna query stuff?” http://www.flickr.com/photos/yiie/4865201576/
    9. 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. 10. Design Document & QueriesDesign documents is special! Special power: Can contain application code that CouchDB can run!We can write and save MapReduce view in itWe’ll use the views to do queries later
    11. 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}
    12. 12. MapReduce for DummiesMap: applies a functionto every single row andemits key-value dataReduce: (optional) Takedata spewed from mapand reduce it aaggregated solution (e.g.sum, count, standarddeviation) © Nadir Muzaffar, hp://www.cs.berkeley.edu/~ballard/cs267.sp11/hw0/results/ htmls/Muzaffar.html used without permission ^-^||
    13. 13. Example: Sofa Blog Post Structure{ “type”: “post”, “author”: “John Doe”, “created_at”: “2011-03-11T11:20:52.22”, “title”: “Blog Post Title”, “body”: “…”, “tags”: [“dummy”, “testing”, “webcamp”]}
    14. 14. MapReduce Views in SofaRecent posts query“recent-posts”: { “map”: “function(doc) { if (doc.type == post) { emit(new Date(doc.created_at), doc); } }”}
    15. 15. MapReduce Views in SofaPost 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. 16. MapReduce Views in SofaList 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}
    17. 17. Querying Views$ curl -X GET http://localhost:5984/sofa/_design/sofa/_view/recent-postsCouDB response{ “total_rows”: 4, “offset”: 0, “rows”: [ {“title”: “Hello world”, …}, … ]}
    18. 18. Filtering Data$ curl -X GET http://localhost:5984/sofa/_design/sofa/_view/post-page?startkey=[“Huh”]&endkey=[“Huh”, {}]CouDB response{ “total_rows”: 5, “offset”: 2, “rows”: [ {“title”: “Huh”, …} ]}Other query parameterskey, limit, descending, skip, group, reduce,include_docs…
    19. 19. “Flexible data is cool, but I need some structure”Documents are free-form in CouchDB,flexibility == WIN!Define validate_doc_update function if youneed 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. 20. Why stop here?“OMG! I can write web apps onCouchDB without frameworks!” http://www.flickr.com/photos/7386864@N08/3802078924
    21. 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. 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. 23. Accessing Show and List Pages Show (single document) hp://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) hp://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 seings)
    24. 24. Batteries ProvidedBaeries (i.e. libraries) provided withCouchApp projects: jquery.cou.app.js: To interact with CouchDB, query, insert, update… mustae.js: Template engine Evently: Writing interactive, event-based widgets without spaghei jery code Utilities: Atom feed generator, MD5 checksum, path builder, validation functions…
    25. 25. Quick Demo of the Sofa Code Ask t (in ca o show se I’d code forgoAdd/Editing post tten : )Listing recent posts How to serve HTML and Atom feed?Why showing an individual post is in “lists”but not “show”?
    26. 26. Learning MoreCouDB Wikihp://wiki.apache.org/couchdb/FrontPage/usr/local/share/couchdb/server/main.js(Seriously!)CouDB: e Definite Guidehp://guide.couchdb.org/index.htmlCouDB HTTP API Referencehp://docs.couchone.com/couchdb-api/
    27. 27. Contact Me! :)Clone my annotated Sofa CouApp! hps://github.com/felixleong/sofa @felixleong hp://felixleong.com/

    ×