Date post: | 22-Jun-2015 |
Category: |
Business |
Upload: | oliver-kurowski |
View: | 4,006 times |
Download: | 0 times |
COUCHDB
<- Watch the car Oliver Kurowski, @okurow
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
TRANSFORMATIONS
Oliver Kurowski, @okurow
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
Shows:Transform Documents I
Oliver Kurowski, @okurow
Shows:Transform Documents I
Oliver Kurowski, @okurow
Shows:Transform Documents I
Oliver Kurowski, @okurow
Shows:Transform Documents I
Oliver Kurowski, @okurow
Shows:Transform Documents I
Oliver Kurowski, @okurow
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
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; }“}
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; }“}
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; }“}
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)
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?
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) { ….}
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 :
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
List:Transform view results I
Oliver Kurowski, @okurow
List:Transform view results I
Oliver Kurowski, @okurow
List:Transform view results I
Oliver Kurowski, @okurow
List:Transform view results I
Oliver Kurowski, @okurow
List:Transform view results I
Oliver Kurowski, @okurow
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“} ]}
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>
Oliver Kurowski, @okurow
Q (&A?)?http://apache.couchdb.org
Irc: #couchdb