CouchDB for the web - fosdem 2010

53
Benoît Chesneau 07/02/2010 FOSDEM 2010 CouchDB for the Web Monday, February 8, 2010

Transcript of CouchDB for the web - fosdem 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 1/53

Benoît Chesneau

07/02/2010 FOSDEM 2010

CouchDB for the Web

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 2/53

[email protected], Couchbeam &Couchdbkit maintainer

Web craftmanminimal web & opensource

benoît chesneau

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 3/53

Document

OrientedMonday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 4/53

DOCUMENT JSON

{

  "_id": "foo",  "_rev": "1-....",

  "url": "http://apache.couchdb.org",

  "vote": 1

}

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 5/53

API HTTP RESTGET / 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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 6/53

API HTTP REST

• Web paradigm

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 7/53

API HTTP REST

• Web paradigm

• Supported everywhere

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 8/53

API HTTP REST

• Web paradigm

• Supported everywhere

Lot of available tools/libs

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 9/53

MAP/REDUCE

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

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 10/53

MAP/REDUCE

• Map/Reduce to extract informations from documents

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

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 11/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 12/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 13/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 14/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 15/53

MAP.JSfunction(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

},

{

  "id":"fa9658810d25cac893748e4ff15e7253",

  "key":"http://apache.couchdb.org",

  "value":1

},

{

  "id":"1fa0c68d8455196507b8b01645e65186",

  "key":"http://mysql.com",

  "value":-1

}]}

 json

result

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 16/53

REDUCE.JS

function(keys, values, rereduce) {

  return sum(values);

}

{

  "rows":[

{

  "key":"http://mysql.com",

  "value":-1

},

{

  "key":"http://apache.couchdb.org",

  "value":2

}

]}

 json

result

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 17/53

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

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 18/53

Futon

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 19/53

OTHER STUFF

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 20/53

OTHER STUFF

• Listen to changes via HTTP: /db/_changes

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 21/53

OTHER STUFF

• Listen to changes via HTTP: /db/_changes

• long-polling or continuous feed

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 22/53

OTHER STUFF

• Listen to changes via HTTP: /db/_changes

• long-polling or continuous feed

• Replicate via HTTP: /_replicate

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 23/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 24/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 25/53

standalone CouchDB applications

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 26/53

COUCHAPPS

• P2P web

• Local Data

• Offline mode

• Decentralize exchanges

• Script : couchapp

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 27/53

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 28/53

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 29/53

AIMPL.ORG

• full CouchApp....,

• ... behind Django

• Proxy auth handler 

supported load without problem during the launch

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 30/53

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 31/53

 WHAT YOU CAN DO

• show

• list

• update

• validation

• rewriting

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 32/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 33/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 34/53

REQUEST OBJECT

{

  "info": {},

  "id":null,

  "method":"GET",

  "path":[],  "query":{},

  "headers":{},

  "body":"undefined",

  "peer":"127.0.0.1",

  "form":{},

  "cookie":{},  "userCtx":{}

}

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 35/53

USER OBJECT

  "userCtx":{

  "db":"somedb",

  "name":null,

  "roles":["_admin"]}

Get username and roles

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 36/53

MORE

• `provides` function to render in the formatrequested by the user 

• return a response obj : {

headers: {},

code: 200,

stop: true,

json: {},

body: "",base64, ""

}

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 37/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 38/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 39/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 40/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 41/53

VALIDATE

• simple javascript function

• on update

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

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 42/53

UPDATE_VALIDATE.JSfunction (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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 43/53

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

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 44/53

SOME EXAMPLES

• Rewrite /* to /_show/someshow/*

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

{

  "from": "*",

  "to": "_show/page/*",

  "method": "GET"}

{

  "from": "/_db/*",

  "to": "../../*"

}

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 45/53

COUCHAPP

• couchapp generate myapp

• couchapp push mydb

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

http://github.com/couchapp/couchapp

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 46/53

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 47/53

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 48/53

THE FUTURE

• CouchApps on the desktop

• The browser is the OS

• On your phone

Monday, February 8, 2010

Stuart Langridge - Canonical

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 49/53

! !

g g

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 50/53

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 mul timedia : http://www.e-engura.com

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 51/53

Questions

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 52/53

Monday, February 8, 2010

8/14/2019 CouchDB for the web - fosdem 2010

http://slidepdf.com/reader/full/couchdb-for-the-web-fosdem-2010 53/53

Cette création est mise à disposition selon le ContratPaternité 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.