CouchApps are web applications built using CouchDB, JavaScript, and HTML5. CouchDB is a document-oriented database that stores JSON documents, has a RESTful HTTP API, and is queried using map/reduce views. This talk will answer your basic questions about CouchDB, but will focus on building CouchApps and related tools.
9. CouchDB is a database,
web server & application server
10. Bene ts
Schema-less “self-contained” JSON documents—no tables, just documents
RESTful HTTP API is simple to integrate with
Custom-written MapReduce “views” index your data for fast retrieval
Multi-master replication can happen continuously, in any direction, and/or
whenever nodes can connect
Can run on anything from a server to a mobile device (CouchDB can be embedded
in Android and iOS apps)
11. Trade-Offs
No ad-hoc queries (you can use temporary views, but they’re slow on
large data sets)
Thinking in terms of MapReduce views can take some time to get used to
No transactions across document boundaries
Your data will be “eventually consistent” and con icts can occur (but
can be resolved)
13. Mapping Document Titles
function(doc) { // JSON object representing a doc to be mapped
if (doc.title) { // make sure this doc has a title
emit(doc.title); // emit the doc’s title as the key
}
}
14. Mapped Document Titles
key id value
"Building iPhone Apps with
"978-0-596-80579-1" null
HTML, CSS, and JavaScript"
"CouchDB: The Definitive
"978-0-596-15589-6" null
Guide"
"DocBook: The Definitive
"978-1-565-92580-9" null
Guide"
"RESTful Web Services" "978-0-596-52926-0" null
15. Mapping Document Authors
function(doc) { // JSON object representing a doc to be mapped
if (doc.authors) { // make sure this doc has an authors eld
for (var i in doc.authors) {
emit(doc.authors[i]); // emit each author as the key
}
}
}
22. Save Updated Document (cont’d)
PUT http://localhost:5984/mydb/0a72c9c36bd169818dc97ed18b000aa4 201 Created
{
"ok":true,
"id":"0a72c9c36bd169818dc97ed18b000aa4",
"rev":"2-516027e3179a22a22e06874c374e8ef0"
}
27. Bene ts
Streamlining of your codebase (no middle tier)
Same language on both the client and server (JavaScript)
Show and list functions let you generate HTML from within CouchDB, if you don’t
want to rely on JavaScript
Replication of both data and code together
Deploy/replicate an application along with its data
Goes “with the grain” of the web
28. “Ground Computing”
Replication lters allow you to replicate relevant data to a user
Local data means faster access to the user’s data
Offline access
Data portability
Decentralization—no need for a canonical database
Potentially gives more control to the user over his or her own data
29. What about the HTML5
Web Storage API?
Allows persistent storage of key/value pairs
Enjoys signi cant cross-browser support
Lacks indexed queries
No replication features
IndexedDB might help, but is not part of the HTML5 speci cation and is only
implemented in a limited number of browsers
Need to look outside the HTML5 speci cation if you need more than just a key/
value storage
30. Trade-Offs
Tooling could use some improvement
Terminology still unclear—CouchApp can mean many different things
Must use JavaScript for the presentation tier
Pure JavaScript applications may have SEO issues
Show and list functions must be side-effect free, which can be limiting
31. Uses
Mobile applications that require offline access
Multi-device applications (e.g. address book, tasks)
Peer-to-peer collaboration applications
Distributed social networking
Any application that stores “documents”—CMS, wiki, etc.
Geospatial applications (via GeoCouch)
41. Non-False
(and Not a List) Values
Template:
{{#user}}
<p>Logged in as {{name}}</p>
{{/user}}
Hash:
{ "user": {
"name": "Bradley"
}
}
Output:
<p>Logged in as Bradley</p>
51. CouchApps & Design Documents
Each CouchApp is entirely self-contained in one design document
Design documents live alongside other documents, but have a special naming
convention of _design/<name>
A database can have multiple CouchApps (and/or design documents) and
CouchDB server can host multiple databases
52. What’s in a design document?
Can contain:
• View de nitions
• Map functions
• Reduce functions
• Show functions
• List functions
• Document update handlers
• Document update validation functions
• Rewrite de nitions
• Any other elds you want, just like with any other document
• Attachments, just like with any other document
ID follows the form of:
_design/<design_name>
65. CouchDB supports virtual hosts via the
“vhosts” server con guration section.
This lets you route to a rewrite handler
based on the Host HTTP header.
{
"example.com": "/db/_design/tutorial/_rewrite",
"www.example.com": "/db/_design/tutorial/_rewrite"
}
71. Function De nition
function (newDoc, oldDoc, userCtx, secObj) {
var v = require("vendor/couchapp/lib/validate").init(newDoc, oldDoc, userCtx, secObj);
…
}
72. Let Admins Do Anything
if (v.isAdmin()) {
return true; // admin can do anything
}
73. Require Login to
Make Changes
if (!userCtx.name) {
// this could be con gurable based on secObj
v.unauthorized("please login to make changes");
}
74. Only Let Admins Delete
// only admin may delete
if (newDoc._deleted) {
v.unauthorized("only admin may delete docs");
}
75. A special _security database property
lets you de ned database admins and
database readers
80. Generate a CouchApp
$ couchapp generate tutorial
2011-06-15 16:25:18 [INFO] /tutorial generated.
$ cd tutorial
81. Generated Files
$ tree -FL 1
.
README.md
_attachments/
_id
couchapp.json
evently/
language
lists/
shows/
updates/
vendor/
82. README.md
Generated by CouchApp
CouchApps are web applications which can be served directly from CouchDB. This
gives them the nice property of replicating just like any other data stored in CouchDB.
They are also simple to write as they can use the built-in jQuery libraries and plugins
that ship with CouchDB.
More info about CouchApps here.
Deploying this app
Assuming you just cloned this app from git, and you have changed into the app directory
in your terminal, you want to push it to your CouchDB with the CouchApp command
line tool…
104. node.couchapp.js
Command-line tool, written for Node.js
Simpler folder structure
Not compatible with the Python version
Links:
• https://github.com/mikeal/node.couchapp.js
• http://japhr.blogspot.com/2010/04/quick-intro-to-nodecouchappjs.html
• http://vimeo.com/26147136
110. CouchDB & CouchApp Resources
CouchApp Wiki Scaling CouchDB
http://couchapp.org/ by Bradley Holt (O’Reilly)
063-6-920-01840-7
CouchDB Wiki
http://wiki.apache.org/couchdb/ Beginning CouchDB
by Joe Lennon (Apress)
CouchDB: The De nitive Guide 978-1-430-27237-3
by J. Chris Anderson, Jan Lehnardt, and
Noah Slater (O’Reilly)
978-0-596-15589-6
Writing and Querying MapReduce Views in
CouchDB
by Bradley Holt (O’Reilly)
978-1-449-30312-9