Writing CouchDB Views using ClojureScript

7,628 views

Published on

Write your views/filters/validators in Clojure(Script), run the results in CouchDB/Couchbase/Cloudant — no special view servers, no special configuration, no JavaScript!

More info: http://wp.me/p10OJi-bL

Published in: Technology, Education, Business
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,628
On SlideShare
0
From Embeds
0
Number of Embeds
5,131
Actions
Shares
0
Downloads
36
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Writing CouchDB Views using ClojureScript

  1. 1. Writing CouchDB Views using ClojureScript Chas Emerick @cemerick http://cemerick.com http://snowtide.comTuesday, October 11, 2011
  2. 2. @cemerick Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  3. 3. @cemerick Founded Snowtide in 2001 Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  4. 4. @cemerick Founded Snowtide in 2001 Clojure full-time since 2008 Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  5. 5. @cemerick Founded Snowtide in 2001 Clojure full-time since 2008 Contributor to Clojure language & libraries Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  6. 6. @cemerick Founded Snowtide in 2001 Clojure full-time since 2008 Contributor to Clojure language & libraries CouchDB user since 2009? Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  7. 7. @cemerick Founded Snowtide in 2001 Clojure full-time since 2008 Contributor to Clojure language & libraries CouchDB user since 2009? O’Reilly author Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  8. 8. CouchDB in 1 minute Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  9. 9. CouchDB in 1 minute • Document-oriented data store Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  10. 10. CouchDB in 1 minute • Document-oriented data store • (Primary) key => JSON object Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  11. 11. CouchDB in 1 minute • Document-oriented data store • (Primary) key => JSON object • HTTP API Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  12. 12. CouchDB in 1 minute • Document-oriented data store • (Primary) key => JSON object • HTTP API • Secondary indexes / queries provided by ‘views’ Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  13. 13. CouchDB in 1 minute • Document-oriented data store • (Primary) key => JSON object • HTTP API • Secondary indexes / queries provided by ‘views’ • viz. materialized views in SQL-land Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  14. 14. CouchDB in 1 minute • Document-oriented data store • (Primary) key => JSON object • HTTP API • Secondary indexes / queries provided by ‘views’ • viz. materialized views in SQL-land • Defined by Javascript functions applied within a flavour of map/reduce Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  15. 15. CouchDB in 1 minute • Document-oriented data store • (Primary) key => JSON object • HTTP API • Secondary indexes / queries provided by ‘views’ • viz. materialized views in SQL-land • Defined by Javascript functions applied within a flavour of map/reduce • Same mechanism used elsewhere (filters, validation, etc) Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  16. 16. Clutch Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  17. 17. Clutch • CouchDB library for Clojure Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  18. 18. Clutch • CouchDB library for Clojure • (Javascript) view creation and querying support: Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  19. 19. Clutch • CouchDB library for Clojure • (Javascript) view creation and querying support: (save-­‐view  "count"  :number-­‐of-­‐players    {:map  "function(d){for  (var  k  in  d.players)  {emit(d.players[k],1);}}"      :reduce  "function  (keys,  values)  {  return  sum(values);  }"}) Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  20. 20. “Javascript sucks” Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  21. 21. “Javascript sucks” Q.E.D. Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  22. 22. Clutch: Clojure view server Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  23. 23. Clutch: Clojure view server • Use Clojure to write view functions: Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  24. 24. Clutch: Clojure view server • Use Clojure to write view functions: (save-­‐view  "count"  :number-­‐of-­‐players    (with-­‐clj-­‐view-­‐server        {:map  (fn  [doc]  (map  #(vector  %  1)  (:players  doc)))          :reduce  (fn  [keys  values  _]  (reduce  +  values))})) Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  25. 25. Clutch: Clojure view server • Use Clojure to write view functions: (save-­‐view  "count"  :number-­‐of-­‐players    (with-­‐clj-­‐view-­‐server        {:map  (fn  [doc]  (map  #(vector  %  1)  (:players  doc)))          :reduce  (fn  [keys  values  _]  (reduce  +  values))})) • Better, but: Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  26. 26. Clutch: Clojure view server • Use Clojure to write view functions: (save-­‐view  "count"  :number-­‐of-­‐players    (with-­‐clj-­‐view-­‐server        {:map  (fn  [doc]  (map  #(vector  %  1)  (:players  doc)))          :reduce  (fn  [keys  values  _]  (reduce  +  values))})) • Better, but: • Increases operational complexity Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  27. 27. Clutch: Clojure view server • Use Clojure to write view functions: (save-­‐view  "count"  :number-­‐of-­‐players    (with-­‐clj-­‐view-­‐server        {:map  (fn  [doc]  (map  #(vector  %  1)  (:players  doc)))          :reduce  (fn  [keys  values  _]  (reduce  +  values))})) • Better, but: • Increases operational complexity • Won’t work on hosted platforms, e.g. Cloudant Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  28. 28. ClojureScript Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  29. 29. ClojureScript • Clojure : JVM :: ClojureScript : JavaScript Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  30. 30. ClojureScript • Clojure : JVM :: ClojureScript : JavaScript • Ahead-of-time compilation (no eval) Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  31. 31. ClojureScript • Clojure : JVM :: ClojureScript : JavaScript • Ahead-of-time compilation (no eval) • (Optionally) uses Google Closure’s optimizing compiler Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  32. 32. ClojureScript • Clojure : JVM :: ClojureScript : JavaScript • Ahead-of-time compilation (no eval) • (Optionally) uses Google Closure’s optimizing compiler • Eliminate need for Clojure/Clutch view server & need to write views in JavaScript! Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  33. 33. Demo Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  34. 34. Language Mapping Thankfully, you don’t need to do much if any interop in views… Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  35. 35. Language Mapping Javascript Thankfully, you don’t need to do much if any interop in views… Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  36. 36. Language Mapping Javascript emit([doc.a,  doc.b],  doc.c); Thankfully, you don’t need to do much if any interop in views… Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  37. 37. Language Mapping Javascript emit([doc.a,  doc.b],  doc.c); ClojureScript Thankfully, you don’t need to do much if any interop in views… Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  38. 38. Language Mapping Javascript emit([doc.a,  doc.b],  doc.c); ClojureScript (js/emit Thankfully, you don’t need to do much if any interop in views… Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  39. 39. Language Mapping Javascript emit([doc.a,  doc.b],  doc.c); ClojureScript (js/emit    (array  (aget  doc  “a”) Thankfully, you don’t need to do much if any interop in views… Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  40. 40. Language Mapping Javascript emit([doc.a,  doc.b],  doc.c); ClojureScript (js/emit    (array  (aget  doc  “a”)                  (aget  doc  “b”)) Thankfully, you don’t need to do much if any interop in views… Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  41. 41. Language Mapping Javascript emit([doc.a,  doc.b],  doc.c); ClojureScript (js/emit    (array  (aget  doc  “a”)                  (aget  doc  “b”))    (aget  doc  “c”)) Thankfully, you don’t need to do much if any interop in views… Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  42. 42. Current issues Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  43. 43. Current issues • ClojureScript is not a library (yet!) Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  44. 44. Current issues • ClojureScript is not a library (yet!) • Cannot be depended upon in Leiningen/ Maven Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  45. 45. Current issues • ClojureScript is not a library (yet!) • Cannot be depended upon in Leiningen/ Maven • Cannot target / test against particular versions (there aren’t any) Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  46. 46. Current issues • ClojureScript is not a library (yet!) • Cannot be depended upon in Leiningen/ Maven • Cannot target / test against particular versions (there aren’t any) • Incomplete optimization: lots of code generated to define the simplest view Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  47. 47. Thank you! snipe book Chas Emerick @cemerick http://cemerick.com http://snowtide.com Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  48. 48. Thank you! • CouchDB: http://couchdb.apache.org snipe book Chas Emerick @cemerick http://cemerick.com http://snowtide.com Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  49. 49. Thank you! • CouchDB: http://couchdb.apache.org • Clutch: http://github.com/ashafa/clutch snipe book Chas Emerick @cemerick http://cemerick.com http://snowtide.com Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  50. 50. Thank you! • CouchDB: http://couchdb.apache.org • Clutch: http://github.com/ashafa/clutch • ClojureScript: https://github.com/clojure/ clojurescript snipe book Chas Emerick @cemerick http://cemerick.com http://snowtide.com Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011
  51. 51. Thank you! • CouchDB: http://couchdb.apache.org • Clutch: http://github.com/ashafa/clutch • ClojureScript: https://github.com/clojure/ clojurescript • Clutch-ClojureScript: http://github.com/ cemerick/clutch-clojurescript snipe book Chas Emerick @cemerick http://cemerick.com http://snowtide.com Chas Emerick: Writing CouchDB Views using ClojureScriptTuesday, October 11, 2011

×