The talk gives an introduction to CouchDB by showing the implementing of a simple Wiki Rails application that I have implemented in all the available Ruby frameworks I could find. It then compares these frameworks and suggests which to use in order to get started with CouchDB and Ruby.
8. TDD
BDD
“agile”
Pair Programming
* all the f****** time
9. About me
• playing with CouchDB since 09/2008
• helped hack the CouchDB statistics module
(with @janl)
• wrote Couch Potato
• working on smaller production apps
11. Who is in this room?
• Chris Anderson
• George Palmer
• Paul Carey
• Johan Sørensen
• Cheah Chu Yeow/Carlos Villela
12. Who is in this room?
• Chris Anderson (couchrest + couchapp)
• George Palmer (couch_foo)
• Paul Carey (relaxdb)
• Johan Sørensen (couchbject)
• Cheah Chu Yeow/Carlos Villela (activecouch)
42. HTTP API
• POST ‘/my_db’, {my_json}
• GET ‘/my_db/my_document_id’
• PUT ‘/my_db/my_document_id’,
{new_json}
• DELETE ‘/my_db/my_document_id’
43. Map/Reduce views
• views are documens
• provide a map (and optional reduce
function) written in JavaScript
• this creates an index over all documents
• query that index via GET
61. That is CouchDB
(the basics)
• upload documents via POST/PUT
• read documents via GET
• create indexes by providing map/reduce
functions
• query views via GET, pass keys + other
options
63. Example Wiki
• ActiveRecord • RelaxDB
• CouchRest • Couch Potato
• ActiveCouch • CouchOject
• CouchFoo • CouchApp
64. • a wiki example app implemented in all
frameworks I could find
• + in ActiveRecord for comparison
DISCLAIMER: implementations are insecure, have
bugs and aren’t meant for production at all
71. routes.rb
map.resources :pages do |pages|
pages.resources :versions
end
map.resources :statistics
map.root :controller => quot;pagesquot;, :action => 'show'
72. page.rb
class Page < ActiveRecord::Base
acts_as_versioned
def to_param
title
end
end
73. pages/show.html.erb
<%= <%= simple_format linkify(@page.body) %>
simple_format linkify(@page.body) %>
def linkify(text)
text.gsub(/([A-Z][a-z]+([A-Z][a-z]+)+)/) do
link_to($1, page_path($1))
end
end
replace CamelCase words with links to #show
74. pages_controller.rb
def show
@page = Page.first unless params[:id]
@page ||= Page.find_by_title params[:id]
redirect_to new_page_path(:title => params[:id]) unless @page
end
redirect to #new if no page found
75. statistics
def self.word_counts
Page.all.map(&:body).join(quot; quot;).split(/s
+/).grep(/w+/i).inject(Hash.new(0)) do |res,
word|
res[word] += 1
res
end
end
92. Counting Words - reduce
key value
“page” 1
“page” 1
“one” 1
“2” 1
key value
“page” 2
function(keys, values) {
return sum(values);
“one” 1
}
“2” 1
93. CouchDB summary
• no schema, arbitrary documents in one
namespace
• still use foreign keys to implement
associations
• create views instead of join tables
• views! views! views!
97. What’s the framework’s job?
ActiveRecord
• map objects to relations and back
• do the whole SQL thing
98. What’s the framework’s job?
ActiveRecord
schema attribute auto
tracking detection
serialized attributes pagination
groups
JOINS unique lazy loading
caching
• map objects to relations and back count
• do the whole SQL thing HABTM
create or update?
has_many :through conditions
eager association loading
connection DDL :dependent => :destroy
99. What’s the framework’s job?
ActiveRecord
Page.all :include => :tags
SELECT pages.id ... JOIN tags ON ... WHERE ...
... [10 more lines] ...
108. Low level part
• relatively thin layer on top of RestClient
• store and retrieve JSON structures
• query views the Couch way
109. Class mapping part
• map Ruby classes to JSON documents
• CRUD
• declarative views with CouchDB
semantics
110. RelaxDB
• CRUD + very basic associations
• automatic view generation via view_by
• a bit of support for custom views via
RelaxDB::View
• CouchDB like view API
111. Couch Object
• last updated in 2007
• pretty low level - okay for learning
the details of Couch the DIY way
• no update, no properties, no
automatic view creation
112. Couch Potato
• CRUD, associations + JSON mapping
• built-in acts_as_versioned - 60 LOC but
doing it by hand only requires 7
• ViewQuery class for creating/querying
custom views
• AR like finders
113. CouchFoo
• takes ActiveRecord and makes it
work with CouchDB
• if you (have) to migrate an AR
app...
• doesn’t give you the power of
CouchDB
114. ActiveCouch
• “Object Relational Mapper for [..]
CouchDB”
• “Since, the Rubyists here at Wego are
already very familiar with ActiveRecord
semantics, care has been taken to ensure
that ActiveCouch resembled it in many
ways.”
115. ActiveCouch
• one database per model
• View class to upload #@!? views via Rake
task
• no support for custom views
124. What is CouchApp
• bunch of Ruby Python scripts to help with
development/deployment
• data, CouchDB views, validations, shows,
lists and assets in one database
125. AJAX apps
• serve HTML, CSS as assets
• do all the work in JavaScript
• in the browser: GET from couch and append
HTML, POST form data to CouchDB to
update documents
• in CouchDB: views to retrieve, compute data,
validations
135. Resources
• The CouchDB book
http://books.couchdb.org/relax/
• jchris CouchApp talk
http://jchrisa.net/drl/CouchDB%20Talk%20at%20Vidoop/
VidoopCouchTalk.pdf
• janl CouchDB talk
http://www.slideshare.net/bbcwebdev/introduction-into-couchdb-
jan-lehnardt-presentation
• The example wiki
http://github.com/langalex/couchdb_example_wiki/
• http://couch.io
support, training, hosting, development