+ All Categories
Home > Business > Couchdb List and Show Introduction

Couchdb List and Show Introduction

Date post: 22-Jun-2015
Category:
Upload: oliver-kurowski
View: 4,006 times
Download: 0 times
Share this document with a friend
Description:
A short introduction of list and show handling functions in couchdb. Examples are taken from the book "CouchDB mit PHP". Talk was given at the berlin couchdb meetup (http://berlin.couchdb.org)
Popular Tags:
26
COUCHDB <- Watch the car Oliver Kurowski, @okurow
Transcript
Page 1: Couchdb List and Show Introduction

COUCHDB

<- Watch the car Oliver Kurowski, @okurow

Page 2: Couchdb List and Show Introduction

Who am I Oliver Kurowski Degree in Computer Science Beuth School of applied Sciences

Berlin

First Computer: Apple II 1983 Since 2003: kw automotive GmbH

Write articles in Magazines 2012: Book „CouchDB mit PHP“ (www.CouchDBmitPHP.de)

Twitter: @okurow Mail: [email protected]

Oliver Kurowski, @okurow

Page 3: Couchdb List and Show Introduction

TRANSFORMATIONS

Oliver Kurowski, @okurow

Page 4: Couchdb List and Show Introduction

Transformation FunctionsDocuments are JSON objects

Results of views are also JSON objects

CouchDB can transfrom those JSON Objects on server side.

Shows: Transformation for documents

Lists: Transformation for views

Shows and lists are performed during request, results are not stored (like views).

Oliver Kurowski, @okurow

Page 5: Couchdb List and Show Introduction

Shows:Transform Documents I

Oliver Kurowski, @okurow

Page 6: Couchdb List and Show Introduction

Shows:Transform Documents I

Oliver Kurowski, @okurow

Page 7: Couchdb List and Show Introduction

Shows:Transform Documents I

Oliver Kurowski, @okurow

Page 8: Couchdb List and Show Introduction

Shows:Transform Documents I

Oliver Kurowski, @okurow

Page 9: Couchdb List and Show Introduction

Shows:Transform Documents I

Oliver Kurowski, @okurow

Page 10: Couchdb List and Show Introduction

Shows:Transform Documents I

Oliver Kurowski, @okurow

{“_id“: “1“,“make“: “Audi“,“model“: “A3“,“year“: 2000,“price“: 5.400}

Data document (JSON): Show function in a design doc:

Result of _show/toHTML/1 :

“shows“: { “toHTML“:“function(doc,req) { var outS=‘‘; outS=‘<b>‘+doc.maker+‘ ‘+doc.model+‘</b><br>`; outS+=‘Year of Prod.: ‘+doc.year+‘<br>‘; outS+=‘Price: ‘+doc.price; return outS; }“ }

Audi A3Year of Prod.: 2000Price: 5.400

Page 11: Couchdb List and Show Introduction

Shows:Transform Documents II

Oliver Kurowski, @okurow

{ “_id“: “1“, “author“: “Oliver Kurowski“, “headline“: “CouchDB is great“, “body“: “CouchDb is a great database for schemeless data“ , “comments“: [ {“name“: “John“, “text“: “Yes, it really is“}, {“name“: “Jane“, “text“: “What about couchbase?“} ]}

Data document (JSON):

“shows“: { “headline“:“function(doc,req) { var comments=0; if (doc.comments) { comments=doc.comments.length }; var outS=‘<b>‘ + doc.headline + ‘</b> (‘ + comments + ‘ comments)`; return outS; }“, “detail“:“function(doc,req) { var outS=‘<b>‘ + doc.headline + ‘</b><br>`; outS+= doc.body + ‘<br>`; if (doc.comments && doc.comments.length>0) { outS+= ‘comments:<br>`; for (var curComment in doc.comments) { outS+=doc.comments[curComment].name+‘:<br>`; outS+=doc.comments[curComment].text+‘<br>`; } } return outS; }“}

Page 12: Couchdb List and Show Introduction

Shows:Transform Documents II

Oliver Kurowski, @okurow

{ “_id“: “1“, “author“: “Oliver Kurowski“, “headline“: “CouchDB is great“, “body“: “CouchDb is a great database for schemeless data“ , “comments“: [ {“name“: “John“, “text“: “Yes, it really is“}, {“name“: “Jane“, “text“: “What about couchbase?“} ]}

Data document (JSON):

“shows“: { “headline“:“function(doc,req) { var comments=0; if (doc.comments) { comments=doc.comments.length }; var outS=‘<b>‘ + doc.headline + ‘</b> (‘ + comments + ‘ comments)`; return outS; }“, “detail“:“function(doc,req) { var outS=‘<b>‘ + doc.headline + ‘</b><br>`; outS+= doc.body + ‘<br>`; if (doc.comments && doc.comments.length>0) { outS+= ‘comments:<br>`; for (var curComment in doc.comments) { outS+=doc.comments[curComment].name+‘:<br>`; outS+=doc.comments[curComment].text+‘<br>`; } } return outS; }“}

Page 13: Couchdb List and Show Introduction

Shows:Transform Documents II

Oliver Kurowski, @okurow

{ “_id“: “1“, “author“: “Oliver Kurowski“, “headline“: “CouchDB is great“, “body“: “CouchDb is a great database for schemeless data“ , “comments“: [ {“name“: “John“, “text“: “Yes, it really is“}, {“name“: “Jane“, “text“: “What about couchbase?“} ]}

Data document (JSON):

“shows“: { “headline“:“function(doc,req) { var comments=0; if (doc.comments) { comments=doc.comments.length }; var outS=‘<b>‘ + doc.headline + ‘</b> (‘ + comments + ‘ comments)`; return outS; }“, “detail“:“function(doc,req) { var outS=‘<b>‘ + doc.headline + ‘</b><br>`; outS+= doc.body + ‘<br>`; if (doc.comments && doc.comments.length>0) { outS+= ‘comments:<br>`; for (var curComment in doc.comments) { outS+=doc.comments[curComment].name+‘:<br>`; outS+=doc.comments[curComment].text+‘<br>`; } } return outS; }“}

Page 14: Couchdb List and Show Introduction

Shows:Transform Documents II

Oliver Kurowski, @okurow

{ “_id“: “1“, “author“: “Oliver Kurowski“, “headline“: “CouchDB is great“, “body“: “CouchDb is a great database for schemeless data.“ , “comments“: [ {“name“: “John“, “text“: “Yes, it really is“}, {“name“: “Jane“, “text“: “What about couchbase?“} ]}

Data document (JSON):

“shows“: { “headline“:“function(doc,req) { var comments=0; if (doc.comments) { comments=doc.comments.length }; var outS=‘<b>‘ + doc.headline + ‘</b> (‘ + comments + ‘ comments)`; return outS; }“, “detail“:“function(doc,req) { var outS=‘<b>‘ + doc.headline + ‘</b><br>`; outS+= doc.body + ‘<br>`; if (doc.comments && doc.comments.length>0) { outS+= ‘<hr>Comments:<br>`; for (var curComment in doc.comments) { outS+=doc.comments[curComment].name+‘:<br>`; outS+=doc.comments[curComment].text+‘<br>`; } } return outS; }“}

Result of _show/headline/1 :CouchDB is great (2 comments)

Page 15: Couchdb List and Show Introduction

Shows:Transform Documents II

Oliver Kurowski, @okurow

{ “_id“: “1“, “author“: “Oliver Kurowski“, “headline“: “CouchDB is great“, “body“: “CouchDb is a great database for schemeless data.“ , “comments“: [ {“name“: “John“, “text“: “Yes, it really is“}, {“name“: “Jane“, “text“: “What about couchbase?“} ]}

Data document (JSON):

“shows“: { “headline“:“function(doc,req) { var comments=0; if (doc.comments) { comments=doc.comments.length }; var outS=‘<b>‘ + doc.headline + ‘</b> (‘ + comments + ‘ comments)`; return outS; }“, “detail“:“function(doc,req) { var outS=‘<b>‘ + doc.headline + ‘</b><br>`; outS+= doc.body + ‘<br>`; if (doc.comments && doc.comments.length>0) { outS+= ‘<hr>Comments:<br>`; for (var curComment in doc.comments) { outS+=doc.comments[curComment].name+‘:<br>`; outS+=doc.comments[curComment].text+‘<br>`; } } return outS; }“}

Result of _show/headline/1 :CouchDB is great (2 comments)

Result of _show/detail/1 :CouchDB is greatCouchDb is a great database for schemeless data.

Comments:John:Yes, it really isJane:What about couchbase?

Page 16: Couchdb List and Show Introduction

The Request ObjectShows and lists can access the Request Object containing:

(the most interesting)

Info: Informations about the DatabaseId: called document ID (null, if not given)requested_path: The request in parts (divided by /) as Arrayheaders: Header, sent from the clientuserCTX: Information about the user, usefull for access limitations! query: Parameters of the call

The field req.query allows the use of own parameters that can be used in a show/list function.F.e: _show/toHTML/1?showPrice=true

handled in the show function:Oliver Kurowski, @okurow

if(req.query.showPrice==true) { ….}

Page 17: Couchdb List and Show Introduction

Shows:Talking Parrots

Oliver Kurowski, @okurow

{ “_id“: “lora“, “name“: “Lora LoL“, “owner“: “Emily“, “color“: [“red“ , “#ff0000“ ] , “languages“: [ “english“ ]}

Data documents :{ “_id“: “_design/parrot“, “shows{ "introduce": "function(doc,req) { var outS=''; if(doc) { // doc with id in database found outS+='<font color='+doc.color[1]+'>'+doc.name +' belongs to '+doc.owner+'</font>'; if(req.query.say) { if(doc.languages) { outS+=' and says: '+req.query.say; }else{ outS+='tweet'; } } } else { // no doc found if(req.path[5]!=undefined) { // no id given outS+='I dont know '+req.path[5]; }else{ outS+='which parrot?'; } } return outS; }“ }}

{ “_id“: “polly“, “name“: “Polly Poll“, “owner“: “Ernest“, “color“: [“blue“ , “#0000ff“ ], “languages“: [ “english“ ]}{ “_id“: “agnus“, “name“: “Agnus Angry“, “owner“: “Ernest“, “color“: [“grey“ , “#cococo“ ] }

Design document :

Page 18: Couchdb List and Show Introduction

Shows: Talking Parrots

Oliver Kurowski, @okurow

http://localhost:5984/parrots/_design/parrot/_show/introduce/->which parrot?

http://localhost:5984/parrots/_design/parrot/_show/introduce/polly->Polly Poll belongs to Ernest

http://localhost:5984/parrots/_design/parrot/_show/introduce/pollx->I dont know pollx

http://localhost:5984/parrots/_design/parrot/_show/introduce/lora?say=Hello->Lora Loll belongs to Emily and says: Hello

http://localhost:5984/parrots/_design/parrot/_show/introduce/agnus?say=Hello->Agnus Angry belongs to Ernest tweet

Page 19: Couchdb List and Show Introduction

List:Transform view results I

Oliver Kurowski, @okurow

Page 20: Couchdb List and Show Introduction

List:Transform view results I

Oliver Kurowski, @okurow

Page 21: Couchdb List and Show Introduction

List:Transform view results I

Oliver Kurowski, @okurow

Page 22: Couchdb List and Show Introduction

List:Transform view results I

Oliver Kurowski, @okurow

Page 23: Couchdb List and Show Introduction

List:Transform view results I

Oliver Kurowski, @okurow

Page 24: Couchdb List and Show Introduction

List:Transform view results IResult of a View byPrice: A simple list function:

result from _list/asLi/byPrice :

- All arguments from a view can also be used in a list call

(startkey, endkey, skip, limit etc)

- Like Shows, you have access to the request Object with all ist possibilities.

Oliver Kurowski, @okurow

“lists“: { “asLI “:“function(head,req) { start({'code':200,'headers':{'Content-Type':'text/html'}}); while(row=getRow()) { send(‘<li>‘+row.value+‘ :‘ +row.key+‘</li>‘); } }“}

• Audi-A3-2000 : 5.400• VW-Golf-2008 : 9.000• VW-Polo-2010 :

12.000• VW-Golf-2009 :

15.000• Audi-A4-2009 :

16.000

{"total_rows":5,"offset":0,"rows":[ {"id":"1","key":5.400,"value":“Audi-A3-2000“}, {"id":"2","key":9.000,"value":“VW-Golf-2008“}, {"id":"3","key":12.000,"value":“VW-Polo-2010“}, {"id":"4","key":15.000,"value":“VW-Golf-2009“}, {"id":"5","key":16.000,"value":“Audi-A4-2009“} ]}

Page 25: Couchdb List and Show Introduction

List:Transform view results IIDifferent headers can be sent

result from _list/asXML/byPrice :

Oliver Kurowski, @okurow

“lists“: { “asXML “:“function(head,req) { start({'code':200,'headers':{'Content-Type':'application/xml'}}); var xmlS=‘<cars>‘; while(row=getRow()) { xmlS+=‘<car id=‘ + row.id + ‘>‘+row.value+‘ :‘ +row.key+‘</car>‘); } xmlS+=‘</cars>‘; return xmlS; }“}

<cars><car id=‘1‘>Audi-A3-2000 : 5.400</car><car id=‘2‘>VW-Golf-2008 : 9.000</car><car id=‘3‘ >VW-Polo-2010 : 12.000</car><car id=‘4‘>VW-Golf-2009 : 15.000</car><car id=‘5‘>Audi-A4-2009 : 16.000</car></cars>

Page 26: Couchdb List and Show Introduction

Oliver Kurowski, @okurow

Q (&A?)?http://apache.couchdb.org

Irc: #couchdb


Recommended