Вы находитесь на странице: 1из 53

CouchDB for the Web

Benoît Chesneau
07/02/2010 FOSDEM 2010
Monday, February 8, 2010
benoît chesneau

benoitc@apache.org
Couchapp, Couchbeam &
Couchdbkit maintainer
Web craftman
minimal web & opensource

Monday, February 8, 2010


Document
Oriented
Monday, February 8, 2010
DOCUMENT JSON

{
"_id": "foo",
"_rev": "1-....",
"url": "http://apache.couchdb.org",
"vote": 1
}

Monday, February 8, 2010


API HTTP REST
GET / HTTP/1.1\r\nHost: 127.0.0.1:5984\r\n\r\n"

{"couchdb":"Welcome","version":"0.11.0b6ba76f83-git"}

Monday, February 8, 2010


API HTTP REST
GET / HTTP/1.1\r\nHost: 127.0.0.1:5984\r\n\r\n"

{"couchdb":"Welcome","version":"0.11.0b6ba76f83-git"}

• Web paradigm

Monday, February 8, 2010


API HTTP REST
GET / HTTP/1.1\r\nHost: 127.0.0.1:5984\r\n\r\n"

{"couchdb":"Welcome","version":"0.11.0b6ba76f83-git"}

• Web paradigm

• Supported everywhere

Monday, February 8, 2010


API HTTP REST
GET / HTTP/1.1\r\nHost: 127.0.0.1:5984\r\n\r\n"

{"couchdb":"Welcome","version":"0.11.0b6ba76f83-git"}

• Web paradigm

• Supported everywhere

• Lot of available tools/libs

Monday, February 8, 2010


MAP/REDUCE

http://wiki.apache.org/couchdb/Using_Views

Monday, February 8, 2010


MAP/REDUCE
• Map/Reduce to extract informations from documents

http://wiki.apache.org/couchdb/Using_Views

Monday, February 8, 2010


MAP/REDUCE
• Map/Reduce to extract informations from documents

• Javascript used to map documents

http://wiki.apache.org/couchdb/Using_Views

Monday, February 8, 2010


MAP/REDUCE
• Map/Reduce to extract informations from documents

• Javascript used to map documents

• Design Doc

http://wiki.apache.org/couchdb/Using_Views

Monday, February 8, 2010


MAP/REDUCE
• Map/Reduce to extract informations from documents

• Javascript used to map documents

• Design Doc

• /db/_design/ddocname/_view

http://wiki.apache.org/couchdb/Using_Views

Monday, February 8, 2010


VIEWS MAP/REDUCE
[{
"url": "http://apache.couchdb.org",
"vote": 1
},{
"url": "http://apache.couchdb.org",
"vote": 1
},{
"url": "http://mysql.com",
"vote": 0
},{
"url": "http://mysql.com",
"vote": -1
}]

Monday, February 8, 2010


MAP.JS
function(doc) {
if (doc.url && doc.vote)
emit(doc.url, doc.vote);
}
{
"total_rows":3,
"offset":0,
"rows":[
{
"id":"15c92051cc81d564db4337a05087bc8d",
"key":"http://apache.couchdb.org",
"value":1
json },
{
result "id":"fa9658810d25cac893748e4ff15e7253",
"key":"http://apache.couchdb.org",
"value":1
},
{
"id":"1fa0c68d8455196507b8b01645e65186",
"key":"http://mysql.com",
"value":-1
}
]}
Monday, February 8, 2010
REDUCE.JS

function(keys, values, rereduce) {


return sum(values);
}

{
"rows":[
{
"key":"http://mysql.com",
json "value":-1
},
result {
"key":"http://apache.couchdb.org",
"value":2
}
]}

Monday, February 8, 2010


http://wiki.apache.org/couchdb/Basics

Monday, February 8, 2010


Futon
Monday, February 8, 2010
OTHER STUFF

Monday, February 8, 2010


OTHER STUFF
• Listen to changes via HTTP: /db/_changes

Monday, February 8, 2010


OTHER STUFF
• Listen to changes via HTTP: /db/_changes

• long-polling or continuous feed

Monday, February 8, 2010


OTHER STUFF
• Listen to changes via HTTP: /db/_changes

• long-polling or continuous feed

• Replicate via HTTP: /_replicate

Monday, February 8, 2010


OTHER STUFF
• Listen to changes via HTTP: /db/_changes

• long-polling or continuous feed

• Replicate via HTTP: /_replicate

• continuous replication is possible

Monday, February 8, 2010


OTHER STUFF
• Listen to changes via HTTP: /db/_changes

• long-polling or continuous feed

• Replicate via HTTP: /_replicate

• continuous replication is possible

• use other querying possibilities

Monday, February 8, 2010


standalone CouchDB applications

Monday, February 8, 2010


COUCHAPPS
• P2P web

• Local Data

• Offline mode

• Decentralize exchanges

• Script : couchapp

Monday, February 8, 2010


Monday, February 8, 2010
Monday, February 8, 2010
AIMPL.ORG

• full CouchApp....,

• ... behind Django

• Proxy auth handler

• supported load without problem during the launch

Monday, February 8, 2010


Monday, February 8, 2010
WHAT YOU CAN DO
• show

• list

• update

• validation

• rewriting

Monday, February 8, 2010


SHOW
• simple javascript function

• /db/_design/ddocname/_show/funname

• /db/_design/ddocname/_show/funname/docid

• render a doc

• could be cached

Monday, February 8, 2010


HELLO.JS
function(doc, req) {
if (doc) {
return "Hello World";
} else {
if(req.id) {
return "New World";
} else {
return "Empty World";
}
}
}

Monday, February 8, 2010


REQUEST OBJECT
{
"info": {},
"id":null,
"method":"GET",
"path":[],
"query":{},
"headers":{},
"body":"undefined",
"peer":"127.0.0.1",
"form":{},
"cookie":{},
"userCtx":{}
}

Monday, February 8, 2010


USER OBJECT
"userCtx":{
"db":"somedb",
"name":null,
"roles":["_admin"]
}

• Get username and roles

Monday, February 8, 2010


MORE
• `provides`
function to render in the format
requested by the user

• return a response obj : {


headers: {},
code: 200,
stop: true,
json: {},
body: "",
base64, ""
}

Monday, February 8, 2010


LIST
• simple javascript function

• /db/_design/ddocname/_list/funname/
viewname

• Render a view

• no impact in ram. Sent line by line

• could be cached

Monday, February 8, 2010


LIST.JS

function(head, req) {
send("head");
var row;
while (row = getRow()) {
log("row: " + toJSON(row));
send(row.key);
};
return "tail";
}

Monday, February 8, 2010


UPDATE

• Like a show but for PUT, POST & DELETE

• /db/_design/ddocname/_update/funname/
docid

• Allows you to update a document

Monday, February 8, 2010


HELLO.JS
function(doc, req) {
if (!doc) {
if (req.id) {
return [
{ _id : req.id,
reqs : [req] },
"<p>New World</p>"];
};
};
doc.world = "hello";
doc.reqs && doc.reqs.push(req);
doc.edited_by = req.userCtx;
return [doc, "<p>hello doc</p>"];
}

Monday, February 8, 2010


VALIDATE

• simple javascript function

• on update

• Allows
you to throw https errors (forbidden,
unauthorized ...)

Monday, February 8, 2010


UPDATE_VALIDATE.JS
function (newDoc, oldDoc, userCtx) {
function forbidden(message) {
throw({forbidden : message});
};

function unauthorized(message) {
throw({unauthorized : message});
};

if (userCtx.roles.indexOf('_admin') == -1) {
// admin can edit anything, only check when not admin...
if (newDoc._deleted)
forbidden("You may not delete a doc.");
}
};

Monday, February 8, 2010


REWRITE
• Don’t want long & “ugly” urls starting with “_”

• Easy proxying of a CouchApp

• `rewrites` member in the design document

• simple pattern matching

• relative to design doc

Monday, February 8, 2010


SOME EXAMPLES
• Rewrite /* to /_show/someshow/*
{
"from": "*",
"to": "_show/page/*",
"method": "GET"
}

• Rewrite /db -> ../../db :


{
"from": "/_db/*",
"to": "../../*"
}

Monday, February 8, 2010


COUCHAPP

• couchapp generate myapp

• couchapp push mydb

• couchapp clone http://benoitc.im/b/_design/blog

• http://github.com/couchapp/couchapp

Monday, February 8, 2010


Monday, February 8, 2010
Monday, February 8, 2010
THE FUTURE

• CouchApps on the desktop

• The browser is the OS

• On your phone

Monday, February 8, 2010


Stuart Langridge - Canonical

! !

Monday, February 8, 2010


GO FURTHER

• COUCHDB : http://couchdb.apache.org

• book : http://books.couchdb.org/relax

• couchapp wiki : http://wiki.github.com/couchapp/couchapp

• irc #couchdb-fr sur freenodes

• Enki multimedia : http://www.e-engura.com

Monday, February 8, 2010


Questions

Monday, February 8, 2010


@benoitc

Monday, February 8, 2010


Cette création est mise à disposition selon le Contrat
Paternité 2.0 France disponible en ligne http://
creativecommons.org/licenses/by/2.0/fr/ ou par courrier
postal à Creative Commons, 171 Second Street, Suite
300, San Francisco, California 94105, USA.

Monday, February 8, 2010

Вам также может понравиться